@tim-code/my-util 0.0.4 → 0.0.5
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 +16 -5
- package/src/array.test.js +27 -1
- package/src/time.js +1 -2
package/package.json
CHANGED
package/src/array.js
CHANGED
|
@@ -93,14 +93,25 @@ export function descending(key) {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
/**
|
|
96
|
-
*
|
|
97
|
-
* @param {string}
|
|
98
|
-
* @returns {
|
|
96
|
+
* Creates a function that accesses an object's value at key.
|
|
97
|
+
* @param {string} key
|
|
98
|
+
* @returns {any}
|
|
99
99
|
*/
|
|
100
|
-
export function
|
|
101
|
-
return
|
|
100
|
+
export function via(key) {
|
|
101
|
+
return (object) => object[key]
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
// export function contains(template) {
|
|
105
|
+
// return (object) => {
|
|
106
|
+
// for (const key in template) {
|
|
107
|
+
// if (object[key] !== template[key]) {
|
|
108
|
+
// return false
|
|
109
|
+
// }
|
|
110
|
+
// }
|
|
111
|
+
// return true
|
|
112
|
+
// }
|
|
113
|
+
// }
|
|
114
|
+
|
|
104
115
|
// not sure how far we want to go down "key" rabbit hole:
|
|
105
116
|
// export function sum(array, key) {}
|
|
106
117
|
// or maybe
|
package/src/array.test.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable no-restricted-syntax */
|
|
2
2
|
import { describe, expect, it, jest } from "@jest/globals"
|
|
3
3
|
|
|
4
|
-
const { chunk, unique, mutateValues, ascending, descending } = await import("./array.js")
|
|
4
|
+
const { chunk, unique, mutateValues, ascending, descending, via } = await import("./array.js")
|
|
5
5
|
|
|
6
6
|
describe("chunk", () => {
|
|
7
7
|
it("splits array into chunks of specified size", () => {
|
|
@@ -187,3 +187,29 @@ describe("descending", () => {
|
|
|
187
187
|
expect(arr.map((o) => o.v)).toEqual([3, 2, undefined])
|
|
188
188
|
})
|
|
189
189
|
})
|
|
190
|
+
|
|
191
|
+
describe("via", () => {
|
|
192
|
+
it("returns a function that accesses the given key", () => {
|
|
193
|
+
const getFoo = via("foo")
|
|
194
|
+
expect(getFoo({ foo: 42 })).toBe(42)
|
|
195
|
+
expect(getFoo({ foo: "bar" })).toBe("bar")
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
it("returns undefined if the key does not exist", () => {
|
|
199
|
+
const getX = via("x")
|
|
200
|
+
expect(getX({})).toBeUndefined()
|
|
201
|
+
expect(getX({ y: 1 })).toBeUndefined()
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
it("works with numeric keys", () => {
|
|
205
|
+
const get0 = via(0)
|
|
206
|
+
expect(get0([10, 20])).toBe(10)
|
|
207
|
+
expect(get0({ 0: "zero" })).toBe("zero")
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
it("returns undefined if object is missing", () => {
|
|
211
|
+
const getFoo = via("foo")
|
|
212
|
+
expect(() => getFoo(undefined)).toThrow(TypeError)
|
|
213
|
+
expect(() => getFoo(null)).toThrow(TypeError)
|
|
214
|
+
})
|
|
215
|
+
})
|
package/src/time.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { parseIntSafe } from "./array.js"
|
|
2
1
|
import { mod } from "./math.js"
|
|
3
2
|
|
|
4
3
|
/**
|
|
@@ -84,7 +83,7 @@ export function isUnixTimestamp(ts, { max = 9999999999 } = {}) {
|
|
|
84
83
|
* @returns {string} HH:mm:ss
|
|
85
84
|
*/
|
|
86
85
|
export function addTime(timeString, { minutes = 0, hours = 0 }) {
|
|
87
|
-
let [hour, minute, second = 0] = timeString.split(":").map(
|
|
86
|
+
let [hour, minute, second = 0] = timeString.split(":").map(Number)
|
|
88
87
|
hour = mod(hour + hours, 24)
|
|
89
88
|
minute += minutes
|
|
90
89
|
while (minute >= 60) {
|