@tim-code/my-util 0.4.5 → 0.4.7

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.4.5",
3
+ "version": "0.4.7",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "Tim Sprowl",
package/src/math.js CHANGED
@@ -123,3 +123,21 @@ export function formatPlus(number, { zero = false } = {}) {
123
123
  }
124
124
  return undefined
125
125
  }
126
+
127
+ /**
128
+ * Create an array of numbers progressing from start up to, but not including, end.
129
+ * @param {number} start
130
+ * @param {number=} end
131
+ * @param {number=} step
132
+ * @returns {number[]}
133
+ */
134
+ export function range(start, end, increment = 1) {
135
+ if (!(increment > 0)) {
136
+ return []
137
+ }
138
+ const results = []
139
+ for (let i = start; i < end; i += increment) {
140
+ results.push(i)
141
+ }
142
+ return results
143
+ }
package/src/math.test.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from "@jest/globals"
2
- const { mod, formatPlus, line, sum, average, variance } = await import("./math.js")
2
+ const { mod, formatPlus, line, sum, average, variance, range } = await import("./math.js")
3
3
 
4
4
  describe("mod", () => {
5
5
  it("returns n when n is less than m and n is non-negative", () => {
@@ -255,3 +255,47 @@ describe("formatPlus", () => {
255
255
  expect(formatPlus(NaN)).toBeUndefined()
256
256
  })
257
257
  })
258
+
259
+ describe("range", () => {
260
+ it("returns an empty array if start >= end", () => {
261
+ expect(range(5, 5)).toEqual([])
262
+ expect(range(10, 5)).toEqual([])
263
+ })
264
+
265
+ it("returns a sequence from start to end-1 with default increment 1", () => {
266
+ expect(range(0, 3)).toEqual([0, 1, 2])
267
+ expect(range(2, 6)).toEqual([2, 3, 4, 5])
268
+ })
269
+
270
+ it("returns a sequence with a custom positive increment", () => {
271
+ expect(range(0, 5, 2)).toEqual([0, 2, 4])
272
+ expect(range(1, 8, 3)).toEqual([1, 4, 7])
273
+ })
274
+ it("works with negative start and end", () => {
275
+ expect(range(-3, 1)).toEqual([-3, -2, -1, 0])
276
+ expect(range(-5, -2)).toEqual([-5, -4, -3])
277
+ })
278
+
279
+ it("returns an empty array if increment is zero", () => {
280
+ expect(range(0, 5, 0)).toEqual([])
281
+ })
282
+
283
+ it("returns an empty array if increment is negative and start < end", () => {
284
+ expect(range(0, 5, -1)).toEqual([])
285
+ expect(range(2, 6, -2)).toEqual([])
286
+ })
287
+
288
+ it("returns an empty array if increment is negative and start > end", () => {
289
+ expect(range(5, 0, -1)).toEqual([])
290
+ expect(range(3, -2, -2)).toEqual([])
291
+ })
292
+
293
+ it("returns an empty array if increment is positive and start > end", () => {
294
+ expect(range(5, 0, 1)).toEqual([])
295
+ expect(range(2, -2, 2)).toEqual([])
296
+ })
297
+
298
+ it("handles floating point increments", () => {
299
+ expect(range(0, 1, 0.25)).toEqual([0, 0.25, 0.5, 0.75])
300
+ })
301
+ })
package/src/time.js CHANGED
@@ -200,3 +200,14 @@ export function getDateRange(start, end, { limit = 1000 } = {}) {
200
200
  }
201
201
  return dates
202
202
  }
203
+
204
+ /**
205
+ * Get the first date in the week containing the passed date string.
206
+ * @param {string} dateString YYYY-MM-DD
207
+ * @returns {string} YYYY-MM-DD
208
+ */
209
+ export function getStartOfWeek(dateString) {
210
+ const index = getDayIndexInWeek(dateString)
211
+ const start = addDays(dateString, { days: -index })
212
+ return start
213
+ }
package/src/time.test.js CHANGED
@@ -7,6 +7,7 @@ import {
7
7
  // getDayOfWeek, // removed, replaced by getDayIndexInWeek
8
8
  getEasternTime,
9
9
  getMinute,
10
+ getStartOfWeek,
10
11
  getTime,
11
12
  getTimeRange,
12
13
  isDate,
@@ -492,3 +493,31 @@ describe("getDateRange", () => {
492
493
  expect(getDateRange("not-a-date", "2024-01-01")).toEqual([])
493
494
  })
494
495
  })
496
+
497
+ // Tests for getStartOfWeek (newly exported function)
498
+ describe("getStartOfWeek", () => {
499
+ test("returns same date if input is Sunday", () => {
500
+ expect(getStartOfWeek("2024-06-02")).toBe("2024-06-02") // Sunday
501
+ expect(getDayIndexInWeek("2024-06-02")).toBe(0)
502
+ })
503
+
504
+ test("returns previous Sunday for a weekday", () => {
505
+ expect(getStartOfWeek("2024-06-05")).toBe("2024-06-02") // Wednesday -> previous Sunday
506
+ expect(getStartOfWeek("2024-06-03")).toBe("2024-06-02") // Monday -> previous Sunday
507
+ expect(getStartOfWeek("2024-06-08")).toBe("2024-06-02") // Saturday -> previous Sunday
508
+ })
509
+
510
+ test("works for leap day", () => {
511
+ // 2024-02-29 is Thursday (4), so start of week should be 2024-02-25
512
+ expect(getStartOfWeek("2024-02-29")).toBe("2024-02-25")
513
+ })
514
+
515
+ test("works for year boundary", () => {
516
+ // 2023-01-01 is Sunday
517
+ expect(getStartOfWeek("2023-01-01")).toBe("2023-01-01")
518
+ // 2023-01-07 is Saturday, so start of week is 2023-01-01
519
+ expect(getStartOfWeek("2023-01-07")).toBe("2023-01-01")
520
+ // 2023-01-02 is Monday, so start of week is 2023-01-01
521
+ expect(getStartOfWeek("2023-01-02")).toBe("2023-01-01")
522
+ })
523
+ })