@tim-code/my-util 0.0.6 → 0.0.8

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.6",
3
+ "version": "0.0.8",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "",
package/src/array.js CHANGED
@@ -92,6 +92,23 @@ export function descending(key) {
92
92
  }
93
93
  }
94
94
 
95
+ /**
96
+ * Combines multiple ascending and descending comparators.
97
+ * @param {...Function} comparators
98
+ * @returns {Function}
99
+ */
100
+ export function multilevel(...comparators) {
101
+ return (a, b) => {
102
+ for (const comparator of comparators) {
103
+ const result = comparator(a, b)
104
+ if (result) {
105
+ return result
106
+ }
107
+ }
108
+ return 0
109
+ }
110
+ }
111
+
95
112
  /**
96
113
  * Creates a function that accesses an object's value at key.
97
114
  * @param {string} key
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, via } = await import("./array.js")
4
+ const { chunk, unique, mutateValues, ascending, descending, multilevel, via } = await import("./array.js")
5
5
 
6
6
  describe("chunk", () => {
7
7
  it("splits array into chunks of specified size", () => {
@@ -188,6 +188,64 @@ describe("descending", () => {
188
188
  })
189
189
  })
190
190
 
191
+ describe("multilevel", () => {
192
+ it("returns 0 if all comparators return 0", () => {
193
+ const cmp = multilevel(
194
+ () => 0,
195
+ () => 0
196
+ )
197
+ expect(cmp(1, 2)).toBe(0)
198
+ expect(cmp("a", "b")).toBe(0)
199
+ })
200
+
201
+ it("returns first non-zero comparator result", () => {
202
+ const cmp = multilevel(
203
+ () => 0,
204
+ () => -1,
205
+ () => 1
206
+ )
207
+ expect(cmp(1, 2)).toBe(-1)
208
+ const cmp2 = multilevel(
209
+ () => 0,
210
+ () => 0,
211
+ () => 1
212
+ )
213
+ expect(cmp2(1, 2)).toBe(1)
214
+ })
215
+
216
+ it("works with ascending and descending comparators", () => {
217
+ const arr = [
218
+ { a: 1, b: 2 },
219
+ { a: 2, b: 1 },
220
+ { a: 1, b: 1 },
221
+ { a: 2, b: 2 },
222
+ ]
223
+ arr.sort(multilevel(ascending("a"), descending("b")))
224
+ expect(arr).toEqual([
225
+ { a: 1, b: 2 },
226
+ { a: 1, b: 1 },
227
+ { a: 2, b: 2 },
228
+ { a: 2, b: 1 },
229
+ ])
230
+ })
231
+
232
+ it("short-circuits after first non-zero comparator", () => {
233
+ const calls = []
234
+ const cmp1 = jest.fn(() => { calls.push("cmp1"); return 0 })
235
+ const cmp2 = jest.fn(() => { calls.push("cmp2"); return -1 })
236
+ const cmp3 = jest.fn(() => { calls.push("cmp3"); return 1 })
237
+ const cmp = multilevel(cmp1, cmp2, cmp3)
238
+ expect(cmp({}, {})).toBe(-1)
239
+ expect(calls).toEqual(["cmp1", "cmp2"])
240
+ })
241
+
242
+ it("returns 0 if no comparators are provided", () => {
243
+ const cmp = multilevel()
244
+ expect(cmp(1, 2)).toBe(0)
245
+ expect(cmp("a", "b")).toBe(0)
246
+ })
247
+ })
248
+
191
249
  describe("via", () => {
192
250
  it("returns a function that accesses the given key", () => {
193
251
  const getFoo = via("foo")
package/src/time.js CHANGED
@@ -79,7 +79,7 @@ export function isUnixTimestamp(ts, { max = 9999999999 } = {}) {
79
79
  * @param {number} $1.minutes Minutes to add to time string
80
80
  * @returns {string} HH:mm:ss
81
81
  */
82
- export function addTime(timeString, { minutes = 0, hours = 0 }) {
82
+ export function addTime(timeString, { minutes = 0, hours = 0 } = {}) {
83
83
  let [hour, minute, second = 0] = timeString.split(":").map(Number)
84
84
  hour = mod(hour + hours, 24)
85
85
  minute += minutes
@@ -109,3 +109,21 @@ export function addDays(dateString, days = 0) {
109
109
  date.setUTCDate(date.getUTCDate() + days)
110
110
  return date.toISOString().slice(0, 10)
111
111
  }
112
+
113
+ /**
114
+ * Get all dates between two dates, with limit.
115
+ * @param {string} start
116
+ * @param {string} end
117
+ * @returns {Array}
118
+ */
119
+ export function getDateRange(start, end, { limit = 1000 } = {}) {
120
+ const dates = []
121
+ while (start <= end) {
122
+ dates.push(start)
123
+ start = addDays(start, 1)
124
+ if (dates.length >= limit) {
125
+ break
126
+ }
127
+ }
128
+ return dates
129
+ }
package/src/time.test.js CHANGED
@@ -1,5 +1,13 @@
1
1
  import { describe, expect, test } from "@jest/globals"
2
- import { addDays, addTime, getEasternTime, isDate, isTime, isUnixTimestamp } from "./time.js"
2
+ import {
3
+ addDays,
4
+ addTime,
5
+ getDateRange,
6
+ getEasternTime,
7
+ isDate,
8
+ isTime,
9
+ isUnixTimestamp,
10
+ } from "./time.js"
3
11
 
4
12
  // getEasternTime changed: removed "days" param, added "timestamp" param
5
13
  describe("getEasternTime", () => {
@@ -212,6 +220,12 @@ describe("addTime", () => {
212
220
  test("handles midnight", () => {
213
221
  expect(addTime("00:00:00", { hours: 0, minutes: 0 })).toBe("00:00:00")
214
222
  })
223
+
224
+ // New: test default parameters (no options argument)
225
+ test("handles missing options argument (all defaults)", () => {
226
+ expect(addTime("12:34:56")).toBe("12:34:56")
227
+ expect(addTime("05:10")).toBe("05:10:00")
228
+ })
215
229
  })
216
230
 
217
231
  describe("addDays", () => {
@@ -264,3 +278,48 @@ describe("addDays", () => {
264
278
  expect(addDays("2024-11-03", -1)).toBe("2024-11-02")
265
279
  })
266
280
  })
281
+
282
+ // New tests for getDateRange (newly exported function)
283
+ describe("getDateRange", () => {
284
+ test("returns all dates between start and end inclusive", () => {
285
+ expect(getDateRange("2024-06-01", "2024-06-03")).toEqual([
286
+ "2024-06-01",
287
+ "2024-06-02",
288
+ "2024-06-03",
289
+ ])
290
+ })
291
+
292
+ test("returns just the start date if start equals end", () => {
293
+ expect(getDateRange("2024-06-01", "2024-06-01")).toEqual(["2024-06-01"])
294
+ })
295
+
296
+ test("returns empty array if start > end", () => {
297
+ expect(getDateRange("2024-06-03", "2024-06-01")).toEqual([])
298
+ })
299
+
300
+ test("respects limit option", () => {
301
+ expect(getDateRange("2024-06-01", "2024-06-10", { limit: 3 })).toEqual([
302
+ "2024-06-01",
303
+ "2024-06-02",
304
+ "2024-06-03",
305
+ ])
306
+ })
307
+
308
+ test("returns at most 1000 dates by default", () => {
309
+ const dates = getDateRange("2020-01-01", "2025-01-01")
310
+ expect(dates.length).toBeLessThanOrEqual(1000)
311
+ expect(dates[0]).toBe("2020-01-01")
312
+ })
313
+
314
+ test("can return more than 1000 dates if limit is raised", () => {
315
+ const dates = getDateRange("2020-01-01", "2025-01-01", { limit: 2000 })
316
+ expect(dates.length).toBeGreaterThan(1000)
317
+ expect(dates[0]).toBe("2020-01-01")
318
+ expect(dates[dates.length - 1]).toBe("2025-01-01")
319
+ })
320
+
321
+ // ISSUE: getDateRange does not validate that start/end are valid dates, so invalid input may yield unexpected results.
322
+ test("handles invalid date input (returns empty array if start > end lexically)", () => {
323
+ expect(getDateRange("not-a-date", "2024-01-01")).toEqual([])
324
+ })
325
+ })