@tim-code/my-util 0.0.18 → 0.0.20
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 +31 -4
- package/src/time.test.js +47 -0
package/package.json
CHANGED
package/src/time.js
CHANGED
|
@@ -95,7 +95,7 @@ export function isUnixTimestamp(ts, { max = 9999999999 } = {}) {
|
|
|
95
95
|
|
|
96
96
|
/**
|
|
97
97
|
* Add an amount of time to a time string.
|
|
98
|
-
* @param {string} timeString HH:mm:ss or HH:mm
|
|
98
|
+
* @param {string} timeString HH:mm:ss or HH:mm
|
|
99
99
|
* @param {Object} $1
|
|
100
100
|
* @param {number} $1.hours Hours to add to time string
|
|
101
101
|
* @param {number} $1.minutes Minutes to add to time string
|
|
@@ -119,9 +119,36 @@ export function addTime(timeString, { minutes = 0, hours = 0 } = {}) {
|
|
|
119
119
|
return newTime
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
+
const MINUTES_IN_DAY = 24 * 60
|
|
123
|
+
/**
|
|
124
|
+
* Get all minutes between two times.
|
|
125
|
+
* This does not work across day i.e 23:59:00 to 00:00:00.
|
|
126
|
+
* @param {string} start HH:mm:ss or HH:mm
|
|
127
|
+
* @param {string} end HH:mm:ss or HH:mm
|
|
128
|
+
* @param {Object} $1
|
|
129
|
+
* @param {number} $1.hours Hours to add to get next time in range. Default 0
|
|
130
|
+
* @param {number} $1.minutes Minutes to add to get next time in range. Default 1
|
|
131
|
+
* @returns {Array} times in HH:mm:ss
|
|
132
|
+
*/
|
|
133
|
+
export function getTimeRange(start, end, { hours = 0, minutes = 1 } = {}) {
|
|
134
|
+
// coerce start and end to seconds
|
|
135
|
+
start = addTime(start)
|
|
136
|
+
end = addTime(end)
|
|
137
|
+
const times = []
|
|
138
|
+
let current = start
|
|
139
|
+
while (current <= end) {
|
|
140
|
+
times.push(current)
|
|
141
|
+
current = addTime(current, { hours, minutes })
|
|
142
|
+
if (times.length >= MINUTES_IN_DAY) {
|
|
143
|
+
break
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return times
|
|
147
|
+
}
|
|
148
|
+
|
|
122
149
|
/**
|
|
123
150
|
* Adds a number of days to a date string.
|
|
124
|
-
* @param {string} dateString
|
|
151
|
+
* @param {string} dateString YYYY-MM-DD
|
|
125
152
|
* @param {number} days
|
|
126
153
|
* @returns {string}
|
|
127
154
|
*/
|
|
@@ -134,8 +161,8 @@ export function addDays(dateString, days = 0) {
|
|
|
134
161
|
|
|
135
162
|
/**
|
|
136
163
|
* Get all dates between two dates, with limit.
|
|
137
|
-
* @param {string} start
|
|
138
|
-
* @param {string} end
|
|
164
|
+
* @param {string} start YYYY-MM-DD
|
|
165
|
+
* @param {string} end YYYY-MM-DD
|
|
139
166
|
* @returns {Array}
|
|
140
167
|
*/
|
|
141
168
|
export function getDateRange(start, end, { limit = 1000 } = {}) {
|
package/src/time.test.js
CHANGED
|
@@ -5,9 +5,11 @@ import {
|
|
|
5
5
|
getDateRange,
|
|
6
6
|
getEasternTime,
|
|
7
7
|
getTime,
|
|
8
|
+
getTimeRange,
|
|
8
9
|
isDate,
|
|
9
10
|
isTime,
|
|
10
11
|
isUnixTimestamp,
|
|
12
|
+
today,
|
|
11
13
|
} from "./time.js"
|
|
12
14
|
|
|
13
15
|
// getEasternTime changed: now accepts a "timezone" param and no longer returns "minute" or "datetime"
|
|
@@ -177,6 +179,14 @@ describe("getTime", () => {
|
|
|
177
179
|
})
|
|
178
180
|
})
|
|
179
181
|
|
|
182
|
+
describe("today", () => {
|
|
183
|
+
test("returns today's date in YYYY-MM-DD format", () => {
|
|
184
|
+
const expected = getTime().date
|
|
185
|
+
expect(today()).toBe(expected)
|
|
186
|
+
expect(/^\d{4}-\d{2}-\d{2}$/u.test(today())).toBe(true)
|
|
187
|
+
})
|
|
188
|
+
})
|
|
189
|
+
|
|
180
190
|
describe("isDate", () => {
|
|
181
191
|
test("returns true for valid YYYY-MM-DD dates", () => {
|
|
182
192
|
expect(isDate("2024-06-01")).toBe(true)
|
|
@@ -316,6 +326,43 @@ describe("addTime", () => {
|
|
|
316
326
|
})
|
|
317
327
|
})
|
|
318
328
|
|
|
329
|
+
describe("getTimeRange", () => {
|
|
330
|
+
test("returns all minutes between start and end inclusive", () => {
|
|
331
|
+
expect(getTimeRange("12:00:00", "12:03:00")).toEqual([
|
|
332
|
+
"12:00:00",
|
|
333
|
+
"12:01:00",
|
|
334
|
+
"12:02:00",
|
|
335
|
+
"12:03:00",
|
|
336
|
+
])
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
test("works with times that cross the hour", () => {
|
|
340
|
+
expect(getTimeRange("12:58:00", "13:01:00")).toEqual([
|
|
341
|
+
"12:58:00",
|
|
342
|
+
"12:59:00",
|
|
343
|
+
"13:00:00",
|
|
344
|
+
"13:01:00",
|
|
345
|
+
])
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
test("works with times with nonzero seconds", () => {
|
|
349
|
+
expect(getTimeRange("12:00:30", "12:02:30")).toEqual(["12:00:30", "12:01:30", "12:02:30"])
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
test("returns single element if start equals end", () => {
|
|
353
|
+
expect(getTimeRange("12:34:56", "12:34:56")).toEqual(["12:34:56"])
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
test("handles if start is greater or equal to end", () => {
|
|
357
|
+
expect(getTimeRange("12:04:00", "12:03:00")).toEqual([])
|
|
358
|
+
expect(getTimeRange("12:00:00", "12:00:00")).toEqual(["12:00:00"])
|
|
359
|
+
})
|
|
360
|
+
|
|
361
|
+
test("handles input with no seconds", () => {
|
|
362
|
+
expect(getTimeRange("12:00", "12:02")).toEqual(["12:00:00", "12:01:00", "12:02:00"])
|
|
363
|
+
})
|
|
364
|
+
})
|
|
365
|
+
|
|
319
366
|
describe("addDays", () => {
|
|
320
367
|
test("adds days within the same month", () => {
|
|
321
368
|
expect(addDays("2024-06-01", 5)).toBe("2024-06-06")
|