@webreflection/utils 0.2.17 → 0.2.19
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 +10 -10
- package/package.json +4 -1
- package/src/README.md +10 -3
- package/src/registry.js +21 -6
- package/types/registry.d.ts +17 -4
package/README.md
CHANGED
|
@@ -7,15 +7,15 @@
|
|
|
7
7
|
|
|
8
8
|
A curated, *TypeScript*-friendly [collection](./src/) of utilities:
|
|
9
9
|
|
|
10
|
-
* **[all](
|
|
11
|
-
* **[ascii](
|
|
12
|
-
* **[bound-once](
|
|
13
|
-
* **[bound](
|
|
14
|
-
* **[iterable](
|
|
15
|
-
* **[json-storage](
|
|
16
|
-
* **[registry](
|
|
17
|
-
* **[shared-array-buffer](
|
|
18
|
-
* **[sticky](
|
|
19
|
-
* **[with-resolvers](
|
|
10
|
+
* **[all](./src/#all)** - `Promise.all` via object destructuring
|
|
11
|
+
* **[ascii](./src#ascii)** - basic string-to-buffer conversion without validation
|
|
12
|
+
* **[bound-once](./src#bound-once)** - retrieve unique bound methods per realm
|
|
13
|
+
* **[bound](./src#bound)** - retrieve one-off bound methods
|
|
14
|
+
* **[iterable](./src#iterable)** - make plain objects iterable as `Object.entries(object)` pairs, without touching objects that are already iterable
|
|
15
|
+
* **[json-storage](./src#json-storage)** - use `localStorage` or `sessionStorage` through a JSON-aware, iterable, *Map*-friendly API
|
|
16
|
+
* **[registry](./src#registry)** - use a `Map`-like API with key/value validation and duplicate-key protection by default
|
|
17
|
+
* **[shared-array-buffer](./src#shared-array-buffer)** - simulate *SAB* when not available
|
|
18
|
+
* **[sticky](./src#sticky)** - keep useful values stable once per realm
|
|
19
|
+
* **[with-resolvers](./src#with-resolvers)** - use a self-bound `Promise.withResolvers()` helper for older runtimes
|
|
20
20
|
|
|
21
21
|
MIT-style license.
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webreflection/utils",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.19",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"types": {
|
|
6
6
|
"./all": "./types/all.d.ts",
|
|
7
7
|
"./ascii": "./types/ascii.d.ts",
|
|
8
8
|
"./bound-once": "./types/bound-once.d.ts",
|
|
9
9
|
"./bound": "./types/bound.d.ts",
|
|
10
|
+
"./iterable": "./types/iterable.d.ts",
|
|
11
|
+
"./json-storage": "./types/json-storage.d.ts",
|
|
12
|
+
"./registry": "./types/registry.d.ts",
|
|
10
13
|
"./shared-array-buffer": "./types/shared-array-buffer.d.ts",
|
|
11
14
|
"./sticky": "./types/sticky.d.ts",
|
|
12
15
|
"./with-resolvers": "./types/with-resolvers.d.ts"
|
package/src/README.md
CHANGED
|
@@ -134,8 +134,10 @@ long as it provides compatible `parse(source)` and `stringify(value)` methods.
|
|
|
134
134
|
## registry
|
|
135
135
|
|
|
136
136
|
A `Map` subclass that validates keys and values before storing them. By default,
|
|
137
|
-
keys
|
|
138
|
-
|
|
137
|
+
keys are permanent: setting the same key twice throws a `TypeError`, and
|
|
138
|
+
deleting an existing key also throws so it cannot be re-appended later. Pass
|
|
139
|
+
`unique: false` when replacement and deletion should behave like a regular
|
|
140
|
+
`Map`.
|
|
139
141
|
|
|
140
142
|
```js
|
|
141
143
|
import Registry from '@webreflection/utils/registry';
|
|
@@ -171,10 +173,15 @@ const mutable = new Registry(
|
|
|
171
173
|
|
|
172
174
|
console.log(mutable.get('answer'));
|
|
173
175
|
// 42
|
|
176
|
+
|
|
177
|
+
console.log(mutable.delete('answer'));
|
|
178
|
+
// true
|
|
174
179
|
```
|
|
175
180
|
|
|
176
181
|
Initial iterable entries are validated with the same rules used by `set()`, so
|
|
177
|
-
invalid keys, invalid values, or duplicate keys fail during construction.
|
|
182
|
+
invalid keys, invalid values, or duplicate keys fail during construction. With
|
|
183
|
+
the default `unique: true` behavior, only missing keys can be passed to
|
|
184
|
+
`delete()` without throwing, in which case it returns `false` like `Map`.
|
|
178
185
|
|
|
179
186
|
|
|
180
187
|
## shared-array-buffer
|
package/src/registry.js
CHANGED
|
@@ -8,11 +8,10 @@
|
|
|
8
8
|
/**
|
|
9
9
|
* @template [Key=unknown]
|
|
10
10
|
* @template [Value=unknown]
|
|
11
|
-
* @typedef {
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* }} RegistryOptions
|
|
11
|
+
* @typedef {object} RegistryOptions
|
|
12
|
+
* @property {RegistryValidator<Key>} [key] Accepts or rejects candidate keys.
|
|
13
|
+
* @property {RegistryValidator<Value>} [value] Accepts or rejects candidate values.
|
|
14
|
+
* @property {boolean} [unique] When true, stored keys cannot be replaced or removed.
|
|
16
15
|
*/
|
|
17
16
|
|
|
18
17
|
/**
|
|
@@ -30,7 +29,9 @@ const defaultOptions = {
|
|
|
30
29
|
};
|
|
31
30
|
|
|
32
31
|
/**
|
|
33
|
-
* Map with optional key/value validation and
|
|
32
|
+
* Map with optional key/value validation and permanent-key protection.
|
|
33
|
+
*
|
|
34
|
+
* When unique keys are enabled, stored keys cannot be replaced or deleted.
|
|
34
35
|
*
|
|
35
36
|
* @template [Key=unknown]
|
|
36
37
|
* @template [Value=unknown]
|
|
@@ -49,6 +50,7 @@ export default class Registry extends Map {
|
|
|
49
50
|
/**
|
|
50
51
|
* @param {Iterable<[Key, Value]> | null} [iterable]
|
|
51
52
|
* @param {RegistryOptions<Key, Value>} [options]
|
|
53
|
+
* @throws {TypeError} If an initial entry violates validation or uniqueness.
|
|
52
54
|
*/
|
|
53
55
|
constructor(
|
|
54
56
|
iterable,
|
|
@@ -66,10 +68,23 @@ export default class Registry extends Map {
|
|
|
66
68
|
for (const [key, value] of iterable ?? []) this.set(key, value);
|
|
67
69
|
}
|
|
68
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Remove a key when unique-key protection is disabled.
|
|
73
|
+
*
|
|
74
|
+
* @param {Key} key
|
|
75
|
+
* @returns {boolean}
|
|
76
|
+
* @throws {TypeError} If the key exists and unique-key protection is enabled.
|
|
77
|
+
*/
|
|
78
|
+
delete(key) {
|
|
79
|
+
if (this.#unique && super.has(key)) fail('Unable to remove key:', key);
|
|
80
|
+
return super.delete(key);
|
|
81
|
+
}
|
|
82
|
+
|
|
69
83
|
/**
|
|
70
84
|
* @param {Key} key
|
|
71
85
|
* @param {Value} value
|
|
72
86
|
* @returns {this}
|
|
87
|
+
* @throws {TypeError} If the key, value, or uniqueness check fails.
|
|
73
88
|
*/
|
|
74
89
|
set(key, value) {
|
|
75
90
|
if (!this.#key(key)) fail('Invalid key:', key);
|
package/types/registry.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Map with optional key/value validation and
|
|
2
|
+
* Map with optional key/value validation and permanent-key protection.
|
|
3
|
+
*
|
|
4
|
+
* When unique keys are enabled, stored keys cannot be replaced or deleted.
|
|
3
5
|
*
|
|
4
6
|
* @template [Key=unknown]
|
|
5
7
|
* @template [Value=unknown]
|
|
@@ -9,19 +11,30 @@ export default class Registry<Key = unknown, Value = unknown> extends Map<Key, V
|
|
|
9
11
|
/**
|
|
10
12
|
* @param {Iterable<[Key, Value]> | null} [iterable]
|
|
11
13
|
* @param {RegistryOptions<Key, Value>} [options]
|
|
14
|
+
* @throws {TypeError} If an initial entry violates validation or uniqueness.
|
|
12
15
|
*/
|
|
13
16
|
constructor(iterable?: Iterable<[Key, Value]> | null, options?: RegistryOptions<Key, Value>);
|
|
14
17
|
/**
|
|
15
18
|
* @param {Key} key
|
|
16
19
|
* @param {Value} value
|
|
17
20
|
* @returns {this}
|
|
21
|
+
* @throws {TypeError} If the key, value, or uniqueness check fails.
|
|
18
22
|
*/
|
|
19
23
|
set(key: Key, value: Value): this;
|
|
20
24
|
#private;
|
|
21
25
|
}
|
|
22
26
|
export type RegistryValidator<Type> = ((value: unknown) => value is Type) | ((value: unknown) => boolean);
|
|
23
27
|
export type RegistryOptions<Key = unknown, Value = unknown> = {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Accepts or rejects candidate keys.
|
|
30
|
+
*/
|
|
31
|
+
key?: RegistryValidator<Key> | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Accepts or rejects candidate values.
|
|
34
|
+
*/
|
|
35
|
+
value?: RegistryValidator<Value> | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* When true, stored keys cannot be replaced or removed.
|
|
38
|
+
*/
|
|
39
|
+
unique?: boolean | undefined;
|
|
27
40
|
};
|