@tim-code/my-util 0.2.3 → 0.2.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 +1 -1
- package/src/time.js +24 -0
- package/src/time.test.js +52 -13
package/package.json
CHANGED
package/src/time.js
CHANGED
|
@@ -52,6 +52,30 @@ export function today() {
|
|
|
52
52
|
return getTime().date
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Get the day of the week index from a YYYY-MM-DD string.
|
|
57
|
+
* @param {string=} string YYYY-MM-DD
|
|
58
|
+
* @returns {string} 0, 1, 2, 3, 4, 5, 6; 0 is Sunday and, 1 is Monday, ... 6 is Saturday
|
|
59
|
+
*/
|
|
60
|
+
export function getDayIndexInWeek(string = today()) {
|
|
61
|
+
const [year, month, day] = string.split("-").map(Number)
|
|
62
|
+
const index = new Date(year, month - 1, day).getDay()
|
|
63
|
+
if (isNaN(index)) {
|
|
64
|
+
throw new Error(`invalid date: ${string}`)
|
|
65
|
+
}
|
|
66
|
+
return index
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Get the minute from a time string.
|
|
71
|
+
* @param {string} time HH:mm or HH:mm::ss
|
|
72
|
+
* @returns {number} Between 0 and 59 inclusive
|
|
73
|
+
*/
|
|
74
|
+
export function getMinute(time) {
|
|
75
|
+
const minute = Number(time.split(":")[1])
|
|
76
|
+
return minute
|
|
77
|
+
}
|
|
78
|
+
|
|
55
79
|
/**
|
|
56
80
|
* Checks if the string represents a valid YYYY-MM-DD date.
|
|
57
81
|
* This will return false for dates like "2024-02-31".
|
package/src/time.test.js
CHANGED
|
@@ -3,7 +3,10 @@ import {
|
|
|
3
3
|
addDays,
|
|
4
4
|
addTime,
|
|
5
5
|
getDateRange,
|
|
6
|
+
getDayIndexInWeek,
|
|
7
|
+
// getDayOfWeek, // removed, replaced by getDayIndexInWeek
|
|
6
8
|
getEasternTime,
|
|
9
|
+
getMinute,
|
|
7
10
|
getTime,
|
|
8
11
|
getTimeRange,
|
|
9
12
|
isDate,
|
|
@@ -12,14 +15,12 @@ import {
|
|
|
12
15
|
today,
|
|
13
16
|
} from "./time.js"
|
|
14
17
|
|
|
15
|
-
// getEasternTime changed: now accepts a "timezone" param and no longer returns "minute" or "datetime"
|
|
16
18
|
describe("getEasternTime", () => {
|
|
17
19
|
test("returns correct structure and types", () => {
|
|
18
20
|
const result = getEasternTime()
|
|
19
21
|
expect(typeof result.timestamp).toBe("number")
|
|
20
22
|
expect(typeof result.date).toBe("string")
|
|
21
23
|
expect(typeof result.time).toBe("string")
|
|
22
|
-
// minute and datetime are no longer returned
|
|
23
24
|
expect(result).not.toHaveProperty("minute")
|
|
24
25
|
expect(result).not.toHaveProperty("datetime")
|
|
25
26
|
})
|
|
@@ -52,7 +53,6 @@ describe("getEasternTime", () => {
|
|
|
52
53
|
expect(def.timestamp).toEqual(explicit.timestamp)
|
|
53
54
|
})
|
|
54
55
|
|
|
55
|
-
// New: test timezone override
|
|
56
56
|
test("respects timezone parameter", () => {
|
|
57
57
|
// 2024-06-01T12:34:56Z (UTC)
|
|
58
58
|
const ts = 1717245296
|
|
@@ -65,7 +65,6 @@ describe("getEasternTime", () => {
|
|
|
65
65
|
expect(eastern.time).not.toBe(pacific.time)
|
|
66
66
|
expect(eastern.time).not.toBe(utc.time)
|
|
67
67
|
expect(pacific.time).not.toBe(utc.time)
|
|
68
|
-
// Should match expected hour offset
|
|
69
68
|
expect(eastern.time.startsWith("08:34")).toBe(true) // EDT
|
|
70
69
|
expect(pacific.time.startsWith("05:34")).toBe(true) // PDT
|
|
71
70
|
expect(utc.time.startsWith("12:34")).toBe(true) // UTC
|
|
@@ -92,7 +91,6 @@ describe("getEasternTime", () => {
|
|
|
92
91
|
expect(local.time).toBe(time)
|
|
93
92
|
})
|
|
94
93
|
|
|
95
|
-
// DST boundary tests
|
|
96
94
|
test("handles DST start (spring forward) correctly", () => {
|
|
97
95
|
// In 2024, DST starts in US/Eastern at 2024-03-10 02:00:00 local time (clocks jump to 03:00:00)
|
|
98
96
|
// 2024-03-10T06:59:59Z = 1:59:59 EST (should be 01:59:59)
|
|
@@ -132,7 +130,6 @@ describe("getEasternTime", () => {
|
|
|
132
130
|
})
|
|
133
131
|
})
|
|
134
132
|
|
|
135
|
-
// New tests for getTime (newly exported function)
|
|
136
133
|
describe("getTime", () => {
|
|
137
134
|
test("returns same structure as getEasternTime", () => {
|
|
138
135
|
const result = getTime({})
|
|
@@ -187,6 +184,55 @@ describe("today", () => {
|
|
|
187
184
|
})
|
|
188
185
|
})
|
|
189
186
|
|
|
187
|
+
describe("getDayIndexInWeek", () => {
|
|
188
|
+
test("returns correct index for known dates", () => {
|
|
189
|
+
expect(getDayIndexInWeek("2024-06-02")).toBe(0)
|
|
190
|
+
expect(getDayIndexInWeek("2024-06-03")).toBe(1)
|
|
191
|
+
expect(getDayIndexInWeek("2024-06-04")).toBe(2)
|
|
192
|
+
expect(getDayIndexInWeek("2024-06-05")).toBe(3)
|
|
193
|
+
expect(getDayIndexInWeek("2024-06-06")).toBe(4)
|
|
194
|
+
expect(getDayIndexInWeek("2024-06-07")).toBe(5)
|
|
195
|
+
expect(getDayIndexInWeek("2024-06-08")).toBe(6)
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
test("returns correct index for leap day", () => {
|
|
199
|
+
// 2024-02-29 is Thursday (4)
|
|
200
|
+
expect(getDayIndexInWeek("2024-02-29")).toBe(4)
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
test("defaults to today() if no argument is given", () => {
|
|
204
|
+
const todayDate = today()
|
|
205
|
+
expect(getDayIndexInWeek()).toBe(getDayIndexInWeek(todayDate))
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
test("throws on invalid date strings", () => {
|
|
209
|
+
expect(() => getDayIndexInWeek("not-a-date")).toThrow(/invalid date/u)
|
|
210
|
+
// don't worry about bad dates like the following; can be caught with isDate()
|
|
211
|
+
expect(getDayIndexInWeek("2024-02-31")).toBe(6)
|
|
212
|
+
})
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
// New tests for getMinute
|
|
216
|
+
describe("getMinute", () => {
|
|
217
|
+
test("extracts minute from HH:mm:ss", () => {
|
|
218
|
+
expect(getMinute("12:34:56")).toBe(34)
|
|
219
|
+
expect(getMinute("00:01:00")).toBe(1)
|
|
220
|
+
expect(getMinute("23:59:59")).toBe(59)
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
test("extracts minute from HH:mm", () => {
|
|
224
|
+
expect(getMinute("12:34")).toBe(34)
|
|
225
|
+
expect(getMinute("00:01")).toBe(1)
|
|
226
|
+
expect(getMinute("23:59")).toBe(59)
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
test("handles single-digit minutes", () => {
|
|
230
|
+
expect(getMinute("12:07:00")).toBe(7)
|
|
231
|
+
expect(getMinute("12:7:00")).toBe(7)
|
|
232
|
+
expect(getMinute("12:7")).toBe(7)
|
|
233
|
+
})
|
|
234
|
+
})
|
|
235
|
+
|
|
190
236
|
describe("isDate", () => {
|
|
191
237
|
test("returns true for valid YYYY-MM-DD dates", () => {
|
|
192
238
|
expect(isDate("2024-06-01")).toBe(true)
|
|
@@ -299,27 +345,22 @@ describe("addTime", () => {
|
|
|
299
345
|
expect(addTime("9:8", { hours: 0, minutes: 0 })).toBe("09:08:00")
|
|
300
346
|
})
|
|
301
347
|
|
|
302
|
-
// Edge case: negative minutes that require multiple hour underflows
|
|
303
348
|
test("handles large negative minutes", () => {
|
|
304
349
|
expect(addTime("05:10:00", { minutes: -130 })).toBe("03:00:00")
|
|
305
350
|
})
|
|
306
351
|
|
|
307
|
-
// Edge case: large positive minutes that require multiple hour rollovers
|
|
308
352
|
test("handles large positive minutes", () => {
|
|
309
353
|
expect(addTime("05:10:00", { minutes: 130 })).toBe("07:20:00")
|
|
310
354
|
})
|
|
311
355
|
|
|
312
|
-
// Edge case: input with seconds omitted
|
|
313
356
|
test("handles input with no seconds", () => {
|
|
314
357
|
expect(addTime("12:34", { minutes: 0 })).toBe("12:34:00")
|
|
315
358
|
})
|
|
316
359
|
|
|
317
|
-
// Edge case: input with all zeros
|
|
318
360
|
test("handles midnight", () => {
|
|
319
361
|
expect(addTime("00:00:00", { hours: 0, minutes: 0 })).toBe("00:00:00")
|
|
320
362
|
})
|
|
321
363
|
|
|
322
|
-
// New: test default parameters (no options argument)
|
|
323
364
|
test("handles missing options argument (all defaults)", () => {
|
|
324
365
|
expect(addTime("12:34:56")).toBe("12:34:56")
|
|
325
366
|
expect(addTime("05:10")).toBe("05:10:00")
|
|
@@ -391,14 +432,12 @@ describe("addDays", () => {
|
|
|
391
432
|
expect(addDays("2024-01-01", { days: -1 })).toBe("2023-12-31")
|
|
392
433
|
})
|
|
393
434
|
|
|
394
|
-
// DST boundary: adding days across US DST start (spring forward)
|
|
395
435
|
test("adds days across DST start (spring forward) (object param)", () => {
|
|
396
436
|
expect(addDays("2024-03-09", { days: 1 })).toBe("2024-03-10")
|
|
397
437
|
expect(addDays("2024-03-09", { days: 2 })).toBe("2024-03-11")
|
|
398
438
|
expect(addDays("2024-03-10", { days: -1 })).toBe("2024-03-09")
|
|
399
439
|
})
|
|
400
440
|
|
|
401
|
-
// DST boundary: adding days across US DST end (fall back)
|
|
402
441
|
test("adds days across DST end (fall back) (object param)", () => {
|
|
403
442
|
expect(addDays("2024-11-02", { days: 1 })).toBe("2024-11-03")
|
|
404
443
|
expect(addDays("2024-11-02", { days: 2 })).toBe("2024-11-04")
|