@dxos/util 0.8.4-main.67995b8 → 0.8.4-main.69d29f4
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 +390 -116
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +390 -116
- 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 +5 -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 +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 +23 -5
- 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 +14 -9
- package/src/array.test.ts +1 -1
- package/src/array.ts +9 -2
- package/src/binder.ts +2 -2
- package/src/circular-buffer.test.ts +1 -1
- package/src/complex.test.ts +1 -1
- 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/human-hash.test.ts +1 -1
- package/src/index.ts +5 -1
- package/src/platform.ts +11 -1
- package/src/position.test.ts +2 -2
- package/src/retry.ts +74 -0
- package/src/safe-parse.ts +21 -17
- package/src/safe-stringify.ts +146 -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.test.ts +11 -1
- package/src/types.ts +38 -10
- 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,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/util",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.69d29f4",
|
|
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
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "DXOS.org",
|
|
9
|
-
"sideEffects":
|
|
13
|
+
"sideEffects": false,
|
|
10
14
|
"type": "module",
|
|
11
15
|
"exports": {
|
|
12
16
|
".": {
|
|
17
|
+
"types": "./dist/types/src/index.d.ts",
|
|
13
18
|
"browser": "./dist/lib/browser/index.mjs",
|
|
14
19
|
"node": {
|
|
15
20
|
"require": "./dist/lib/node/index.cjs",
|
|
16
21
|
"default": "./dist/lib/node-esm/index.mjs"
|
|
17
|
-
}
|
|
18
|
-
"types": "./dist/types/src/index.d.ts"
|
|
22
|
+
}
|
|
19
23
|
}
|
|
20
24
|
},
|
|
21
25
|
"types": "dist/types/src/index.d.ts",
|
|
@@ -27,17 +31,18 @@
|
|
|
27
31
|
"src"
|
|
28
32
|
],
|
|
29
33
|
"dependencies": {
|
|
34
|
+
"@hazae41/symbol-dispose-polyfill": "^1.0.2",
|
|
30
35
|
"lodash.get": "^4.4.2",
|
|
31
36
|
"lodash.set": "^4.3.2",
|
|
32
|
-
"@dxos/
|
|
33
|
-
"@dxos/
|
|
34
|
-
"@dxos/keys": "0.8.4-main.
|
|
35
|
-
"@dxos/node-std": "0.8.4-main.
|
|
37
|
+
"@dxos/invariant": "0.8.4-main.69d29f4",
|
|
38
|
+
"@dxos/debug": "0.8.4-main.69d29f4",
|
|
39
|
+
"@dxos/keys": "0.8.4-main.69d29f4",
|
|
40
|
+
"@dxos/node-std": "0.8.4-main.69d29f4"
|
|
36
41
|
},
|
|
37
42
|
"devDependencies": {
|
|
38
43
|
"@types/lodash.get": "^4.4.9",
|
|
39
44
|
"@types/lodash.set": "^4.3.9",
|
|
40
|
-
"@dxos/crypto": "0.8.4-main.
|
|
45
|
+
"@dxos/crypto": "0.8.4-main.69d29f4"
|
|
41
46
|
},
|
|
42
47
|
"publishConfig": {
|
|
43
48
|
"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
|
@@ -42,14 +42,14 @@ export const diff = <A, B = A>(
|
|
|
42
42
|
return result;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
export const intersection = <A, B = A>(a: A[], b: B[], comparator: Comparator<A, B>): A[] =>
|
|
45
|
+
export const intersection = <A, B = A>(a: readonly A[], b: readonly B[], comparator: Comparator<A, B>): A[] =>
|
|
46
46
|
a.filter((a) => b.find((b) => comparator(a, b)) !== undefined);
|
|
47
47
|
|
|
48
48
|
/**
|
|
49
49
|
* Returns a new array with only the first instance of each unique item
|
|
50
50
|
* based on a specified property.
|
|
51
51
|
*
|
|
52
|
-
* @
|
|
52
|
+
* @typeProp T - The type of items in the input array.
|
|
53
53
|
* @param array - The array to filter for distinct items.
|
|
54
54
|
* @param key - The property key to determine uniqueness for each item.
|
|
55
55
|
* @returns A new array with only distinct items based on the specified property.
|
|
@@ -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/deep.ts
CHANGED
|
@@ -19,6 +19,8 @@ export const setDeep = <T>(obj: any, path: readonly (string | number)[], value:
|
|
|
19
19
|
let parent = obj;
|
|
20
20
|
for (const key of path.slice(0, -1)) {
|
|
21
21
|
if (parent[key] === undefined) {
|
|
22
|
+
// TODO(wittjosiah): This logic is flawed. This shouldn't be used for initializing arrays.
|
|
23
|
+
// Prefer `Obj.setValue` for ECHO objects.
|
|
22
24
|
const isArrayIndex = !isNaN(Number(key));
|
|
23
25
|
parent[key] = isArrayIndex ? [] : {};
|
|
24
26
|
}
|
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,8 @@ 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';
|
|
59
|
+
export * from './retry';
|
package/src/platform.ts
CHANGED
|
@@ -5,10 +5,20 @@
|
|
|
5
5
|
// NOTE: `!=` is required.
|
|
6
6
|
export const isNode = () => typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
|
|
7
7
|
|
|
8
|
+
export const isBun = () => (globalThis as any).Bun !== undefined;
|
|
9
|
+
|
|
10
|
+
export const isTauri = () => !!(globalThis as any).__TAURI__;
|
|
11
|
+
|
|
8
12
|
/* eslint-disable */
|
|
9
13
|
|
|
10
14
|
// From https://stackoverflow.com/a/11381730/2804332.
|
|
11
|
-
export const
|
|
15
|
+
export const isMobile = () => {
|
|
16
|
+
let check = false;
|
|
17
|
+
(function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))) { check = true; } })(navigator.userAgent || navigator.vendor || (window as any).opera);
|
|
18
|
+
return check;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const isMobileOrTablet = () => {
|
|
12
22
|
let check = false;
|
|
13
23
|
((a) => { if (/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))) { check = true; } })(navigator.userAgent || navigator.vendor || (window as any).opera);
|
|
14
24
|
return check;
|
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/retry.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
export interface RetryOptions<T> {
|
|
6
|
+
/**
|
|
7
|
+
* @default 3
|
|
8
|
+
*/
|
|
9
|
+
count?: number;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @default 100
|
|
13
|
+
*/
|
|
14
|
+
delayMs?: number;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Factor to increase delay by.
|
|
18
|
+
* @default 2
|
|
19
|
+
*/
|
|
20
|
+
exponent?: number;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Retry if error matches the predicate.
|
|
24
|
+
* @default () => true
|
|
25
|
+
*/
|
|
26
|
+
retryOnError?: (error: unknown) => Promise<boolean>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Retry if value matches the predicate.
|
|
30
|
+
* @default () => false
|
|
31
|
+
*/
|
|
32
|
+
retryOnValue?: (value: T) => Promise<boolean>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const DEFAULT_RETRY_OPTIONS: Required<RetryOptions<unknown>> = {
|
|
36
|
+
count: 3,
|
|
37
|
+
delayMs: 100,
|
|
38
|
+
exponent: 2,
|
|
39
|
+
retryOnError: async () => true,
|
|
40
|
+
retryOnValue: async () => false,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Retries the operation a number of times.
|
|
45
|
+
* @returns The result of the succesfull invocation
|
|
46
|
+
* @throws Last error if all retries failed
|
|
47
|
+
*/
|
|
48
|
+
export const retry = async <T>(options: RetryOptions<T>, cb: () => Promise<T>): Promise<T> => {
|
|
49
|
+
const fullOptions: Required<RetryOptions<T>> = { ...DEFAULT_RETRY_OPTIONS, ...options };
|
|
50
|
+
|
|
51
|
+
let numRetries = 0,
|
|
52
|
+
currentDelay = fullOptions.delayMs;
|
|
53
|
+
while (true) {
|
|
54
|
+
let result: T;
|
|
55
|
+
try {
|
|
56
|
+
result = await cb();
|
|
57
|
+
} catch (err) {
|
|
58
|
+
if (numRetries > fullOptions.count || !(await fullOptions.retryOnError(err))) {
|
|
59
|
+
throw err;
|
|
60
|
+
}
|
|
61
|
+
await new Promise((resolve) => setTimeout(resolve, currentDelay));
|
|
62
|
+
currentDelay *= fullOptions.exponent;
|
|
63
|
+
numRetries++;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (!(await fullOptions.retryOnValue(result))) {
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
await new Promise((resolve) => setTimeout(resolve, currentDelay));
|
|
70
|
+
currentDelay *= fullOptions.exponent;
|
|
71
|
+
numRetries++;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
};
|
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,146 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
export const SKIP = Object.freeze({});
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* JSON.stringify replacer function.
|
|
9
|
+
*/
|
|
10
|
+
export type StringifyReplacer = (key: string, value: any) => typeof SKIP | any;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Safely stringifies an object.
|
|
14
|
+
*/
|
|
15
|
+
export function safeStringify(obj: any, filter: StringifyReplacer = defaultFilter, indent = 2) {
|
|
16
|
+
const seen = new WeakMap<object, string>();
|
|
17
|
+
|
|
18
|
+
// NOTE: Called for the root object with undefined key.
|
|
19
|
+
function replacer(this: any, key: string, value: any) {
|
|
20
|
+
try {
|
|
21
|
+
let path = key;
|
|
22
|
+
if (!key) {
|
|
23
|
+
path = '$';
|
|
24
|
+
return value;
|
|
25
|
+
} else if (this) {
|
|
26
|
+
const parentPath = seen.get(this);
|
|
27
|
+
path = parentPath ? `${parentPath}.${key}` : key;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Null or undefined.
|
|
31
|
+
if (value == null) {
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Ignore functions.
|
|
36
|
+
if (typeof value === 'function') {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Ignore exotic objects (non-plain objects like DOM elements, class instances, etc.)
|
|
41
|
+
if (typeof value === 'object' && Object.getPrototypeOf(value) !== Object.prototype) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Check cycles.
|
|
46
|
+
if (typeof value === 'object' && value !== null) {
|
|
47
|
+
const exists = seen.get(value);
|
|
48
|
+
if (exists) {
|
|
49
|
+
return `[${path} => ${exists}]`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
seen.set(value, path);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (filter) {
|
|
56
|
+
const filteredValue = filter?.(key, value);
|
|
57
|
+
if (filteredValue !== undefined) {
|
|
58
|
+
return filteredValue === SKIP ? undefined : filteredValue;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return value;
|
|
63
|
+
} catch (error: any) {
|
|
64
|
+
return `ERROR: ${error.message}`;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return JSON.stringify(obj, replacer, indent);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type CreateReplacerProps = {
|
|
72
|
+
omit?: string[];
|
|
73
|
+
parse?: string[]; // TODO(burdon): Parse JSON value.
|
|
74
|
+
maxDepth?: number;
|
|
75
|
+
maxArrayLen?: number;
|
|
76
|
+
maxStringLen?: number;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Construct JSON.stringify replacer.
|
|
81
|
+
*/
|
|
82
|
+
// TODO(burdon): Change to composite effect.
|
|
83
|
+
export const createReplacer = ({
|
|
84
|
+
omit,
|
|
85
|
+
parse,
|
|
86
|
+
maxDepth,
|
|
87
|
+
maxArrayLen,
|
|
88
|
+
maxStringLen,
|
|
89
|
+
}: CreateReplacerProps = {}): StringifyReplacer => {
|
|
90
|
+
let currentDepth = 0;
|
|
91
|
+
const depthMap = new WeakMap<object, number>();
|
|
92
|
+
|
|
93
|
+
return function (this: any, key: string, value: any) {
|
|
94
|
+
// Track depth.
|
|
95
|
+
if (key === '') {
|
|
96
|
+
// Root.
|
|
97
|
+
currentDepth = 0;
|
|
98
|
+
} else if (this && typeof this === 'object') {
|
|
99
|
+
const parentDepth = depthMap.get(this) ?? 0;
|
|
100
|
+
currentDepth = parentDepth + 1;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Skip functions.
|
|
104
|
+
if (typeof value === 'function') {
|
|
105
|
+
return SKIP;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Store depth for this object.
|
|
109
|
+
if (value && typeof value === 'object') {
|
|
110
|
+
depthMap.set(value, currentDepth);
|
|
111
|
+
|
|
112
|
+
// Check max depth.
|
|
113
|
+
if (maxDepth != null && currentDepth >= maxDepth) {
|
|
114
|
+
return Array.isArray(value) ? `[{ length: ${value.length} }]` : `{ keys: ${Object.keys(value).length} }`;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Apply other filters.
|
|
119
|
+
if (omit?.includes(key)) {
|
|
120
|
+
return SKIP;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Parse JSON values.
|
|
124
|
+
if (parse?.includes(key) && typeof value === 'string') {
|
|
125
|
+
try {
|
|
126
|
+
return JSON.parse(value);
|
|
127
|
+
} catch {
|
|
128
|
+
return value;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Show array length.
|
|
133
|
+
if (maxArrayLen != null && Array.isArray(value) && value.length > maxArrayLen) {
|
|
134
|
+
return `[length: ${value.length}]`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Truncate strings.
|
|
138
|
+
if (maxStringLen != null && typeof value === 'string' && value.length > maxStringLen) {
|
|
139
|
+
return value.slice(0, maxStringLen) + '...';
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return value;
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
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.test.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { describe, expect, test } from 'vitest';
|
|
6
6
|
|
|
7
|
-
import { isNonNullable } from './types';
|
|
7
|
+
import { arrayMove, arraySwap, isNonNullable } from './types';
|
|
8
8
|
|
|
9
9
|
describe('types', () => {
|
|
10
10
|
test('filter', async () => {
|
|
@@ -12,4 +12,14 @@ describe('types', () => {
|
|
|
12
12
|
const filtered: number[] = values.filter(isNonNullable);
|
|
13
13
|
expect(filtered).to.deep.equal([1, 2, 3, 4]);
|
|
14
14
|
});
|
|
15
|
+
|
|
16
|
+
test('arrayMove', () => {
|
|
17
|
+
expect(arrayMove([1, 2, 3, 4], 0, 2)).to.deep.equal([2, 3, 1, 4]);
|
|
18
|
+
expect(arrayMove([1, 2, 3, 4], 2, 0)).to.deep.equal([3, 1, 2, 4]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('arraySwap', () => {
|
|
22
|
+
expect(arraySwap([1, 2, 3, 4], 0, 2)).to.deep.equal([3, 2, 1, 4]);
|
|
23
|
+
expect(arraySwap([1, 2, 3, 4], 2, 0)).to.deep.equal([3, 2, 1, 4]);
|
|
24
|
+
});
|
|
15
25
|
});
|