@tim-code/my-util 0.2.3 → 0.2.4
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 +16 -0
- package/src/time.test.js +28 -0
package/package.json
CHANGED
package/src/time.js
CHANGED
|
@@ -52,6 +52,22 @@ export function today() {
|
|
|
52
52
|
return getTime().date
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Get the day of the week from a YYYY-MM-DD string.
|
|
57
|
+
* @param {string=} string YYYY-MM-DD
|
|
58
|
+
* @returns {string} "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"
|
|
59
|
+
*/
|
|
60
|
+
export function getDayOfWeek(string = today()) {
|
|
61
|
+
const [year, month, day] = string.split("-").map(Number)
|
|
62
|
+
const dayOfWeek = new Date(year, month - 1, day)
|
|
63
|
+
.toLocaleDateString("en-US", { weekday: "long" })
|
|
64
|
+
.toLowerCase()
|
|
65
|
+
if (dayOfWeek === "invalid date") {
|
|
66
|
+
throw new Error(`invalid date passed: ${string}`)
|
|
67
|
+
}
|
|
68
|
+
return dayOfWeek
|
|
69
|
+
}
|
|
70
|
+
|
|
55
71
|
/**
|
|
56
72
|
* Checks if the string represents a valid YYYY-MM-DD date.
|
|
57
73
|
* This will return false for dates like "2024-02-31".
|
package/src/time.test.js
CHANGED
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
addDays,
|
|
4
4
|
addTime,
|
|
5
5
|
getDateRange,
|
|
6
|
+
getDayOfWeek,
|
|
6
7
|
getEasternTime,
|
|
7
8
|
getTime,
|
|
8
9
|
getTimeRange,
|
|
@@ -187,6 +188,33 @@ describe("today", () => {
|
|
|
187
188
|
})
|
|
188
189
|
})
|
|
189
190
|
|
|
191
|
+
describe("getDayOfWeek", () => {
|
|
192
|
+
test("returns correct day of week for known dates", () => {
|
|
193
|
+
expect(getDayOfWeek("2024-06-01")).toBe("saturday")
|
|
194
|
+
expect(getDayOfWeek("2024-06-02")).toBe("sunday")
|
|
195
|
+
expect(getDayOfWeek("2024-06-03")).toBe("monday")
|
|
196
|
+
expect(getDayOfWeek("2024-06-04")).toBe("tuesday")
|
|
197
|
+
expect(getDayOfWeek("2024-06-05")).toBe("wednesday")
|
|
198
|
+
expect(getDayOfWeek("2024-06-06")).toBe("thursday")
|
|
199
|
+
expect(getDayOfWeek("2024-06-07")).toBe("friday")
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
test("returns correct day for leap day", () => {
|
|
203
|
+
expect(getDayOfWeek("2024-02-29")).toBe("thursday")
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
test("defaults to today() if no argument is given", () => {
|
|
207
|
+
// Should match today()'s day of week
|
|
208
|
+
const todayDate = today()
|
|
209
|
+
expect(getDayOfWeek()).toBe(getDayOfWeek(todayDate))
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
test("handles invalid date strings (returns 'invalid date')", () => {
|
|
213
|
+
expect(() => getDayOfWeek("not-a-date")).toThrow(/invalid date/u)
|
|
214
|
+
expect(getDayOfWeek("2024-02-31")).toBe("saturday")
|
|
215
|
+
})
|
|
216
|
+
})
|
|
217
|
+
|
|
190
218
|
describe("isDate", () => {
|
|
191
219
|
test("returns true for valid YYYY-MM-DD dates", () => {
|
|
192
220
|
expect(isDate("2024-06-01")).toBe(true)
|