@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.
Files changed (98) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/index.mjs +276 -152
  3. package/dist/lib/browser/index.mjs.map +4 -4
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/node-esm/index.mjs +276 -152
  6. package/dist/lib/node-esm/index.mjs.map +4 -4
  7. package/dist/lib/node-esm/meta.json +1 -1
  8. package/dist/types/src/array-to-hex.d.ts.map +1 -1
  9. package/dist/types/src/array.d.ts.map +1 -1
  10. package/dist/types/src/binder.d.ts.map +1 -1
  11. package/dist/types/src/bitfield.d.ts.map +1 -1
  12. package/dist/types/src/browser-storage.d.ts +9 -0
  13. package/dist/types/src/browser-storage.d.ts.map +1 -0
  14. package/dist/types/src/callback-collection.d.ts.map +1 -1
  15. package/dist/types/src/callback.d.ts.map +1 -1
  16. package/dist/types/src/case.d.ts.map +1 -1
  17. package/dist/types/src/chunk-array.d.ts.map +1 -1
  18. package/dist/types/src/circular-buffer.d.ts.map +1 -1
  19. package/dist/types/src/clear-undefined.d.ts.map +1 -1
  20. package/dist/types/src/complex.d.ts +15 -0
  21. package/dist/types/src/complex.d.ts.map +1 -1
  22. package/dist/types/src/composite-key.d.ts +10 -0
  23. package/dist/types/src/composite-key.d.ts.map +1 -0
  24. package/dist/types/src/composite-key.test.d.ts +2 -0
  25. package/dist/types/src/composite-key.test.d.ts.map +1 -0
  26. package/dist/types/src/deep.d.ts.map +1 -1
  27. package/dist/types/src/defer-function.d.ts.map +1 -1
  28. package/dist/types/src/defer.d.ts.map +1 -1
  29. package/dist/types/src/di-container.d.ts.map +1 -1
  30. package/dist/types/src/di-key.d.ts +7 -5
  31. package/dist/types/src/di-key.d.ts.map +1 -1
  32. package/dist/types/src/entry.d.ts.map +1 -1
  33. package/dist/types/src/error-format.d.ts.map +1 -1
  34. package/dist/types/src/filename.d.ts.map +1 -1
  35. package/dist/types/src/for-each-async.d.ts.map +1 -1
  36. package/dist/types/src/human-hash.d.ts.map +1 -1
  37. package/dist/types/src/id.d.ts +30 -0
  38. package/dist/types/src/id.d.ts.map +1 -0
  39. package/dist/types/src/id.test.d.ts +2 -0
  40. package/dist/types/src/id.test.d.ts.map +1 -0
  41. package/dist/types/src/index.d.ts +5 -2
  42. package/dist/types/src/index.d.ts.map +1 -1
  43. package/dist/types/src/instance-id.d.ts.map +1 -1
  44. package/dist/types/src/interval.d.ts.map +1 -1
  45. package/dist/types/src/join-tables.d.ts.map +1 -1
  46. package/dist/types/src/json.d.ts.map +1 -1
  47. package/dist/types/src/map-values.d.ts.map +1 -1
  48. package/dist/types/src/map.d.ts.map +1 -1
  49. package/dist/types/src/object.d.ts.map +1 -1
  50. package/dist/types/src/order-keys.d.ts.map +1 -1
  51. package/dist/types/src/order.d.ts.map +1 -1
  52. package/dist/types/src/pick.d.ts.map +1 -1
  53. package/dist/types/src/platform.d.ts +1 -1
  54. package/dist/types/src/position.d.ts +5 -5
  55. package/dist/types/src/position.d.ts.map +1 -1
  56. package/dist/types/src/random.d.ts.map +1 -1
  57. package/dist/types/src/range.d.ts.map +1 -1
  58. package/dist/types/src/reducers.d.ts.map +1 -1
  59. package/dist/types/src/remove-undefined-keys.d.ts.map +1 -1
  60. package/dist/types/src/retry.d.ts.map +1 -1
  61. package/dist/types/src/safe-await.d.ts.map +1 -1
  62. package/dist/types/src/safe-instanceof.d.ts.map +1 -1
  63. package/dist/types/src/safe-parse.d.ts.map +1 -1
  64. package/dist/types/src/safe-stringify.d.ts.map +1 -1
  65. package/dist/types/src/sliding-window-summary.d.ts.map +1 -1
  66. package/dist/types/src/sort.d.ts.map +1 -1
  67. package/dist/types/src/string.d.ts.map +1 -1
  68. package/dist/types/src/sum.d.ts.map +1 -1
  69. package/dist/types/src/throw-unhandled-error.d.ts.map +1 -1
  70. package/dist/types/src/to-fallback.d.ts +4 -4
  71. package/dist/types/src/to-fallback.d.ts.map +1 -1
  72. package/dist/types/src/tracer.d.ts.map +1 -1
  73. package/dist/types/src/tree.d.ts.map +1 -1
  74. package/dist/types/src/types.d.ts.map +1 -1
  75. package/dist/types/src/uint8array.d.ts +27 -0
  76. package/dist/types/src/uint8array.d.ts.map +1 -1
  77. package/dist/types/src/unit.d.ts.map +1 -1
  78. package/dist/types/src/url.d.ts.map +1 -1
  79. package/dist/types/src/weak.d.ts +15 -0
  80. package/dist/types/src/weak.d.ts.map +1 -1
  81. package/dist/types/tsconfig.tsbuildinfo +1 -1
  82. package/package.json +7 -10
  83. package/src/browser-storage.ts +60 -0
  84. package/src/complex.ts +32 -0
  85. package/src/composite-key.test.ts +31 -0
  86. package/src/composite-key.ts +16 -0
  87. package/src/di-key.ts +10 -8
  88. package/src/error-format.ts +3 -1
  89. package/src/id.test.ts +109 -0
  90. package/src/id.ts +48 -0
  91. package/src/index.ts +5 -2
  92. package/src/map-values.ts +6 -0
  93. package/src/position.test.ts +38 -44
  94. package/src/position.ts +7 -11
  95. package/src/string.ts +6 -2
  96. package/src/uint8array.ts +44 -0
  97. package/src/unit.ts +6 -2
  98. package/src/weak.ts +52 -14
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/util",
3
- "version": "0.8.4-main.8360d9e660",
3
+ "version": "0.8.4-main.8baae0fced",
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",
@@ -8,7 +8,7 @@
8
8
  "type": "git",
9
9
  "url": "https://github.com/dxos/dxos"
10
10
  },
11
- "license": "MIT",
11
+ "license": "FSL-1.1-Apache-2.0",
12
12
  "author": "DXOS.org",
13
13
  "sideEffects": false,
14
14
  "type": "module",
@@ -23,22 +23,19 @@
23
23
  }
24
24
  },
25
25
  "types": "dist/types/src/index.d.ts",
26
- "typesVersions": {
27
- "*": {}
28
- },
29
26
  "files": [
30
27
  "dist",
31
28
  "src"
32
29
  ],
33
30
  "dependencies": {
34
31
  "@hazae41/symbol-dispose-polyfill": "^1.0.2",
35
- "@dxos/invariant": "0.8.4-main.8360d9e660",
36
- "@dxos/node-std": "0.8.4-main.8360d9e660",
37
- "@dxos/keys": "0.8.4-main.8360d9e660",
38
- "@dxos/debug": "0.8.4-main.8360d9e660"
32
+ "@dxos/invariant": "0.8.4-main.8baae0fced",
33
+ "@dxos/debug": "0.8.4-main.8baae0fced",
34
+ "@dxos/keys": "0.8.4-main.8baae0fced",
35
+ "@dxos/node-std": "0.8.4-main.8baae0fced"
39
36
  },
40
37
  "devDependencies": {
41
- "@dxos/crypto": "0.8.4-main.8360d9e660"
38
+ "@dxos/crypto": "0.8.4-main.8baae0fced"
42
39
  },
43
40
  "publishConfig": {
44
41
  "access": "public"
@@ -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
+ };
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/di-key.ts CHANGED
@@ -47,7 +47,7 @@ export type DiKey<T> =
47
47
  }
48
48
  | SymbolDiKey<T>;
49
49
 
50
- export const DiKey = new (class DiKeyConstructor {
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
- #combinedRegistry = new WeakMap();
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.#lookupCombined(this.#combinedRegistry, ids);
92
- return defaultMap(this.#combinedRegistry, map, () => this.#combinedDescription(ids));
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
- #lookupCombined(map: WeakMap<any, any>, [first, ...rest]: DiKey<any>[]): WeakMap<any, any> {
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.#lookupCombined(value, rest);
102
+ return this._lookupCombined(value, rest);
103
103
  } else {
104
104
  return value;
105
105
  }
106
106
  }
107
107
 
108
- #combinedDescription([first, ...rest]: DiKey<any>[]) {
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();
@@ -13,7 +13,9 @@ export const formatErrorWithCauses = (error: Error): string => {
13
13
  while (current) {
14
14
  const prefix = level === 0 ? '' : `Caused by: `;
15
15
  lines.push(prefix + (current.stack ?? String(current)));
16
- if (!(current.cause instanceof Error)) break;
16
+ if (!(current.cause instanceof Error)) {
17
+ break;
18
+ }
17
19
  current = current.cause;
18
20
  level += 1;
19
21
  }
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,13 +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';
21
24
  export * from './filename';
22
25
  export * from './for-each-async';
23
26
  export * from './human-hash';
27
+ export * from './id';
24
28
  export * from './instance-id';
25
29
  export * from './interval';
26
30
  export * from './join-tables';
@@ -38,6 +42,7 @@ export * from './random';
38
42
  export * from './range';
39
43
  export * from './reducers';
40
44
  export * from './remove-undefined-keys';
45
+ export * from './retry';
41
46
  export * from './safe-await';
42
47
  export * from './safe-instanceof';
43
48
  export * from './safe-parse';
@@ -55,5 +60,3 @@ export * from './uint8array';
55
60
  export * from './unit';
56
61
  export * from './url';
57
62
  export * from './weak';
58
- export * from './error-format';
59
- export * from './retry';
package/src/map-values.ts CHANGED
@@ -52,6 +52,9 @@ class DeepMapper {
52
52
  res[i] = this._map(value[i], i);
53
53
  }
54
54
  return res;
55
+ } else if (ArrayBuffer.isView(value)) {
56
+ // Typed arrays (Uint8Array, etc.) are opaque leaf values, not POJOs to recurse into.
57
+ return value;
55
58
  } else if (value !== null && typeof value === 'object') {
56
59
  const res: any = {};
57
60
  this._cyclic.set(value, res);
@@ -112,6 +115,9 @@ class DeepMapperAsync {
112
115
  res[i] = await this._map(value[i], i);
113
116
  }
114
117
  return res;
118
+ } else if (ArrayBuffer.isView(value)) {
119
+ // Typed arrays (Uint8Array, etc.) are opaque leaf values, not POJOs to recurse into.
120
+ return value;
115
121
  } else if (value !== null && typeof value === 'object') {
116
122
  const res: any = {};
117
123
  this._cyclic.set(value, res);
@@ -2,7 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { describe, expect, it } from 'vitest';
5
+ import { describe, test } from 'vitest';
6
6
 
7
7
  import { type Position, byPosition } from './position';
8
8
 
@@ -12,14 +12,14 @@ type TestItem = {
12
12
  };
13
13
 
14
14
  describe('byPosition', () => {
15
- it('should keep items with same position in their original order', () => {
15
+ test('should keep items with same position in their original order', ({ expect }) => {
16
16
  const items: TestItem[] = [
17
- { id: 1, position: 'static' },
18
- { id: 2, position: 'static' },
19
- { id: 3, position: 'hoist' },
20
- { id: 4, position: 'hoist' },
21
- { id: 5, position: 'fallback' },
22
- { id: 6, position: 'fallback' },
17
+ { id: 1 },
18
+ { id: 2 },
19
+ { id: 3, position: 'first' },
20
+ { id: 4, position: 'first' },
21
+ { id: 5, position: 'last' },
22
+ { id: 6, position: 'last' },
23
23
  ];
24
24
 
25
25
  const sorted = [...items].sort(byPosition);
@@ -30,69 +30,63 @@ describe('byPosition', () => {
30
30
  expect(sorted.findIndex((item) => item.id === 5)).toBeLessThan(sorted.findIndex((item) => item.id === 6));
31
31
  });
32
32
 
33
- it('should place "hoist" items before "static" items', () => {
34
- const items: TestItem[] = [
35
- { id: 1, position: 'static' },
36
- { id: 2, position: 'hoist' },
37
- ];
33
+ test('should place "first" items before items in natural order', ({ expect }) => {
34
+ const items: TestItem[] = [{ id: 1 }, { id: 2, position: 'first' }];
38
35
 
39
36
  const sorted = [...items].sort(byPosition);
40
- expect(sorted[0].position).toBe('hoist');
41
- expect(sorted[1].position).toBe('static');
37
+ expect(sorted[0].position).toBe('first');
38
+ expect(sorted[1].position).toBeUndefined();
42
39
  });
43
40
 
44
- it('should place "fallback" items after "static" items', () => {
45
- const items: TestItem[] = [
46
- { id: 1, position: 'fallback' },
47
- { id: 2, position: 'static' },
48
- ];
41
+ test('should place "last" items after items in natural order', ({ expect }) => {
42
+ const items: TestItem[] = [{ id: 1, position: 'last' }, { id: 2 }];
49
43
 
50
44
  const sorted = [...items].sort(byPosition);
51
- expect(sorted[0].position).toBe('static');
52
- expect(sorted[1].position).toBe('fallback');
45
+ expect(sorted[0].position).toBeUndefined();
46
+ expect(sorted[1].position).toBe('last');
53
47
  });
54
48
 
55
- it('should treat items without position as "static"', () => {
56
- const items: TestItem[] = [{ id: 1 }, { id: 2, position: 'hoist' }, { id: 3, position: 'fallback' }];
49
+ test('should treat items without position as natural order', ({ expect }) => {
50
+ const items: TestItem[] = [{ id: 1 }, { id: 2, position: 'first' }, { id: 3, position: 'last' }];
57
51
 
58
52
  const sorted = [...items].sort(byPosition);
59
- expect(sorted[0].position).toBe('hoist');
53
+ expect(sorted[0].position).toBe('first');
60
54
  expect(sorted[1].position).toBeUndefined();
61
- expect(sorted[2].position).toBe('fallback');
55
+ expect(sorted[2].position).toBe('last');
62
56
  });
63
57
 
64
- it('should correctly sort mixed positions', () => {
58
+ test('should correctly sort mixed positions', ({ expect }) => {
65
59
  const items: TestItem[] = [
66
- { id: 1, position: 'fallback' },
67
- { id: 2, position: 'static' },
68
- { id: 3, position: 'hoist' },
69
- { id: 4 }, // implicit static
70
- { id: 5, position: 'hoist' },
71
- { id: 6, position: 'fallback' },
60
+ { id: 1, position: 'last' },
61
+ { id: 2 },
62
+ { id: 3, position: 'first' },
63
+ { id: 4 },
64
+ { id: 5, position: 'first' },
65
+ { id: 6, position: 'last' },
72
66
  ];
73
67
 
74
68
  const sorted = [...items].sort(byPosition);
75
69
 
76
- // All hoisted items should come first
77
- expect(sorted[0].position).toBe('hoist');
78
- expect(sorted[1].position).toBe('hoist');
70
+ // All "first" items should come first
71
+ expect(sorted[0].position).toBe('first');
72
+ expect(sorted[1].position).toBe('first');
79
73
 
80
- // Static items (including undefined) should be in the middle
81
- expect(sorted[2].position).toBe('static');
74
+ // Natural-order items (undefined) should be in the middle
75
+ expect(sorted[2].position).toBeUndefined();
82
76
  expect(sorted[3].position).toBeUndefined();
83
77
 
84
- // Fallback items should be last
85
- expect(sorted[4].position).toBe('fallback');
86
- expect(sorted[5].position).toBe('fallback');
78
+ // "last" items should be at the end
79
+ expect(sorted[4].position).toBe('last');
80
+ expect(sorted[5].position).toBe('last');
87
81
  });
88
82
 
89
- it('should handle empty arrays', () => {
83
+ test('should handle empty arrays', ({ expect }) => {
90
84
  const items: TestItem[] = [];
91
85
  expect(() => [...items].sort(byPosition)).not.toThrow();
92
86
  });
93
87
 
94
- it('should handle single item arrays', () => {
95
- const items: TestItem[] = [{ id: 1, position: 'static' }];
88
+ test('should handle single item arrays', ({ expect }) => {
89
+ const items: TestItem[] = [{ id: 1 }];
96
90
  expect(() => [...items].sort(byPosition)).not.toThrow();
97
91
  });
98
92
  });
package/src/position.ts CHANGED
@@ -6,25 +6,21 @@
6
6
 
7
7
  /**
8
8
  * Determines priority order:
9
- * - `static` - Remain in natural order.
10
- * - `hoist` - Placed before `static`.
11
- * - `fallback` - Placed after `static`.
9
+ * - `undefined` - Remain in natural order.
10
+ * - `first` - Placed before items in natural order.
11
+ * - `last` - Placed after items in natural order.
12
12
  */
13
- // TODO(wittjosiah): Change to 'static' | 'start' | 'end'.
14
- export type Position = 'static' | 'hoist' | 'fallback';
13
+ export type Position = 'first' | 'last';
15
14
 
16
15
  /**
17
16
  * Sorting function for sorting by position.
18
17
  */
19
- export const byPosition = <T extends { position?: Position }>(
20
- { position: a = 'static' }: T,
21
- { position: b = 'static' }: T,
22
- ) => {
18
+ export const byPosition = <T extends { position?: Position }>({ position: a }: T, { position: b }: T) => {
23
19
  if (a === b) {
24
20
  return 0;
25
- } else if (a === 'hoist' || b === 'fallback') {
21
+ } else if (a === 'first' || b === 'last') {
26
22
  return -1;
27
- } else if (b === 'hoist' || a === 'fallback') {
23
+ } else if (b === 'first' || a === 'last') {
28
24
  return 1;
29
25
  } else {
30
26
  return 0;
package/src/string.ts CHANGED
@@ -32,8 +32,12 @@ export function trim(strings: TemplateStringsArray, ...values: any[]) {
32
32
  // Split into lines and trim leading/trailing blank lines.
33
33
  const lines = raw.split('\n');
34
34
 
35
- while (lines.length && !lines[0].trim()) lines.shift();
36
- while (lines.length && !lines[lines.length - 1].trim()) lines.pop();
35
+ while (lines.length && !lines[0].trim()) {
36
+ lines.shift();
37
+ }
38
+ while (lines.length && !lines[lines.length - 1].trim()) {
39
+ lines.pop();
40
+ }
37
41
 
38
42
  // Find smallest indent across all non-blank lines.
39
43
  const minIndent = Math.min(...lines.filter((l) => l.trim()).map((l) => l.match(/^[ \t]*/)?.[0].length ?? 0));