@dxos/util 0.8.3 → 0.8.4-main.1068cf700f

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 (90) hide show
  1. package/dist/lib/browser/index.mjs +460 -169
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +460 -169
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/array.d.ts +3 -2
  8. package/dist/types/src/array.d.ts.map +1 -1
  9. package/dist/types/src/assume.d.ts.map +1 -1
  10. package/dist/types/src/deep.d.ts.map +1 -1
  11. package/dist/types/src/defer.d.ts +1 -1
  12. package/dist/types/src/defer.d.ts.map +1 -1
  13. package/dist/types/src/di-key.d.ts +3 -3
  14. package/dist/types/src/di-key.d.ts.map +1 -1
  15. package/dist/types/src/error-format.d.ts +5 -0
  16. package/dist/types/src/error-format.d.ts.map +1 -0
  17. package/dist/types/src/filename.d.ts +9 -0
  18. package/dist/types/src/filename.d.ts.map +1 -0
  19. package/dist/types/src/index.d.ts +7 -3
  20. package/dist/types/src/index.d.ts.map +1 -1
  21. package/dist/types/src/json.d.ts +1 -1
  22. package/dist/types/src/json.d.ts.map +1 -1
  23. package/dist/types/src/object.d.ts +3 -0
  24. package/dist/types/src/object.d.ts.map +1 -1
  25. package/dist/types/src/platform.d.ts +4 -1
  26. package/dist/types/src/platform.d.ts.map +1 -1
  27. package/dist/types/src/retry.d.ts +32 -0
  28. package/dist/types/src/retry.d.ts.map +1 -0
  29. package/dist/types/src/safe-parse.d.ts +6 -4
  30. package/dist/types/src/safe-parse.d.ts.map +1 -1
  31. package/dist/types/src/safe-stringify.d.ts +22 -0
  32. package/dist/types/src/safe-stringify.d.ts.map +1 -0
  33. package/dist/types/src/string.d.ts +8 -0
  34. package/dist/types/src/string.d.ts.map +1 -1
  35. package/dist/types/src/string.test.d.ts +2 -0
  36. package/dist/types/src/string.test.d.ts.map +1 -0
  37. package/dist/types/src/to-fallback.d.ts.map +1 -1
  38. package/dist/types/src/types.d.ts +23 -5
  39. package/dist/types/src/types.d.ts.map +1 -1
  40. package/dist/types/src/unit.d.ts +14 -0
  41. package/dist/types/src/unit.d.ts.map +1 -0
  42. package/dist/types/src/unit.test.d.ts +2 -0
  43. package/dist/types/src/unit.test.d.ts.map +1 -0
  44. package/dist/types/src/url.d.ts +5 -0
  45. package/dist/types/src/url.d.ts.map +1 -0
  46. package/dist/types/src/url.test.d.ts +2 -0
  47. package/dist/types/src/url.test.d.ts.map +1 -0
  48. package/dist/types/tsconfig.tsbuildinfo +1 -1
  49. package/package.json +14 -9
  50. package/src/array.test.ts +1 -1
  51. package/src/array.ts +9 -4
  52. package/src/assume.ts +0 -1
  53. package/src/binder.ts +2 -2
  54. package/src/circular-buffer.test.ts +1 -1
  55. package/src/complex.test.ts +1 -1
  56. package/src/deep.ts +2 -0
  57. package/src/defer.ts +1 -1
  58. package/src/error-format.ts +22 -0
  59. package/src/filename.ts +16 -0
  60. package/src/human-hash.test.ts +1 -1
  61. package/src/index.ts +7 -3
  62. package/src/json.ts +2 -7
  63. package/src/object.ts +3 -0
  64. package/src/platform.ts +12 -2
  65. package/src/position.test.ts +2 -2
  66. package/src/retry.ts +74 -0
  67. package/src/safe-parse.ts +21 -15
  68. package/src/safe-stringify.ts +146 -0
  69. package/src/sort.test.ts +1 -1
  70. package/src/string.test.ts +19 -0
  71. package/src/string.ts +36 -0
  72. package/src/to-fallback.ts +5 -4
  73. package/src/tree.test.ts +1 -1
  74. package/src/types.test.ts +11 -1
  75. package/src/types.ts +38 -10
  76. package/src/uint8array.test.ts +1 -1
  77. package/src/unit.test.ts +25 -0
  78. package/src/unit.ts +83 -0
  79. package/src/url.test.ts +22 -0
  80. package/src/url.ts +15 -0
  81. package/src/weak.test.ts +1 -1
  82. package/dist/lib/node/index.cjs +0 -2309
  83. package/dist/lib/node/index.cjs.map +0 -7
  84. package/dist/lib/node/meta.json +0 -1
  85. package/dist/types/src/explicit-resource-management-polyfill.d.ts +0 -1
  86. package/dist/types/src/explicit-resource-management-polyfill.d.ts.map +0 -1
  87. package/dist/types/src/first-two-chars.d.ts +0 -9
  88. package/dist/types/src/first-two-chars.d.ts.map +0 -1
  89. package/src/explicit-resource-management-polyfill.ts +0 -13
  90. package/src/first-two-chars.ts +0 -44
package/src/string.ts CHANGED
@@ -12,3 +12,39 @@ export const capitalize = (str: string): string => {
12
12
 
13
13
  return str.charAt(0).toUpperCase() + str.slice(1);
14
14
  };
15
+
16
+ /**
17
+ * Remove leading space from multi-line strings.
18
+ */
19
+ export function trim(strings: TemplateStringsArray, ...values: any[]) {
20
+ // First, build the raw result with relative indentation.
21
+ const raw = strings.reduce((out, str, i) => {
22
+ out += str;
23
+ if (i < values.length) {
24
+ const match = str.match(/(^|\n)([ \t]*)$/);
25
+ const baseIndent = match ? match[2] : '';
26
+ const val = String(values[i]).replace(/\r?\n/g, '\n' + baseIndent);
27
+ out += val;
28
+ }
29
+ return out;
30
+ }, '');
31
+
32
+ // Split into lines and trim leading/trailing blank lines.
33
+ const lines = raw.split('\n');
34
+
35
+ while (lines.length && !lines[0].trim()) lines.shift();
36
+ while (lines.length && !lines[lines.length - 1].trim()) lines.pop();
37
+
38
+ // Find smallest indent across all non-blank lines.
39
+ const minIndent = Math.min(...lines.filter((l) => l.trim()).map((l) => l.match(/^[ \t]*/)?.[0].length ?? 0));
40
+
41
+ // Remove that indent from all lines.
42
+ return lines.map((l) => l.slice(minIndent)).join('\n');
43
+ }
44
+
45
+ // From https://stackoverflow.com/a/67243723/2804332
46
+ /**
47
+ * Converts a string to kebab case.
48
+ */
49
+ export const kebabize = (str: string) =>
50
+ str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? '-' : '') + $.toLowerCase());
@@ -194,17 +194,18 @@ export const keyToFallback = (key: PublicKey) => hexToFallback(key.toHex());
194
194
  // TODO(wittjosiah): Support non-hex strings (e.g. DIDs, UUIDs, etc.)
195
195
  export const hexToFallback = (hex: string) => toFallback(parseInt(hex, 16));
196
196
 
197
+ // TODO(burdon): Rename?
197
198
  export const toFallback = (hash: number): FallbackValue => {
198
- // Calculate total possible combinations of emoji and hue pairs
199
+ // Calculate total possible combinations of emoji and hue pairs.
199
200
  const totalCombinations = idEmoji.length * idHue.length;
200
201
 
201
- // Get a deterministic index within the range of all possible combinations
202
+ // Get a deterministic index within the range of all possible combinations.
202
203
  const combinationIndex = hash % totalCombinations;
203
204
 
204
- // Calculate which emoji to use based on the combination index
205
+ // Calculate which emoji to use based on the combination index.
205
206
  const emojiIndex = Math.floor(combinationIndex / idHue.length);
206
207
 
207
- // Calculate which hue to use based on the combination index
208
+ // Calculate which hue to use based on the combination index.
208
209
  const hueIndex = combinationIndex % idHue.length;
209
210
 
210
211
  return {
package/src/tree.test.ts CHANGED
@@ -6,7 +6,7 @@ import { describe, test } from 'vitest';
6
6
 
7
7
  import { PublicKey } from '@dxos/keys';
8
8
 
9
- import { stringifyTree, type TreeNode } from './tree';
9
+ import { type TreeNode, stringifyTree } from './tree';
10
10
 
11
11
  describe('Tree logging', () => {
12
12
  test('simple', () => {
package/src/types.test.ts CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { describe, expect, test } from 'vitest';
6
6
 
7
- import { isNonNullable } from './types';
7
+ import { arrayMove, arraySwap, isNonNullable } from './types';
8
8
 
9
9
  describe('types', () => {
10
10
  test('filter', async () => {
@@ -12,4 +12,14 @@ describe('types', () => {
12
12
  const filtered: number[] = values.filter(isNonNullable);
13
13
  expect(filtered).to.deep.equal([1, 2, 3, 4]);
14
14
  });
15
+
16
+ test('arrayMove', () => {
17
+ expect(arrayMove([1, 2, 3, 4], 0, 2)).to.deep.equal([2, 3, 1, 4]);
18
+ expect(arrayMove([1, 2, 3, 4], 2, 0)).to.deep.equal([3, 1, 2, 4]);
19
+ });
20
+
21
+ test('arraySwap', () => {
22
+ expect(arraySwap([1, 2, 3, 4], 0, 2)).to.deep.equal([3, 2, 1, 4]);
23
+ expect(arraySwap([1, 2, 3, 4], 2, 0)).to.deep.equal([3, 2, 1, 4]);
24
+ });
15
25
  });
package/src/types.ts CHANGED
@@ -12,13 +12,31 @@ export type MaybePromise<T> = T | Promise<T>;
12
12
 
13
13
  export type GuardedType<T> = T extends (value: any) => value is infer R ? R : never;
14
14
 
15
- export type DeepReadonly<T> = {
16
- readonly [P in keyof T]: T[P] extends Record<string, any>
17
- ? DeepReadonly<T[P]>
18
- : T[P] extends Array<infer U>
19
- ? ReadonlyArray<DeepReadonly<U>>
20
- : T[P];
21
- };
15
+ /**
16
+ * Removes readonly modifiers from top-level properties of T.
17
+ * Also converts readonly arrays at the top level to mutable arrays.
18
+ * For nested properties, mutability depends on the schema definition.
19
+ */
20
+ export type ToMutable<T> = T extends object
21
+ ? { -readonly [K in keyof T]: T[K] extends readonly (infer U)[] ? U[] : T[K] }
22
+ : T;
23
+
24
+ export type Intersection<Types extends readonly unknown[]> = Types extends [infer First, ...infer Rest]
25
+ ? First & Intersection<Rest>
26
+ : unknown;
27
+
28
+ /**
29
+ * Recursively makes all properties of T readonly.
30
+ * Primitives (including branded types like `string & { __brand: true }`) are returned as-is.
31
+ * Arrays become ReadonlyArrays with deeply readonly elements.
32
+ */
33
+ export type DeepReadonly<T> = T extends string | number | boolean | bigint | symbol | null | undefined
34
+ ? T // Primitives (including branded primitives) stay as-is.
35
+ : T extends Array<infer U>
36
+ ? ReadonlyArray<DeepReadonly<U>>
37
+ : T extends object
38
+ ? { readonly [P in keyof T]: DeepReadonly<T[P]> }
39
+ : T;
22
40
 
23
41
  export type DeepWriteable<T> = { -readonly [K in keyof T]: T[K] extends object ? DeepWriteable<T[K]> : T[K] };
24
42
 
@@ -48,7 +66,7 @@ export type Falsy = false | 0 | '' | null | undefined;
48
66
  * NOTE: To filter by type:
49
67
  * items.filter((item: any): item is RangeSet<Decoration> => item instanceof RangeSet)
50
68
  */
51
- export const isNotFalsy = <T>(value: T): value is Exclude<T, Falsy> => !!value;
69
+ export const isTruthy = <T>(value: T): value is Exclude<T, Falsy> => !!value;
52
70
  export const isNonNullable = <T>(value: T | null | undefined): value is T => value != null;
53
71
 
54
72
  // TODO(burdon): Replace use of setTimeout everywhere?
@@ -104,9 +122,19 @@ export const sortKeys = <T extends object>(obj: T): T =>
104
122
  }, {} as T);
105
123
 
106
124
  /**
107
- * Swap position of element within array.
125
+ * Move element within array.
108
126
  */
109
- export const arrayMove = <T>(array: T[], from: number, to: number): Array<T> => {
127
+ export const arrayMove = <T>(array: T[], from: number, to: number): T[] => {
110
128
  array.splice(to < 0 ? array.length + to : to, 0, array.splice(from, 1)[0]);
111
129
  return array;
112
130
  };
131
+
132
+ /**
133
+ * Swap position of element within array.
134
+ */
135
+ export function arraySwap<T>(array: T[], from: number, to: number): T[] {
136
+ const current = array[from];
137
+ array[from] = array[to];
138
+ array[to] = current;
139
+ return array;
140
+ }
@@ -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 { arrayToBuffer, arrayToString, bufferToArray, stringToArray } from './uint8array';
8
8
 
@@ -0,0 +1,25 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { describe, it } from 'vitest';
6
+
7
+ import { Unit } from './unit';
8
+
9
+ describe('url', () => {
10
+ it('duration', ({ expect }) => {
11
+ const tests: [number, string][] = [
12
+ [0, '0ms'],
13
+ [1, '1ms'],
14
+ [1_000, '1.0s'],
15
+ [2_887, '2.9s'],
16
+ [1_000 * 60, '1m'],
17
+ [1_000 * 60 + 1_000 * 5, '1m 5s'],
18
+ [1_000 * 60 * 60 + 1_000 * 60 * 5, '1h 5m'],
19
+ ];
20
+
21
+ tests.forEach(([input, output]) => {
22
+ expect(Unit.Duration(input).toString(), input.toString()).toBe(output);
23
+ });
24
+ });
25
+ });
package/src/unit.ts ADDED
@@ -0,0 +1,83 @@
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
+ return {
24
+ unit,
25
+ value,
26
+ formattedValue: value.toFixed(precision),
27
+ toString: () => `${value.toFixed(precision)}${unit.symbol}`,
28
+ };
29
+ };
30
+ };
31
+
32
+ const MS_SECONDS = 1_000;
33
+ const MS_MINUTES = 60 * MS_SECONDS;
34
+ const MS_HOURS = 60 * MS_MINUTES;
35
+
36
+ export const Unit: Record<string, UnitFormat> = {
37
+ // General.
38
+ Percent: createFormat({ symbol: '%', quotient: 1 / 100, precision: 2 }),
39
+ Thousand: createFormat({ symbol: 'k', quotient: 1_000, precision: 2 }),
40
+
41
+ // Bytes (note KB vs KiB).
42
+ Gigabyte: createFormat({ symbol: 'GB', quotient: 1_000 * 1_000 * 1_000, precision: 2 }),
43
+ Megabyte: createFormat({ symbol: 'MB', quotient: 1_000 * 1_000, precision: 2 }),
44
+ Kilobyte: createFormat({ symbol: 'KB', quotient: 1_000, precision: 2 }),
45
+ Byte: createFormat({ symbol: 'B', quotient: 1 }),
46
+
47
+ // Time.
48
+ Hour: createFormat({ symbol: 'h', quotient: MS_HOURS }),
49
+ Minute: createFormat({ symbol: 'm', quotient: MS_MINUTES }),
50
+ Second: createFormat({ symbol: 's', quotient: MS_SECONDS, precision: 1 }),
51
+ Millisecond: createFormat({ symbol: 'ms', quotient: 1 }),
52
+ Duration: (n: number) => {
53
+ const hours = Math.floor(n / MS_HOURS);
54
+ const minutes = Math.floor((n % MS_HOURS) / MS_MINUTES);
55
+ if (hours) {
56
+ const formattedValue = minutes ? `${hours}h ${minutes}m` : `${hours}h`;
57
+ return {
58
+ unit: { symbol: 'h', quotient: MS_HOURS },
59
+ value: hours,
60
+ formattedValue,
61
+ toString: () => formattedValue,
62
+ };
63
+ }
64
+
65
+ if (minutes) {
66
+ const seconds = (n - MS_MINUTES * minutes) / MS_SECONDS;
67
+ const formattedValue = seconds ? `${minutes}m ${seconds}s` : `${minutes}m`;
68
+ return {
69
+ unit: { symbol: 'm', quotient: MS_MINUTES },
70
+ value: minutes,
71
+ formattedValue,
72
+ toString: () => formattedValue,
73
+ };
74
+ }
75
+
76
+ const seconds = n >= MS_SECONDS;
77
+ if (seconds) {
78
+ return Unit.Second(n);
79
+ }
80
+
81
+ return Unit.Millisecond(n);
82
+ },
83
+ } 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