@dxos/util 0.8.4-main.2e9d522 → 0.8.4-main.3c1ae3b
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 +313 -114
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +313 -114
- 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 +1 -0
- package/dist/types/src/array.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 +4 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/safe-parse.d.ts +6 -4
- package/dist/types/src/safe-parse.d.ts.map +1 -1
- package/dist/types/src/safe-stringify.d.ts +13 -0
- package/dist/types/src/safe-stringify.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.map +1 -1
- package/dist/types/src/types.d.ts +5 -1
- 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 +10 -9
- package/src/array.test.ts +1 -1
- package/src/array.ts +7 -0
- package/src/binder.ts +2 -2
- package/src/circular-buffer.test.ts +1 -1
- package/src/complex.test.ts +1 -1
- 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 +4 -1
- package/src/position.test.ts +2 -2
- package/src/safe-parse.ts +21 -17
- package/src/safe-stringify.ts +104 -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 +5 -4
- package/src/tree.test.ts +1 -1
- package/src/types.ts +9 -1
- 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
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/util",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.3c1ae3b",
|
|
4
4
|
"description": "Temporary bucket for misc functions, which should graduate into separate packages.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"author": "DXOS.org",
|
|
9
|
-
"sideEffects":
|
|
9
|
+
"sideEffects": false,
|
|
10
10
|
"type": "module",
|
|
11
11
|
"exports": {
|
|
12
12
|
".": {
|
|
13
|
+
"types": "./dist/types/src/index.d.ts",
|
|
13
14
|
"browser": "./dist/lib/browser/index.mjs",
|
|
14
15
|
"node": {
|
|
15
16
|
"require": "./dist/lib/node/index.cjs",
|
|
16
17
|
"default": "./dist/lib/node-esm/index.mjs"
|
|
17
|
-
}
|
|
18
|
-
"types": "./dist/types/src/index.d.ts"
|
|
18
|
+
}
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"types": "dist/types/src/index.d.ts",
|
|
@@ -27,17 +27,18 @@
|
|
|
27
27
|
"src"
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
|
+
"@hazae41/symbol-dispose-polyfill": "^1.0.2",
|
|
30
31
|
"lodash.get": "^4.4.2",
|
|
31
32
|
"lodash.set": "^4.3.2",
|
|
32
|
-
"@dxos/debug": "0.8.4-main.
|
|
33
|
-
"@dxos/invariant": "0.8.4-main.
|
|
34
|
-
"@dxos/keys": "0.8.4-main.
|
|
35
|
-
"@dxos/node-std": "0.8.4-main.
|
|
33
|
+
"@dxos/debug": "0.8.4-main.3c1ae3b",
|
|
34
|
+
"@dxos/invariant": "0.8.4-main.3c1ae3b",
|
|
35
|
+
"@dxos/keys": "0.8.4-main.3c1ae3b",
|
|
36
|
+
"@dxos/node-std": "0.8.4-main.3c1ae3b"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
39
|
"@types/lodash.get": "^4.4.9",
|
|
39
40
|
"@types/lodash.set": "^4.3.9",
|
|
40
|
-
"@dxos/crypto": "0.8.4-main.
|
|
41
|
+
"@dxos/crypto": "0.8.4-main.3c1ae3b"
|
|
41
42
|
},
|
|
42
43
|
"publishConfig": {
|
|
43
44
|
"access": "public"
|
package/src/array.test.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { describe, expect, test } from 'vitest';
|
|
6
6
|
|
|
7
|
-
import { diff,
|
|
7
|
+
import { diff, distinctBy, intersection, partition } from './array';
|
|
8
8
|
|
|
9
9
|
describe('diff', () => {
|
|
10
10
|
test('returns the difference between two sets', () => {
|
package/src/array.ts
CHANGED
|
@@ -130,3 +130,10 @@ export const intersectBy = <T, K>(arrays: T[][], selector: (item: T) => K): T[]
|
|
|
130
130
|
return lookups.every((lookup) => lookup.has(key));
|
|
131
131
|
});
|
|
132
132
|
};
|
|
133
|
+
|
|
134
|
+
export const coerceArray = <T>(arr: T | T[] | undefined): T[] => {
|
|
135
|
+
if (arr === undefined) {
|
|
136
|
+
return [];
|
|
137
|
+
}
|
|
138
|
+
return Array.isArray(arr) ? arr : [arr];
|
|
139
|
+
};
|
package/src/binder.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Function binder replaces pify.
|
|
@@ -10,5 +10,5 @@ import util from 'node:util';
|
|
|
10
10
|
// TODO(burdon): Replace pify everywhere.
|
|
11
11
|
export const createBinder = (obj: any) => ({
|
|
12
12
|
fn: (fn: Function) => fn.bind(obj),
|
|
13
|
-
async: (fn: Function) =>
|
|
13
|
+
async: (fn: Function) => promisify(fn.bind(obj)),
|
|
14
14
|
});
|
package/src/complex.test.ts
CHANGED
package/src/defer.ts
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats an error with its cause chain.
|
|
3
|
+
*/
|
|
4
|
+
//
|
|
5
|
+
// Copyright 2025 DXOS.org
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
export const formatErrorWithCauses = (error: Error): string => {
|
|
9
|
+
const lines: string[] = [];
|
|
10
|
+
let current: Error | undefined = error;
|
|
11
|
+
let level = 0;
|
|
12
|
+
|
|
13
|
+
while (current) {
|
|
14
|
+
const prefix = level === 0 ? '' : `Caused by: `;
|
|
15
|
+
lines.push(prefix + (current.stack ?? String(current)));
|
|
16
|
+
if (!(current.cause instanceof Error)) break;
|
|
17
|
+
current = current.cause;
|
|
18
|
+
level += 1;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return lines.join('\n\n');
|
|
22
|
+
};
|
package/src/filename.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a unique filename.
|
|
7
|
+
*/
|
|
8
|
+
export const createFilename = ({
|
|
9
|
+
parts = [],
|
|
10
|
+
ext,
|
|
11
|
+
date = new Date(),
|
|
12
|
+
}: {
|
|
13
|
+
parts?: string[];
|
|
14
|
+
ext?: string;
|
|
15
|
+
date?: Date;
|
|
16
|
+
}) => [date.toISOString().replace(/[:.]/g, '-'), ...parts].join('_') + (ext ? `.${ext}` : '');
|
package/src/human-hash.test.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -18,7 +18,7 @@ export * from './deep';
|
|
|
18
18
|
export * from './defer-function';
|
|
19
19
|
export * from './defer';
|
|
20
20
|
export * from './entry';
|
|
21
|
-
export * from './
|
|
21
|
+
export * from './filename';
|
|
22
22
|
export * from './for-each-async';
|
|
23
23
|
export * from './human-hash';
|
|
24
24
|
export * from './instance-id';
|
|
@@ -41,6 +41,7 @@ export * from './remove-undefined-keys';
|
|
|
41
41
|
export * from './safe-await';
|
|
42
42
|
export * from './safe-instanceof';
|
|
43
43
|
export * from './safe-parse';
|
|
44
|
+
export * from './safe-stringify';
|
|
44
45
|
export * from './sliding-window-summary';
|
|
45
46
|
export * from './sort';
|
|
46
47
|
export * from './string';
|
|
@@ -51,5 +52,7 @@ export * from './tracer';
|
|
|
51
52
|
export * from './tree';
|
|
52
53
|
export * from './types';
|
|
53
54
|
export * from './uint8array';
|
|
55
|
+
export * from './unit';
|
|
54
56
|
export * from './url';
|
|
55
57
|
export * from './weak';
|
|
58
|
+
export * from './error-format';
|
package/src/position.test.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { describe,
|
|
5
|
+
import { describe, expect, it } from 'vitest';
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { type Position, byPosition } from './position';
|
|
8
8
|
|
|
9
9
|
type TestItem = {
|
|
10
10
|
id: number;
|
package/src/safe-parse.ts
CHANGED
|
@@ -2,33 +2,37 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export
|
|
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
|
|
8
|
-
return isNaN(
|
|
9
|
-
} catch
|
|
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
|
|
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
|
-
|
|
20
|
+
const value = parseFloat(str ?? '');
|
|
21
|
+
return isNaN(value) ? defaultValue : value;
|
|
17
22
|
} catch {
|
|
18
|
-
return defaultValue
|
|
23
|
+
return defaultValue;
|
|
19
24
|
}
|
|
20
|
-
}
|
|
25
|
+
}
|
|
21
26
|
|
|
22
27
|
export const safeParseJson: {
|
|
23
|
-
<T extends object>(
|
|
24
|
-
<T extends object>(
|
|
25
|
-
} = <T extends object>(
|
|
26
|
-
if (
|
|
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(
|
|
29
|
-
} catch
|
|
30
|
-
// no-op.
|
|
31
|
-
}
|
|
33
|
+
return JSON.parse(str);
|
|
34
|
+
} catch {}
|
|
32
35
|
}
|
|
36
|
+
|
|
33
37
|
return defaultValue;
|
|
34
38
|
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
export const SKIP = Object.freeze({});
|
|
6
|
+
|
|
7
|
+
export type StringifyReplacer = (key: string, value: any) => typeof SKIP | any;
|
|
8
|
+
|
|
9
|
+
export function safeStringify(obj: any, filter: StringifyReplacer = defaultFilter, indent = 2) {
|
|
10
|
+
const seen = new WeakSet();
|
|
11
|
+
|
|
12
|
+
function replacer(key: string, value: any) {
|
|
13
|
+
if (typeof value === 'object' && value !== null) {
|
|
14
|
+
if (seen.has(value)) {
|
|
15
|
+
return '[Circular]';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
seen.add(value);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (filter) {
|
|
22
|
+
const v2 = filter?.(key, value);
|
|
23
|
+
if (v2 !== undefined) {
|
|
24
|
+
return v2 === SKIP ? undefined : v2;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let result = '';
|
|
32
|
+
try {
|
|
33
|
+
result = JSON.stringify(obj, replacer, indent);
|
|
34
|
+
} catch (error: any) {
|
|
35
|
+
result = `Error: ${error.message}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type CreateReplacerProps = {
|
|
42
|
+
omit?: string[];
|
|
43
|
+
parse?: string[];
|
|
44
|
+
maxDepth?: number;
|
|
45
|
+
maxArrayLen?: number;
|
|
46
|
+
maxStringLen?: number;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const createReplacer = ({
|
|
50
|
+
omit,
|
|
51
|
+
parse,
|
|
52
|
+
maxDepth,
|
|
53
|
+
maxArrayLen,
|
|
54
|
+
maxStringLen,
|
|
55
|
+
}: CreateReplacerProps = {}): StringifyReplacer => {
|
|
56
|
+
let currentDepth = 0;
|
|
57
|
+
const depthMap = new WeakMap<object, number>();
|
|
58
|
+
|
|
59
|
+
return function (this: any, key: string, value: any) {
|
|
60
|
+
// Track depth.
|
|
61
|
+
if (key === '') {
|
|
62
|
+
currentDepth = 0;
|
|
63
|
+
} else if (this && typeof this === 'object') {
|
|
64
|
+
const parentDepth = depthMap.get(this) ?? 0;
|
|
65
|
+
currentDepth = parentDepth + 1;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (typeof value === 'function') {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Store depth for this object.
|
|
73
|
+
if (value && typeof value === 'object') {
|
|
74
|
+
depthMap.set(value, currentDepth);
|
|
75
|
+
|
|
76
|
+
// Check max depth.
|
|
77
|
+
if (maxDepth != null && currentDepth >= maxDepth) {
|
|
78
|
+
return Array.isArray(value) ? `[{ length: ${value.length} }]` : `{ keys: ${Object.keys(value).length} }`;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Apply other filters.
|
|
83
|
+
if (omit?.includes(key)) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
if (parse?.includes(key) && typeof value === 'string') {
|
|
87
|
+
try {
|
|
88
|
+
return JSON.parse(value);
|
|
89
|
+
} catch {
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (maxArrayLen != null && Array.isArray(value) && value.length > maxArrayLen) {
|
|
94
|
+
return `[length: ${value.length}]`;
|
|
95
|
+
}
|
|
96
|
+
if (maxStringLen != null && typeof value === 'string' && value.length > maxStringLen) {
|
|
97
|
+
return value.slice(0, maxStringLen) + '...';
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return value;
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
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
|
@@ -194,17 +194,18 @@ export const keyToFallback = (key: PublicKey) => hexToFallback(key.toHex());
|
|
|
194
194
|
// TODO(wittjosiah): Support non-hex strings (e.g. DIDs, UUIDs, etc.)
|
|
195
195
|
export const hexToFallback = (hex: string) => toFallback(parseInt(hex, 16));
|
|
196
196
|
|
|
197
|
+
// TODO(burdon): Rename?
|
|
197
198
|
export const toFallback = (hash: number): FallbackValue => {
|
|
198
|
-
// Calculate total possible combinations of emoji and hue pairs
|
|
199
|
+
// Calculate total possible combinations of emoji and hue pairs.
|
|
199
200
|
const totalCombinations = idEmoji.length * idHue.length;
|
|
200
201
|
|
|
201
|
-
// Get a deterministic index within the range of all possible combinations
|
|
202
|
+
// Get a deterministic index within the range of all possible combinations.
|
|
202
203
|
const combinationIndex = hash % totalCombinations;
|
|
203
204
|
|
|
204
|
-
// Calculate which emoji to use based on the combination index
|
|
205
|
+
// Calculate which emoji to use based on the combination index.
|
|
205
206
|
const emojiIndex = Math.floor(combinationIndex / idHue.length);
|
|
206
207
|
|
|
207
|
-
// Calculate which hue to use based on the combination index
|
|
208
|
+
// Calculate which hue to use based on the combination index.
|
|
208
209
|
const hueIndex = combinationIndex % idHue.length;
|
|
209
210
|
|
|
210
211
|
return {
|
package/src/tree.test.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { describe, test } from 'vitest';
|
|
|
6
6
|
|
|
7
7
|
import { PublicKey } from '@dxos/keys';
|
|
8
8
|
|
|
9
|
-
import {
|
|
9
|
+
import { type TreeNode, stringifyTree } from './tree';
|
|
10
10
|
|
|
11
11
|
describe('Tree logging', () => {
|
|
12
12
|
test('simple', () => {
|
package/src/types.ts
CHANGED
|
@@ -12,6 +12,14 @@ export type MaybePromise<T> = T | Promise<T>;
|
|
|
12
12
|
|
|
13
13
|
export type GuardedType<T> = T extends (value: any) => value is infer R ? R : never;
|
|
14
14
|
|
|
15
|
+
export type ToMutable<T> = T extends object
|
|
16
|
+
? { -readonly [K in keyof T]: T[K] extends readonly (infer U)[] ? U[] : T[K] }
|
|
17
|
+
: T;
|
|
18
|
+
|
|
19
|
+
export type Intersection<Types extends readonly unknown[]> = Types extends [infer First, ...infer Rest]
|
|
20
|
+
? First & Intersection<Rest>
|
|
21
|
+
: unknown;
|
|
22
|
+
|
|
15
23
|
export type DeepReadonly<T> = {
|
|
16
24
|
readonly [P in keyof T]: T[P] extends Record<string, any>
|
|
17
25
|
? DeepReadonly<T[P]>
|
|
@@ -48,7 +56,7 @@ export type Falsy = false | 0 | '' | null | undefined;
|
|
|
48
56
|
* NOTE: To filter by type:
|
|
49
57
|
* items.filter((item: any): item is RangeSet<Decoration> => item instanceof RangeSet)
|
|
50
58
|
*/
|
|
51
|
-
export const
|
|
59
|
+
export const isTruthy = <T>(value: T): value is Exclude<T, Falsy> => !!value;
|
|
52
60
|
export const isNonNullable = <T>(value: T | null | undefined): value is T => value != null;
|
|
53
61
|
|
|
54
62
|
// TODO(burdon): Replace use of setTimeout everywhere?
|
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
|
+
});
|
package/src/unit.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
export type Unit = {
|
|
6
|
+
symbol: string;
|
|
7
|
+
quotient: number;
|
|
8
|
+
precision?: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type UnitValue<T> = {
|
|
12
|
+
unit: Unit;
|
|
13
|
+
value: number;
|
|
14
|
+
formattedValue: T;
|
|
15
|
+
toString: () => string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type UnitFormat<T = any> = (n: number, precision?: number) => UnitValue<T>;
|
|
19
|
+
|
|
20
|
+
const createFormat = (unit: Unit): UnitFormat<string> => {
|
|
21
|
+
return (n: number, precision = unit.precision ?? 0) => {
|
|
22
|
+
const value = n / unit.quotient;
|
|
23
|
+
return {
|
|
24
|
+
unit,
|
|
25
|
+
value,
|
|
26
|
+
formattedValue: value.toFixed(precision),
|
|
27
|
+
toString: () => `${value.toFixed(precision)}${unit.symbol}`,
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const MS_SECONDS = 1_000;
|
|
33
|
+
const MS_MINUTES = 60 * MS_SECONDS;
|
|
34
|
+
const MS_HOURS = 60 * MS_MINUTES;
|
|
35
|
+
|
|
36
|
+
export const Unit: Record<string, UnitFormat> = {
|
|
37
|
+
// General.
|
|
38
|
+
Percent: createFormat({ symbol: '%', quotient: 1 / 100, precision: 2 }),
|
|
39
|
+
Thousand: createFormat({ symbol: 'k', quotient: 1_000, precision: 2 }),
|
|
40
|
+
|
|
41
|
+
// Bytes (note KB vs KiB).
|
|
42
|
+
Gigabyte: createFormat({ symbol: 'GB', quotient: 1_000 * 1_000 * 1_000, precision: 2 }),
|
|
43
|
+
Megabyte: createFormat({ symbol: 'MB', quotient: 1_000 * 1_000, precision: 2 }),
|
|
44
|
+
Kilobyte: createFormat({ symbol: 'KB', quotient: 1_000, precision: 2 }),
|
|
45
|
+
Byte: createFormat({ symbol: 'B', quotient: 1 }),
|
|
46
|
+
|
|
47
|
+
// Time.
|
|
48
|
+
Hour: createFormat({ symbol: 'h', quotient: MS_HOURS }),
|
|
49
|
+
Minute: createFormat({ symbol: 'm', quotient: MS_MINUTES }),
|
|
50
|
+
Second: createFormat({ symbol: 's', quotient: MS_SECONDS, precision: 1 }),
|
|
51
|
+
Millisecond: createFormat({ symbol: 'ms', quotient: 1 }),
|
|
52
|
+
Duration: (n: number) => {
|
|
53
|
+
const hours = Math.floor(n / MS_HOURS);
|
|
54
|
+
const minutes = Math.floor((n % MS_HOURS) / MS_MINUTES);
|
|
55
|
+
if (hours) {
|
|
56
|
+
const formattedValue = minutes ? `${hours}h ${minutes}m` : `${hours}h`;
|
|
57
|
+
return {
|
|
58
|
+
unit: { symbol: 'h', quotient: MS_HOURS },
|
|
59
|
+
value: hours,
|
|
60
|
+
formattedValue,
|
|
61
|
+
toString: () => formattedValue,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (minutes) {
|
|
66
|
+
const seconds = (n - MS_MINUTES * minutes) / MS_SECONDS;
|
|
67
|
+
const formattedValue = seconds ? `${minutes}m ${seconds}s` : `${minutes}m`;
|
|
68
|
+
return {
|
|
69
|
+
unit: { symbol: 'm', quotient: MS_MINUTES },
|
|
70
|
+
value: minutes,
|
|
71
|
+
formattedValue,
|
|
72
|
+
toString: () => formattedValue,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const seconds = n >= MS_SECONDS;
|
|
77
|
+
if (seconds) {
|
|
78
|
+
return Unit.Second(n);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return Unit.Millisecond(n);
|
|
82
|
+
},
|
|
83
|
+
} as const;
|
package/src/weak.test.ts
CHANGED
|
@@ -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,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns the first two renderable characters from a string that are separated by non-word characters.
|
|
3
|
-
* Handles Unicode characters correctly.
|
|
4
|
-
*
|
|
5
|
-
* @param {string} label - The input string to process
|
|
6
|
-
* @returns {[string, string]} Array containing the two characters, or empty strings if not found
|
|
7
|
-
*/
|
|
8
|
-
export declare const getFirstTwoRenderableChars: (label: string) => string[];
|
|
9
|
-
//# sourceMappingURL=first-two-chars.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"first-two-chars.d.ts","sourceRoot":"","sources":["../../../src/first-two-chars.ts"],"names":[],"mappings":"AASA;;;;;;GAMG;AACH,eAAO,MAAM,0BAA0B,GAAI,OAAO,MAAM,aA2BvD,CAAC"}
|
|
@@ -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');
|