@tim-code/my-util 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tim-code/my-util",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "",
package/src/array.js CHANGED
@@ -92,6 +92,26 @@ export function descending(key) {
92
92
  }
93
93
  }
94
94
 
95
+ /**
96
+ * Creates a function that accesses an object's value at key.
97
+ * @param {string} key
98
+ * @returns {any}
99
+ */
100
+ export function via(key) {
101
+ return (object) => object[key]
102
+ }
103
+
104
+ // export function contains(template) {
105
+ // return (object) => {
106
+ // for (const key in template) {
107
+ // if (object[key] !== template[key]) {
108
+ // return false
109
+ // }
110
+ // }
111
+ // return true
112
+ // }
113
+ // }
114
+
95
115
  // not sure how far we want to go down "key" rabbit hole:
96
116
  // export function sum(array, key) {}
97
117
  // or maybe
package/src/array.test.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable no-restricted-syntax */
2
2
  import { describe, expect, it, jest } from "@jest/globals"
3
3
 
4
- const { chunk, unique, mutateValues, ascending, descending } = await import("./array.js")
4
+ const { chunk, unique, mutateValues, ascending, descending, via } = await import("./array.js")
5
5
 
6
6
  describe("chunk", () => {
7
7
  it("splits array into chunks of specified size", () => {
@@ -187,3 +187,29 @@ describe("descending", () => {
187
187
  expect(arr.map((o) => o.v)).toEqual([3, 2, undefined])
188
188
  })
189
189
  })
190
+
191
+ describe("via", () => {
192
+ it("returns a function that accesses the given key", () => {
193
+ const getFoo = via("foo")
194
+ expect(getFoo({ foo: 42 })).toBe(42)
195
+ expect(getFoo({ foo: "bar" })).toBe("bar")
196
+ })
197
+
198
+ it("returns undefined if the key does not exist", () => {
199
+ const getX = via("x")
200
+ expect(getX({})).toBeUndefined()
201
+ expect(getX({ y: 1 })).toBeUndefined()
202
+ })
203
+
204
+ it("works with numeric keys", () => {
205
+ const get0 = via(0)
206
+ expect(get0([10, 20])).toBe(10)
207
+ expect(get0({ 0: "zero" })).toBe("zero")
208
+ })
209
+
210
+ it("returns undefined if object is missing", () => {
211
+ const getFoo = via("foo")
212
+ expect(() => getFoo(undefined)).toThrow(TypeError)
213
+ expect(() => getFoo(null)).toThrow(TypeError)
214
+ })
215
+ })
package/src/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./array.js"
2
2
  export * from "./fs.js"
3
+ export * from "./math.js"
3
4
  export * from "./promise.js"
4
5
  export * from "./run.js"
5
6
  export * from "./time.js"
package/src/math.js ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Module safe for negative numbers i.e. mod(-1, 3) === 2 (not -1 as in -1 % 3).
3
+ * @param {number} n
4
+ * @param {number} m If 0, returns NaN as does n % m.
5
+ * @returns {number}
6
+ */
7
+ export function mod(n, m) {
8
+ return ((n % m) + m) % m
9
+ }
@@ -0,0 +1,37 @@
1
+ import { describe, expect, it } from "@jest/globals"
2
+ const { mod } = await import("./math.js")
3
+
4
+ describe("mod", () => {
5
+ it("returns n when n is less than m and n is non-negative", () => {
6
+ expect(mod(2, 3)).toBe(2)
7
+ expect(mod(0, 5)).toBe(0)
8
+ })
9
+
10
+ it("wraps negative n into the [0, m) range", () => {
11
+ expect(mod(-1, 3)).toBe(2)
12
+ expect(mod(-4, 3)).toBe(2)
13
+ expect(mod(-2, 5)).toBe(3)
14
+ })
15
+
16
+ it("returns 0 when n is a multiple of m", () => {
17
+ expect(mod(6, 3)).toBe(0)
18
+ expect(mod(-6, 3)).toBe(0)
19
+ })
20
+
21
+ it("handles n greater than m", () => {
22
+ expect(mod(7, 3)).toBe(1)
23
+ expect(mod(14, 5)).toBe(4)
24
+ })
25
+
26
+ it("handles m = 1 (should always return 0)", () => {
27
+ expect(mod(0, 1)).toBe(0)
28
+ expect(mod(5, 1)).toBe(0)
29
+ expect(mod(-3, 1)).toBe(0)
30
+ })
31
+
32
+ it("returns NaN when m is 0", () => {
33
+ expect(mod(5, 0)).toBeNaN()
34
+ expect(mod(0, 0)).toBeNaN()
35
+ expect(mod(-3, 0)).toBeNaN()
36
+ })
37
+ })
package/src/time.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { mod } from "./math.js"
2
+
1
3
  /**
2
4
  * Gets various ways of representing the current time in EDT. Floors to nearest second by default.
3
5
  * @param {Object} $1
@@ -62,6 +64,7 @@ export function isTime(string) {
62
64
 
63
65
  /**
64
66
  * Checks if a number is a Unix timestamp (i.e. in seconds).
67
+ * Would not validate timestamps set very far into the future.
65
68
  * @param {number} ts
66
69
  * @param {Object} $1
67
70
  * @param {number} $1.max Maximum value for timestamp to allow - default is up to ~2286-11-20; this allows catching ms timestamps
@@ -70,3 +73,29 @@ export function isTime(string) {
70
73
  export function isUnixTimestamp(ts, { max = 9999999999 } = {}) {
71
74
  return Number.isInteger(ts) && ts >= 0 && ts <= max
72
75
  }
76
+
77
+ /**
78
+ * Add an amount of time to a time string.
79
+ * @param {string} timeString HH:mm:ss or HH:mm; parsable numbers separated by :
80
+ * @param {Object} $1
81
+ * @param {number} $1.hours Hours to add to time string
82
+ * @param {number} $1.minutes Minutes to add to time string
83
+ * @returns {string} HH:mm:ss
84
+ */
85
+ export function addTime(timeString, { minutes = 0, hours = 0 }) {
86
+ let [hour, minute, second = 0] = timeString.split(":").map(Number)
87
+ hour = mod(hour + hours, 24)
88
+ minute += minutes
89
+ while (minute >= 60) {
90
+ hour = mod(hour + 1, 24)
91
+ minute -= 60
92
+ }
93
+ while (minute < 0) {
94
+ hour = mod(hour - 1, 24)
95
+ minute += 60
96
+ }
97
+ const newTime = [hour, minute, second]
98
+ .map((number) => `${number}`.padStart(2, "0"))
99
+ .join(":")
100
+ return newTime
101
+ }
package/src/time.test.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, test } from "@jest/globals"
2
- import { getEasternTime, isDate, isTime, isUnixTimestamp } from "./time.js"
2
+ import { addTime, getEasternTime, isDate, isTime, isUnixTimestamp } from "./time.js"
3
3
 
4
4
  describe("getEasternTime", () => {
5
5
  test("returns correct structure and types", () => {
@@ -119,4 +119,63 @@ describe("isUnixTimestamp", () => {
119
119
  expect(isUnixTimestamp(50, { max: 50 })).toBe(true)
120
120
  expect(isUnixTimestamp(51, { max: 50 })).toBe(false)
121
121
  })
122
- })
122
+ })
123
+
124
+ describe("addTime", () => {
125
+ test("adds minutes within the same hour", () => {
126
+ expect(addTime("12:30:00", { minutes: 15 })).toBe("12:45:00")
127
+ expect(addTime("12:30", { minutes: 15 })).toBe("12:45:00")
128
+ })
129
+
130
+ test("adds minutes with hour rollover", () => {
131
+ expect(addTime("12:50:00", { minutes: 15 })).toBe("13:05:00")
132
+ expect(addTime("23:50:00", { minutes: 15 })).toBe("00:05:00")
133
+ })
134
+
135
+ test("subtracts minutes with hour underflow", () => {
136
+ expect(addTime("12:10:00", { minutes: -15 })).toBe("11:55:00")
137
+ expect(addTime("00:10:00", { minutes: -15 })).toBe("23:55:00")
138
+ })
139
+
140
+ test("adds hours with 24-hour rollover", () => {
141
+ expect(addTime("22:15:00", { hours: 3 })).toBe("01:15:00")
142
+ expect(addTime("00:00:00", { hours: 24 })).toBe("00:00:00")
143
+ expect(addTime("23:59:59", { hours: 1 })).toBe("00:59:59")
144
+ })
145
+
146
+ test("subtracts hours with 24-hour underflow", () => {
147
+ expect(addTime("01:15:00", { hours: -3 })).toBe("22:15:00")
148
+ expect(addTime("00:00:00", { hours: -24 })).toBe("00:00:00")
149
+ expect(addTime("00:59:59", { hours: -1 })).toBe("23:59:59")
150
+ })
151
+
152
+ test("handles both hours and minutes together", () => {
153
+ expect(addTime("22:30:00", { hours: 2, minutes: 45 })).toBe("01:15:00")
154
+ expect(addTime("01:15:00", { hours: -2, minutes: -30 })).toBe("22:45:00")
155
+ })
156
+
157
+ test("pads single digit hours, minutes, seconds", () => {
158
+ expect(addTime("1:2:3", { hours: 0, minutes: 0 })).toBe("01:02:03")
159
+ expect(addTime("9:8", { hours: 0, minutes: 0 })).toBe("09:08:00")
160
+ })
161
+
162
+ // Edge case: negative minutes that require multiple hour underflows
163
+ test("handles large negative minutes", () => {
164
+ expect(addTime("05:10:00", { minutes: -130 })).toBe("03:00:00")
165
+ })
166
+
167
+ // Edge case: large positive minutes that require multiple hour rollovers
168
+ test("handles large positive minutes", () => {
169
+ expect(addTime("05:10:00", { minutes: 130 })).toBe("07:20:00")
170
+ })
171
+
172
+ // Edge case: input with seconds omitted
173
+ test("handles input with no seconds", () => {
174
+ expect(addTime("12:34", { minutes: 0 })).toBe("12:34:00")
175
+ })
176
+
177
+ // Edge case: input with all zeros
178
+ test("handles midnight", () => {
179
+ expect(addTime("00:00:00", { hours: 0, minutes: 0 })).toBe("00:00:00")
180
+ })
181
+ })