@tim-code/my-util 0.0.7 → 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 +1 -1
- package/src/time.js +19 -1
- package/src/time.test.js +60 -1
package/package.json
CHANGED
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 {
|
|
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
|
+
})
|