@dxos/util 0.8.4-main.dedc0f3 → 0.8.4-main.e8ec1fe
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 +325 -447
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +325 -447
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +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/index.d.ts +1 -1
- 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/string.d.ts +4 -0
- 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 +1 -2
- 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 +7 -7
- package/src/binder.ts +2 -2
- package/src/index.ts +1 -1
- package/src/safe-stringify.ts +104 -0
- package/src/string.ts +7 -0
- package/src/to-fallback.ts +5 -4
- package/src/types.ts +1 -2
- package/src/unit.test.ts +1 -1
- package/src/unit.ts +58 -28
- 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/first-two-chars.ts +0 -44
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/util",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.e8ec1fe",
|
|
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
|
".": {
|
|
@@ -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/
|
|
34
|
-
"@dxos/
|
|
35
|
-
"@dxos/keys": "0.8.4-main.
|
|
32
|
+
"@dxos/debug": "0.8.4-main.e8ec1fe",
|
|
33
|
+
"@dxos/invariant": "0.8.4-main.e8ec1fe",
|
|
34
|
+
"@dxos/node-std": "0.8.4-main.e8ec1fe",
|
|
35
|
+
"@dxos/keys": "0.8.4-main.e8ec1fe"
|
|
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.e8ec1fe"
|
|
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
|
@@ -18,7 +18,6 @@ export * from './deep';
|
|
|
18
18
|
export * from './defer-function';
|
|
19
19
|
export * from './defer';
|
|
20
20
|
export * from './entry';
|
|
21
|
-
export * from './first-two-chars';
|
|
22
21
|
export * from './for-each-async';
|
|
23
22
|
export * from './human-hash';
|
|
24
23
|
export * from './instance-id';
|
|
@@ -41,6 +40,7 @@ export * from './remove-undefined-keys';
|
|
|
41
40
|
export * from './safe-await';
|
|
42
41
|
export * from './safe-instanceof';
|
|
43
42
|
export * from './safe-parse';
|
|
43
|
+
export * from './safe-stringify';
|
|
44
44
|
export * from './sliding-window-summary';
|
|
45
45
|
export * from './sort';
|
|
46
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/string.ts
CHANGED
|
@@ -41,3 +41,10 @@ export function trim(strings: TemplateStringsArray, ...values: any[]) {
|
|
|
41
41
|
// Remove that indent from all lines.
|
|
42
42
|
return lines.map((l) => l.slice(minIndent)).join('\n');
|
|
43
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/types.ts
CHANGED
|
@@ -47,9 +47,8 @@ export type Falsy = false | 0 | '' | null | undefined;
|
|
|
47
47
|
* Use with filter chaining instead of filter(Boolean) to preserve type.
|
|
48
48
|
* NOTE: To filter by type:
|
|
49
49
|
* items.filter((item: any): item is RangeSet<Decoration> => item instanceof RangeSet)
|
|
50
|
-
* @deprecated Replace with Predicate.isTruthy
|
|
51
50
|
*/
|
|
52
|
-
export const
|
|
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
|
|
|
55
54
|
// TODO(burdon): Replace use of setTimeout everywhere?
|
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;
|
|
@@ -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"}
|
package/src/first-two-chars.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
// Regular expression to match renderable characters
|
|
6
|
-
// Excludes control characters, combining marks, and other non-renderable characters. Also excludes punctuation.
|
|
7
|
-
const renderableCharRegex =
|
|
8
|
-
/^(?![\p{Control}\p{Mark}\p{Separator}\p{Surrogate}\p{Unassigned}\p{P}])[\p{L}\p{N}\p{S}\p{Emoji}]$/u;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Returns the first two renderable characters from a string that are separated by non-word characters.
|
|
12
|
-
* Handles Unicode characters correctly.
|
|
13
|
-
*
|
|
14
|
-
* @param {string} label - The input string to process
|
|
15
|
-
* @returns {[string, string]} Array containing the two characters, or empty strings if not found
|
|
16
|
-
*/
|
|
17
|
-
export const getFirstTwoRenderableChars = (label: string) => {
|
|
18
|
-
// Convert string to array of Unicode characters
|
|
19
|
-
const characters = Array.from(label);
|
|
20
|
-
|
|
21
|
-
// Keep track of found renderable characters
|
|
22
|
-
const result = ['', ''];
|
|
23
|
-
let foundFirst = false;
|
|
24
|
-
|
|
25
|
-
for (let i = 0; i < characters.length; i++) {
|
|
26
|
-
const char = characters[i];
|
|
27
|
-
|
|
28
|
-
if (renderableCharRegex.test(char)) {
|
|
29
|
-
if (!foundFirst) {
|
|
30
|
-
result[0] = char;
|
|
31
|
-
foundFirst = true;
|
|
32
|
-
} else {
|
|
33
|
-
// Check if there's at least one non-word character between the first and current char
|
|
34
|
-
const textBetween = characters.slice(result[0].length, i).join('');
|
|
35
|
-
if (/[^\p{L}\p{N}_]/u.test(textBetween)) {
|
|
36
|
-
result[1] = char;
|
|
37
|
-
break;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return result;
|
|
44
|
-
};
|