@dxos/util 0.8.4-main.ead640a → 0.8.4-main.f466a3d56e
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 +642 -524
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +642 -524
- 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 +3 -2
- 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 +1 -0
- 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 +0 -3
- 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 +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 +5 -0
- package/dist/types/src/error-format.d.ts.map +1 -0
- package/dist/types/src/filename.d.ts +9 -0
- package/dist/types/src/filename.d.ts.map +1 -0
- 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 +7 -0
- 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 +5 -2
- package/dist/types/src/platform.d.ts.map +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 +32 -0
- package/dist/types/src/retry.d.ts.map +1 -0
- 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 +7 -4
- package/dist/types/src/safe-parse.d.ts.map +1 -1
- package/dist/types/src/safe-stringify.d.ts +22 -0
- package/dist/types/src/safe-stringify.d.ts.map +1 -0
- package/dist/types/src/safe-stringify.test.d.ts +2 -0
- package/dist/types/src/safe-stringify.test.d.ts.map +1 -0
- 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 +18 -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 +23 -5
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/uint8array.d.ts.map +1 -1
- package/dist/types/src/unit.d.ts +12 -13
- 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 +13 -15
- package/src/array.ts +9 -2
- package/src/binder.ts +2 -2
- package/src/browser-storage.ts +60 -0
- package/src/circular-buffer.test.ts +26 -0
- package/src/circular-buffer.ts +5 -0
- package/src/complex.ts +32 -0
- package/src/composite-key.test.ts +31 -0
- package/src/composite-key.ts +16 -0
- package/src/deep.ts +2 -6
- package/src/defer.ts +1 -1
- package/src/di-key.ts +10 -8
- package/src/error-format.ts +24 -0
- package/src/filename.ts +16 -0
- package/src/id.test.ts +109 -0
- package/src/id.ts +48 -0
- package/src/index.ts +7 -0
- package/src/platform.ts +35 -3
- package/src/position.test.ts +38 -44
- package/src/position.ts +7 -11
- package/src/retry.ts +74 -0
- package/src/safe-parse.ts +27 -16
- package/src/safe-stringify.test.ts +96 -0
- package/src/safe-stringify.ts +153 -0
- package/src/string.ts +6 -2
- package/src/to-fallback.ts +39 -156
- package/src/types.test.ts +11 -1
- package/src/types.ts +39 -11
- package/src/unit.test.ts +1 -1
- package/src/unit.ts +63 -28
- package/src/weak.ts +52 -14
- package/dist/types/src/explicit-resource-management-polyfill.d.ts +0 -1
- package/dist/types/src/explicit-resource-management-polyfill.d.ts.map +0 -1
- package/src/explicit-resource-management-polyfill.ts +0 -13
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/util",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.f466a3d56e",
|
|
4
4
|
"description": "Temporary bucket for misc functions, which should graduate into separate packages.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
-
"
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
11
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
8
12
|
"author": "DXOS.org",
|
|
9
|
-
"sideEffects":
|
|
13
|
+
"sideEffects": false,
|
|
10
14
|
"type": "module",
|
|
11
15
|
"exports": {
|
|
12
16
|
".": {
|
|
@@ -19,25 +23,19 @@
|
|
|
19
23
|
}
|
|
20
24
|
},
|
|
21
25
|
"types": "dist/types/src/index.d.ts",
|
|
22
|
-
"typesVersions": {
|
|
23
|
-
"*": {}
|
|
24
|
-
},
|
|
25
26
|
"files": [
|
|
26
27
|
"dist",
|
|
27
28
|
"src"
|
|
28
29
|
],
|
|
29
30
|
"dependencies": {
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"@dxos/
|
|
33
|
-
"@dxos/keys": "0.8.4-main.
|
|
34
|
-
"@dxos/invariant": "0.8.4-main.
|
|
35
|
-
"@dxos/node-std": "0.8.4-main.ead640a"
|
|
31
|
+
"@hazae41/symbol-dispose-polyfill": "^1.0.2",
|
|
32
|
+
"@dxos/debug": "0.8.4-main.f466a3d56e",
|
|
33
|
+
"@dxos/node-std": "0.8.4-main.f466a3d56e",
|
|
34
|
+
"@dxos/keys": "0.8.4-main.f466a3d56e",
|
|
35
|
+
"@dxos/invariant": "0.8.4-main.f466a3d56e"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@
|
|
39
|
-
"@types/lodash.set": "^4.3.9",
|
|
40
|
-
"@dxos/crypto": "0.8.4-main.ead640a"
|
|
38
|
+
"@dxos/crypto": "0.8.4-main.f466a3d56e"
|
|
41
39
|
},
|
|
42
40
|
"publishConfig": {
|
|
43
41
|
"access": "public"
|
package/src/array.ts
CHANGED
|
@@ -42,14 +42,14 @@ export const diff = <A, B = A>(
|
|
|
42
42
|
return result;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
export const intersection = <A, B = A>(a: A[], b: B[], comparator: Comparator<A, B>): A[] =>
|
|
45
|
+
export const intersection = <A, B = A>(a: readonly A[], b: readonly B[], comparator: Comparator<A, B>): A[] =>
|
|
46
46
|
a.filter((a) => b.find((b) => comparator(a, b)) !== undefined);
|
|
47
47
|
|
|
48
48
|
/**
|
|
49
49
|
* Returns a new array with only the first instance of each unique item
|
|
50
50
|
* based on a specified property.
|
|
51
51
|
*
|
|
52
|
-
* @
|
|
52
|
+
* @typeProp T - The type of items in the input array.
|
|
53
53
|
* @param array - The array to filter for distinct items.
|
|
54
54
|
* @param key - The property key to determine uniqueness for each item.
|
|
55
55
|
* @returns A new array with only distinct items based on the specified property.
|
|
@@ -130,3 +130,10 @@ export const intersectBy = <T, K>(arrays: T[][], selector: (item: T) => K): T[]
|
|
|
130
130
|
return lookups.every((lookup) => lookup.has(key));
|
|
131
131
|
});
|
|
132
132
|
};
|
|
133
|
+
|
|
134
|
+
export const coerceArray = <T>(arr: T | T[] | undefined): T[] => {
|
|
135
|
+
if (arr === undefined) {
|
|
136
|
+
return [];
|
|
137
|
+
}
|
|
138
|
+
return Array.isArray(arr) ? arr : [arr];
|
|
139
|
+
};
|
package/src/binder.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Function binder replaces pify.
|
|
@@ -10,5 +10,5 @@ import util from 'node:util';
|
|
|
10
10
|
// TODO(burdon): Replace pify everywhere.
|
|
11
11
|
export const createBinder = (obj: any) => ({
|
|
12
12
|
fn: (fn: Function) => fn.bind(obj),
|
|
13
|
-
async: (fn: Function) =>
|
|
13
|
+
async: (fn: Function) => promisify(fn.bind(obj)),
|
|
14
14
|
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
/** Delete all IndexedDB databases for this origin. */
|
|
6
|
+
export const clearIndexedDB = async (): Promise<void> => {
|
|
7
|
+
const dbs = await indexedDB.databases();
|
|
8
|
+
const results = await Promise.allSettled(
|
|
9
|
+
dbs
|
|
10
|
+
.filter((db) => db.name != null)
|
|
11
|
+
.map(
|
|
12
|
+
(db) =>
|
|
13
|
+
new Promise<void>((resolve, reject) => {
|
|
14
|
+
const request = indexedDB.deleteDatabase(db.name!);
|
|
15
|
+
request.onsuccess = () => resolve();
|
|
16
|
+
request.onerror = () => reject(request.error);
|
|
17
|
+
request.onblocked = () => reject(new Error(`IndexedDB deletion blocked: ${db.name}`));
|
|
18
|
+
}),
|
|
19
|
+
),
|
|
20
|
+
);
|
|
21
|
+
const failures = results.filter((r): r is PromiseRejectedResult => r.status === 'rejected');
|
|
22
|
+
if (failures.length > 0) {
|
|
23
|
+
throw new AggregateError(
|
|
24
|
+
failures.map((r) => r.reason),
|
|
25
|
+
'Failed to delete some IndexedDB databases',
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Remove all entries from the Origin Private File System. */
|
|
31
|
+
export const clearOPFS = async (): Promise<void> => {
|
|
32
|
+
const root = await navigator.storage.getDirectory();
|
|
33
|
+
const errors: unknown[] = [];
|
|
34
|
+
for await (const [name] of root.entries()) {
|
|
35
|
+
try {
|
|
36
|
+
await root.removeEntry(name, { recursive: true });
|
|
37
|
+
} catch (err) {
|
|
38
|
+
errors.push(err);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (errors.length > 0) {
|
|
42
|
+
throw new AggregateError(errors, 'Failed to remove some OPFS entries');
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Unregister all service workers for this origin. */
|
|
47
|
+
export const clearServiceWorkers = async (): Promise<void> => {
|
|
48
|
+
const regs = await navigator.serviceWorker.getRegistrations();
|
|
49
|
+
for (const reg of regs) {
|
|
50
|
+
await reg.unregister();
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** Delete all HTTP caches for this origin. */
|
|
55
|
+
export const clearCaches = async (): Promise<void> => {
|
|
56
|
+
const keys = await caches.keys();
|
|
57
|
+
for (const key of keys) {
|
|
58
|
+
await caches.delete(key);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
@@ -16,6 +16,32 @@ describe('CircularBuffer', () => {
|
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
18
|
|
|
19
|
+
test('clear resets buffer', () => {
|
|
20
|
+
const buffer = new CircularBuffer<number>(5);
|
|
21
|
+
buffer.push(1);
|
|
22
|
+
buffer.push(2);
|
|
23
|
+
buffer.push(3);
|
|
24
|
+
expect(buffer.elementCount).toBe(3);
|
|
25
|
+
|
|
26
|
+
buffer.clear();
|
|
27
|
+
expect(buffer.elementCount).toBe(0);
|
|
28
|
+
expect([...buffer]).toStrictEqual([]);
|
|
29
|
+
expect(buffer.getLast()).toBeUndefined();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('clear allows reuse', () => {
|
|
33
|
+
const buffer = new CircularBuffer<number>(3);
|
|
34
|
+
buffer.push(10);
|
|
35
|
+
buffer.push(20);
|
|
36
|
+
buffer.clear();
|
|
37
|
+
|
|
38
|
+
buffer.push(30);
|
|
39
|
+
buffer.push(40);
|
|
40
|
+
expect(buffer.elementCount).toBe(2);
|
|
41
|
+
expect([...buffer]).toStrictEqual([30, 40]);
|
|
42
|
+
expect(buffer.getLast()).toBe(40);
|
|
43
|
+
});
|
|
44
|
+
|
|
19
45
|
test('full cycle', () => {
|
|
20
46
|
const maxSize = 10;
|
|
21
47
|
const regularArray: number[] = [];
|
package/src/circular-buffer.ts
CHANGED
package/src/complex.ts
CHANGED
|
@@ -214,6 +214,38 @@ export class ComplexMap<K, V> implements Map<K, V> {
|
|
|
214
214
|
return this;
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
+
/**
|
|
218
|
+
* Returns the value for the given key if present, otherwise inserts and returns the default value.
|
|
219
|
+
* @param key - The key to look up or insert.
|
|
220
|
+
* @param defaultValue - The value to insert if the key is not present.
|
|
221
|
+
* @returns The existing or newly inserted value.
|
|
222
|
+
*/
|
|
223
|
+
getOrInsert(key: K, defaultValue: V): V {
|
|
224
|
+
const primitive = this._keyProjection(key);
|
|
225
|
+
if (this._values.has(primitive)) {
|
|
226
|
+
return this._values.get(primitive)!;
|
|
227
|
+
}
|
|
228
|
+
this.set(key, defaultValue);
|
|
229
|
+
return defaultValue;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Returns the value for the given key if present, otherwise computes, inserts, and returns a new value.
|
|
234
|
+
* The callback is only invoked when the key is missing.
|
|
235
|
+
* @param key - The key to look up or insert.
|
|
236
|
+
* @param callbackfn - Function to compute the value if the key is not present.
|
|
237
|
+
* @returns The existing or newly computed value.
|
|
238
|
+
*/
|
|
239
|
+
getOrInsertComputed(key: K, callbackfn: (key: K) => V): V {
|
|
240
|
+
const primitive = this._keyProjection(key);
|
|
241
|
+
if (this._values.has(primitive)) {
|
|
242
|
+
return this._values.get(primitive)!;
|
|
243
|
+
}
|
|
244
|
+
const value = callbackfn(key);
|
|
245
|
+
this.set(key, value);
|
|
246
|
+
return value;
|
|
247
|
+
}
|
|
248
|
+
|
|
217
249
|
get size(): number {
|
|
218
250
|
return this._keys.size;
|
|
219
251
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { compositeKey, splitCompositeKey } from './composite-key';
|
|
8
|
+
|
|
9
|
+
describe('compositeKey', () => {
|
|
10
|
+
test('joins parts with colon', ({ expect }) => {
|
|
11
|
+
expect(compositeKey('a', 'b', 'c')).to.eq('a:b:c');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('single part returns as-is', ({ expect }) => {
|
|
15
|
+
expect(compositeKey('only')).to.eq('only');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('handles empty strings in parts', ({ expect }) => {
|
|
19
|
+
expect(compositeKey('', 'b')).to.eq(':b');
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('splitCompositeKey', () => {
|
|
24
|
+
test('splits on colon', ({ expect }) => {
|
|
25
|
+
expect(splitCompositeKey('a:b:c')).to.deep.eq(['a', 'b', 'c']);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('single part returns array of one', ({ expect }) => {
|
|
29
|
+
expect(splitCompositeKey('only')).to.deep.eq(['only']);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
const SEPARATOR = ':';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Build a composite key from string parts joined by a colon separator.
|
|
9
|
+
* Parts must not contain the separator character.
|
|
10
|
+
*/
|
|
11
|
+
export const compositeKey = (...parts: string[]): string => parts.join(SEPARATOR);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Split a composite key back into its parts.
|
|
15
|
+
*/
|
|
16
|
+
export const splitCompositeKey = (key: string): string[] => key.split(SEPARATOR);
|
package/src/deep.ts
CHANGED
|
@@ -2,14 +2,8 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import get from 'lodash.get';
|
|
6
|
-
import set from 'lodash.set';
|
|
7
|
-
|
|
8
5
|
import { invariant } from '@dxos/invariant';
|
|
9
6
|
|
|
10
|
-
// TODO(burdon): Re-export most common utils? (isEqual, defaultsDeep, merge, omit, pick, etc.)
|
|
11
|
-
export { get, set };
|
|
12
|
-
|
|
13
7
|
/**
|
|
14
8
|
* Initialize a deeply nested object.
|
|
15
9
|
* @returns The value of the prop after assignment.
|
|
@@ -19,6 +13,8 @@ export const setDeep = <T>(obj: any, path: readonly (string | number)[], value:
|
|
|
19
13
|
let parent = obj;
|
|
20
14
|
for (const key of path.slice(0, -1)) {
|
|
21
15
|
if (parent[key] === undefined) {
|
|
16
|
+
// TODO(wittjosiah): This logic is flawed. This shouldn't be used for initializing arrays.
|
|
17
|
+
// Prefer `Obj.setValue` for ECHO objects.
|
|
22
18
|
const isArrayIndex = !isNaN(Number(key));
|
|
23
19
|
parent[key] = isArrayIndex ? [] : {};
|
|
24
20
|
}
|
package/src/defer.ts
CHANGED
package/src/di-key.ts
CHANGED
|
@@ -47,7 +47,7 @@ export type DiKey<T> =
|
|
|
47
47
|
}
|
|
48
48
|
| SymbolDiKey<T>;
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
class DiKeyConstructor {
|
|
51
51
|
/**
|
|
52
52
|
* Needed to ensure referential equality of combined keys.
|
|
53
53
|
*
|
|
@@ -57,7 +57,7 @@ export const DiKey = new (class DiKeyConstructor {
|
|
|
57
57
|
*/
|
|
58
58
|
// TODO(dmaretskyi): Disable private members lowering for dev env.
|
|
59
59
|
// TODO(dmaretskyi): Could be a weak map after a NodeJS upgrade.
|
|
60
|
-
|
|
60
|
+
private _combinedRegistry = new WeakMap();
|
|
61
61
|
|
|
62
62
|
define<T>(name: string): DiKey<T> {
|
|
63
63
|
return new SymbolDiKey(name);
|
|
@@ -88,24 +88,26 @@ export const DiKey = new (class DiKeyConstructor {
|
|
|
88
88
|
* Maintains referential equality: `DiKey.combine(A, B) === DiKey.combine(A, B)`
|
|
89
89
|
*/
|
|
90
90
|
combine(...ids: DiKey<any>[]) {
|
|
91
|
-
const map = this
|
|
92
|
-
return defaultMap(this
|
|
91
|
+
const map = this._lookupCombined(this._combinedRegistry, ids);
|
|
92
|
+
return defaultMap(this._combinedRegistry, map, () => this._combinedDescription(ids));
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
getSingletonFactory<T>(id: DiKey<T>): SingletonFactory<T> | undefined {
|
|
96
96
|
return (id as any)[symbolSingleton];
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
|
|
99
|
+
private _lookupCombined(map: WeakMap<any, any>, [first, ...rest]: DiKey<any>[]): WeakMap<any, any> {
|
|
100
100
|
const value = defaultMap(map, first, () => new Map());
|
|
101
101
|
if (rest.length > 0) {
|
|
102
|
-
return this
|
|
102
|
+
return this._lookupCombined(value, rest);
|
|
103
103
|
} else {
|
|
104
104
|
return value;
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
|
|
108
|
+
private _combinedDescription([first, ...rest]: DiKey<any>[]) {
|
|
109
109
|
return `${this.stringify(first)}<${rest.map(this.stringify).join(', ')}>`;
|
|
110
110
|
}
|
|
111
|
-
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export const DiKey = new DiKeyConstructor();
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats an error with its cause chain.
|
|
3
|
+
*/
|
|
4
|
+
//
|
|
5
|
+
// Copyright 2025 DXOS.org
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
export const formatErrorWithCauses = (error: Error): string => {
|
|
9
|
+
const lines: string[] = [];
|
|
10
|
+
let current: Error | undefined = error;
|
|
11
|
+
let level = 0;
|
|
12
|
+
|
|
13
|
+
while (current) {
|
|
14
|
+
const prefix = level === 0 ? '' : `Caused by: `;
|
|
15
|
+
lines.push(prefix + (current.stack ?? String(current)));
|
|
16
|
+
if (!(current.cause instanceof Error)) {
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
current = current.cause;
|
|
20
|
+
level += 1;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return lines.join('\n\n');
|
|
24
|
+
};
|
package/src/filename.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a unique filename.
|
|
7
|
+
*/
|
|
8
|
+
export const createFilename = ({
|
|
9
|
+
parts = [],
|
|
10
|
+
ext,
|
|
11
|
+
date = new Date(),
|
|
12
|
+
}: {
|
|
13
|
+
parts?: string[];
|
|
14
|
+
ext?: string;
|
|
15
|
+
date?: Date;
|
|
16
|
+
}) => [date.toISOString().replace(/[:.]/g, '-'), ...parts].join('_') + (ext ? `.${ext}` : '');
|
package/src/id.test.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { id, isWellFormedId, isWellFormedIdPart } from './id';
|
|
8
|
+
|
|
9
|
+
describe('id', () => {
|
|
10
|
+
test('accepts a three-part id', ({ expect }) => {
|
|
11
|
+
expect(id`com.example.foo`).toBe('com.example.foo');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('accepts a four-part id', ({ expect }) => {
|
|
15
|
+
expect(id`org.dxos.plugin.deck`).toBe('org.dxos.plugin.deck');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('accepts hyphens in authority segments', ({ expect }) => {
|
|
19
|
+
expect(id`my-org.example.foo`).toBe('my-org.example.foo');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('accepts mixed-case name segment', ({ expect }) => {
|
|
23
|
+
expect(id`org.dxos.plugin.deckPlugin`).toBe('org.dxos.plugin.deckPlugin');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('accepts alphanumeric characters in authority segments', ({ expect }) => {
|
|
27
|
+
expect(id`foo123.bar456.qux`).toBe('foo123.bar456.qux');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('interpolates values', ({ expect }) => {
|
|
31
|
+
const namespace = 'org.dxos';
|
|
32
|
+
const name = 'plugin';
|
|
33
|
+
expect(id`${namespace}.${name}.deck`).toBe('org.dxos.plugin.deck');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('throws on empty string', ({ expect }) => {
|
|
37
|
+
expect(() => id``).toThrow(/Invalid id/);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('throws for fewer than three parts', ({ expect }) => {
|
|
41
|
+
expect(() => id`foo`).toThrow(/Invalid id/);
|
|
42
|
+
expect(() => id`foo.bar`).toThrow(/Invalid id/);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('throws when first character is a digit', ({ expect }) => {
|
|
46
|
+
expect(() => id`1foo.bar.baz`).toThrow(/Invalid id/);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('throws on leading or trailing dot', ({ expect }) => {
|
|
50
|
+
expect(() => id`.foo.bar.baz`).toThrow(/Invalid id/);
|
|
51
|
+
expect(() => id`foo.bar.baz.`).toThrow(/Invalid id/);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('throws on consecutive dots', ({ expect }) => {
|
|
55
|
+
expect(() => id`foo..bar`).toThrow(/Invalid id/);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('throws on hyphens in the name segment', ({ expect }) => {
|
|
59
|
+
expect(() => id`org.example.foo-bar`).toThrow(/Invalid id/);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('throws on disallowed characters', ({ expect }) => {
|
|
63
|
+
expect(() => id`foo_bar.baz.qux`).toThrow(/Invalid id/);
|
|
64
|
+
expect(() => id`foo bar.baz.qux`).toThrow(/Invalid id/);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('throws when an interpolated value produces an invalid name segment', ({ expect }) => {
|
|
68
|
+
const bad = '1bad';
|
|
69
|
+
expect(() => id`org.dxos.${bad}`).toThrow(/Invalid id/);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('isWellFormedId', () => {
|
|
74
|
+
test('matches valid ids', ({ expect }) => {
|
|
75
|
+
expect(isWellFormedId('com.example.foo')).toBe(true);
|
|
76
|
+
expect(isWellFormedId('org.dxos.plugin.deck')).toBe(true);
|
|
77
|
+
expect(isWellFormedId('a1.b2.c3')).toBe(true);
|
|
78
|
+
expect(isWellFormedId('my-org.example.foo')).toBe(true);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('rejects ids with fewer than three parts', ({ expect }) => {
|
|
82
|
+
expect(isWellFormedId('foo')).toBe(false);
|
|
83
|
+
expect(isWellFormedId('foo.bar')).toBe(false);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('rejects invalid ids', ({ expect }) => {
|
|
87
|
+
expect(isWellFormedId('')).toBe(false);
|
|
88
|
+
expect(isWellFormedId('1foo.bar.baz')).toBe(false);
|
|
89
|
+
expect(isWellFormedId('foo.bar.')).toBe(false);
|
|
90
|
+
expect(isWellFormedId('.foo.bar')).toBe(false);
|
|
91
|
+
expect(isWellFormedId('foo..bar.baz')).toBe(false);
|
|
92
|
+
expect(isWellFormedId('org.example.foo-bar')).toBe(false);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe('isWellFormedIdPart', () => {
|
|
97
|
+
test('matches valid name segments', ({ expect }) => {
|
|
98
|
+
expect(isWellFormedIdPart('foo')).toBe(true);
|
|
99
|
+
expect(isWellFormedIdPart('Foo')).toBe(true);
|
|
100
|
+
expect(isWellFormedIdPart('fooBar123')).toBe(true);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('rejects invalid name segments', ({ expect }) => {
|
|
104
|
+
expect(isWellFormedIdPart('foo.bar')).toBe(false);
|
|
105
|
+
expect(isWellFormedIdPart('foo-bar')).toBe(false);
|
|
106
|
+
expect(isWellFormedIdPart('1foo')).toBe(false);
|
|
107
|
+
expect(isWellFormedIdPart('')).toBe(false);
|
|
108
|
+
});
|
|
109
|
+
});
|
package/src/id.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
// Name segment: starts with alpha, alphanumeric only (no hyphens), max 63 chars.
|
|
6
|
+
const PART = /^[a-zA-Z][a-zA-Z0-9]{0,62}$/;
|
|
7
|
+
|
|
8
|
+
// Reference: https://atproto.com/specs/nsid
|
|
9
|
+
const ID =
|
|
10
|
+
/^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(\.[a-zA-Z]([a-zA-Z0-9]{0,62})?)$/;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Branded string type for well-formed ids.
|
|
14
|
+
*/
|
|
15
|
+
export type Id = string & { readonly __id: unique symbol };
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Tagged template literal that constructs a well-formed, dot-delimited id string.
|
|
19
|
+
* Throws if the resulting string is not well-formed.
|
|
20
|
+
*
|
|
21
|
+
* Follows the AT Protocol NSID convention (https://atproto.com/specs/nsid)
|
|
22
|
+
* Full NSID: authority segments (hyphens allowed) + dot + name segment (no hyphens).
|
|
23
|
+
* - Authority segments: at most 253 characters (including periods), and must contain at least two segments.
|
|
24
|
+
* - Name segment: starts with alpha, alphanumeric only (no hyphens), max 63 chars.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* id`org.dxos.plugin.deck` // 'org.dxos.plugin.deck'
|
|
28
|
+
* id`${ns}.${plugin}.deck` // joins interpolated values
|
|
29
|
+
*/
|
|
30
|
+
export function id(strings: TemplateStringsArray, ...values: unknown[]): Id {
|
|
31
|
+
const raw = strings.reduce((out, str, i) => out + str + (i < values.length ? String(values[i]) : ''), '');
|
|
32
|
+
if (!ID.test(raw)) {
|
|
33
|
+
throw new Error(`Invalid id (expected AT Protocol NSID): ${JSON.stringify(raw)}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return raw as Id;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Returns true if the given string is a well-formed NSID.
|
|
41
|
+
*/
|
|
42
|
+
export const isWellFormedId = (value: string): boolean => ID.test(value);
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Returns true if the given string is a well-formed NSID name segment:
|
|
46
|
+
* `/^[a-zA-Z][a-zA-Z0-9]{0,62}$/` (starts with alpha, alphanumeric only, no dots or hyphens).
|
|
47
|
+
*/
|
|
48
|
+
export const isWellFormedIdPart = (value: string): boolean => PART.test(value);
|
package/src/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from './array';
|
|
|
7
7
|
export * from './assume';
|
|
8
8
|
export * from './binder';
|
|
9
9
|
export * from './bitfield';
|
|
10
|
+
export * from './browser-storage';
|
|
10
11
|
export * from './callback-collection';
|
|
11
12
|
export * from './callback';
|
|
12
13
|
export * from './case';
|
|
@@ -14,12 +15,16 @@ export * from './chunk-array';
|
|
|
14
15
|
export * from './circular-buffer';
|
|
15
16
|
export * from './clear-undefined';
|
|
16
17
|
export * from './complex';
|
|
18
|
+
export * from './composite-key';
|
|
17
19
|
export * from './deep';
|
|
18
20
|
export * from './defer-function';
|
|
19
21
|
export * from './defer';
|
|
20
22
|
export * from './entry';
|
|
23
|
+
export * from './error-format';
|
|
24
|
+
export * from './filename';
|
|
21
25
|
export * from './for-each-async';
|
|
22
26
|
export * from './human-hash';
|
|
27
|
+
export * from './id';
|
|
23
28
|
export * from './instance-id';
|
|
24
29
|
export * from './interval';
|
|
25
30
|
export * from './join-tables';
|
|
@@ -37,9 +42,11 @@ export * from './random';
|
|
|
37
42
|
export * from './range';
|
|
38
43
|
export * from './reducers';
|
|
39
44
|
export * from './remove-undefined-keys';
|
|
45
|
+
export * from './retry';
|
|
40
46
|
export * from './safe-await';
|
|
41
47
|
export * from './safe-instanceof';
|
|
42
48
|
export * from './safe-parse';
|
|
49
|
+
export * from './safe-stringify';
|
|
43
50
|
export * from './sliding-window-summary';
|
|
44
51
|
export * from './sort';
|
|
45
52
|
export * from './string';
|