@dxos/util 0.8.4-main.ead640a → 0.8.4-main.f466a3d56e
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/LICENSE +102 -5
- package/dist/lib/browser/index.mjs +642 -524
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +642 -524
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/array-to-hex.d.ts.map +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/binder.d.ts.map +1 -1
- package/dist/types/src/bitfield.d.ts.map +1 -1
- package/dist/types/src/browser-storage.d.ts +9 -0
- package/dist/types/src/browser-storage.d.ts.map +1 -0
- package/dist/types/src/callback-collection.d.ts.map +1 -1
- package/dist/types/src/callback.d.ts.map +1 -1
- package/dist/types/src/case.d.ts.map +1 -1
- package/dist/types/src/chunk-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/clear-undefined.d.ts.map +1 -1
- package/dist/types/src/complex.d.ts +15 -0
- package/dist/types/src/complex.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-function.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-container.d.ts.map +1 -1
- package/dist/types/src/di-key.d.ts +7 -5
- package/dist/types/src/di-key.d.ts.map +1 -1
- package/dist/types/src/entry.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/for-each-async.d.ts.map +1 -1
- package/dist/types/src/human-hash.d.ts.map +1 -1
- package/dist/types/src/id.d.ts +30 -0
- package/dist/types/src/id.d.ts.map +1 -0
- package/dist/types/src/id.test.d.ts +2 -0
- package/dist/types/src/id.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +7 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/instance-id.d.ts.map +1 -1
- package/dist/types/src/interval.d.ts.map +1 -1
- package/dist/types/src/join-tables.d.ts.map +1 -1
- package/dist/types/src/json.d.ts.map +1 -1
- package/dist/types/src/map-values.d.ts.map +1 -1
- package/dist/types/src/map.d.ts.map +1 -1
- package/dist/types/src/object.d.ts.map +1 -1
- package/dist/types/src/order-keys.d.ts.map +1 -1
- package/dist/types/src/order.d.ts.map +1 -1
- package/dist/types/src/pick.d.ts.map +1 -1
- package/dist/types/src/platform.d.ts +5 -2
- package/dist/types/src/platform.d.ts.map +1 -1
- package/dist/types/src/position.d.ts +5 -5
- package/dist/types/src/position.d.ts.map +1 -1
- package/dist/types/src/random.d.ts.map +1 -1
- package/dist/types/src/range.d.ts.map +1 -1
- package/dist/types/src/reducers.d.ts.map +1 -1
- package/dist/types/src/remove-undefined-keys.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-await.d.ts.map +1 -1
- package/dist/types/src/safe-instanceof.d.ts.map +1 -1
- 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/sliding-window-summary.d.ts.map +1 -1
- package/dist/types/src/sort.d.ts.map +1 -1
- package/dist/types/src/string.d.ts.map +1 -1
- package/dist/types/src/sum.d.ts.map +1 -1
- package/dist/types/src/throw-unhandled-error.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/tracer.d.ts.map +1 -1
- package/dist/types/src/tree.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +23 -5
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/uint8array.d.ts.map +1 -1
- package/dist/types/src/unit.d.ts +12 -13
- package/dist/types/src/unit.d.ts.map +1 -1
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/src/weak.d.ts +15 -0
- package/dist/types/src/weak.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +13 -15
- package/src/array.ts +9 -2
- package/src/binder.ts +2 -2
- package/src/browser-storage.ts +60 -0
- package/src/circular-buffer.test.ts +26 -0
- package/src/circular-buffer.ts +5 -0
- package/src/complex.ts +32 -0
- 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/di-key.ts +10 -8
- package/src/error-format.ts +24 -0
- package/src/filename.ts +16 -0
- package/src/id.test.ts +109 -0
- package/src/id.ts +48 -0
- package/src/index.ts +7 -0
- package/src/platform.ts +35 -3
- package/src/position.test.ts +38 -44
- package/src/position.ts +7 -11
- package/src/retry.ts +74 -0
- package/src/safe-parse.ts +27 -16
- package/src/safe-stringify.test.ts +96 -0
- package/src/safe-stringify.ts +153 -0
- package/src/string.ts +6 -2
- package/src/to-fallback.ts +39 -156
- package/src/types.test.ts +11 -1
- package/src/types.ts +39 -11
- package/src/unit.test.ts +1 -1
- package/src/unit.ts +63 -28
- package/src/weak.ts +52 -14
- 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/src/explicit-resource-management-polyfill.ts +0 -13
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 = {
|
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
|
|
|
@@ -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/unit.test.ts
CHANGED
package/src/unit.ts
CHANGED
|
@@ -2,51 +2,86 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
type Unit = {
|
|
5
|
+
export type Unit = {
|
|
6
6
|
symbol: string;
|
|
7
7
|
quotient: number;
|
|
8
|
+
precision?: number;
|
|
8
9
|
};
|
|
9
10
|
|
|
10
|
-
type
|
|
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>;
|
|
11
19
|
|
|
12
|
-
const
|
|
13
|
-
return (n: number, precision =
|
|
20
|
+
const createFormat = (unit: Unit): UnitFormat<string> => {
|
|
21
|
+
return (n: number, precision = unit.precision ?? 0) => {
|
|
14
22
|
const value = n / unit.quotient;
|
|
15
|
-
|
|
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
|
+
};
|
|
16
33
|
};
|
|
17
34
|
};
|
|
18
35
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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 }),
|
|
25
56
|
Duration: (n: number) => {
|
|
26
|
-
const hours = Math.floor(n /
|
|
27
|
-
const minutes = Math.floor((n %
|
|
57
|
+
const hours = Math.floor(n / MS_HOURS);
|
|
58
|
+
const minutes = Math.floor((n % MS_HOURS) / MS_MINUTES);
|
|
28
59
|
if (hours) {
|
|
29
|
-
|
|
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
|
+
};
|
|
30
67
|
}
|
|
31
68
|
|
|
32
|
-
const seconds = Math.floor((n % (60 * 1_000)) / 1_000);
|
|
33
69
|
if (minutes) {
|
|
34
|
-
|
|
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
|
+
};
|
|
35
78
|
}
|
|
36
79
|
|
|
80
|
+
const seconds = n >= MS_SECONDS;
|
|
37
81
|
if (seconds) {
|
|
38
|
-
return
|
|
82
|
+
return Unit.Second(n);
|
|
39
83
|
}
|
|
40
84
|
|
|
41
|
-
return
|
|
85
|
+
return Unit.Millisecond(n);
|
|
42
86
|
},
|
|
43
|
-
|
|
44
|
-
// bytes (note KB via KiB).
|
|
45
|
-
Gigabyte: Formatter({ symbol: 'GB', quotient: 1_000 * 1_000 * 1_000 }),
|
|
46
|
-
Megabyte: Formatter({ symbol: 'MB', quotient: 1_000 * 1_000 }),
|
|
47
|
-
Kilobyte: Formatter({ symbol: 'KB', quotient: 1_000 }),
|
|
48
|
-
|
|
49
|
-
// general.
|
|
50
|
-
Thousand: Formatter({ symbol: 'k', quotient: 1_000 }),
|
|
51
|
-
Percent: Formatter({ symbol: '%', quotient: 1 / 100 }),
|
|
52
|
-
};
|
|
87
|
+
} as const;
|
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
|
|
11
|
-
|
|
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
|
-
|
|
16
|
-
|
|
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.
|
|
59
|
-
|
|
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 {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
//# sourceMappingURL=explicit-resource-management-polyfill.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"explicit-resource-management-polyfill.d.ts","sourceRoot":"","sources":["../../../src/explicit-resource-management-polyfill.ts"],"names":[],"mappings":""}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
|
|
6
|
-
|
|
7
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
8
|
-
// @ts-ignore
|
|
9
|
-
Symbol.dispose ??= Symbol('Symbol.dispose');
|
|
10
|
-
|
|
11
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
12
|
-
// @ts-ignore
|
|
13
|
-
Symbol.asyncDispose ??= Symbol('Symbol.asyncDispose');
|