@dxos/util 0.8.4-main.b97322e → 0.8.4-main.bd9b33e6c8
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/dist/lib/browser/index.mjs +425 -140
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +425 -140
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +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/circular-buffer.d.ts +1 -0
- package/dist/types/src/circular-buffer.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.d.ts +1 -1
- package/dist/types/src/defer.d.ts.map +1 -1
- package/dist/types/src/di-key.d.ts +3 -3
- package/dist/types/src/di-key.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/index.d.ts +6 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/platform.d.ts +4 -1
- package/dist/types/src/platform.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-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/string.d.ts +5 -1
- package/dist/types/src/string.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/types.d.ts +24 -6
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/unit.d.ts +14 -0
- package/dist/types/src/unit.d.ts.map +1 -0
- package/dist/types/src/unit.test.d.ts +2 -0
- package/dist/types/src/unit.test.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +14 -13
- package/src/array.test.ts +1 -1
- package/src/array.ts +9 -2
- package/src/binder.ts +2 -2
- package/src/circular-buffer.test.ts +27 -1
- package/src/circular-buffer.ts +5 -0
- package/src/complex.test.ts +1 -1
- 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/error-format.ts +22 -0
- package/src/filename.ts +16 -0
- package/src/human-hash.test.ts +1 -1
- package/src/index.ts +6 -1
- package/src/platform.ts +35 -3
- package/src/position.test.ts +2 -2
- package/src/retry.ts +74 -0
- package/src/safe-parse.ts +29 -17
- package/src/safe-stringify.test.ts +96 -0
- package/src/safe-stringify.ts +153 -0
- package/src/sort.test.ts +1 -1
- package/src/string.test.ts +1 -1
- package/src/string.ts +32 -6
- package/src/to-fallback.ts +44 -160
- package/src/tree.test.ts +1 -1
- package/src/types.test.ts +11 -1
- package/src/types.ts +40 -12
- package/src/uint8array.test.ts +1 -1
- package/src/unit.test.ts +25 -0
- package/src/unit.ts +83 -0
- package/src/weak.test.ts +1 -1
- 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/dist/types/src/first-two-chars.d.ts +0 -9
- package/dist/types/src/first-two-chars.d.ts.map +0 -1
- package/src/explicit-resource-management-polyfill.ts +0 -13
- package/src/first-two-chars.ts +0 -44
|
@@ -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,
|
|
7
|
+
import { compareMulti, compareObject, compareScalar, compareString } from './sort';
|
|
8
8
|
|
|
9
9
|
const data = [
|
|
10
10
|
{ i: 0, idx: 1, label: 'apple' },
|
package/src/string.test.ts
CHANGED
package/src/string.ts
CHANGED
|
@@ -16,9 +16,35 @@ export const capitalize = (str: string): string => {
|
|
|
16
16
|
/**
|
|
17
17
|
* Remove leading space from multi-line strings.
|
|
18
18
|
*/
|
|
19
|
-
export
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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());
|
package/src/to-fallback.ts
CHANGED
|
@@ -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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
157
|
-
'amber' as const,
|
|
158
|
-
|
|
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
|
-
//
|
|
165
|
-
// '
|
|
166
|
-
// '
|
|
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 {
|
|
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
|
@@ -10,15 +10,33 @@ export type MaybeProvider<T, V = void> = T | ((arg: V) => T);
|
|
|
10
10
|
|
|
11
11
|
export type MaybePromise<T> = T | Promise<T>;
|
|
12
12
|
|
|
13
|
-
export type GuardedType<T> = T extends (value: any) => value is infer R ? R : never;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
13
|
+
export type GuardedType<T> = T extends ((value: any) => value is infer R) ? R : never;
|
|
14
|
+
|
|
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
|
|
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
|
-
*
|
|
125
|
+
* Move element within array.
|
|
108
126
|
*/
|
|
109
|
-
export const arrayMove = <T>(array: T[], from: number, to: number):
|
|
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
|
+
}
|
package/src/uint8array.test.ts
CHANGED
package/src/unit.test.ts
ADDED
|
@@ -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
|
+
});
|