@dxos/util 0.8.4-main.ef1bc66f44 → 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 +281 -180
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +281 -180
- 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.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.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.map +1 -1
- package/dist/types/src/filename.d.ts.map +1 -1
- 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 +5 -2
- 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 +1 -1
- 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.map +1 -1
- 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 +1 -0
- package/dist/types/src/safe-parse.d.ts.map +1 -1
- package/dist/types/src/safe-stringify.d.ts +1 -1
- package/dist/types/src/safe-stringify.d.ts.map +1 -1
- 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 +1 -1
- 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.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 +7 -14
- 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 +0 -6
- package/src/di-key.ts +10 -8
- package/src/error-format.ts +3 -1
- package/src/id.test.ts +109 -0
- package/src/id.ts +48 -0
- package/src/index.ts +5 -2
- package/src/platform.ts +27 -5
- package/src/position.test.ts +38 -44
- package/src/position.ts +7 -11
- package/src/safe-parse.ts +8 -0
- package/src/safe-stringify.test.ts +96 -0
- package/src/safe-stringify.ts +15 -8
- package/src/string.ts +6 -2
- package/src/to-fallback.ts +39 -156
- package/src/types.ts +1 -1
- package/src/unit.ts +6 -2
- package/src/weak.ts +52 -14
package/src/position.test.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { describe,
|
|
5
|
+
import { describe, test } from 'vitest';
|
|
6
6
|
|
|
7
7
|
import { type Position, byPosition } from './position';
|
|
8
8
|
|
|
@@ -12,14 +12,14 @@ type TestItem = {
|
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
describe('byPosition', () => {
|
|
15
|
-
|
|
15
|
+
test('should keep items with same position in their original order', ({ expect }) => {
|
|
16
16
|
const items: TestItem[] = [
|
|
17
|
-
{ id: 1
|
|
18
|
-
{ id: 2
|
|
19
|
-
{ id: 3, position: '
|
|
20
|
-
{ id: 4, position: '
|
|
21
|
-
{ id: 5, position: '
|
|
22
|
-
{ id: 6, position: '
|
|
17
|
+
{ id: 1 },
|
|
18
|
+
{ id: 2 },
|
|
19
|
+
{ id: 3, position: 'first' },
|
|
20
|
+
{ id: 4, position: 'first' },
|
|
21
|
+
{ id: 5, position: 'last' },
|
|
22
|
+
{ id: 6, position: 'last' },
|
|
23
23
|
];
|
|
24
24
|
|
|
25
25
|
const sorted = [...items].sort(byPosition);
|
|
@@ -30,69 +30,63 @@ describe('byPosition', () => {
|
|
|
30
30
|
expect(sorted.findIndex((item) => item.id === 5)).toBeLessThan(sorted.findIndex((item) => item.id === 6));
|
|
31
31
|
});
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
const items: TestItem[] = [
|
|
35
|
-
{ id: 1, position: 'static' },
|
|
36
|
-
{ id: 2, position: 'hoist' },
|
|
37
|
-
];
|
|
33
|
+
test('should place "first" items before items in natural order', ({ expect }) => {
|
|
34
|
+
const items: TestItem[] = [{ id: 1 }, { id: 2, position: 'first' }];
|
|
38
35
|
|
|
39
36
|
const sorted = [...items].sort(byPosition);
|
|
40
|
-
expect(sorted[0].position).toBe('
|
|
41
|
-
expect(sorted[1].position).
|
|
37
|
+
expect(sorted[0].position).toBe('first');
|
|
38
|
+
expect(sorted[1].position).toBeUndefined();
|
|
42
39
|
});
|
|
43
40
|
|
|
44
|
-
|
|
45
|
-
const items: TestItem[] = [
|
|
46
|
-
{ id: 1, position: 'fallback' },
|
|
47
|
-
{ id: 2, position: 'static' },
|
|
48
|
-
];
|
|
41
|
+
test('should place "last" items after items in natural order', ({ expect }) => {
|
|
42
|
+
const items: TestItem[] = [{ id: 1, position: 'last' }, { id: 2 }];
|
|
49
43
|
|
|
50
44
|
const sorted = [...items].sort(byPosition);
|
|
51
|
-
expect(sorted[0].position).
|
|
52
|
-
expect(sorted[1].position).toBe('
|
|
45
|
+
expect(sorted[0].position).toBeUndefined();
|
|
46
|
+
expect(sorted[1].position).toBe('last');
|
|
53
47
|
});
|
|
54
48
|
|
|
55
|
-
|
|
56
|
-
const items: TestItem[] = [{ id: 1 }, { id: 2, position: '
|
|
49
|
+
test('should treat items without position as natural order', ({ expect }) => {
|
|
50
|
+
const items: TestItem[] = [{ id: 1 }, { id: 2, position: 'first' }, { id: 3, position: 'last' }];
|
|
57
51
|
|
|
58
52
|
const sorted = [...items].sort(byPosition);
|
|
59
|
-
expect(sorted[0].position).toBe('
|
|
53
|
+
expect(sorted[0].position).toBe('first');
|
|
60
54
|
expect(sorted[1].position).toBeUndefined();
|
|
61
|
-
expect(sorted[2].position).toBe('
|
|
55
|
+
expect(sorted[2].position).toBe('last');
|
|
62
56
|
});
|
|
63
57
|
|
|
64
|
-
|
|
58
|
+
test('should correctly sort mixed positions', ({ expect }) => {
|
|
65
59
|
const items: TestItem[] = [
|
|
66
|
-
{ id: 1, position: '
|
|
67
|
-
{ id: 2
|
|
68
|
-
{ id: 3, position: '
|
|
69
|
-
{ id: 4 },
|
|
70
|
-
{ id: 5, position: '
|
|
71
|
-
{ id: 6, position: '
|
|
60
|
+
{ id: 1, position: 'last' },
|
|
61
|
+
{ id: 2 },
|
|
62
|
+
{ id: 3, position: 'first' },
|
|
63
|
+
{ id: 4 },
|
|
64
|
+
{ id: 5, position: 'first' },
|
|
65
|
+
{ id: 6, position: 'last' },
|
|
72
66
|
];
|
|
73
67
|
|
|
74
68
|
const sorted = [...items].sort(byPosition);
|
|
75
69
|
|
|
76
|
-
// All
|
|
77
|
-
expect(sorted[0].position).toBe('
|
|
78
|
-
expect(sorted[1].position).toBe('
|
|
70
|
+
// All "first" items should come first
|
|
71
|
+
expect(sorted[0].position).toBe('first');
|
|
72
|
+
expect(sorted[1].position).toBe('first');
|
|
79
73
|
|
|
80
|
-
//
|
|
81
|
-
expect(sorted[2].position).
|
|
74
|
+
// Natural-order items (undefined) should be in the middle
|
|
75
|
+
expect(sorted[2].position).toBeUndefined();
|
|
82
76
|
expect(sorted[3].position).toBeUndefined();
|
|
83
77
|
|
|
84
|
-
//
|
|
85
|
-
expect(sorted[4].position).toBe('
|
|
86
|
-
expect(sorted[5].position).toBe('
|
|
78
|
+
// "last" items should be at the end
|
|
79
|
+
expect(sorted[4].position).toBe('last');
|
|
80
|
+
expect(sorted[5].position).toBe('last');
|
|
87
81
|
});
|
|
88
82
|
|
|
89
|
-
|
|
83
|
+
test('should handle empty arrays', ({ expect }) => {
|
|
90
84
|
const items: TestItem[] = [];
|
|
91
85
|
expect(() => [...items].sort(byPosition)).not.toThrow();
|
|
92
86
|
});
|
|
93
87
|
|
|
94
|
-
|
|
95
|
-
const items: TestItem[] = [{ id: 1
|
|
88
|
+
test('should handle single item arrays', ({ expect }) => {
|
|
89
|
+
const items: TestItem[] = [{ id: 1 }];
|
|
96
90
|
expect(() => [...items].sort(byPosition)).not.toThrow();
|
|
97
91
|
});
|
|
98
92
|
});
|
package/src/position.ts
CHANGED
|
@@ -6,25 +6,21 @@
|
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Determines priority order:
|
|
9
|
-
* - `
|
|
10
|
-
* - `
|
|
11
|
-
* - `
|
|
9
|
+
* - `undefined` - Remain in natural order.
|
|
10
|
+
* - `first` - Placed before items in natural order.
|
|
11
|
+
* - `last` - Placed after items in natural order.
|
|
12
12
|
*/
|
|
13
|
-
|
|
14
|
-
export type Position = 'static' | 'hoist' | 'fallback';
|
|
13
|
+
export type Position = 'first' | 'last';
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* Sorting function for sorting by position.
|
|
18
17
|
*/
|
|
19
|
-
export const byPosition = <T extends { position?: Position }>(
|
|
20
|
-
{ position: a = 'static' }: T,
|
|
21
|
-
{ position: b = 'static' }: T,
|
|
22
|
-
) => {
|
|
18
|
+
export const byPosition = <T extends { position?: Position }>({ position: a }: T, { position: b }: T) => {
|
|
23
19
|
if (a === b) {
|
|
24
20
|
return 0;
|
|
25
|
-
} else if (a === '
|
|
21
|
+
} else if (a === 'first' || b === 'last') {
|
|
26
22
|
return -1;
|
|
27
|
-
} else if (b === '
|
|
23
|
+
} else if (b === 'first' || a === 'last') {
|
|
28
24
|
return 1;
|
|
29
25
|
} else {
|
|
30
26
|
return 0;
|
package/src/safe-parse.ts
CHANGED
|
@@ -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
|
+
});
|
package/src/safe-stringify.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type StringifyReplacer = (key: string, value: any) => typeof SKIP | any;
|
|
|
12
12
|
/**
|
|
13
13
|
* Safely stringifies an object.
|
|
14
14
|
*/
|
|
15
|
-
export function safeStringify(obj: any, filter: StringifyReplacer = defaultFilter, indent = 2) {
|
|
15
|
+
export function safeStringify(obj: any, filter: StringifyReplacer | undefined = defaultFilter, indent = 2) {
|
|
16
16
|
const seen = new WeakMap<object, string>();
|
|
17
17
|
|
|
18
18
|
// NOTE: Called for the root object with undefined key.
|
|
@@ -21,6 +21,9 @@ export function safeStringify(obj: any, filter: StringifyReplacer = defaultFilte
|
|
|
21
21
|
let path = key;
|
|
22
22
|
if (!key) {
|
|
23
23
|
path = '$';
|
|
24
|
+
if (value != null && typeof value === 'object') {
|
|
25
|
+
seen.set(value, path);
|
|
26
|
+
}
|
|
24
27
|
return value;
|
|
25
28
|
} else if (this) {
|
|
26
29
|
const parentPath = seen.get(this);
|
|
@@ -38,7 +41,7 @@ export function safeStringify(obj: any, filter: StringifyReplacer = defaultFilte
|
|
|
38
41
|
}
|
|
39
42
|
|
|
40
43
|
// Ignore exotic objects (non-plain objects like DOM elements, class instances, etc.)
|
|
41
|
-
if (typeof value === 'object' && Object.getPrototypeOf(value) !== Object.prototype) {
|
|
44
|
+
if (typeof value === 'object' && Object.getPrototypeOf(value) !== Object.prototype && !Array.isArray(value)) {
|
|
42
45
|
return undefined;
|
|
43
46
|
}
|
|
44
47
|
|
|
@@ -65,7 +68,11 @@ export function safeStringify(obj: any, filter: StringifyReplacer = defaultFilte
|
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
70
|
|
|
68
|
-
|
|
71
|
+
try {
|
|
72
|
+
return JSON.stringify(obj, replacer, indent);
|
|
73
|
+
} catch (error: any) {
|
|
74
|
+
return `ERROR: ${error.message}`;
|
|
75
|
+
}
|
|
69
76
|
}
|
|
70
77
|
|
|
71
78
|
export type CreateReplacerProps = {
|
|
@@ -105,6 +112,11 @@ export const createReplacer = ({
|
|
|
105
112
|
return SKIP;
|
|
106
113
|
}
|
|
107
114
|
|
|
115
|
+
// Show array length.
|
|
116
|
+
if (maxArrayLen != null && Array.isArray(value) && value.length > maxArrayLen) {
|
|
117
|
+
return `[length: ${value.length}]`;
|
|
118
|
+
}
|
|
119
|
+
|
|
108
120
|
// Store depth for this object.
|
|
109
121
|
if (value && typeof value === 'object') {
|
|
110
122
|
depthMap.set(value, currentDepth);
|
|
@@ -129,11 +141,6 @@ export const createReplacer = ({
|
|
|
129
141
|
}
|
|
130
142
|
}
|
|
131
143
|
|
|
132
|
-
// Show array length.
|
|
133
|
-
if (maxArrayLen != null && Array.isArray(value) && value.length > maxArrayLen) {
|
|
134
|
-
return `[length: ${value.length}]`;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
144
|
// Truncate strings.
|
|
138
145
|
if (maxStringLen != null && typeof value === 'string' && value.length > maxStringLen) {
|
|
139
146
|
return value.slice(0, maxStringLen) + '...';
|
package/src/string.ts
CHANGED
|
@@ -32,8 +32,12 @@ export function trim(strings: TemplateStringsArray, ...values: any[]) {
|
|
|
32
32
|
// Split into lines and trim leading/trailing blank lines.
|
|
33
33
|
const lines = raw.split('\n');
|
|
34
34
|
|
|
35
|
-
while (lines.length && !lines[0].trim())
|
|
36
|
-
|
|
35
|
+
while (lines.length && !lines[0].trim()) {
|
|
36
|
+
lines.shift();
|
|
37
|
+
}
|
|
38
|
+
while (lines.length && !lines[lines.length - 1].trim()) {
|
|
39
|
+
lines.pop();
|
|
40
|
+
}
|
|
37
41
|
|
|
38
42
|
// Find smallest indent across all non-blank lines.
|
|
39
43
|
const minIndent = Math.min(...lines.filter((l) => l.trim()).map((l) => l.match(/^[ \t]*/)?.[0].length ?? 0));
|
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.ts
CHANGED
|
@@ -10,7 +10,7 @@ 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;
|
|
13
|
+
export type GuardedType<T> = T extends ((value: any) => value is infer R) ? R : never;
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Removes readonly modifiers from top-level properties of T.
|
package/src/unit.ts
CHANGED
|
@@ -20,11 +20,15 @@ export type UnitFormat<T = any> = (n: number, precision?: number) => UnitValue<T
|
|
|
20
20
|
const createFormat = (unit: Unit): UnitFormat<string> => {
|
|
21
21
|
return (n: number, precision = unit.precision ?? 0) => {
|
|
22
22
|
const value = n / unit.quotient;
|
|
23
|
+
const formattedValue = value.toLocaleString('en-US', {
|
|
24
|
+
minimumFractionDigits: precision,
|
|
25
|
+
maximumFractionDigits: precision,
|
|
26
|
+
});
|
|
23
27
|
return {
|
|
24
28
|
unit,
|
|
25
29
|
value,
|
|
26
|
-
formattedValue
|
|
27
|
-
toString: () => `${
|
|
30
|
+
formattedValue,
|
|
31
|
+
toString: () => `${formattedValue}${unit.symbol}`,
|
|
28
32
|
};
|
|
29
33
|
};
|
|
30
34
|
};
|