@tim-code/my-util 0.0.3 → 0.0.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/array.js +9 -0
- package/src/index.js +1 -0
- package/src/math.js +9 -0
- package/src/math.test.js +37 -0
- package/src/time.js +30 -0
- package/src/time.test.js +61 -2
package/package.json
CHANGED
package/src/array.js
CHANGED
|
@@ -92,6 +92,15 @@ export function descending(key) {
|
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Parse an integer in base 10. Safe to use for array.map() since it only takes one argument and ignores the rest.
|
|
97
|
+
* @param {string} number
|
|
98
|
+
* @returns {number} Integer
|
|
99
|
+
*/
|
|
100
|
+
export function parseIntSafe(number) {
|
|
101
|
+
return parseInt(number, 10)
|
|
102
|
+
}
|
|
103
|
+
|
|
95
104
|
// not sure how far we want to go down "key" rabbit hole:
|
|
96
105
|
// export function sum(array, key) {}
|
|
97
106
|
// or maybe
|
package/src/index.js
CHANGED
package/src/math.js
ADDED
package/src/math.test.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, expect, it } from "@jest/globals"
|
|
2
|
+
const { mod } = await import("./math.js")
|
|
3
|
+
|
|
4
|
+
describe("mod", () => {
|
|
5
|
+
it("returns n when n is less than m and n is non-negative", () => {
|
|
6
|
+
expect(mod(2, 3)).toBe(2)
|
|
7
|
+
expect(mod(0, 5)).toBe(0)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it("wraps negative n into the [0, m) range", () => {
|
|
11
|
+
expect(mod(-1, 3)).toBe(2)
|
|
12
|
+
expect(mod(-4, 3)).toBe(2)
|
|
13
|
+
expect(mod(-2, 5)).toBe(3)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it("returns 0 when n is a multiple of m", () => {
|
|
17
|
+
expect(mod(6, 3)).toBe(0)
|
|
18
|
+
expect(mod(-6, 3)).toBe(0)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it("handles n greater than m", () => {
|
|
22
|
+
expect(mod(7, 3)).toBe(1)
|
|
23
|
+
expect(mod(14, 5)).toBe(4)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it("handles m = 1 (should always return 0)", () => {
|
|
27
|
+
expect(mod(0, 1)).toBe(0)
|
|
28
|
+
expect(mod(5, 1)).toBe(0)
|
|
29
|
+
expect(mod(-3, 1)).toBe(0)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it("returns NaN when m is 0", () => {
|
|
33
|
+
expect(mod(5, 0)).toBeNaN()
|
|
34
|
+
expect(mod(0, 0)).toBeNaN()
|
|
35
|
+
expect(mod(-3, 0)).toBeNaN()
|
|
36
|
+
})
|
|
37
|
+
})
|
package/src/time.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { parseIntSafe } from "./array.js"
|
|
2
|
+
import { mod } from "./math.js"
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Gets various ways of representing the current time in EDT. Floors to nearest second by default.
|
|
3
6
|
* @param {Object} $1
|
|
@@ -62,6 +65,7 @@ export function isTime(string) {
|
|
|
62
65
|
|
|
63
66
|
/**
|
|
64
67
|
* Checks if a number is a Unix timestamp (i.e. in seconds).
|
|
68
|
+
* Would not validate timestamps set very far into the future.
|
|
65
69
|
* @param {number} ts
|
|
66
70
|
* @param {Object} $1
|
|
67
71
|
* @param {number} $1.max Maximum value for timestamp to allow - default is up to ~2286-11-20; this allows catching ms timestamps
|
|
@@ -70,3 +74,29 @@ export function isTime(string) {
|
|
|
70
74
|
export function isUnixTimestamp(ts, { max = 9999999999 } = {}) {
|
|
71
75
|
return Number.isInteger(ts) && ts >= 0 && ts <= max
|
|
72
76
|
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Add an amount of time to a time string.
|
|
80
|
+
* @param {string} timeString HH:mm:ss or HH:mm; parsable numbers separated by :
|
|
81
|
+
* @param {Object} $1
|
|
82
|
+
* @param {number} $1.hours Hours to add to time string
|
|
83
|
+
* @param {number} $1.minutes Minutes to add to time string
|
|
84
|
+
* @returns {string} HH:mm:ss
|
|
85
|
+
*/
|
|
86
|
+
export function addTime(timeString, { minutes = 0, hours = 0 }) {
|
|
87
|
+
let [hour, minute, second = 0] = timeString.split(":").map(parseIntSafe)
|
|
88
|
+
hour = mod(hour + hours, 24)
|
|
89
|
+
minute += minutes
|
|
90
|
+
while (minute >= 60) {
|
|
91
|
+
hour = mod(hour + 1, 24)
|
|
92
|
+
minute -= 60
|
|
93
|
+
}
|
|
94
|
+
while (minute < 0) {
|
|
95
|
+
hour = mod(hour - 1, 24)
|
|
96
|
+
minute += 60
|
|
97
|
+
}
|
|
98
|
+
const newTime = [hour, minute, second]
|
|
99
|
+
.map((number) => `${number}`.padStart(2, "0"))
|
|
100
|
+
.join(":")
|
|
101
|
+
return newTime
|
|
102
|
+
}
|
package/src/time.test.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, test } from "@jest/globals"
|
|
2
|
-
import { getEasternTime, isDate, isTime, isUnixTimestamp } from "./time.js"
|
|
2
|
+
import { addTime, getEasternTime, isDate, isTime, isUnixTimestamp } from "./time.js"
|
|
3
3
|
|
|
4
4
|
describe("getEasternTime", () => {
|
|
5
5
|
test("returns correct structure and types", () => {
|
|
@@ -119,4 +119,63 @@ describe("isUnixTimestamp", () => {
|
|
|
119
119
|
expect(isUnixTimestamp(50, { max: 50 })).toBe(true)
|
|
120
120
|
expect(isUnixTimestamp(51, { max: 50 })).toBe(false)
|
|
121
121
|
})
|
|
122
|
-
})
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
describe("addTime", () => {
|
|
125
|
+
test("adds minutes within the same hour", () => {
|
|
126
|
+
expect(addTime("12:30:00", { minutes: 15 })).toBe("12:45:00")
|
|
127
|
+
expect(addTime("12:30", { minutes: 15 })).toBe("12:45:00")
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
test("adds minutes with hour rollover", () => {
|
|
131
|
+
expect(addTime("12:50:00", { minutes: 15 })).toBe("13:05:00")
|
|
132
|
+
expect(addTime("23:50:00", { minutes: 15 })).toBe("00:05:00")
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
test("subtracts minutes with hour underflow", () => {
|
|
136
|
+
expect(addTime("12:10:00", { minutes: -15 })).toBe("11:55:00")
|
|
137
|
+
expect(addTime("00:10:00", { minutes: -15 })).toBe("23:55:00")
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
test("adds hours with 24-hour rollover", () => {
|
|
141
|
+
expect(addTime("22:15:00", { hours: 3 })).toBe("01:15:00")
|
|
142
|
+
expect(addTime("00:00:00", { hours: 24 })).toBe("00:00:00")
|
|
143
|
+
expect(addTime("23:59:59", { hours: 1 })).toBe("00:59:59")
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
test("subtracts hours with 24-hour underflow", () => {
|
|
147
|
+
expect(addTime("01:15:00", { hours: -3 })).toBe("22:15:00")
|
|
148
|
+
expect(addTime("00:00:00", { hours: -24 })).toBe("00:00:00")
|
|
149
|
+
expect(addTime("00:59:59", { hours: -1 })).toBe("23:59:59")
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
test("handles both hours and minutes together", () => {
|
|
153
|
+
expect(addTime("22:30:00", { hours: 2, minutes: 45 })).toBe("01:15:00")
|
|
154
|
+
expect(addTime("01:15:00", { hours: -2, minutes: -30 })).toBe("22:45:00")
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
test("pads single digit hours, minutes, seconds", () => {
|
|
158
|
+
expect(addTime("1:2:3", { hours: 0, minutes: 0 })).toBe("01:02:03")
|
|
159
|
+
expect(addTime("9:8", { hours: 0, minutes: 0 })).toBe("09:08:00")
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
// Edge case: negative minutes that require multiple hour underflows
|
|
163
|
+
test("handles large negative minutes", () => {
|
|
164
|
+
expect(addTime("05:10:00", { minutes: -130 })).toBe("03:00:00")
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
// Edge case: large positive minutes that require multiple hour rollovers
|
|
168
|
+
test("handles large positive minutes", () => {
|
|
169
|
+
expect(addTime("05:10:00", { minutes: 130 })).toBe("07:20:00")
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
// Edge case: input with seconds omitted
|
|
173
|
+
test("handles input with no seconds", () => {
|
|
174
|
+
expect(addTime("12:34", { minutes: 0 })).toBe("12:34:00")
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
// Edge case: input with all zeros
|
|
178
|
+
test("handles midnight", () => {
|
|
179
|
+
expect(addTime("00:00:00", { hours: 0, minutes: 0 })).toBe("00:00:00")
|
|
180
|
+
})
|
|
181
|
+
})
|