@tim-code/my-util 0.8.1 → 0.8.2
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 +20 -0
- package/src/time.test.js +31 -1
package/package.json
CHANGED
package/src/time.js
CHANGED
|
@@ -260,3 +260,23 @@ export function getStartOfWeek(dateString) {
|
|
|
260
260
|
const start = addDays(dateString, { days: -index })
|
|
261
261
|
return start
|
|
262
262
|
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Returns number of seconds from any combination of weeks, days, hours, and minutes.
|
|
266
|
+
* @param {Object} $1
|
|
267
|
+
* @param {number=} $1.weeks Default is 0
|
|
268
|
+
* @param {number=} $1.days Default is 0
|
|
269
|
+
* @param {number=} $1.hours Default is 0
|
|
270
|
+
* @param {number=} $1.minutes Default is 0
|
|
271
|
+
* @returns {number}
|
|
272
|
+
*/
|
|
273
|
+
export function convertToSeconds({ weeks = 0, days = 0, hours = 0, minutes = 0 }) {
|
|
274
|
+
if (!(weeks >= 0) || !(days >= 0) || !(hours >= 0) || !(minutes >= 0)) {
|
|
275
|
+
throw new Error("weeks, days, hours, and minutes must be nonnegative")
|
|
276
|
+
}
|
|
277
|
+
days += weeks * 7
|
|
278
|
+
hours += days * 24
|
|
279
|
+
minutes += hours * 60
|
|
280
|
+
const seconds = minutes * 60
|
|
281
|
+
return seconds
|
|
282
|
+
}
|
package/src/time.test.js
CHANGED
|
@@ -2,6 +2,7 @@ import { describe, expect, test } from "@jest/globals"
|
|
|
2
2
|
import {
|
|
3
3
|
addDays,
|
|
4
4
|
addTime,
|
|
5
|
+
convertToSeconds,
|
|
5
6
|
getDateRange,
|
|
6
7
|
getDayIndexInWeek,
|
|
7
8
|
getEasternTime,
|
|
@@ -21,7 +22,7 @@ import {
|
|
|
21
22
|
// Exported functions:
|
|
22
23
|
// getEasternTime, getTime, getUnixTimestamp, today, getDayIndexInWeek, getMinute,
|
|
23
24
|
// isDateString, isTimeString, isDateTimeString, isUTCString, isUnixTimestamp,
|
|
24
|
-
// addTime, getTimeRange, addDays, getDateRange, getStartOfWeek
|
|
25
|
+
// addTime, getTimeRange, addDays, getDateRange, getStartOfWeek, convertToSeconds
|
|
25
26
|
|
|
26
27
|
describe("getEasternTime", () => {
|
|
27
28
|
test("returns correct structure and types", () => {
|
|
@@ -586,3 +587,32 @@ describe("getStartOfWeek", () => {
|
|
|
586
587
|
expect(getStartOfWeek("2023-01-02")).toBe("2023-01-01")
|
|
587
588
|
})
|
|
588
589
|
})
|
|
590
|
+
|
|
591
|
+
describe("convertToSeconds", () => {
|
|
592
|
+
test("returns 0 for empty input object", () => {
|
|
593
|
+
expect(convertToSeconds({})).toBe(0)
|
|
594
|
+
})
|
|
595
|
+
|
|
596
|
+
test("converts single units correctly", () => {
|
|
597
|
+
expect(convertToSeconds({ minutes: 5 })).toBe(5 * 60)
|
|
598
|
+
expect(convertToSeconds({ hours: 2 })).toBe(2 * 3600)
|
|
599
|
+
expect(convertToSeconds({ days: 1 })).toBe(24 * 3600)
|
|
600
|
+
expect(convertToSeconds({ weeks: 1 })).toBe(7 * 24 * 3600)
|
|
601
|
+
})
|
|
602
|
+
|
|
603
|
+
test("converts combined weeks, days, hours, and minutes", () => {
|
|
604
|
+
// 1w + 2d + 3h + 4m = 788640 seconds
|
|
605
|
+
expect(convertToSeconds({ weeks: 1, days: 2, hours: 3, minutes: 4 })).toBe(788640)
|
|
606
|
+
})
|
|
607
|
+
|
|
608
|
+
test("supports fractional values (minutes: 0.5 => 30 seconds)", () => {
|
|
609
|
+
expect(convertToSeconds({ minutes: 0.5 })).toBe(30)
|
|
610
|
+
})
|
|
611
|
+
|
|
612
|
+
test("throws if any value is negative", () => {
|
|
613
|
+
const cases = [{ weeks: -1 }, { days: -1 }, { hours: -1 }, { minutes: -1 }]
|
|
614
|
+
for (const opts of cases) {
|
|
615
|
+
expect(() => convertToSeconds(opts)).toThrow("must be nonnegative")
|
|
616
|
+
}
|
|
617
|
+
})
|
|
618
|
+
})
|