@inglorious/utils 5.3.0 → 5.4.1
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/README.md +6 -4
- package/package.json +6 -6
- package/src/data-structures/object.js +16 -0
- package/src/data-structures/promise.js +10 -0
- package/src/storage.js +64 -13
- package/types/data-structures/object.d.ts +10 -0
- package/types/data-structures/promise.d.ts +3 -0
- package/types/storage.d.ts +37 -2
package/README.md
CHANGED
|
@@ -131,7 +131,9 @@ const scores = readJSON("high-scores", [])
|
|
|
131
131
|
writeJSON("high-scores", [...scores, 1200]) // => true
|
|
132
132
|
```
|
|
133
133
|
|
|
134
|
-
Pass another `Storage` as the last argument, such as `sessionStorage` or a fake in tests.
|
|
134
|
+
Pass another `Storage` as the last argument, such as `sessionStorage` or a fake in tests, or a function giving one: a function giving nothing means no storage.
|
|
135
|
+
|
|
136
|
+
`readItem` and `writeItem` do the same for plain strings, and also take a storage answering with promises, such as Capacitor Preferences: `readItem` then gives its promise back. `parseJSON(raw, fallback)` parses what was read.
|
|
135
137
|
|
|
136
138
|
### Using promises as they settle
|
|
137
139
|
|
|
@@ -164,14 +166,14 @@ The four largest modules are also available as top-level subpaths, so you don't
|
|
|
164
166
|
know whether a helper lives under `data-structures` or `math`:
|
|
165
167
|
|
|
166
168
|
- **`@inglorious/utils/functions`**: `compose`, `debounce`, `isFunction`, `pipe`, `throttle`.
|
|
167
|
-
- **`@inglorious/utils/object`**: Single-object helpers (`clone`, `get`, `set`, `produce`, ...) alongside multi-object helpers (`merge`, `extend`, `defaults`, ...).
|
|
169
|
+
- **`@inglorious/utils/object`**: Single-object helpers (`clone`, `get`, `pick`, `set`, `produce`, ...) alongside multi-object helpers (`merge`, `extend`, `defaults`, ...).
|
|
168
170
|
- **`@inglorious/utils/quaternion`**: `quaternion` and `combine`.
|
|
169
171
|
- **`@inglorious/utils/vector`**: Single-vector helpers (`magnitude`, `normalize`, `rotate`, ...) alongside multi-vector helpers (`cross`, `dot`, `sum`, ...).
|
|
170
172
|
|
|
171
173
|
Two modules stand on their own:
|
|
172
174
|
|
|
173
|
-
- **`@inglorious/utils/promise`**: `asSettled`, for using promises in the order they settle
|
|
174
|
-
- **`@inglorious/utils/storage`**: `readJSON` and `writeJSON`, for keeping JSON in `localStorage
|
|
175
|
+
- **`@inglorious/utils/promise`**: `asSettled`, for using promises in the order they settle, and `isPromiseLike`.
|
|
176
|
+
- **`@inglorious/utils/storage`**: `readJSON` and `writeJSON`, for keeping JSON in `localStorage`, and `readItem`, `writeItem` and `parseJSON` underneath them.
|
|
175
177
|
|
|
176
178
|
`divide`, `mod`, `multiply` and `power` accept either a scalar or one or more vectors:
|
|
177
179
|
a scalar is applied to every component, while vectors are combined component-wise.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/utils",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.4.1",
|
|
4
4
|
"description": "A set of general-purpose utility functions designed with functional programming principles in mind.",
|
|
5
5
|
"author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -107,8 +107,8 @@
|
|
|
107
107
|
"import": "./src/data-structures/promise.js"
|
|
108
108
|
},
|
|
109
109
|
"./promise": {
|
|
110
|
-
"types": "./types/promise.d.ts",
|
|
111
|
-
"import": "./src/promise.js"
|
|
110
|
+
"types": "./types/data-structures/promise.d.ts",
|
|
111
|
+
"import": "./src/data-structures/promise.js"
|
|
112
112
|
},
|
|
113
113
|
"./quaternion.js": {
|
|
114
114
|
"types": "./types/math/quaternion.d.ts",
|
|
@@ -154,9 +154,9 @@
|
|
|
154
154
|
"sideEffects": false,
|
|
155
155
|
"devDependencies": {
|
|
156
156
|
"@inglorious/eslint-config": "1.2.0",
|
|
157
|
-
"prettier": "^3.
|
|
158
|
-
"vite": "^8.
|
|
159
|
-
"vitest": "^5.
|
|
157
|
+
"prettier": "^3.10.1",
|
|
158
|
+
"vite": "^8.3.8",
|
|
159
|
+
"vitest": "^5.1.1"
|
|
160
160
|
},
|
|
161
161
|
"engines": {
|
|
162
162
|
"node": ">= 26"
|
|
@@ -316,6 +316,22 @@ export function mergeWith(merger, target, ...sources) {
|
|
|
316
316
|
.reduce((acc, source) => deepMerge(acc, source, merger), target)
|
|
317
317
|
}
|
|
318
318
|
|
|
319
|
+
/**
|
|
320
|
+
* A new object with only the given keys that the source has.
|
|
321
|
+
*
|
|
322
|
+
* @param {Object | null | undefined} source - The object to pick from.
|
|
323
|
+
* @param {string[]} keys - The keys to keep.
|
|
324
|
+
* @returns {Object} The picked properties.
|
|
325
|
+
*/
|
|
326
|
+
export function pick(source, keys) {
|
|
327
|
+
const picked = {}
|
|
328
|
+
if (source == null) return picked
|
|
329
|
+
for (const key of keys) {
|
|
330
|
+
if (key in source) picked[key] = source[key]
|
|
331
|
+
}
|
|
332
|
+
return picked
|
|
333
|
+
}
|
|
334
|
+
|
|
319
335
|
/**
|
|
320
336
|
* A utility function inspired by Immer's `produce` API. It provides a convenient
|
|
321
337
|
* way to work with immutable data structures by allowing "mutations" on a
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a value can be awaited like a promise: anything with a `then` method.
|
|
3
|
+
*
|
|
4
|
+
* @param {*} value - The value to check.
|
|
5
|
+
* @returns {boolean} `true` for a promise or thenable.
|
|
6
|
+
*/
|
|
7
|
+
export function isPromiseLike(value) {
|
|
8
|
+
return typeof value?.then === "function"
|
|
9
|
+
}
|
|
10
|
+
|
|
1
11
|
/**
|
|
2
12
|
* Yields the results of some promises in the order they settle, not the order given,
|
|
3
13
|
* so each can be used as soon as it is ready.
|
package/src/storage.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { isPromiseLike } from "./data-structures/promise.js"
|
|
2
|
+
|
|
3
|
+
// The browser's, never Node's: on a server it would be shared by every request,
|
|
4
|
+
// and Node 26 warns when it is read without --localstorage-file.
|
|
5
|
+
const DEFAULT_STORAGE = () => globalThis.window?.localStorage
|
|
2
6
|
|
|
3
7
|
/**
|
|
4
8
|
* Reads a value back as JSON.
|
|
@@ -11,17 +15,11 @@ const DEFAULT_STORAGE = () => globalThis.localStorage
|
|
|
11
15
|
*
|
|
12
16
|
* @param {string} key - Where the value is kept.
|
|
13
17
|
* @param {*} fallback - What to answer when there is nothing usable to read.
|
|
14
|
-
* @param {Storage} [storage] - Where to read from. Defaults to the browser's own.
|
|
18
|
+
* @param {Storage | (() => Storage | undefined)} [storage] - Where to read from. Defaults to the browser's own.
|
|
15
19
|
* @returns {*} The stored value, or the fallback.
|
|
16
20
|
*/
|
|
17
21
|
export function readJSON(key, fallback, storage) {
|
|
18
|
-
|
|
19
|
-
const raw = store(storage)?.getItem(key)
|
|
20
|
-
if (raw === null || raw === undefined) return fallback
|
|
21
|
-
return JSON.parse(raw)
|
|
22
|
-
} catch {
|
|
23
|
-
return fallback
|
|
24
|
-
}
|
|
22
|
+
return parseJSON(readItem(key, storage), fallback)
|
|
25
23
|
}
|
|
26
24
|
|
|
27
25
|
/**
|
|
@@ -33,20 +31,73 @@ export function readJSON(key, fallback, storage) {
|
|
|
33
31
|
*
|
|
34
32
|
* @param {string} key - Where to keep the value.
|
|
35
33
|
* @param {*} value - The value to keep.
|
|
36
|
-
* @param {Storage} [storage] - Where to write to. Defaults to the browser's own.
|
|
34
|
+
* @param {Storage | (() => Storage | undefined)} [storage] - Where to write to. Defaults to the browser's own.
|
|
37
35
|
* @returns {boolean} `true` if the value was written.
|
|
38
36
|
*/
|
|
39
37
|
export function writeJSON(key, value, storage) {
|
|
40
38
|
try {
|
|
41
|
-
|
|
39
|
+
return writeItem(key, JSON.stringify(value), storage)
|
|
40
|
+
} catch {
|
|
41
|
+
return false
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Parses what storage held, or answers the fallback when it held nothing or
|
|
47
|
+
* nothing that parses.
|
|
48
|
+
*
|
|
49
|
+
* @param {string | null | undefined} raw - What was read.
|
|
50
|
+
* @param {*} [fallback=null] - What to answer otherwise.
|
|
51
|
+
* @returns {*} The parsed value, or the fallback.
|
|
52
|
+
*/
|
|
53
|
+
export function parseJSON(raw, fallback = null) {
|
|
54
|
+
if (raw === null || raw === undefined) return fallback
|
|
55
|
+
try {
|
|
56
|
+
return JSON.parse(raw)
|
|
57
|
+
} catch {
|
|
58
|
+
return fallback
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Reads a string without throwing: absent or refusing storage reads as `null`.
|
|
64
|
+
* A storage answering with promises, such as Capacitor Preferences, gives its
|
|
65
|
+
* promise back.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} key - Where the value is kept.
|
|
68
|
+
* @param {Storage | (() => Storage | undefined)} [storage] - Where to read from, or a function giving it: one giving nothing means no storage. Defaults to the browser's own.
|
|
69
|
+
* @returns {string | null | Promise<string | null>} The stored string.
|
|
70
|
+
*/
|
|
71
|
+
export function readItem(key, storage) {
|
|
72
|
+
try {
|
|
73
|
+
return resolve(storage)?.getItem(key) ?? null
|
|
74
|
+
} catch {
|
|
75
|
+
return null
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Writes a string without throwing, and answers whether it was handed over. A
|
|
81
|
+
* storage answering with promises has its failures swallowed too.
|
|
82
|
+
*
|
|
83
|
+
* @param {string} key - Where to keep the value.
|
|
84
|
+
* @param {string} value - The value to keep.
|
|
85
|
+
* @param {Storage | (() => Storage | undefined)} [storage] - Where to write to, or a function giving it. Defaults to the browser's own.
|
|
86
|
+
* @returns {boolean} `true` if the storage took it.
|
|
87
|
+
*/
|
|
88
|
+
export function writeItem(key, value, storage) {
|
|
89
|
+
try {
|
|
90
|
+
const target = resolve(storage)
|
|
42
91
|
if (!target) return false
|
|
43
|
-
target.setItem(key,
|
|
92
|
+
const written = target.setItem(key, value)
|
|
93
|
+
if (isPromiseLike(written)) written.then(undefined, () => {})
|
|
44
94
|
return true
|
|
45
95
|
} catch {
|
|
46
96
|
return false
|
|
47
97
|
}
|
|
48
98
|
}
|
|
49
99
|
|
|
50
|
-
function
|
|
100
|
+
function resolve(storage) {
|
|
101
|
+
if (typeof storage === "function") return storage()
|
|
51
102
|
return storage ?? DEFAULT_STORAGE()
|
|
52
103
|
}
|
|
@@ -168,6 +168,16 @@ export function map<T extends object, R>(
|
|
|
168
168
|
callback: (key: string, value: ObjectValue<T>, obj: T) => R,
|
|
169
169
|
): Record<string, R>
|
|
170
170
|
|
|
171
|
+
/** A new object with only the given keys that the source has. */
|
|
172
|
+
export function pick<T extends object, K extends keyof T & string>(
|
|
173
|
+
source: T | null | undefined,
|
|
174
|
+
keys: readonly K[],
|
|
175
|
+
): Partial<Pick<T, K>>
|
|
176
|
+
export function pick(
|
|
177
|
+
source: object | null | undefined,
|
|
178
|
+
keys: readonly string[],
|
|
179
|
+
): Record<string, unknown>
|
|
180
|
+
|
|
171
181
|
/**
|
|
172
182
|
* Merges source objects into a target in place.
|
|
173
183
|
*/
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/** Whether a value can be awaited like a promise: anything with a `then` method. */
|
|
2
|
+
export function isPromiseLike(value: unknown): value is PromiseLike<unknown>
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Yields the results of some promises in the order they settle, not the order
|
|
3
6
|
* given. The first rejection is thrown, and the promises still pending never
|
package/types/storage.d.ts
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
/** A storage, or a function giving one: a function giving nothing means no storage. */
|
|
2
|
+
export type StorageSource =
|
|
3
|
+
| Storage
|
|
4
|
+
| KeyValueStorage
|
|
5
|
+
| null
|
|
6
|
+
| (() => Storage | KeyValueStorage | undefined)
|
|
7
|
+
|
|
8
|
+
/** Anything that keeps strings like a Web Storage, now or with promises. */
|
|
9
|
+
export interface KeyValueStorage {
|
|
10
|
+
getItem(key: string): string | null | Promise<string | null>
|
|
11
|
+
setItem(key: string, value: string): void | Promise<void>
|
|
12
|
+
}
|
|
13
|
+
|
|
1
14
|
/**
|
|
2
15
|
* Reads a value back as JSON. Absent storage, a missing key or a value that
|
|
3
16
|
* cannot be parsed all read as the fallback, rather than throwing.
|
|
@@ -10,7 +23,7 @@
|
|
|
10
23
|
export function readJSON<T>(
|
|
11
24
|
key: string,
|
|
12
25
|
fallback: T,
|
|
13
|
-
storage?:
|
|
26
|
+
storage?: StorageSource,
|
|
14
27
|
): T
|
|
15
28
|
|
|
16
29
|
/**
|
|
@@ -25,5 +38,27 @@ export function readJSON<T>(
|
|
|
25
38
|
export function writeJSON(
|
|
26
39
|
key: string,
|
|
27
40
|
value: unknown,
|
|
28
|
-
storage?:
|
|
41
|
+
storage?: StorageSource,
|
|
42
|
+
): boolean
|
|
43
|
+
|
|
44
|
+
/** Parses what storage held, or answers the fallback (`null` by default). */
|
|
45
|
+
export function parseJSON<T = unknown>(
|
|
46
|
+
raw: string | null | undefined,
|
|
47
|
+
fallback?: T,
|
|
48
|
+
): T
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Reads a string without throwing: absent or refusing storage reads as `null`.
|
|
52
|
+
* A storage answering with promises gives its promise back.
|
|
53
|
+
*/
|
|
54
|
+
export function readItem(
|
|
55
|
+
key: string,
|
|
56
|
+
storage?: StorageSource,
|
|
57
|
+
): string | null | Promise<string | null>
|
|
58
|
+
|
|
59
|
+
/** Writes a string without throwing, and answers whether the storage took it. */
|
|
60
|
+
export function writeItem(
|
|
61
|
+
key: string,
|
|
62
|
+
value: string,
|
|
63
|
+
storage?: StorageSource,
|
|
29
64
|
): boolean
|