@dxos/util 0.8.4-main.f9ba587 โ†’ 0.8.4-main.fcfe5033a5

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 (102) hide show
  1. package/dist/lib/browser/index.mjs +508 -146
  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 +508 -146
  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/circular-buffer.d.ts +1 -0
  11. package/dist/types/src/circular-buffer.d.ts.map +1 -1
  12. package/dist/types/src/complex.d.ts +15 -0
  13. package/dist/types/src/complex.d.ts.map +1 -1
  14. package/dist/types/src/composite-key.d.ts +10 -0
  15. package/dist/types/src/composite-key.d.ts.map +1 -0
  16. package/dist/types/src/composite-key.test.d.ts +2 -0
  17. package/dist/types/src/composite-key.test.d.ts.map +1 -0
  18. package/dist/types/src/deep.d.ts +0 -3
  19. package/dist/types/src/deep.d.ts.map +1 -1
  20. package/dist/types/src/defer.d.ts +1 -1
  21. package/dist/types/src/defer.d.ts.map +1 -1
  22. package/dist/types/src/di-key.d.ts +7 -5
  23. package/dist/types/src/di-key.d.ts.map +1 -1
  24. package/dist/types/src/error-format.d.ts +5 -0
  25. package/dist/types/src/error-format.d.ts.map +1 -0
  26. package/dist/types/src/filename.d.ts +9 -0
  27. package/dist/types/src/filename.d.ts.map +1 -0
  28. package/dist/types/src/index.d.ts +6 -2
  29. package/dist/types/src/index.d.ts.map +1 -1
  30. package/dist/types/src/json.d.ts +1 -1
  31. package/dist/types/src/json.d.ts.map +1 -1
  32. package/dist/types/src/object.d.ts +3 -0
  33. package/dist/types/src/object.d.ts.map +1 -1
  34. package/dist/types/src/platform.d.ts +4 -1
  35. package/dist/types/src/platform.d.ts.map +1 -1
  36. package/dist/types/src/retry.d.ts +32 -0
  37. package/dist/types/src/retry.d.ts.map +1 -0
  38. package/dist/types/src/safe-parse.d.ts +7 -4
  39. package/dist/types/src/safe-parse.d.ts.map +1 -1
  40. package/dist/types/src/safe-stringify.d.ts +22 -0
  41. package/dist/types/src/safe-stringify.d.ts.map +1 -0
  42. package/dist/types/src/safe-stringify.test.d.ts +2 -0
  43. package/dist/types/src/safe-stringify.test.d.ts.map +1 -0
  44. package/dist/types/src/string.d.ts +8 -0
  45. package/dist/types/src/string.d.ts.map +1 -1
  46. package/dist/types/src/string.test.d.ts +2 -0
  47. package/dist/types/src/string.test.d.ts.map +1 -0
  48. package/dist/types/src/to-fallback.d.ts +18 -4
  49. package/dist/types/src/to-fallback.d.ts.map +1 -1
  50. package/dist/types/src/types.d.ts +24 -6
  51. package/dist/types/src/types.d.ts.map +1 -1
  52. package/dist/types/src/unit.d.ts +14 -0
  53. package/dist/types/src/unit.d.ts.map +1 -0
  54. package/dist/types/src/unit.test.d.ts +2 -0
  55. package/dist/types/src/unit.test.d.ts.map +1 -0
  56. package/dist/types/src/weak.d.ts +15 -0
  57. package/dist/types/src/weak.d.ts.map +1 -1
  58. package/dist/types/tsconfig.tsbuildinfo +1 -1
  59. package/package.json +14 -13
  60. package/src/array.test.ts +1 -1
  61. package/src/array.ts +9 -4
  62. package/src/assume.ts +0 -1
  63. package/src/binder.ts +2 -2
  64. package/src/circular-buffer.test.ts +27 -1
  65. package/src/circular-buffer.ts +5 -0
  66. package/src/complex.test.ts +1 -1
  67. package/src/complex.ts +32 -0
  68. package/src/composite-key.test.ts +31 -0
  69. package/src/composite-key.ts +16 -0
  70. package/src/deep.ts +2 -6
  71. package/src/defer.ts +1 -1
  72. package/src/di-key.ts +10 -8
  73. package/src/error-format.ts +22 -0
  74. package/src/filename.ts +16 -0
  75. package/src/human-hash.test.ts +1 -1
  76. package/src/index.ts +6 -2
  77. package/src/json.ts +2 -7
  78. package/src/object.ts +3 -0
  79. package/src/platform.ts +35 -3
  80. package/src/position.test.ts +2 -2
  81. package/src/retry.ts +74 -0
  82. package/src/safe-parse.ts +29 -15
  83. package/src/safe-stringify.test.ts +96 -0
  84. package/src/safe-stringify.ts +153 -0
  85. package/src/sort.test.ts +1 -1
  86. package/src/string.test.ts +19 -0
  87. package/src/string.ts +36 -0
  88. package/src/to-fallback.ts +44 -160
  89. package/src/tree.test.ts +1 -1
  90. package/src/types.test.ts +11 -1
  91. package/src/types.ts +40 -12
  92. package/src/uint8array.test.ts +1 -1
  93. package/src/unit.test.ts +25 -0
  94. package/src/unit.ts +83 -0
  95. package/src/weak.test.ts +1 -1
  96. package/src/weak.ts +52 -14
  97. package/dist/types/src/explicit-resource-management-polyfill.d.ts +0 -1
  98. package/dist/types/src/explicit-resource-management-polyfill.d.ts.map +0 -1
  99. package/dist/types/src/first-two-chars.d.ts +0 -9
  100. package/dist/types/src/first-two-chars.d.ts.map +0 -1
  101. package/src/explicit-resource-management-polyfill.ts +0 -13
  102. package/src/first-two-chars.ts +0 -44
package/src/safe-parse.ts CHANGED
@@ -2,31 +2,45 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- export const safeParseInt = (value: string | undefined, defaultValue?: number) => {
5
+ export function safeParseInt(str: string | undefined, defaultValue: number): number;
6
+ export function safeParseInt(str: string | undefined): number | undefined;
7
+ export function safeParseInt(str: string | undefined, defaultValue?: number): number | undefined {
6
8
  try {
7
- const n = parseInt(value ?? '');
8
- return isNaN(n) ? defaultValue : n;
9
- } catch (err) {
9
+ const value = parseInt(str ?? '');
10
+ return isNaN(value) ? defaultValue : value;
11
+ } catch {
10
12
  return defaultValue;
11
13
  }
12
- };
14
+ }
13
15
 
14
- export const safeParseFloat = (str: string, defaultValue?: number): number | undefined => {
16
+ export function safeParseFloat(str: string | undefined, defaultValue: number): number;
17
+ export function safeParseFloat(str: string | undefined): number | undefined;
18
+ export function safeParseFloat(str: string | undefined, defaultValue?: number): number | undefined {
15
19
  try {
16
- return parseFloat(str);
20
+ const value = parseFloat(str ?? '');
21
+ return isNaN(value) ? defaultValue : value;
17
22
  } catch {
18
- return defaultValue ?? 0;
23
+ return defaultValue;
19
24
  }
20
- };
25
+ }
21
26
 
22
27
  export const safeParseJson: {
23
- <T extends object>(data: string | undefined | null, defaultValue: T): T;
24
- <T extends object>(data: string | undefined | null): T | undefined;
25
- } = <T extends object>(data: string | undefined | null, defaultValue?: T) => {
26
- if (data && data.length > 0) {
28
+ <T extends object>(str: string | undefined | null, defaultValue: T): T;
29
+ <T extends object>(str: string | undefined | null): T | undefined;
30
+ } = <T extends object = any>(str: string | undefined | null, defaultValue?: T): T | undefined => {
31
+ if (str && str.length > 0) {
27
32
  try {
28
- return JSON.parse(data);
29
- } catch (err) {}
33
+ return JSON.parse(str);
34
+ } catch {}
30
35
  }
36
+
31
37
  return defaultValue;
32
38
  };
39
+
40
+ export const safeUrl = (str: string | undefined | null): URL | undefined => {
41
+ try {
42
+ return new URL(str ?? '');
43
+ } catch {
44
+ return undefined;
45
+ }
46
+ };
@@ -0,0 +1,96 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { describe, test } from 'vitest';
6
+
7
+ import { SKIP, createReplacer, safeStringify } from './safe-stringify';
8
+
9
+ describe('safeStringify', () => {
10
+ test('stringifies plain nested objects', ({ expect }) => {
11
+ const data = { a: 1, b: { c: 'hello' } };
12
+ const result = safeStringify(data);
13
+ expect(result).to.include('"a": 1');
14
+ expect(result).to.include('"b"');
15
+ expect(result).to.include('"c": "hello"');
16
+ });
17
+
18
+ test('handles circular references (Cycle story)', ({ expect }) => {
19
+ const cycle: Record<string, unknown> = { a: 1, b: [] };
20
+ (cycle.b as unknown[]).push(cycle);
21
+
22
+ const result = safeStringify(cycle);
23
+ expect(result).to.include('"a": 1');
24
+ expect(result).to.include('=> $]');
25
+ expect(result).to.match(/\[\$\.b\.\d+ => \$\]/);
26
+ });
27
+
28
+ test('handles multiple circular references', ({ expect }) => {
29
+ const obj: Record<string, unknown> = { x: 1 };
30
+ obj.self = obj;
31
+
32
+ const result = safeStringify(obj);
33
+ expect(result).to.include('"x": 1');
34
+ expect(result).to.include('[$.self => $]');
35
+ });
36
+
37
+ test('filters functions', ({ expect }) => {
38
+ const data = { a: 1, fn: () => {} };
39
+ const result = safeStringify(data);
40
+ expect(result).to.include('"a": 1');
41
+ expect(result).not.to.include('fn');
42
+ });
43
+
44
+ test('filters exotic objects', ({ expect }) => {
45
+ class Custom {}
46
+ const data = { a: 1, exotic: new Custom() };
47
+ const result = safeStringify(data);
48
+ expect(result).to.include('"a": 1');
49
+ expect(result).not.to.include('exotic');
50
+ });
51
+
52
+ test('preserves arrays', ({ expect }) => {
53
+ const data = { arr: [1, 2, 3] };
54
+ const result = safeStringify(data);
55
+ expect(result).to.include('"arr"');
56
+ expect(result).to.include('1');
57
+ expect(result).to.include('2');
58
+ expect(result).to.include('3');
59
+ });
60
+ });
61
+
62
+ describe('createReplacer', () => {
63
+ test('maxArrayLen truncates long arrays (Large story)', ({ expect }) => {
64
+ const data = { arr: Array.from({ length: 15 }, (_, index) => index) };
65
+ const result = safeStringify(data, createReplacer({ maxArrayLen: 10 }));
66
+ expect(result).to.include('"[length: 15]"');
67
+ });
68
+
69
+ test('maxStringLen truncates long strings (Large story)', ({ expect }) => {
70
+ const data = { str: 'a'.repeat(20) };
71
+ const result = safeStringify(data, createReplacer({ maxStringLen: 10 }));
72
+ expect(result).to.include('"aaaaaaaaaa..."');
73
+ });
74
+
75
+ test('omit skips specified keys', ({ expect }) => {
76
+ const data = { a: 1, secret: 'hide', b: 2 };
77
+ const result = safeStringify(data, createReplacer({ omit: ['secret'] }));
78
+ expect(result).to.include('"a": 1');
79
+ expect(result).to.include('"b": 2');
80
+ expect(result).not.to.include('secret');
81
+ });
82
+
83
+ test('parse parses JSON string values', ({ expect }) => {
84
+ const data = { nested: '{"x":1}' };
85
+ const result = safeStringify(data, createReplacer({ parse: ['nested'] }));
86
+ expect(result).to.include('"x": 1');
87
+ });
88
+
89
+ test('SKIP return value removes key from output', ({ expect }) => {
90
+ const customReplacer = (_key: string, value: unknown) => (_key === 'skip' ? SKIP : value);
91
+ const data = { a: 1, skip: 'hidden' };
92
+ const result = safeStringify(data, customReplacer);
93
+ expect(result).to.include('"a": 1');
94
+ expect(result).not.to.include('skip');
95
+ });
96
+ });
@@ -0,0 +1,153 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export const SKIP = Object.freeze({});
6
+
7
+ /**
8
+ * JSON.stringify replacer function.
9
+ */
10
+ export type StringifyReplacer = (key: string, value: any) => typeof SKIP | any;
11
+
12
+ /**
13
+ * Safely stringifies an object.
14
+ */
15
+ export function safeStringify(obj: any, filter: StringifyReplacer | undefined = defaultFilter, indent = 2) {
16
+ const seen = new WeakMap<object, string>();
17
+
18
+ // NOTE: Called for the root object with undefined key.
19
+ function replacer(this: any, key: string, value: any) {
20
+ try {
21
+ let path = key;
22
+ if (!key) {
23
+ path = '$';
24
+ if (value != null && typeof value === 'object') {
25
+ seen.set(value, path);
26
+ }
27
+ return value;
28
+ } else if (this) {
29
+ const parentPath = seen.get(this);
30
+ path = parentPath ? `${parentPath}.${key}` : key;
31
+ }
32
+
33
+ // Null or undefined.
34
+ if (value == null) {
35
+ return value;
36
+ }
37
+
38
+ // Ignore functions.
39
+ if (typeof value === 'function') {
40
+ return undefined;
41
+ }
42
+
43
+ // Ignore exotic objects (non-plain objects like DOM elements, class instances, etc.)
44
+ if (typeof value === 'object' && Object.getPrototypeOf(value) !== Object.prototype && !Array.isArray(value)) {
45
+ return undefined;
46
+ }
47
+
48
+ // Check cycles.
49
+ if (typeof value === 'object' && value !== null) {
50
+ const exists = seen.get(value);
51
+ if (exists) {
52
+ return `[${path} => ${exists}]`;
53
+ }
54
+
55
+ seen.set(value, path);
56
+ }
57
+
58
+ if (filter) {
59
+ const filteredValue = filter?.(key, value);
60
+ if (filteredValue !== undefined) {
61
+ return filteredValue === SKIP ? undefined : filteredValue;
62
+ }
63
+ }
64
+
65
+ return value;
66
+ } catch (error: any) {
67
+ return `ERROR: ${error.message}`;
68
+ }
69
+ }
70
+
71
+ try {
72
+ return JSON.stringify(obj, replacer, indent);
73
+ } catch (error: any) {
74
+ return `ERROR: ${error.message}`;
75
+ }
76
+ }
77
+
78
+ export type CreateReplacerProps = {
79
+ omit?: string[];
80
+ parse?: string[]; // TODO(burdon): Parse JSON value.
81
+ maxDepth?: number;
82
+ maxArrayLen?: number;
83
+ maxStringLen?: number;
84
+ };
85
+
86
+ /**
87
+ * Construct JSON.stringify replacer.
88
+ */
89
+ // TODO(burdon): Change to composite effect.
90
+ export const createReplacer = ({
91
+ omit,
92
+ parse,
93
+ maxDepth,
94
+ maxArrayLen,
95
+ maxStringLen,
96
+ }: CreateReplacerProps = {}): StringifyReplacer => {
97
+ let currentDepth = 0;
98
+ const depthMap = new WeakMap<object, number>();
99
+
100
+ return function (this: any, key: string, value: any) {
101
+ // Track depth.
102
+ if (key === '') {
103
+ // Root.
104
+ currentDepth = 0;
105
+ } else if (this && typeof this === 'object') {
106
+ const parentDepth = depthMap.get(this) ?? 0;
107
+ currentDepth = parentDepth + 1;
108
+ }
109
+
110
+ // Skip functions.
111
+ if (typeof value === 'function') {
112
+ return SKIP;
113
+ }
114
+
115
+ // Show array length.
116
+ if (maxArrayLen != null && Array.isArray(value) && value.length > maxArrayLen) {
117
+ return `[length: ${value.length}]`;
118
+ }
119
+
120
+ // Store depth for this object.
121
+ if (value && typeof value === 'object') {
122
+ depthMap.set(value, currentDepth);
123
+
124
+ // Check max depth.
125
+ if (maxDepth != null && currentDepth >= maxDepth) {
126
+ return Array.isArray(value) ? `[{ length: ${value.length} }]` : `{ keys: ${Object.keys(value).length} }`;
127
+ }
128
+ }
129
+
130
+ // Apply other filters.
131
+ if (omit?.includes(key)) {
132
+ return SKIP;
133
+ }
134
+
135
+ // Parse JSON values.
136
+ if (parse?.includes(key) && typeof value === 'string') {
137
+ try {
138
+ return JSON.parse(value);
139
+ } catch {
140
+ return value;
141
+ }
142
+ }
143
+
144
+ // Truncate strings.
145
+ if (maxStringLen != null && typeof value === 'string' && value.length > maxStringLen) {
146
+ return value.slice(0, maxStringLen) + '...';
147
+ }
148
+
149
+ return value;
150
+ };
151
+ };
152
+
153
+ export const defaultFilter: StringifyReplacer = createReplacer();
package/src/sort.test.ts CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { describe, expect, test } from 'vitest';
6
6
 
7
- import { compareMulti, compareScalar, compareObject, compareString } from './sort';
7
+ import { compareMulti, compareObject, compareScalar, compareString } from './sort';
8
8
 
9
9
  const data = [
10
10
  { i: 0, idx: 1, label: 'apple' },
@@ -0,0 +1,19 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { describe, test } from 'vitest';
6
+
7
+ import { trim } from './string';
8
+
9
+ describe('string', () => {
10
+ test('dedent', async ({ expect }) => {
11
+ const text = trim`
12
+ - 1
13
+ - 2
14
+ - 3
15
+ `;
16
+
17
+ expect(text).to.eq('- 1\n- 2\n - 3');
18
+ });
19
+ });
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());
@@ -4,166 +4,55 @@
4
4
 
5
5
  import { type PublicKey } from '@dxos/keys';
6
6
 
7
+ /**
8
+ * When changing this set, please check the result in a console or e.g. RunKit (https://runkit.com/thure/642214441dd6ae000855a8de)
9
+ * Emoji sometimes use a combination of code points, and some code points aren't visible on their own, so by adding or deleting you may unintentionally create non-visible items.
10
+ * This set was chosen from the characters in Unicode Emoji v15.0 based on the following criteria:
11
+ * โ€“ not people or isolated anthropomorphic faces
12
+ * โ€“ not flags
13
+ * โ€“ more concrete than abstract
14
+ * โ€“ less culturally specific
15
+ * โ€“ less easily confused with another emoji in the set
16
+ * โ€“ requires less special knowledge to identify
17
+ * โ€“ less likely to evoke negative feelings (no meat, no drugs, no weapons, etc)
18
+ * โ€“ less common as a signifier in UX
19
+ * NOTE that this is intentionally an array of strings because of the way emoji graphemes work.
20
+ */
21
+ // prettier-ignore
7
22
  export const idEmoji = [
8
- // When changing this set, please check the result in a console or e.g. RunKit (https://runkit.com/thure/642214441dd6ae000855a8de)
9
- // Emoji sometimes use a combination of code points, and some code points aren't visible on their own, so by adding or deleting you may unintentionally create non-visible items.
10
- // This set was chosen from the characters in Unicode Emoji v15.0 based on the following criteria:
11
- // โ€“ not people or isolated anthropomorphic faces
12
- // โ€“ not flags
13
- // โ€“ more concrete than abstract
14
- // โ€“ less culturally specific
15
- // โ€“ less easily confused with another emoji in the set
16
- // โ€“ requires less special knowledge to identify
17
- // โ€“ less likely to evoke negative feelings (no meat, no drugs, no weapons, etc)
18
- // โ€“ less common as a signifier in UX
19
- // NOTE that this is intentionally an array of strings because of the way emoji graphemes work.
20
- '๐Ÿ‘ป',
21
- '๐Ÿ‘น',
22
- '๐Ÿ‘ฝ',
23
- '๐Ÿค–',
24
- '๐ŸŽƒ',
25
- '๐Ÿฆพ',
26
- '๐Ÿฆฟ',
27
- '๐Ÿฆท',
28
- '๐Ÿ‘ฃ',
29
- '๐Ÿ‘๏ธ',
30
- '๐Ÿงถ',
31
- '๐Ÿ‘‘',
32
- '๐Ÿ’',
33
- '๐Ÿฆ†',
34
- '๐Ÿฆ‰',
35
- '๐Ÿด',
36
- '๐Ÿฆ„',
37
- '๐Ÿ',
38
- '๐Ÿฆ‹',
39
- '๐Ÿž',
40
- '๐Ÿชฒ',
41
- '๐Ÿข',
42
- '๐ŸฆŽ',
43
- '๐Ÿฆ•',
44
- '๐Ÿฆ‘',
45
- '๐Ÿฆ€',
46
- '๐Ÿ ',
47
- '๐Ÿฌ',
48
- '๐Ÿ‹',
49
- '๐Ÿฆญ',
50
- '๐Ÿ…',
51
- '๐Ÿ†',
52
- '๐Ÿฆ“',
53
- '๐Ÿฆ',
54
- '๐Ÿฆง',
55
- '๐Ÿ˜',
56
- '๐Ÿซ',
57
- '๐Ÿฆ’',
58
- '๐Ÿฆ˜',
59
- '๐Ÿฆฌ',
60
- '๐Ÿ–',
61
- '๐Ÿ',
62
- '๐ŸฆŒ',
63
- '๐Ÿ•',
64
- '๐Ÿˆ',
65
- '๐Ÿ“',
66
- '๐Ÿฆš',
67
- '๐Ÿฆœ',
68
- '๐Ÿฆข',
69
- '๐Ÿฆฉ',
70
- '๐Ÿฆฆ',
71
- '๐Ÿ',
72
- '๐Ÿฟ๏ธ',
73
- '๐ŸŒต',
74
- '๐ŸŒฒ',
75
- '๐ŸŒณ',
76
- '๐Ÿชต',
77
- '๐ŸŒฑ',
78
- '๐Ÿ',
79
- '๐Ÿชบ',
80
- '๐Ÿ„',
81
- '๐Ÿš',
82
- '๐Ÿชธ',
83
- '๐Ÿชจ',
84
- '๐ŸŒพ',
85
- '๐ŸŒท',
86
- '๐ŸŒป',
87
- 'โ˜€๏ธ',
88
- '๐ŸŒ™',
89
- '๐Ÿช',
90
- 'โญ๏ธ',
91
- 'โšก๏ธ',
92
- 'โ˜„๏ธ',
93
- '๐Ÿ”ฅ',
94
- '๐ŸŒˆ',
95
- 'โ˜๏ธ',
96
- '๐Ÿ’ง',
97
- 'โ›ฑ๏ธ',
98
- '๐ŸŒŠ',
99
- '๐ŸŽ',
100
- '๐Ÿ‹',
101
- '๐Ÿ‰',
102
- '๐Ÿ‡',
103
- '๐Ÿซ',
104
- '๐Ÿˆ',
105
- '๐Ÿ’',
106
- '๐Ÿ‘',
107
- '๐Ÿฅญ',
108
- '๐Ÿ',
109
- '๐Ÿฅฅ',
110
- '๐Ÿฅ',
111
- '๐Ÿฅ‘',
112
- '๐ŸŒถ๏ธ',
113
- '๐ŸŒฝ',
114
- '๐Ÿฅ•',
115
- '๐Ÿฌ',
116
- '๐Ÿฅœ',
117
- '๐Ÿซ–',
118
- 'โ˜•๏ธ',
119
- '๐Ÿต',
120
- '๐ŸงŠ',
121
- '๐Ÿง‚',
122
- '๐Ÿ”๏ธ',
123
- 'โš“๏ธ',
124
- '๐Ÿ›Ÿ',
125
- '๐Ÿ๏ธ',
126
- '๐Ÿ›ถ',
127
- '๐Ÿš€',
128
- '๐Ÿ›ฐ๏ธ',
129
- 'โ›ฒ๏ธ',
130
- '๐Ÿฐ',
131
- '๐Ÿšฒ',
132
- 'โ›บ๏ธ',
133
- '๐ŸŽ™๏ธ',
134
- '๐Ÿงฒ',
135
- 'โš™๏ธ',
136
- '๐Ÿ”ฉ',
137
- '๐Ÿ”ฎ',
138
- '๐Ÿ”ญ',
139
- '๐Ÿ”ฌ',
140
- '๐Ÿงฌ',
141
- '๐ŸŒก๏ธ',
142
- '๐Ÿงบ',
143
- '๐Ÿ›Ž๏ธ',
144
- '๐Ÿ”‘',
145
- '๐Ÿช‘',
146
- '๐Ÿงธ',
147
- '๐ŸŽˆ',
148
- '๐ŸŽ€',
149
- '๐ŸŽŠ',
150
- 'โ™ป๏ธ',
151
- '๐ŸŽต',
23
+ '๐Ÿ‘ป', '๐Ÿ‘น', '๐Ÿ‘ฝ', '๐Ÿค–', '๐ŸŽƒ', '๐Ÿฆพ', '๐Ÿฆฟ', '๐Ÿฆท',
24
+ '๐Ÿ‘ฃ', '๐Ÿ‘๏ธ', '๐Ÿงถ', '๐Ÿ‘‘', '๐Ÿ’', '๐Ÿฆ†', '๐Ÿฆ‰', '๐Ÿด',
25
+ '๐Ÿฆ„', '๐Ÿ', '๐Ÿฆ‹', '๐Ÿž', '๐Ÿชฒ', '๐Ÿข', '๐ŸฆŽ', '๐Ÿฆ•',
26
+ '๐Ÿฆ‘', '๐Ÿฆ€', '๐Ÿ ', '๐Ÿฌ', '๐Ÿ‹', '๐Ÿฆญ', '๐Ÿ…', '๐Ÿ†',
27
+ '๐Ÿฆ“', '๐Ÿฆ', '๐Ÿฆง', '๐Ÿ˜', '๐Ÿซ', '๐Ÿฆ’', '๐Ÿฆ˜', '๐Ÿฆฌ',
28
+ '๐Ÿ–', '๐Ÿ', '๐ŸฆŒ', '๐Ÿ•', '๐Ÿˆ', '๐Ÿ“', '๐Ÿฆš', '๐Ÿฆœ',
29
+ '๐Ÿฆข', '๐Ÿฆฉ', '๐Ÿฆฆ', '๐Ÿ', '๐Ÿฟ๏ธ', '๐ŸŒต', '๐ŸŒฒ', '๐ŸŒณ',
30
+ '๐Ÿชต', '๐ŸŒฑ', '๐Ÿ', '๐Ÿชบ', '๐Ÿ„', '๐Ÿš', '๐Ÿชธ', '๐Ÿชจ',
31
+ '๐ŸŒพ', '๐ŸŒท', '๐ŸŒป', 'โ˜€๏ธ', '๐ŸŒ™', '๐Ÿช', 'โญ๏ธ', 'โšก๏ธ',
32
+ 'โ˜„๏ธ', '๐Ÿ”ฅ', '๐ŸŒˆ', 'โ˜๏ธ', '๐Ÿ’ง', 'โ›ฑ๏ธ', '๐ŸŒŠ', '๐ŸŽ',
33
+ '๐Ÿ‹', '๐Ÿ‰', '๐Ÿ‡', '๐Ÿซ', '๐Ÿˆ', '๐Ÿ’', '๐Ÿ‘', '๐Ÿฅญ',
34
+ '๐Ÿ', '๐Ÿฅฅ', '๐Ÿฅ', '๐Ÿฅ‘', '๐ŸŒถ๏ธ', '๐ŸŒฝ', '๐Ÿฅ•', '๐Ÿฌ',
35
+ '๐Ÿฅœ', '๐Ÿซ–', 'โ˜•๏ธ', '๐Ÿต', '๐ŸงŠ', '๐Ÿง‚', '๐Ÿ”๏ธ', 'โš“๏ธ',
36
+ '๐Ÿ›Ÿ', '๐Ÿ๏ธ', '๐Ÿ›ถ', '๐Ÿš€', '๐Ÿ›ฐ๏ธ', 'โ›ฒ๏ธ', '๐Ÿฐ', '๐Ÿšฒ',
37
+ 'โ›บ๏ธ', '๐ŸŽ™๏ธ', '๐Ÿงฒ', 'โš™๏ธ', '๐Ÿ”ฉ', '๐Ÿ”ฎ', '๐Ÿ”ญ', '๐Ÿ”ฌ',
38
+ '๐Ÿงฌ', '๐ŸŒก๏ธ', '๐Ÿงบ', '๐Ÿ›Ž๏ธ', '๐Ÿ”‘', '๐Ÿช‘', '๐Ÿงธ', '๐ŸŽˆ',
39
+ '๐ŸŽ€', '๐ŸŽŠ', 'โ™ป๏ธ', '๐ŸŽต',
152
40
  ];
153
41
 
154
42
  export const idHue = [
155
43
  'red' as const,
156
- // 'orange' as const, /* More shades in these palettes are considered โ€œuglyโ€ */
157
- 'amber' as const, // Amber arcs between red-orange and yellow as it gets lighter, so improves aesthetics.
158
- // 'yellow' as const, /* More shades in these palettes are considered โ€œuglyโ€ */
44
+ 'orange' as const,
45
+ 'amber' as const,
46
+ 'yellow' as const,
159
47
  'lime' as const,
160
48
  'green' as const,
161
49
  'emerald' as const,
162
50
  'teal' as const,
163
51
  'cyan' as const,
164
- // 'sky' as const, /* Omitted since it is quite similar to the primary accent palette */
165
- // 'blue' as const, /* Omitted since it is quite similar to the primary accent palette */
166
- // 'indigo' as const, /* Omitted since it is quite similar to the primary accent palette */
52
+ // Omit colors similar to primary accent.
53
+ // 'sky' as const,
54
+ // 'blue' as const,
55
+ // 'indigo' as const,
167
56
  'violet' as const,
168
57
  'purple' as const,
169
58
  'fuchsia' as const,
@@ -171,17 +60,11 @@ export const idHue = [
171
60
  'rose' as const,
172
61
  ];
173
62
 
174
- // TODO(wittjosiah): Do we need all of these variants?
175
63
  export const keyToEmoji = (key: PublicKey) => keyToFallback(key).emoji;
176
-
177
64
  export const hexToEmoji = (hex: string) => hexToFallback(hex).emoji;
178
-
179
65
  export const toEmoji = (hash: number) => toFallback(hash).emoji;
180
-
181
66
  export const keyToHue = (key: PublicKey) => keyToFallback(key).hue;
182
-
183
67
  export const hexToHue = (hex: string) => hexToFallback(hex).hue;
184
-
185
68
  export const toHue = (hash: number) => toFallback(hash).hue;
186
69
 
187
70
  export type FallbackValue = {
@@ -194,17 +77,18 @@ export const keyToFallback = (key: PublicKey) => hexToFallback(key.toHex());
194
77
  // TODO(wittjosiah): Support non-hex strings (e.g. DIDs, UUIDs, etc.)
195
78
  export const hexToFallback = (hex: string) => toFallback(parseInt(hex, 16));
196
79
 
80
+ // TODO(burdon): Rename?
197
81
  export const toFallback = (hash: number): FallbackValue => {
198
- // Calculate total possible combinations of emoji and hue pairs
82
+ // Calculate total possible combinations of emoji and hue pairs.
199
83
  const totalCombinations = idEmoji.length * idHue.length;
200
84
 
201
- // Get a deterministic index within the range of all possible combinations
85
+ // Get a deterministic index within the range of all possible combinations.
202
86
  const combinationIndex = hash % totalCombinations;
203
87
 
204
- // Calculate which emoji to use based on the combination index
88
+ // Calculate which emoji to use based on the combination index.
205
89
  const emojiIndex = Math.floor(combinationIndex / idHue.length);
206
90
 
207
- // Calculate which hue to use based on the combination index
91
+ // Calculate which hue to use based on the combination index.
208
92
  const hueIndex = combinationIndex % idHue.length;
209
93
 
210
94
  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
  });