@dxos/util 0.8.4-main.e99c46d → 0.8.4-main.ef1bc66f44
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 +450 -459
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +450 -459
- 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 +3 -2
- package/dist/types/src/array.d.ts.map +1 -1
- package/dist/types/src/deep.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/platform.d.ts +4 -1
- package/dist/types/src/platform.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-parse.d.ts +6 -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/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 +23 -6
- 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 +12 -7
- package/src/array.ts +9 -2
- package/src/binder.ts +2 -2
- package/src/deep.ts +2 -0
- package/src/defer.ts +1 -1
- package/src/error-format.ts +22 -0
- package/src/filename.ts +16 -0
- package/src/index.ts +4 -1
- package/src/platform.ts +12 -2
- package/src/retry.ts +74 -0
- package/src/safe-parse.ts +19 -16
- package/src/safe-stringify.ts +146 -0
- package/src/string.ts +7 -0
- package/src/to-fallback.ts +5 -4
- package/src/types.test.ts +11 -1
- package/src/types.ts +38 -11
- package/src/unit.test.ts +1 -1
- package/src/unit.ts +59 -28
- 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/src/unit.ts
CHANGED
|
@@ -2,51 +2,82 @@
|
|
|
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
|
+
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 }),
|
|
25
52
|
Duration: (n: number) => {
|
|
26
|
-
const hours = Math.floor(n /
|
|
27
|
-
const minutes = Math.floor((n %
|
|
53
|
+
const hours = Math.floor(n / MS_HOURS);
|
|
54
|
+
const minutes = Math.floor((n % MS_HOURS) / MS_MINUTES);
|
|
28
55
|
if (hours) {
|
|
29
|
-
|
|
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
|
+
};
|
|
30
63
|
}
|
|
31
64
|
|
|
32
|
-
const seconds = Math.floor((n % (60 * 1_000)) / 1_000);
|
|
33
65
|
if (minutes) {
|
|
34
|
-
|
|
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
|
+
};
|
|
35
74
|
}
|
|
36
75
|
|
|
76
|
+
const seconds = n >= MS_SECONDS;
|
|
37
77
|
if (seconds) {
|
|
38
|
-
return
|
|
78
|
+
return Unit.Second(n);
|
|
39
79
|
}
|
|
40
80
|
|
|
41
|
-
return
|
|
81
|
+
return Unit.Millisecond(n);
|
|
42
82
|
},
|
|
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
|
-
};
|
|
83
|
+
} as const;
|
|
@@ -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');
|
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
|
-
};
|