@tim-code/my-util 0.0.1 → 0.0.3
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 +4 -1
- package/src/array.js +72 -0
- package/src/array.test.js +121 -3
- package/src/fs.js +3 -0
- package/src/fs.test.js +3 -0
- package/src/time.js +40 -0
- package/src/time.test.js +75 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tim-code/my-util",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "",
|
|
@@ -23,5 +23,8 @@
|
|
|
23
23
|
},
|
|
24
24
|
"jest": {
|
|
25
25
|
"transform": {}
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
26
29
|
}
|
|
27
30
|
}
|
package/src/array.js
CHANGED
|
@@ -27,3 +27,75 @@ export function chunk(array, chunkSize = array.length) {
|
|
|
27
27
|
export function unique(array) {
|
|
28
28
|
return [...new Set(array)]
|
|
29
29
|
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Mutates the passed in object by calling callback on each of its values.
|
|
33
|
+
* @param {Object} object
|
|
34
|
+
* @param {Function} callback (value, key, object) => newValue // note if not changing value, should return value
|
|
35
|
+
* @returns {Object}
|
|
36
|
+
*/
|
|
37
|
+
export function mutateValues(object, callback) {
|
|
38
|
+
for (const key in object) {
|
|
39
|
+
object[key] = callback(object[key], key, object)
|
|
40
|
+
}
|
|
41
|
+
return object
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// sorts undefined and null to the end if applicable
|
|
45
|
+
function compareUndefinedNull(a, b) {
|
|
46
|
+
if (b === undefined || b === null) {
|
|
47
|
+
if (a === undefined || a === null) {
|
|
48
|
+
return 0
|
|
49
|
+
}
|
|
50
|
+
return -1
|
|
51
|
+
} else if (a === undefined || a === null) {
|
|
52
|
+
return 1
|
|
53
|
+
}
|
|
54
|
+
return undefined
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Returns an "ascending" comparator, via "<", to be used to sort an array.
|
|
59
|
+
* Undefined or null values are always sorted to the end.
|
|
60
|
+
* @param {String=} key If sorting objects, can specify a key to use to compare.
|
|
61
|
+
* @returns {Function}
|
|
62
|
+
*/
|
|
63
|
+
export function ascending(key) {
|
|
64
|
+
if (!key) {
|
|
65
|
+
return (a, b) => {
|
|
66
|
+
return compareUndefinedNull(a, b) ?? (a < b ? -1 : b < a ? 1 : 0)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return (a, b) => {
|
|
70
|
+
return (
|
|
71
|
+
compareUndefinedNull(a[key], b[key]) ?? (a[key] < b[key] ? -1 : b[key] < a[key] ? 1 : 0)
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Returns a "descending" comparator, via ">", to be used to sort an array.
|
|
78
|
+
* Undefined or null values are always sorted to the end.
|
|
79
|
+
* @param {String=} key If sorting objects, can specify a key to use to compare.
|
|
80
|
+
* @returns {Function}
|
|
81
|
+
*/
|
|
82
|
+
export function descending(key) {
|
|
83
|
+
if (!key) {
|
|
84
|
+
return (a, b) => {
|
|
85
|
+
return compareUndefinedNull(a, b) ?? (a > b ? -1 : b > a ? 1 : 0)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return (a, b) => {
|
|
89
|
+
return (
|
|
90
|
+
compareUndefinedNull(a[key], b[key]) ?? (a[key] > b[key] ? -1 : b[key] > a[key] ? 1 : 0)
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// not sure how far we want to go down "key" rabbit hole:
|
|
96
|
+
// export function sum(array, key) {}
|
|
97
|
+
// or maybe
|
|
98
|
+
// export function add(key) {
|
|
99
|
+
// if(!key) return (acc, value) => acc + value
|
|
100
|
+
// return (acc, obj) => acc + obj[key]
|
|
101
|
+
// }
|
package/src/array.test.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
import { describe, expect, it } from "@jest/globals"
|
|
1
|
+
/* eslint-disable no-restricted-syntax */
|
|
2
|
+
import { describe, expect, it, jest } from "@jest/globals"
|
|
3
3
|
|
|
4
|
-
const { chunk, unique } = await import("./array.js")
|
|
4
|
+
const { chunk, unique, mutateValues, ascending, descending } = await import("./array.js")
|
|
5
5
|
|
|
6
6
|
describe("chunk", () => {
|
|
7
7
|
it("splits array into chunks of specified size", () => {
|
|
@@ -69,3 +69,121 @@ describe("unique", () => {
|
|
|
69
69
|
expect(unique([a, b, a])).toEqual([a, b])
|
|
70
70
|
})
|
|
71
71
|
})
|
|
72
|
+
|
|
73
|
+
describe("mutateValues", () => {
|
|
74
|
+
it("mutates values in the object using the callback", () => {
|
|
75
|
+
const obj = { a: 1, b: 2 }
|
|
76
|
+
const result = mutateValues(obj, (v) => v * 2)
|
|
77
|
+
expect(result).toEqual({ a: 2, b: 4 })
|
|
78
|
+
expect(obj).toBe(result) // should mutate in place
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it("callback receives value, key, and object", () => {
|
|
82
|
+
const obj = { x: 1 }
|
|
83
|
+
const cb = jest.fn((v) => v + 1)
|
|
84
|
+
mutateValues(obj, cb)
|
|
85
|
+
expect(cb).toHaveBeenCalledWith(1, "x", obj)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it("returns the same object reference", () => {
|
|
89
|
+
const obj = { foo: "bar" }
|
|
90
|
+
const returned = mutateValues(obj, (v) => v)
|
|
91
|
+
expect(returned).toBe(obj)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it("handles empty object", () => {
|
|
95
|
+
const obj = {}
|
|
96
|
+
expect(mutateValues(obj, (v) => v)).toEqual({})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it("mutates inherited enumerable properties", () => {
|
|
100
|
+
const proto = { inherited: 1 }
|
|
101
|
+
const obj = Object.create(proto)
|
|
102
|
+
obj.own = 2
|
|
103
|
+
const result = mutateValues(obj, (v) => v + 1)
|
|
104
|
+
expect(result.own).toBe(3)
|
|
105
|
+
expect(result.inherited).toBe(2)
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
describe("ascending", () => {
|
|
110
|
+
it("sorts primitives ascending, undefined/null at end", () => {
|
|
111
|
+
const arr = [undefined, null, 3, 1, 2]
|
|
112
|
+
arr.sort(ascending())
|
|
113
|
+
expect(arr).toEqual([1, 2, 3, null, undefined])
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it("returns 0 for equal values", () => {
|
|
117
|
+
expect(ascending()(2, 2)).toBe(0)
|
|
118
|
+
expect(ascending()("a", "a")).toBe(0)
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it("sorts objects by key ascending, undefined/null at end", () => {
|
|
122
|
+
const arr = [{ v: undefined }, { v: 3 }, { v: null }, { v: 1 }, { v: 2 }]
|
|
123
|
+
arr.sort(ascending("v"))
|
|
124
|
+
expect(arr.map((o) => o.v)).toEqual([1, 2, 3, undefined, null])
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
it("returns 0 for equal key values", () => {
|
|
128
|
+
expect(ascending("x")({ x: 5 }, { x: 5 })).toBe(0)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it("sorts negative numbers and zero correctly", () => {
|
|
132
|
+
const arr = [0, -2, -1, 2]
|
|
133
|
+
arr.sort(ascending())
|
|
134
|
+
expect(arr).toEqual([-2, -1, 0, 2])
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it("sorts strings alphabetically", () => {
|
|
138
|
+
const arr = ["b", "a", "c"]
|
|
139
|
+
arr.sort(ascending())
|
|
140
|
+
expect(arr).toEqual(["a", "b", "c"])
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it("handles objects missing the key", () => {
|
|
144
|
+
const arr = [{ v: 2 }, {}, { v: 1 }]
|
|
145
|
+
arr.sort(ascending("v"))
|
|
146
|
+
expect(arr.map((o) => o.v)).toEqual([1, 2, undefined])
|
|
147
|
+
})
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
describe("descending", () => {
|
|
151
|
+
it("sorts primitives descending, undefined/null at end", () => {
|
|
152
|
+
const arr = [undefined, 1, null, 3, 2]
|
|
153
|
+
arr.sort(descending())
|
|
154
|
+
expect(arr).toEqual([3, 2, 1, null, undefined])
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it("returns 0 for equal values", () => {
|
|
158
|
+
expect(descending()(2, 2)).toBe(0)
|
|
159
|
+
expect(descending()("a", "a")).toBe(0)
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
it("sorts objects by key descending, undefined/null at end", () => {
|
|
163
|
+
const arr = [{ v: undefined }, { v: 1 }, { v: 3 }, { v: null }, { v: 2 }]
|
|
164
|
+
arr.sort(descending("v"))
|
|
165
|
+
expect(arr.map((o) => o.v)).toEqual([3, 2, 1, undefined, null])
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
it("returns 0 for equal key values", () => {
|
|
169
|
+
expect(descending("x")({ x: 5 }, { x: 5 })).toBe(0)
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it("sorts negative numbers and zero correctly", () => {
|
|
173
|
+
const arr = [0, -2, -1, 2]
|
|
174
|
+
arr.sort(descending())
|
|
175
|
+
expect(arr).toEqual([2, 0, -1, -2])
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
it("sorts strings reverse alphabetically", () => {
|
|
179
|
+
const arr = ["b", "a", "c"]
|
|
180
|
+
arr.sort(descending())
|
|
181
|
+
expect(arr).toEqual(["c", "b", "a"])
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
it("handles objects missing the key", () => {
|
|
185
|
+
const arr = [{ v: 2 }, {}, { v: 3 }]
|
|
186
|
+
arr.sort(descending("v"))
|
|
187
|
+
expect(arr.map((o) => o.v)).toEqual([3, 2, undefined])
|
|
188
|
+
})
|
|
189
|
+
})
|
package/src/fs.js
CHANGED
|
@@ -24,6 +24,9 @@ export async function getJSON(path) {
|
|
|
24
24
|
* @returns {Object|Array}
|
|
25
25
|
*/
|
|
26
26
|
export async function getCompressedJSON(path) {
|
|
27
|
+
if (!path.endsWith(".gz")) {
|
|
28
|
+
throw new Error("path does not suggest a compressed file")
|
|
29
|
+
}
|
|
27
30
|
const buffer = await readFile(path)
|
|
28
31
|
const uncompressed = await gunzip(buffer)
|
|
29
32
|
return JSON.parse(uncompressed.toString())
|
package/src/fs.test.js
CHANGED
|
@@ -42,6 +42,9 @@ describe("getCompressedJSON", () => {
|
|
|
42
42
|
expect(readFileMock).toHaveBeenCalledWith("bar.gz")
|
|
43
43
|
expect(gunzipMock).toHaveBeenCalled()
|
|
44
44
|
})
|
|
45
|
+
it("throws for bad filename", async () => {
|
|
46
|
+
await expect(getCompressedJSON("bar")).rejects.toThrow(/a compressed file/u)
|
|
47
|
+
})
|
|
45
48
|
})
|
|
46
49
|
|
|
47
50
|
describe("pathExists", () => {
|
package/src/time.js
CHANGED
|
@@ -30,3 +30,43 @@ export function getEasternTime({ days = 0, floorMinute = false } = {}) {
|
|
|
30
30
|
const datetime = `${date} ${time}`
|
|
31
31
|
return { timestamp, date, time, minute, datetime }
|
|
32
32
|
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Checks if the string represents a valid YYYY-MM-DD date.
|
|
36
|
+
* This will return false for dates like "2024-02-31".
|
|
37
|
+
* @param {string} string
|
|
38
|
+
* @returns {boolean}
|
|
39
|
+
*/
|
|
40
|
+
export function isDate(string) {
|
|
41
|
+
const match = /^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/u.exec(string)
|
|
42
|
+
if (!match) {
|
|
43
|
+
return false
|
|
44
|
+
}
|
|
45
|
+
const [, year, month, day] = match.map(Number)
|
|
46
|
+
const date = new Date(`${string}T00:00:00Z`)
|
|
47
|
+
return (
|
|
48
|
+
date.getUTCFullYear() === year &&
|
|
49
|
+
date.getUTCMonth() + 1 === month &&
|
|
50
|
+
date.getUTCDate() === day
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Checks if the string represent a valid HH:mm:ss time.
|
|
56
|
+
* @param {string} string
|
|
57
|
+
* @returns {boolean}
|
|
58
|
+
*/
|
|
59
|
+
export function isTime(string) {
|
|
60
|
+
return /^([01]\d|2[0-3]):[0-5]\d:[0-5]\d$/u.test(string)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Checks if a number is a Unix timestamp (i.e. in seconds).
|
|
65
|
+
* @param {number} ts
|
|
66
|
+
* @param {Object} $1
|
|
67
|
+
* @param {number} $1.max Maximum value for timestamp to allow - default is up to ~2286-11-20; this allows catching ms timestamps
|
|
68
|
+
* @returns {boolean}
|
|
69
|
+
*/
|
|
70
|
+
export function isUnixTimestamp(ts, { max = 9999999999 } = {}) {
|
|
71
|
+
return Number.isInteger(ts) && ts >= 0 && ts <= max
|
|
72
|
+
}
|
package/src/time.test.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, test } from "@jest/globals"
|
|
2
|
-
import { getEasternTime } from "./time.js"
|
|
2
|
+
import { getEasternTime, isDate, isTime, isUnixTimestamp } from "./time.js"
|
|
3
3
|
|
|
4
4
|
describe("getEasternTime", () => {
|
|
5
5
|
test("returns correct structure and types", () => {
|
|
@@ -46,3 +46,77 @@ describe("getEasternTime", () => {
|
|
|
46
46
|
expect(def.datetime).toEqual(explicit.datetime)
|
|
47
47
|
})
|
|
48
48
|
})
|
|
49
|
+
|
|
50
|
+
describe("isDate", () => {
|
|
51
|
+
test("returns true for valid YYYY-MM-DD dates", () => {
|
|
52
|
+
expect(isDate("2024-06-01")).toBe(true)
|
|
53
|
+
expect(isDate("1999-12-31")).toBe(true)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test("returns false for invalid dates (e.g., 2024-02-31)", () => {
|
|
57
|
+
expect(isDate("2024-02-31")).toBe(false)
|
|
58
|
+
expect(isDate("2023-04-31")).toBe(false)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test("returns false for invalid formats", () => {
|
|
62
|
+
expect(isDate("2024/06/01")).toBe(false)
|
|
63
|
+
expect(isDate("06-01-2024")).toBe(false)
|
|
64
|
+
expect(isDate("2024-6-1")).toBe(false)
|
|
65
|
+
expect(isDate("20240601")).toBe(false)
|
|
66
|
+
expect(isDate("abcd-ef-gh")).toBe(false)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test("returns false for impossible months and days", () => {
|
|
70
|
+
expect(isDate("2024-00-10")).toBe(false)
|
|
71
|
+
expect(isDate("2024-13-10")).toBe(false)
|
|
72
|
+
expect(isDate("2024-01-00")).toBe(false)
|
|
73
|
+
expect(isDate("2024-01-32")).toBe(false)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test("returns true for leap day", () => {
|
|
77
|
+
expect(isDate("2024-02-29")).toBe(true)
|
|
78
|
+
expect(isDate("2023-02-29")).toBe(false)
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
describe("isTime", () => {
|
|
83
|
+
test("returns true for valid HH:mm:ss times", () => {
|
|
84
|
+
expect(isTime("00:00:00")).toBe(true)
|
|
85
|
+
expect(isTime("23:59:59")).toBe(true)
|
|
86
|
+
expect(isTime("12:34:56")).toBe(true)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test("returns false for invalid times", () => {
|
|
90
|
+
expect(isTime("24:00:00")).toBe(false)
|
|
91
|
+
expect(isTime("12:60:00")).toBe(false)
|
|
92
|
+
expect(isTime("12:00:60")).toBe(false)
|
|
93
|
+
expect(isTime("1:00:00")).toBe(false)
|
|
94
|
+
expect(isTime("12:0:00")).toBe(false)
|
|
95
|
+
expect(isTime("12:00:0")).toBe(false)
|
|
96
|
+
expect(isTime("12:00")).toBe(false)
|
|
97
|
+
expect(isTime("120000")).toBe(false)
|
|
98
|
+
expect(isTime("ab:cd:ef")).toBe(false)
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
describe("isUnixTimestamp", () => {
|
|
103
|
+
test("returns true for valid unix timestamps in seconds", () => {
|
|
104
|
+
expect(isUnixTimestamp(0)).toBe(true)
|
|
105
|
+
expect(isUnixTimestamp(1700000000)).toBe(true)
|
|
106
|
+
expect(isUnixTimestamp(9999999999)).toBe(true)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test("returns false for negative or non-integer or too large values", () => {
|
|
110
|
+
expect(isUnixTimestamp(-1)).toBe(false)
|
|
111
|
+
expect(isUnixTimestamp(1.5)).toBe(false)
|
|
112
|
+
expect(isUnixTimestamp(10000000000)).toBe(false)
|
|
113
|
+
expect(isUnixTimestamp(NaN)).toBe(false)
|
|
114
|
+
expect(isUnixTimestamp(Infinity)).toBe(false)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
test("respects custom max option", () => {
|
|
118
|
+
expect(isUnixTimestamp(100, { max: 50 })).toBe(false)
|
|
119
|
+
expect(isUnixTimestamp(50, { max: 50 })).toBe(true)
|
|
120
|
+
expect(isUnixTimestamp(51, { max: 50 })).toBe(false)
|
|
121
|
+
})
|
|
122
|
+
})
|