@dxos/util 0.8.3 → 0.8.4-main.05e74ebcff

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 (158) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/index.mjs +672 -280
  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 +672 -280
  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 +3 -2
  10. package/dist/types/src/array.d.ts.map +1 -1
  11. package/dist/types/src/assume.d.ts.map +1 -1
  12. package/dist/types/src/binder.d.ts.map +1 -1
  13. package/dist/types/src/bitfield.d.ts.map +1 -1
  14. package/dist/types/src/browser-storage.d.ts +9 -0
  15. package/dist/types/src/browser-storage.d.ts.map +1 -0
  16. package/dist/types/src/callback-collection.d.ts.map +1 -1
  17. package/dist/types/src/callback.d.ts.map +1 -1
  18. package/dist/types/src/case.d.ts.map +1 -1
  19. package/dist/types/src/chunk-array.d.ts.map +1 -1
  20. package/dist/types/src/circular-buffer.d.ts +1 -0
  21. package/dist/types/src/circular-buffer.d.ts.map +1 -1
  22. package/dist/types/src/clear-undefined.d.ts.map +1 -1
  23. package/dist/types/src/complex.d.ts +15 -0
  24. package/dist/types/src/complex.d.ts.map +1 -1
  25. package/dist/types/src/composite-key.d.ts +10 -0
  26. package/dist/types/src/composite-key.d.ts.map +1 -0
  27. package/dist/types/src/composite-key.test.d.ts +2 -0
  28. package/dist/types/src/composite-key.test.d.ts.map +1 -0
  29. package/dist/types/src/deep.d.ts +0 -3
  30. package/dist/types/src/deep.d.ts.map +1 -1
  31. package/dist/types/src/defer-function.d.ts.map +1 -1
  32. package/dist/types/src/defer.d.ts +1 -1
  33. package/dist/types/src/defer.d.ts.map +1 -1
  34. package/dist/types/src/di-container.d.ts.map +1 -1
  35. package/dist/types/src/di-key.d.ts +7 -5
  36. package/dist/types/src/di-key.d.ts.map +1 -1
  37. package/dist/types/src/entry.d.ts.map +1 -1
  38. package/dist/types/src/error-format.d.ts +5 -0
  39. package/dist/types/src/error-format.d.ts.map +1 -0
  40. package/dist/types/src/filename.d.ts +9 -0
  41. package/dist/types/src/filename.d.ts.map +1 -0
  42. package/dist/types/src/for-each-async.d.ts.map +1 -1
  43. package/dist/types/src/human-hash.d.ts.map +1 -1
  44. package/dist/types/src/id.d.ts +30 -0
  45. package/dist/types/src/id.d.ts.map +1 -0
  46. package/dist/types/src/id.test.d.ts +2 -0
  47. package/dist/types/src/id.test.d.ts.map +1 -0
  48. package/dist/types/src/index.d.ts +10 -3
  49. package/dist/types/src/index.d.ts.map +1 -1
  50. package/dist/types/src/instance-id.d.ts.map +1 -1
  51. package/dist/types/src/interval.d.ts.map +1 -1
  52. package/dist/types/src/join-tables.d.ts.map +1 -1
  53. package/dist/types/src/json.d.ts +1 -1
  54. package/dist/types/src/json.d.ts.map +1 -1
  55. package/dist/types/src/map-values.d.ts.map +1 -1
  56. package/dist/types/src/map.d.ts.map +1 -1
  57. package/dist/types/src/object.d.ts +3 -0
  58. package/dist/types/src/object.d.ts.map +1 -1
  59. package/dist/types/src/order-keys.d.ts.map +1 -1
  60. package/dist/types/src/order.d.ts.map +1 -1
  61. package/dist/types/src/pick.d.ts.map +1 -1
  62. package/dist/types/src/platform.d.ts +5 -2
  63. package/dist/types/src/platform.d.ts.map +1 -1
  64. package/dist/types/src/position.d.ts +5 -5
  65. package/dist/types/src/position.d.ts.map +1 -1
  66. package/dist/types/src/random.d.ts.map +1 -1
  67. package/dist/types/src/range.d.ts.map +1 -1
  68. package/dist/types/src/reducers.d.ts.map +1 -1
  69. package/dist/types/src/remove-undefined-keys.d.ts.map +1 -1
  70. package/dist/types/src/retry.d.ts +32 -0
  71. package/dist/types/src/retry.d.ts.map +1 -0
  72. package/dist/types/src/safe-await.d.ts.map +1 -1
  73. package/dist/types/src/safe-instanceof.d.ts.map +1 -1
  74. package/dist/types/src/safe-parse.d.ts +7 -4
  75. package/dist/types/src/safe-parse.d.ts.map +1 -1
  76. package/dist/types/src/safe-stringify.d.ts +22 -0
  77. package/dist/types/src/safe-stringify.d.ts.map +1 -0
  78. package/dist/types/src/safe-stringify.test.d.ts +2 -0
  79. package/dist/types/src/safe-stringify.test.d.ts.map +1 -0
  80. package/dist/types/src/sliding-window-summary.d.ts.map +1 -1
  81. package/dist/types/src/sort.d.ts.map +1 -1
  82. package/dist/types/src/string.d.ts +8 -0
  83. package/dist/types/src/string.d.ts.map +1 -1
  84. package/dist/types/src/string.test.d.ts +2 -0
  85. package/dist/types/src/string.test.d.ts.map +1 -0
  86. package/dist/types/src/sum.d.ts.map +1 -1
  87. package/dist/types/src/throw-unhandled-error.d.ts.map +1 -1
  88. package/dist/types/src/to-fallback.d.ts +18 -4
  89. package/dist/types/src/to-fallback.d.ts.map +1 -1
  90. package/dist/types/src/tracer.d.ts.map +1 -1
  91. package/dist/types/src/tree.d.ts.map +1 -1
  92. package/dist/types/src/types.d.ts +24 -6
  93. package/dist/types/src/types.d.ts.map +1 -1
  94. package/dist/types/src/uint8array.d.ts.map +1 -1
  95. package/dist/types/src/unit.d.ts +14 -0
  96. package/dist/types/src/unit.d.ts.map +1 -0
  97. package/dist/types/src/unit.test.d.ts +2 -0
  98. package/dist/types/src/unit.test.d.ts.map +1 -0
  99. package/dist/types/src/url.d.ts +5 -0
  100. package/dist/types/src/url.d.ts.map +1 -0
  101. package/dist/types/src/url.test.d.ts +2 -0
  102. package/dist/types/src/url.test.d.ts.map +1 -0
  103. package/dist/types/src/weak.d.ts +15 -0
  104. package/dist/types/src/weak.d.ts.map +1 -1
  105. package/dist/types/tsconfig.tsbuildinfo +1 -1
  106. package/package.json +15 -17
  107. package/src/array.test.ts +1 -1
  108. package/src/array.ts +9 -4
  109. package/src/assume.ts +0 -1
  110. package/src/binder.ts +2 -2
  111. package/src/browser-storage.ts +60 -0
  112. package/src/circular-buffer.test.ts +27 -1
  113. package/src/circular-buffer.ts +5 -0
  114. package/src/complex.test.ts +1 -1
  115. package/src/complex.ts +32 -0
  116. package/src/composite-key.test.ts +31 -0
  117. package/src/composite-key.ts +16 -0
  118. package/src/deep.ts +2 -6
  119. package/src/defer.ts +1 -1
  120. package/src/di-key.ts +10 -8
  121. package/src/error-format.ts +24 -0
  122. package/src/filename.ts +16 -0
  123. package/src/human-hash.test.ts +1 -1
  124. package/src/id.test.ts +109 -0
  125. package/src/id.ts +48 -0
  126. package/src/index.ts +10 -3
  127. package/src/json.ts +2 -7
  128. package/src/object.ts +3 -0
  129. package/src/platform.ts +35 -3
  130. package/src/position.test.ts +39 -45
  131. package/src/position.ts +7 -11
  132. package/src/retry.ts +74 -0
  133. package/src/safe-parse.ts +29 -15
  134. package/src/safe-stringify.test.ts +96 -0
  135. package/src/safe-stringify.ts +153 -0
  136. package/src/sort.test.ts +1 -1
  137. package/src/string.test.ts +19 -0
  138. package/src/string.ts +40 -0
  139. package/src/to-fallback.ts +44 -160
  140. package/src/tree.test.ts +1 -1
  141. package/src/types.test.ts +11 -1
  142. package/src/types.ts +40 -12
  143. package/src/uint8array.test.ts +1 -1
  144. package/src/unit.test.ts +25 -0
  145. package/src/unit.ts +87 -0
  146. package/src/url.test.ts +22 -0
  147. package/src/url.ts +15 -0
  148. package/src/weak.test.ts +1 -1
  149. package/src/weak.ts +52 -14
  150. package/dist/lib/node/index.cjs +0 -2309
  151. package/dist/lib/node/index.cjs.map +0 -7
  152. package/dist/lib/node/meta.json +0 -1
  153. package/dist/types/src/explicit-resource-management-polyfill.d.ts +0 -1
  154. package/dist/types/src/explicit-resource-management-polyfill.d.ts.map +0 -1
  155. package/dist/types/src/first-two-chars.d.ts +0 -9
  156. package/dist/types/src/first-two-chars.d.ts.map +0 -1
  157. package/src/explicit-resource-management-polyfill.ts +0 -13
  158. package/src/first-two-chars.ts +0 -44
package/src/unit.ts ADDED
@@ -0,0 +1,87 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export type Unit = {
6
+ symbol: string;
7
+ quotient: number;
8
+ precision?: number;
9
+ };
10
+
11
+ export type UnitValue<T> = {
12
+ unit: Unit;
13
+ value: number;
14
+ formattedValue: T;
15
+ toString: () => string;
16
+ };
17
+
18
+ export type UnitFormat<T = any> = (n: number, precision?: number) => UnitValue<T>;
19
+
20
+ const createFormat = (unit: Unit): UnitFormat<string> => {
21
+ return (n: number, precision = unit.precision ?? 0) => {
22
+ const value = n / unit.quotient;
23
+ const formattedValue = value.toLocaleString('en-US', {
24
+ minimumFractionDigits: precision,
25
+ maximumFractionDigits: precision,
26
+ });
27
+ return {
28
+ unit,
29
+ value,
30
+ formattedValue,
31
+ toString: () => `${formattedValue}${unit.symbol}`,
32
+ };
33
+ };
34
+ };
35
+
36
+ const MS_SECONDS = 1_000;
37
+ const MS_MINUTES = 60 * MS_SECONDS;
38
+ const MS_HOURS = 60 * MS_MINUTES;
39
+
40
+ export const Unit: Record<string, UnitFormat> = {
41
+ // General.
42
+ Percent: createFormat({ symbol: '%', quotient: 1 / 100, precision: 2 }),
43
+ Thousand: createFormat({ symbol: 'k', quotient: 1_000, precision: 2 }),
44
+
45
+ // Bytes (note KB vs KiB).
46
+ Gigabyte: createFormat({ symbol: 'GB', quotient: 1_000 * 1_000 * 1_000, precision: 2 }),
47
+ Megabyte: createFormat({ symbol: 'MB', quotient: 1_000 * 1_000, precision: 2 }),
48
+ Kilobyte: createFormat({ symbol: 'KB', quotient: 1_000, precision: 2 }),
49
+ Byte: createFormat({ symbol: 'B', quotient: 1 }),
50
+
51
+ // Time.
52
+ Hour: createFormat({ symbol: 'h', quotient: MS_HOURS }),
53
+ Minute: createFormat({ symbol: 'm', quotient: MS_MINUTES }),
54
+ Second: createFormat({ symbol: 's', quotient: MS_SECONDS, precision: 1 }),
55
+ Millisecond: createFormat({ symbol: 'ms', quotient: 1 }),
56
+ Duration: (n: number) => {
57
+ const hours = Math.floor(n / MS_HOURS);
58
+ const minutes = Math.floor((n % MS_HOURS) / MS_MINUTES);
59
+ if (hours) {
60
+ const formattedValue = minutes ? `${hours}h ${minutes}m` : `${hours}h`;
61
+ return {
62
+ unit: { symbol: 'h', quotient: MS_HOURS },
63
+ value: hours,
64
+ formattedValue,
65
+ toString: () => formattedValue,
66
+ };
67
+ }
68
+
69
+ if (minutes) {
70
+ const seconds = (n - MS_MINUTES * minutes) / MS_SECONDS;
71
+ const formattedValue = seconds ? `${minutes}m ${seconds}s` : `${minutes}m`;
72
+ return {
73
+ unit: { symbol: 'm', quotient: MS_MINUTES },
74
+ value: minutes,
75
+ formattedValue,
76
+ toString: () => formattedValue,
77
+ };
78
+ }
79
+
80
+ const seconds = n >= MS_SECONDS;
81
+ if (seconds) {
82
+ return Unit.Second(n);
83
+ }
84
+
85
+ return Unit.Millisecond(n);
86
+ },
87
+ } as const;
@@ -0,0 +1,22 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { describe, it } from 'vitest';
6
+
7
+ import { createUrl } from './url';
8
+
9
+ describe('url', () => {
10
+ it('should create a url', ({ expect }) => {
11
+ const url = createUrl('https://example.com', {
12
+ i1: undefined,
13
+ i2: null,
14
+ p1: true,
15
+ p2: false,
16
+ p3: 'dxos',
17
+ p4: 100,
18
+ });
19
+
20
+ expect(url.toString()).toBe('https://example.com/?p1=true&p2=false&p3=dxos&p4=100');
21
+ });
22
+ });
package/src/url.ts ADDED
@@ -0,0 +1,15 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ /**
6
+ * Normalize construction of URLs.
7
+ */
8
+ export const createUrl = (url: URL | string, search?: Record<string, any | undefined>): URL => {
9
+ const base = typeof url === 'string' ? new URL(url) : url;
10
+ if (search) {
11
+ base.search = new URLSearchParams(Object.entries(search).filter(([_, value]) => value != null)).toString();
12
+ }
13
+
14
+ return base;
15
+ };
package/src/weak.test.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { test, expect, describe } from 'vitest';
5
+ import { describe, expect, test } from 'vitest';
6
6
 
7
7
  import { WeakDictionary } from './weak';
8
8
 
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((cleanUpCallback: () => void) => {
11
- cleanUpCallback();
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
- this._internal = new Map(entries?.map(([key, value]) => [key, new WeakRef(value)]));
16
- entries?.forEach(([key, value]) => this._register(key, value));
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.set(key, new WeakRef(value));
59
- this._register(key, value);
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 {