@dxos/util 0.8.4-main.406dc2a → 0.8.4-main.548089c
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 +323 -415
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +323 -415
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.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/types.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/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -6
- package/src/binder.ts +2 -2
- package/src/index.ts +1 -0
- package/src/safe-stringify.ts +104 -0
- package/src/types.ts +0 -1
- package/src/unit.test.ts +1 -1
- package/src/unit.ts +58 -28
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/util",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.548089c",
|
|
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",
|
|
@@ -29,15 +29,15 @@
|
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"lodash.get": "^4.4.2",
|
|
31
31
|
"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.
|
|
32
|
+
"@dxos/debug": "0.8.4-main.548089c",
|
|
33
|
+
"@dxos/invariant": "0.8.4-main.548089c",
|
|
34
|
+
"@dxos/keys": "0.8.4-main.548089c",
|
|
35
|
+
"@dxos/node-std": "0.8.4-main.548089c"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@types/lodash.get": "^4.4.9",
|
|
39
39
|
"@types/lodash.set": "^4.3.9",
|
|
40
|
-
"@dxos/crypto": "0.8.4-main.
|
|
40
|
+
"@dxos/crypto": "0.8.4-main.548089c"
|
|
41
41
|
},
|
|
42
42
|
"publishConfig": {
|
|
43
43
|
"access": "public"
|
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/index.ts
CHANGED
|
@@ -40,6 +40,7 @@ export * from './remove-undefined-keys';
|
|
|
40
40
|
export * from './safe-await';
|
|
41
41
|
export * from './safe-instanceof';
|
|
42
42
|
export * from './safe-parse';
|
|
43
|
+
export * from './safe-stringify';
|
|
43
44
|
export * from './sliding-window-summary';
|
|
44
45
|
export * from './sort';
|
|
45
46
|
export * from './string';
|
|
@@ -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/types.ts
CHANGED
|
@@ -48,7 +48,6 @@ export type Falsy = false | 0 | '' | null | undefined;
|
|
|
48
48
|
* NOTE: To filter by type:
|
|
49
49
|
* items.filter((item: any): item is RangeSet<Decoration> => item instanceof RangeSet)
|
|
50
50
|
*/
|
|
51
|
-
// TODO(burdon): Replace with Predicate.isTruthy?
|
|
52
51
|
export const isTruthy = <T>(value: T): value is Exclude<T, Falsy> => !!value;
|
|
53
52
|
export const isNonNullable = <T>(value: T | null | undefined): value is T => value != null;
|
|
54
53
|
|
package/src/unit.test.ts
CHANGED
package/src/unit.ts
CHANGED
|
@@ -2,51 +2,81 @@
|
|
|
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
|
-
return
|
|
23
|
+
return {
|
|
24
|
+
unit,
|
|
25
|
+
value,
|
|
26
|
+
formattedValue: value.toFixed(precision),
|
|
27
|
+
toString: () => `${value.toFixed(precision)}${unit.symbol}`,
|
|
28
|
+
};
|
|
16
29
|
};
|
|
17
30
|
};
|
|
18
31
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
+
|
|
46
|
+
// Time.
|
|
47
|
+
Hour: createFormat({ symbol: 'h', quotient: MS_HOURS }),
|
|
48
|
+
Minute: createFormat({ symbol: 'm', quotient: MS_MINUTES }),
|
|
49
|
+
Second: createFormat({ symbol: 's', quotient: MS_SECONDS, precision: 1 }),
|
|
50
|
+
Millisecond: createFormat({ symbol: 'ms', quotient: 1 }),
|
|
25
51
|
Duration: (n: number) => {
|
|
26
|
-
const hours = Math.floor(n /
|
|
27
|
-
const minutes = Math.floor((n %
|
|
52
|
+
const hours = Math.floor(n / MS_HOURS);
|
|
53
|
+
const minutes = Math.floor((n % MS_HOURS) / MS_MINUTES);
|
|
28
54
|
if (hours) {
|
|
29
|
-
|
|
55
|
+
const formattedValue = minutes ? `${hours}h ${minutes}m` : `${hours}h`;
|
|
56
|
+
return {
|
|
57
|
+
unit: { symbol: 'h', quotient: MS_HOURS },
|
|
58
|
+
value: hours,
|
|
59
|
+
formattedValue,
|
|
60
|
+
toString: () => formattedValue,
|
|
61
|
+
};
|
|
30
62
|
}
|
|
31
63
|
|
|
32
|
-
const seconds = Math.floor((n % (60 * 1_000)) / 1_000);
|
|
33
64
|
if (minutes) {
|
|
34
|
-
|
|
65
|
+
const seconds = (n - MS_MINUTES * minutes) / MS_SECONDS;
|
|
66
|
+
const formattedValue = seconds ? `${minutes}m ${seconds}s` : `${minutes}m`;
|
|
67
|
+
return {
|
|
68
|
+
unit: { symbol: 'm', quotient: MS_MINUTES },
|
|
69
|
+
value: minutes,
|
|
70
|
+
formattedValue,
|
|
71
|
+
toString: () => formattedValue,
|
|
72
|
+
};
|
|
35
73
|
}
|
|
36
74
|
|
|
75
|
+
const seconds = n >= MS_SECONDS;
|
|
37
76
|
if (seconds) {
|
|
38
|
-
return
|
|
77
|
+
return Unit.Second(n);
|
|
39
78
|
}
|
|
40
79
|
|
|
41
|
-
return
|
|
80
|
+
return Unit.Millisecond(n);
|
|
42
81
|
},
|
|
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
|
-
};
|
|
82
|
+
} as const;
|