@dxos/util 0.8.4-main.8360d9e660 → 0.8.4-main.8baae0fced
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/LICENSE +102 -5
- package/dist/lib/browser/index.mjs +276 -152
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +276 -152
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/array-to-hex.d.ts.map +1 -1
- package/dist/types/src/array.d.ts.map +1 -1
- package/dist/types/src/binder.d.ts.map +1 -1
- package/dist/types/src/bitfield.d.ts.map +1 -1
- package/dist/types/src/browser-storage.d.ts +9 -0
- package/dist/types/src/browser-storage.d.ts.map +1 -0
- package/dist/types/src/callback-collection.d.ts.map +1 -1
- package/dist/types/src/callback.d.ts.map +1 -1
- package/dist/types/src/case.d.ts.map +1 -1
- package/dist/types/src/chunk-array.d.ts.map +1 -1
- package/dist/types/src/circular-buffer.d.ts.map +1 -1
- package/dist/types/src/clear-undefined.d.ts.map +1 -1
- package/dist/types/src/complex.d.ts +15 -0
- package/dist/types/src/complex.d.ts.map +1 -1
- package/dist/types/src/composite-key.d.ts +10 -0
- package/dist/types/src/composite-key.d.ts.map +1 -0
- package/dist/types/src/composite-key.test.d.ts +2 -0
- package/dist/types/src/composite-key.test.d.ts.map +1 -0
- package/dist/types/src/deep.d.ts.map +1 -1
- package/dist/types/src/defer-function.d.ts.map +1 -1
- package/dist/types/src/defer.d.ts.map +1 -1
- package/dist/types/src/di-container.d.ts.map +1 -1
- package/dist/types/src/di-key.d.ts +7 -5
- package/dist/types/src/di-key.d.ts.map +1 -1
- package/dist/types/src/entry.d.ts.map +1 -1
- package/dist/types/src/error-format.d.ts.map +1 -1
- package/dist/types/src/filename.d.ts.map +1 -1
- package/dist/types/src/for-each-async.d.ts.map +1 -1
- package/dist/types/src/human-hash.d.ts.map +1 -1
- package/dist/types/src/id.d.ts +30 -0
- package/dist/types/src/id.d.ts.map +1 -0
- package/dist/types/src/id.test.d.ts +2 -0
- package/dist/types/src/id.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +5 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/instance-id.d.ts.map +1 -1
- package/dist/types/src/interval.d.ts.map +1 -1
- package/dist/types/src/join-tables.d.ts.map +1 -1
- package/dist/types/src/json.d.ts.map +1 -1
- package/dist/types/src/map-values.d.ts.map +1 -1
- package/dist/types/src/map.d.ts.map +1 -1
- package/dist/types/src/object.d.ts.map +1 -1
- package/dist/types/src/order-keys.d.ts.map +1 -1
- package/dist/types/src/order.d.ts.map +1 -1
- package/dist/types/src/pick.d.ts.map +1 -1
- package/dist/types/src/platform.d.ts +1 -1
- package/dist/types/src/position.d.ts +5 -5
- package/dist/types/src/position.d.ts.map +1 -1
- package/dist/types/src/random.d.ts.map +1 -1
- package/dist/types/src/range.d.ts.map +1 -1
- package/dist/types/src/reducers.d.ts.map +1 -1
- package/dist/types/src/remove-undefined-keys.d.ts.map +1 -1
- package/dist/types/src/retry.d.ts.map +1 -1
- package/dist/types/src/safe-await.d.ts.map +1 -1
- package/dist/types/src/safe-instanceof.d.ts.map +1 -1
- package/dist/types/src/safe-parse.d.ts.map +1 -1
- package/dist/types/src/safe-stringify.d.ts.map +1 -1
- package/dist/types/src/sliding-window-summary.d.ts.map +1 -1
- package/dist/types/src/sort.d.ts.map +1 -1
- package/dist/types/src/string.d.ts.map +1 -1
- package/dist/types/src/sum.d.ts.map +1 -1
- package/dist/types/src/throw-unhandled-error.d.ts.map +1 -1
- package/dist/types/src/to-fallback.d.ts +4 -4
- package/dist/types/src/to-fallback.d.ts.map +1 -1
- package/dist/types/src/tracer.d.ts.map +1 -1
- package/dist/types/src/tree.d.ts.map +1 -1
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/uint8array.d.ts +27 -0
- package/dist/types/src/uint8array.d.ts.map +1 -1
- package/dist/types/src/unit.d.ts.map +1 -1
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/src/weak.d.ts +15 -0
- package/dist/types/src/weak.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -10
- package/src/browser-storage.ts +60 -0
- package/src/complex.ts +32 -0
- package/src/composite-key.test.ts +31 -0
- package/src/composite-key.ts +16 -0
- package/src/di-key.ts +10 -8
- package/src/error-format.ts +3 -1
- package/src/id.test.ts +109 -0
- package/src/id.ts +48 -0
- package/src/index.ts +5 -2
- package/src/map-values.ts +6 -0
- package/src/position.test.ts +38 -44
- package/src/position.ts +7 -11
- package/src/string.ts +6 -2
- package/src/uint8array.ts +44 -0
- package/src/unit.ts +6 -2
- package/src/weak.ts +52 -14
package/src/uint8array.ts
CHANGED
|
@@ -35,3 +35,47 @@ export const bufferToArray = (buffer: Buffer): Uint8Array => {
|
|
|
35
35
|
export const stringToArray = (string: string): Uint8Array => bufferToArray(Buffer.from(string, 'hex'));
|
|
36
36
|
|
|
37
37
|
export const arrayToString = (array: Uint8Array): string => arrayToBuffer(array).toString('hex');
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* JSON-safe representation of a `Uint8Array`, per the IPLD DAG-JSON spec.
|
|
41
|
+
* https://ipld.io/specs/codecs/dag-json/spec/#bytes
|
|
42
|
+
*
|
|
43
|
+
* Bytes are encoded as `{ "/": { "bytes": "<base64-no-padding>" } }`.
|
|
44
|
+
* Distinguishable from encoded references `{ "/": "<string>" }` by the type of the `/` value.
|
|
45
|
+
*/
|
|
46
|
+
export type EncodedUint8Array = { '/': { bytes: string } };
|
|
47
|
+
|
|
48
|
+
const toUnpaddedBase64 = (bytes: Uint8Array): string => arrayToBuffer(bytes).toString('base64').replace(/=+$/, '');
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Encode a `Uint8Array` as the DAG-JSON bytes form `{ '/': { bytes: '<base64-no-padding>' } }`.
|
|
52
|
+
*/
|
|
53
|
+
export const encodeUint8ArrayToJson = (bytes: Uint8Array): EncodedUint8Array => ({
|
|
54
|
+
'/': { bytes: toUnpaddedBase64(bytes) },
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Type-guard that returns true iff `value` is the DAG-JSON bytes form produced by
|
|
59
|
+
* {@link encodeUint8ArrayToJson} — an object with exactly one key `'/'` whose value is an
|
|
60
|
+
* object with exactly one string key `bytes`.
|
|
61
|
+
*/
|
|
62
|
+
export const isEncodedUint8Array = (value: unknown): value is EncodedUint8Array => {
|
|
63
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
if (Object.keys(value).length !== 1) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
const inner = (value as any)['/'];
|
|
70
|
+
if (typeof inner !== 'object' || inner === null || Array.isArray(inner)) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
return Object.keys(inner).length === 1 && typeof inner.bytes === 'string';
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Decode a DAG-JSON bytes form (as produced by {@link encodeUint8ArrayToJson}) back to a
|
|
78
|
+
* `Uint8Array`. The base64 string is tolerated with or without padding.
|
|
79
|
+
*/
|
|
80
|
+
export const decodeUint8ArrayFromJson = (encoded: EncodedUint8Array): Uint8Array =>
|
|
81
|
+
bufferToArray(Buffer.from(encoded['/'].bytes, 'base64'));
|
package/src/unit.ts
CHANGED
|
@@ -20,11 +20,15 @@ export type UnitFormat<T = any> = (n: number, precision?: number) => UnitValue<T
|
|
|
20
20
|
const createFormat = (unit: Unit): UnitFormat<string> => {
|
|
21
21
|
return (n: number, precision = unit.precision ?? 0) => {
|
|
22
22
|
const value = n / unit.quotient;
|
|
23
|
+
const formattedValue = value.toLocaleString('en-US', {
|
|
24
|
+
minimumFractionDigits: precision,
|
|
25
|
+
maximumFractionDigits: precision,
|
|
26
|
+
});
|
|
23
27
|
return {
|
|
24
28
|
unit,
|
|
25
29
|
value,
|
|
26
|
-
formattedValue
|
|
27
|
-
toString: () => `${
|
|
30
|
+
formattedValue,
|
|
31
|
+
toString: () => `${formattedValue}${unit.symbol}`,
|
|
28
32
|
};
|
|
29
33
|
};
|
|
30
34
|
};
|
package/src/weak.ts
CHANGED
|
@@ -7,13 +7,20 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export class WeakDictionary<K, V extends object> implements Map<K, V> {
|
|
9
9
|
private readonly _internal = new Map<K, WeakRef<V>>();
|
|
10
|
-
private readonly _finalization = new FinalizationRegistry
|
|
11
|
-
|
|
10
|
+
private readonly _finalization = new FinalizationRegistry<{ key: K; ref: WeakRef<V> }>(({ key, ref }) => {
|
|
11
|
+
if (this._internal.get(key) === ref) {
|
|
12
|
+
this._internal.delete(key);
|
|
13
|
+
}
|
|
12
14
|
});
|
|
13
15
|
|
|
14
16
|
constructor(entries?: [K, V][]) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
if (entries) {
|
|
18
|
+
for (const [key, value] of entries) {
|
|
19
|
+
const ref = new WeakRef(value);
|
|
20
|
+
this._internal.set(key, ref);
|
|
21
|
+
this._register(key, value, ref);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
17
24
|
}
|
|
18
25
|
|
|
19
26
|
*entries(): SetIterator<[K, V]> {
|
|
@@ -55,11 +62,48 @@ export class WeakDictionary<K, V extends object> implements Map<K, V> {
|
|
|
55
62
|
}
|
|
56
63
|
|
|
57
64
|
set(key: K, value: V): this {
|
|
58
|
-
this._internal.
|
|
59
|
-
|
|
65
|
+
const previous = this._internal.get(key)?.deref();
|
|
66
|
+
if (previous) {
|
|
67
|
+
this._unregister(previous);
|
|
68
|
+
}
|
|
69
|
+
const ref = new WeakRef(value);
|
|
70
|
+
this._internal.set(key, ref);
|
|
71
|
+
this._register(key, value, ref);
|
|
60
72
|
return this;
|
|
61
73
|
}
|
|
62
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Returns the value for the given key if present, otherwise inserts and returns the default value.
|
|
77
|
+
* @param key - The key to look up or insert.
|
|
78
|
+
* @param defaultValue - The value to insert if the key is not present.
|
|
79
|
+
* @returns The existing or newly inserted value.
|
|
80
|
+
*/
|
|
81
|
+
getOrInsert(key: K, defaultValue: V): V {
|
|
82
|
+
const existing = this.get(key);
|
|
83
|
+
if (existing !== undefined) {
|
|
84
|
+
return existing;
|
|
85
|
+
}
|
|
86
|
+
this.set(key, defaultValue);
|
|
87
|
+
return defaultValue;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Returns the value for the given key if present, otherwise computes, inserts, and returns a new value.
|
|
92
|
+
* The callback is only invoked when the key is missing.
|
|
93
|
+
* @param key - The key to look up or insert.
|
|
94
|
+
* @param callbackfn - Function to compute the value if the key is not present.
|
|
95
|
+
* @returns The existing or newly computed value.
|
|
96
|
+
*/
|
|
97
|
+
getOrInsertComputed(key: K, callbackfn: (key: K) => V): V {
|
|
98
|
+
const existing = this.get(key);
|
|
99
|
+
if (existing !== undefined) {
|
|
100
|
+
return existing;
|
|
101
|
+
}
|
|
102
|
+
const value = callbackfn(key);
|
|
103
|
+
this.set(key, value);
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
|
|
63
107
|
has(key: K): boolean {
|
|
64
108
|
return this._internal.has(key) && this._internal.get(key)!.deref() !== undefined;
|
|
65
109
|
}
|
|
@@ -96,14 +140,8 @@ export class WeakDictionary<K, V extends object> implements Map<K, V> {
|
|
|
96
140
|
});
|
|
97
141
|
}
|
|
98
142
|
|
|
99
|
-
private _register(key: K, value: V): void {
|
|
100
|
-
this._finalization.register(
|
|
101
|
-
value,
|
|
102
|
-
() => {
|
|
103
|
-
this._internal.delete(key);
|
|
104
|
-
},
|
|
105
|
-
value,
|
|
106
|
-
);
|
|
143
|
+
private _register(key: K, value: V, ref: WeakRef<V>): void {
|
|
144
|
+
this._finalization.register(value, { key, ref }, value);
|
|
107
145
|
}
|
|
108
146
|
|
|
109
147
|
private _unregister(value: V): void {
|