@tim-code/my-util 0.4.6 → 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.6",
3
+ "version": "0.4.7",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "Tim Sprowl",
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
+ })