@tim-code/my-util 0.0.19 → 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 +7 -6
- package/src/time.test.js +10 -16
package/package.json
CHANGED
package/src/time.js
CHANGED
|
@@ -121,23 +121,24 @@ export function addTime(timeString, { minutes = 0, hours = 0 } = {}) {
|
|
|
121
121
|
|
|
122
122
|
const MINUTES_IN_DAY = 24 * 60
|
|
123
123
|
/**
|
|
124
|
-
* Get all minutes between two times.
|
|
124
|
+
* Get all minutes between two times.
|
|
125
|
+
* This does not work across day i.e 23:59:00 to 00:00:00.
|
|
125
126
|
* @param {string} start HH:mm:ss or HH:mm
|
|
126
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
|
|
127
131
|
* @returns {Array} times in HH:mm:ss
|
|
128
132
|
*/
|
|
129
|
-
export function
|
|
133
|
+
export function getTimeRange(start, end, { hours = 0, minutes = 1 } = {}) {
|
|
130
134
|
// coerce start and end to seconds
|
|
131
135
|
start = addTime(start)
|
|
132
136
|
end = addTime(end)
|
|
133
|
-
if (!(start <= end)) {
|
|
134
|
-
throw new Error("start time is not less than end time")
|
|
135
|
-
}
|
|
136
137
|
const times = []
|
|
137
138
|
let current = start
|
|
138
139
|
while (current <= end) {
|
|
139
140
|
times.push(current)
|
|
140
|
-
current = addTime(current, { minutes
|
|
141
|
+
current = addTime(current, { hours, minutes })
|
|
141
142
|
if (times.length >= MINUTES_IN_DAY) {
|
|
142
143
|
break
|
|
143
144
|
}
|
package/src/time.test.js
CHANGED
|
@@ -4,8 +4,8 @@ import {
|
|
|
4
4
|
addTime,
|
|
5
5
|
getDateRange,
|
|
6
6
|
getEasternTime,
|
|
7
|
-
getMinuteRange,
|
|
8
7
|
getTime,
|
|
8
|
+
getTimeRange,
|
|
9
9
|
isDate,
|
|
10
10
|
isTime,
|
|
11
11
|
isUnixTimestamp,
|
|
@@ -326,9 +326,9 @@ describe("addTime", () => {
|
|
|
326
326
|
})
|
|
327
327
|
})
|
|
328
328
|
|
|
329
|
-
describe("
|
|
329
|
+
describe("getTimeRange", () => {
|
|
330
330
|
test("returns all minutes between start and end inclusive", () => {
|
|
331
|
-
expect(
|
|
331
|
+
expect(getTimeRange("12:00:00", "12:03:00")).toEqual([
|
|
332
332
|
"12:00:00",
|
|
333
333
|
"12:01:00",
|
|
334
334
|
"12:02:00",
|
|
@@ -337,7 +337,7 @@ describe("getMinuteRange", () => {
|
|
|
337
337
|
})
|
|
338
338
|
|
|
339
339
|
test("works with times that cross the hour", () => {
|
|
340
|
-
expect(
|
|
340
|
+
expect(getTimeRange("12:58:00", "13:01:00")).toEqual([
|
|
341
341
|
"12:58:00",
|
|
342
342
|
"12:59:00",
|
|
343
343
|
"13:00:00",
|
|
@@ -346,26 +346,20 @@ describe("getMinuteRange", () => {
|
|
|
346
346
|
})
|
|
347
347
|
|
|
348
348
|
test("works with times with nonzero seconds", () => {
|
|
349
|
-
expect(
|
|
350
|
-
"12:00:30",
|
|
351
|
-
"12:01:30",
|
|
352
|
-
"12:02:30",
|
|
353
|
-
])
|
|
349
|
+
expect(getTimeRange("12:00:30", "12:02:30")).toEqual(["12:00:30", "12:01:30", "12:02:30"])
|
|
354
350
|
})
|
|
355
351
|
|
|
356
352
|
test("returns single element if start equals end", () => {
|
|
357
|
-
expect(
|
|
353
|
+
expect(getTimeRange("12:34:56", "12:34:56")).toEqual(["12:34:56"])
|
|
358
354
|
})
|
|
359
355
|
|
|
360
|
-
test("
|
|
361
|
-
expect((
|
|
362
|
-
|
|
363
|
-
)
|
|
364
|
-
expect(() => getMinuteRange("12:00:00", "12:00:00")).not.toThrow()
|
|
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"])
|
|
365
359
|
})
|
|
366
360
|
|
|
367
361
|
test("handles input with no seconds", () => {
|
|
368
|
-
expect(
|
|
362
|
+
expect(getTimeRange("12:00", "12:02")).toEqual(["12:00:00", "12:01:00", "12:02:00"])
|
|
369
363
|
})
|
|
370
364
|
})
|
|
371
365
|
|