@keeex/utils 7.0.1
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/LICENSE +9 -0
- package/README.md +105 -0
- package/lib/array.d.ts +28 -0
- package/lib/array.js +36 -0
- package/lib/arraybuffer.d.ts +138 -0
- package/lib/arraybuffer.js +141 -0
- package/lib/async/asynctrigger.d.ts +50 -0
- package/lib/async/asynctrigger.js +108 -0
- package/lib/async/deferredpromise.d.ts +32 -0
- package/lib/async/deferredpromise.js +66 -0
- package/lib/async/keycache.d.ts +56 -0
- package/lib/async/keycache.js +103 -0
- package/lib/async/queues.d.ts +69 -0
- package/lib/async/queues.js +135 -0
- package/lib/async/timecache.d.ts +58 -0
- package/lib/async/timecache.js +118 -0
- package/lib/base58.d.ts +27 -0
- package/lib/base58.js +83 -0
- package/lib/base64.d.ts +51 -0
- package/lib/base64.js +126 -0
- package/lib/benchmark.d.ts +126 -0
- package/lib/benchmark.js +177 -0
- package/lib/bits/arraybuffer.d.ts +35 -0
- package/lib/bits/arraybuffer.js +64 -0
- package/lib/bits/base64.d.ts +35 -0
- package/lib/bits/base64.js +70 -0
- package/lib/bits/hex.d.ts +17 -0
- package/lib/bits/hex.js +30 -0
- package/lib/bits/uint8array.d.ts +28 -0
- package/lib/bits/uint8array.js +42 -0
- package/lib/bytebuffer.d.ts +27 -0
- package/lib/bytebuffer.js +29 -0
- package/lib/consts.d.ts +33 -0
- package/lib/consts.js +33 -0
- package/lib/cron/logger.d.ts +22 -0
- package/lib/cron/logger.js +31 -0
- package/lib/cron/scheduledtask.d.ts +71 -0
- package/lib/cron/scheduledtask.js +137 -0
- package/lib/cron/types.d.ts +53 -0
- package/lib/cron/types.js +31 -0
- package/lib/cron.d.ts +29 -0
- package/lib/cron.js +47 -0
- package/lib/dict.d.ts +56 -0
- package/lib/dict.js +74 -0
- package/lib/error.d.ts +25 -0
- package/lib/error.js +41 -0
- package/lib/global.d.ts +27 -0
- package/lib/global.js +53 -0
- package/lib/hex.d.ts +32 -0
- package/lib/hex.js +58 -0
- package/lib/idx.d.ts +51 -0
- package/lib/idx.js +81 -0
- package/lib/json.d.ts +57 -0
- package/lib/json.js +116 -0
- package/lib/marshalling/marshaller.d.ts +51 -0
- package/lib/marshalling/marshaller.js +155 -0
- package/lib/marshalling/unmarshaller.d.ts +53 -0
- package/lib/marshalling/unmarshaller.js +124 -0
- package/lib/marshalling/util.d.ts +25 -0
- package/lib/marshalling/util.js +25 -0
- package/lib/number.d.ts +17 -0
- package/lib/number.js +21 -0
- package/lib/path.d.ts +25 -0
- package/lib/path.js +29 -0
- package/lib/promise.d.ts +42 -0
- package/lib/promise.js +78 -0
- package/lib/starttime.d.ts +23 -0
- package/lib/starttime.js +29 -0
- package/lib/string.d.ts +65 -0
- package/lib/string.js +108 -0
- package/lib/types/array.d.ts +34 -0
- package/lib/types/array.js +64 -0
- package/lib/types/enum.d.ts +30 -0
- package/lib/types/enum.js +44 -0
- package/lib/types/predicateerror.d.ts +40 -0
- package/lib/types/predicateerror.js +107 -0
- package/lib/types/primitive.d.ts +23 -0
- package/lib/types/primitive.js +34 -0
- package/lib/types/record.d.ts +67 -0
- package/lib/types/record.js +235 -0
- package/lib/types/types.d.ts +64 -0
- package/lib/types/types.js +115 -0
- package/lib/types/utils.d.ts +18 -0
- package/lib/types/utils.js +67 -0
- package/lib/uint8array.d.ts +176 -0
- package/lib/uint8array.js +438 -0
- package/lib/units.d.ts +159 -0
- package/lib/units.js +290 -0
- package/lib/utils/buffer.d.ts +49 -0
- package/lib/utils/buffer.js +79 -0
- package/lib/utils/fourbytes.d.ts +29 -0
- package/lib/utils/fourbytes.js +45 -0
- package/package.json +1 -0
- package/web/array.d.ts +28 -0
- package/web/array.js +34 -0
- package/web/arraybuffer.d.ts +138 -0
- package/web/arraybuffer.js +141 -0
- package/web/async/asynctrigger.d.ts +50 -0
- package/web/async/asynctrigger.js +106 -0
- package/web/async/deferredpromise.d.ts +32 -0
- package/web/async/deferredpromise.js +65 -0
- package/web/async/keycache.d.ts +56 -0
- package/web/async/keycache.js +97 -0
- package/web/async/queues.d.ts +69 -0
- package/web/async/queues.js +131 -0
- package/web/async/timecache.d.ts +58 -0
- package/web/async/timecache.js +107 -0
- package/web/base58.d.ts +27 -0
- package/web/base58.js +78 -0
- package/web/base64.d.ts +51 -0
- package/web/base64.js +136 -0
- package/web/benchmark.d.ts +126 -0
- package/web/benchmark.js +183 -0
- package/web/bits/arraybuffer.d.ts +35 -0
- package/web/bits/arraybuffer.js +59 -0
- package/web/bits/base64.d.ts +35 -0
- package/web/bits/base64.js +67 -0
- package/web/bits/hex.d.ts +17 -0
- package/web/bits/hex.js +27 -0
- package/web/bits/uint8array.d.ts +28 -0
- package/web/bits/uint8array.js +41 -0
- package/web/bytebuffer.d.ts +27 -0
- package/web/bytebuffer.js +29 -0
- package/web/consts.d.ts +33 -0
- package/web/consts.js +33 -0
- package/web/cron/logger.d.ts +22 -0
- package/web/cron/logger.js +30 -0
- package/web/cron/scheduledtask.d.ts +71 -0
- package/web/cron/scheduledtask.js +136 -0
- package/web/cron/types.d.ts +53 -0
- package/web/cron/types.js +31 -0
- package/web/cron.d.ts +29 -0
- package/web/cron.js +47 -0
- package/web/dict.d.ts +56 -0
- package/web/dict.js +67 -0
- package/web/error.d.ts +25 -0
- package/web/error.js +39 -0
- package/web/global.d.ts +27 -0
- package/web/global.js +49 -0
- package/web/hex.d.ts +32 -0
- package/web/hex.js +52 -0
- package/web/idx.d.ts +51 -0
- package/web/idx.js +76 -0
- package/web/json.d.ts +57 -0
- package/web/json.js +98 -0
- package/web/marshalling/marshaller.d.ts +51 -0
- package/web/marshalling/marshaller.js +150 -0
- package/web/marshalling/unmarshaller.d.ts +53 -0
- package/web/marshalling/unmarshaller.js +115 -0
- package/web/marshalling/util.d.ts +25 -0
- package/web/marshalling/util.js +25 -0
- package/web/number.d.ts +17 -0
- package/web/number.js +21 -0
- package/web/path.d.ts +25 -0
- package/web/path.js +26 -0
- package/web/promise.d.ts +42 -0
- package/web/promise.js +74 -0
- package/web/starttime.d.ts +23 -0
- package/web/starttime.js +29 -0
- package/web/string.d.ts +65 -0
- package/web/string.js +101 -0
- package/web/types/array.d.ts +34 -0
- package/web/types/array.js +63 -0
- package/web/types/enum.d.ts +30 -0
- package/web/types/enum.js +40 -0
- package/web/types/predicateerror.d.ts +40 -0
- package/web/types/predicateerror.js +128 -0
- package/web/types/primitive.d.ts +23 -0
- package/web/types/primitive.js +33 -0
- package/web/types/record.d.ts +67 -0
- package/web/types/record.js +213 -0
- package/web/types/types.d.ts +64 -0
- package/web/types/types.js +123 -0
- package/web/types/utils.d.ts +18 -0
- package/web/types/utils.js +30 -0
- package/web/uint8array.d.ts +176 -0
- package/web/uint8array.js +412 -0
- package/web/units.d.ts +159 -0
- package/web/units.js +312 -0
- package/web/utils/buffer.d.ts +49 -0
- package/web/utils/buffer.js +76 -0
- package/web/utils/fourbytes.d.ts +29 -0
- package/web/utils/fourbytes.js +45 -0
package/web/dict.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
export type Dictionary = Record<string, unknown>;
|
|
17
|
+
/** Function used for smart copy of objects. */
|
|
18
|
+
export type CopyFunction = (valueFromSecondary: unknown) => unknown;
|
|
19
|
+
/**
|
|
20
|
+
* Copy all properties from secondary into primary.
|
|
21
|
+
*
|
|
22
|
+
* Update primary in place.
|
|
23
|
+
*
|
|
24
|
+
* @param copier - Function to create copy of each property values.
|
|
25
|
+
* By default create a shallow copy.
|
|
26
|
+
* The function accept one argument: the value to copy.
|
|
27
|
+
*
|
|
28
|
+
* @returns
|
|
29
|
+
* Returns primary.
|
|
30
|
+
*
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
export declare const update: (primary: Dictionary, secondary: Dictionary, copier?: CopyFunction) => Dictionary;
|
|
34
|
+
/**
|
|
35
|
+
* Merge two dictionaries.
|
|
36
|
+
*
|
|
37
|
+
* If both op1 and op2 have properties with the same key, values from op2 will erase values from
|
|
38
|
+
* op1.
|
|
39
|
+
*
|
|
40
|
+
* @param copier - Function to create copy of each property values.
|
|
41
|
+
* By default create a shallow copy.
|
|
42
|
+
* The function accept one argument: the value to copy.
|
|
43
|
+
*
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
46
|
+
export declare const merge: (op1: Dictionary, op2: Dictionary, copier?: CopyFunction) => Dictionary;
|
|
47
|
+
export type PrimitiveTypeObject = Record<string, unknown> | Array<unknown> | string | number | boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Perform a deep copy of a dictionary with only primitive types (string, number, boolean) and other
|
|
50
|
+
* objects and arrays of such types.
|
|
51
|
+
*
|
|
52
|
+
* This is not strongly typed because lazy.
|
|
53
|
+
*
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
|
+
export declare const deepCopyPrimitive: (source: unknown) => unknown;
|
package/web/dict.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Copy all properties from secondary into primary.
|
|
18
|
+
*
|
|
19
|
+
* Update primary in place.
|
|
20
|
+
*
|
|
21
|
+
* @param copier - Function to create copy of each property values.
|
|
22
|
+
* By default create a shallow copy.
|
|
23
|
+
* The function accept one argument: the value to copy.
|
|
24
|
+
*
|
|
25
|
+
* @returns
|
|
26
|
+
* Returns primary.
|
|
27
|
+
*
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export const update = (primary, secondary, copier) => copier ? (Object.keys(secondary).forEach(propName => {
|
|
31
|
+
primary[propName] = copier(secondary[propName]);
|
|
32
|
+
}), primary) : Object.assign(primary, secondary);
|
|
33
|
+
/**
|
|
34
|
+
* Merge two dictionaries.
|
|
35
|
+
*
|
|
36
|
+
* If both op1 and op2 have properties with the same key, values from op2 will erase values from
|
|
37
|
+
* op1.
|
|
38
|
+
*
|
|
39
|
+
* @param copier - Function to create copy of each property values.
|
|
40
|
+
* By default create a shallow copy.
|
|
41
|
+
* The function accept one argument: the value to copy.
|
|
42
|
+
*
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export const merge = (op1, op2, copier) => update(update({}, op1, copier), op2, copier);
|
|
46
|
+
/**
|
|
47
|
+
* Perform a deep copy of a dictionary with only primitive types (string, number, boolean) and other
|
|
48
|
+
* objects and arrays of such types.
|
|
49
|
+
*
|
|
50
|
+
* This is not strongly typed because lazy.
|
|
51
|
+
*
|
|
52
|
+
* @public
|
|
53
|
+
*/
|
|
54
|
+
export const deepCopyPrimitive = source => {
|
|
55
|
+
if (typeof source === "string") return source;
|
|
56
|
+
if (typeof source === "number") return source;
|
|
57
|
+
if (typeof source === "boolean") return source;
|
|
58
|
+
if (Array.isArray(source)) return source.map(deepCopyPrimitive);
|
|
59
|
+
if (typeof source === "object" && source) {
|
|
60
|
+
const res = {};
|
|
61
|
+
const rec = source;
|
|
62
|
+
for (const key of Object.keys(rec)) {
|
|
63
|
+
res[key] = deepCopyPrimitive(rec[key]);
|
|
64
|
+
}
|
|
65
|
+
return res;
|
|
66
|
+
}
|
|
67
|
+
};
|
package/web/error.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Make sure the provided value is an Error instance
|
|
18
|
+
*
|
|
19
|
+
* For things that are not an error, they are stringified as possible and used as a message.
|
|
20
|
+
*
|
|
21
|
+
* This function should never throw on its own.
|
|
22
|
+
*
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export declare const asError: (e: unknown) => Error;
|
package/web/error.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Make sure the provided value is an Error instance
|
|
18
|
+
*
|
|
19
|
+
* For things that are not an error, they are stringified as possible and used as a message.
|
|
20
|
+
*
|
|
21
|
+
* This function should never throw on its own.
|
|
22
|
+
*
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export const asError = e => {
|
|
26
|
+
if (e instanceof Error) return e;
|
|
27
|
+
if (typeof e === "string") return new Error(e);
|
|
28
|
+
const cast = e;
|
|
29
|
+
if (cast.toString && typeof cast.toString === "function") {
|
|
30
|
+
const str = cast.toString();
|
|
31
|
+
if (str !== "[object Object]") return new Error(cast.toString());
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
return new Error(JSON.stringify(e));
|
|
35
|
+
} catch {}
|
|
36
|
+
return new Error("Unknown error", {
|
|
37
|
+
cause: e
|
|
38
|
+
});
|
|
39
|
+
};
|
package/web/global.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Retrieve a value stored at a global level in the current execution environment.
|
|
18
|
+
*
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
export declare const getGlobalValue: (key: string) => unknown;
|
|
22
|
+
/**
|
|
23
|
+
* Store a value at a global level in the current execution environment.
|
|
24
|
+
*
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export declare const setGlobalValue: (key: string, value: unknown) => void;
|
package/web/global.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
const getGlobalObject = () => {
|
|
17
|
+
if (typeof window !== "undefined") return window;
|
|
18
|
+
if (typeof global !== "undefined") return global;
|
|
19
|
+
throw new Error("No global object available");
|
|
20
|
+
};
|
|
21
|
+
const globalKey = "kx_global_holder";
|
|
22
|
+
/**
|
|
23
|
+
* Retrieve a value stored at a global level in the current execution environment.
|
|
24
|
+
*
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export const getGlobalValue = key => {
|
|
28
|
+
const globalObject = getGlobalObject();
|
|
29
|
+
if (!(globalKey in globalObject)) return;
|
|
30
|
+
const globalHolder = globalObject[globalKey];
|
|
31
|
+
return globalHolder[key];
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Store a value at a global level in the current execution environment.
|
|
35
|
+
*
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export const setGlobalValue = (key, value) => {
|
|
39
|
+
const globalObject = getGlobalObject();
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/init-declarations
|
|
41
|
+
let globalHolder;
|
|
42
|
+
if (globalKey in globalObject) {
|
|
43
|
+
globalHolder = globalObject[globalKey];
|
|
44
|
+
} else {
|
|
45
|
+
globalHolder = {};
|
|
46
|
+
globalObject[globalKey] = globalHolder;
|
|
47
|
+
}
|
|
48
|
+
globalHolder[key] = value;
|
|
49
|
+
};
|
package/web/hex.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/** Add the "0x" if asked */
|
|
17
|
+
export declare const prependPrefix: (value: string, prefix: boolean) => string;
|
|
18
|
+
/** Check that a string is an hexadecimal string */
|
|
19
|
+
export declare const isHexString: (value: string) => boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Convert source data to hexadecimal representation.
|
|
22
|
+
*
|
|
23
|
+
* @param src - The source value.
|
|
24
|
+
* Accepts many type of input, handled in reasonable ways:
|
|
25
|
+
*
|
|
26
|
+
* - `number` and `bigint`: converted to their hexadecimal representation
|
|
27
|
+
* - `string`: can be either an hexadecimal, base64, or IDX string
|
|
28
|
+
* - `Uint8Array` and `Array<number>`: converted to the hexadecimal representation of their content
|
|
29
|
+
*
|
|
30
|
+
* @param withPrefix - Whether to prefix the result with `0x`.
|
|
31
|
+
*/
|
|
32
|
+
export declare const toHex: (src: number | bigint | string | Uint8Array | Array<number>, withPrefix?: boolean) => string;
|
package/web/hex.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
import * as bubble from "@keeex/bubble_babble/web/bubble_babble.js";
|
|
17
|
+
import { OutputFormat } from "@keeex/bubble_babble/web/bubble_babble.js";
|
|
18
|
+
import { check } from "./idx.js";
|
|
19
|
+
import { b642buf8, buf82hex } from "./uint8array.js";
|
|
20
|
+
const HEXA_BASE = 16;
|
|
21
|
+
const HEX_PREFIX = "0x";
|
|
22
|
+
/** Add the "0x" if asked */
|
|
23
|
+
export const prependPrefix = (value, prefix) => {
|
|
24
|
+
if (value.startsWith(HEX_PREFIX) && !prefix) return value.slice(HEX_PREFIX.length);
|
|
25
|
+
if (!value.startsWith(HEX_PREFIX) && prefix) return `0x${value}`;
|
|
26
|
+
return value;
|
|
27
|
+
};
|
|
28
|
+
/** Check that a string is an hexadecimal string */
|
|
29
|
+
export const isHexString = value => /^(?:0x)?[0-9a-fA-F]+$/u.test(value);
|
|
30
|
+
/**
|
|
31
|
+
* Convert source data to hexadecimal representation.
|
|
32
|
+
*
|
|
33
|
+
* @param src - The source value.
|
|
34
|
+
* Accepts many type of input, handled in reasonable ways:
|
|
35
|
+
*
|
|
36
|
+
* - `number` and `bigint`: converted to their hexadecimal representation
|
|
37
|
+
* - `string`: can be either an hexadecimal, base64, or IDX string
|
|
38
|
+
* - `Uint8Array` and `Array<number>`: converted to the hexadecimal representation of their content
|
|
39
|
+
*
|
|
40
|
+
* @param withPrefix - Whether to prefix the result with `0x`.
|
|
41
|
+
*/
|
|
42
|
+
export const toHex = (src, withPrefix = false) => {
|
|
43
|
+
if (typeof src === "number" || typeof src === "bigint") {
|
|
44
|
+
const bi = BigInt(src);
|
|
45
|
+
return prependPrefix(bi.toString(HEXA_BASE), withPrefix);
|
|
46
|
+
}
|
|
47
|
+
if (src instanceof Uint8Array) return buf82hex(src, withPrefix);
|
|
48
|
+
if (Array.isArray(src)) return buf82hex(new Uint8Array(src), withPrefix);
|
|
49
|
+
if (check(src)) return toHex(bubble.decode(src, OutputFormat.Uint8Array), withPrefix);
|
|
50
|
+
if (isHexString(src)) return prependPrefix(src, withPrefix);
|
|
51
|
+
return toHex(b642buf8(src, true), withPrefix);
|
|
52
|
+
};
|
package/web/idx.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Check that an IDX is valid
|
|
18
|
+
*
|
|
19
|
+
* Accept anything that is valid regardless of the first and last character.
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
export declare const check: (candidate: string) => boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Raise an exception if the value isn't a valid IDX.
|
|
26
|
+
*
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
29
|
+
export declare const checkException: (candidate: string) => void;
|
|
30
|
+
/**
|
|
31
|
+
* Change first and last letter of an IDX.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export declare const changeExtrem: (candidate: string, extrems?: string) => string;
|
|
36
|
+
/**
|
|
37
|
+
* Split an IDX into its words.
|
|
38
|
+
*
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
export declare const split: (idx: string) => Array<string>;
|
|
42
|
+
/**
|
|
43
|
+
* Return the head of an IDX.
|
|
44
|
+
*
|
|
45
|
+
* @param count - Number of words to include
|
|
46
|
+
*
|
|
47
|
+
* @param ellipsis - Append an ellipsis at the end
|
|
48
|
+
*
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
51
|
+
export declare const head: (idx: string, count: number, ellipsis?: boolean) => string;
|
package/web/idx.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
import { decode as bubbleDecode } from "@keeex/bubble_babble/web/bubble_babble.js";
|
|
17
|
+
const IDX_REGEX = /^(?<delim>[a-z])[aeiouy][bcdfghjklmnpqrstvwxz][aeiouy][bcdfghjklmnpqrstvwxz]-(?:[bcdfghjklmnpqrstvwxz][aeiouy][bcdfghjklmnpqrstvwxz][aeiouy][bcdfghjklmnpqrstvwxz]-){15}[bcdfghjklmnpqrstvwxz][aeiouy][bcdfghjklmnpqrstvwxz][aeiouy]\k<delim>$/u;
|
|
18
|
+
/** Replace the first and last character of a string */
|
|
19
|
+
const changeExtremNoCheck = (candidate, extrems) => `${extrems}${candidate.substring(1, candidate.length - 1)}${extrems}`;
|
|
20
|
+
const SHA256_BYTE_LENGTH = 32;
|
|
21
|
+
/**
|
|
22
|
+
* Check that an IDX is valid
|
|
23
|
+
*
|
|
24
|
+
* Accept anything that is valid regardless of the first and last character.
|
|
25
|
+
*
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export const check = candidate => {
|
|
29
|
+
try {
|
|
30
|
+
return IDX_REGEX.test(candidate) && bubbleDecode(changeExtremNoCheck(candidate, "x")).byteLength === SHA256_BYTE_LENGTH;
|
|
31
|
+
} catch {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Raise an exception if the value isn't a valid IDX.
|
|
37
|
+
*
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export const checkException = candidate => {
|
|
41
|
+
if (!check(candidate)) throw new Error(`Invalid IDX: "${candidate}"`);
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Change first and last letter of an IDX.
|
|
45
|
+
*
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
export const changeExtrem = (candidate, extrems = "x") => {
|
|
49
|
+
if (extrems.length !== 1) throw new Error(`Invalid extrems value "${extrems}"`);
|
|
50
|
+
checkException(candidate);
|
|
51
|
+
return changeExtremNoCheck(candidate, extrems);
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Split an IDX into its words.
|
|
55
|
+
*
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
58
|
+
export const split = idx => {
|
|
59
|
+
checkException(idx);
|
|
60
|
+
return idx.split("-");
|
|
61
|
+
};
|
|
62
|
+
const IDX_WORDS_COUNT = 17;
|
|
63
|
+
/**
|
|
64
|
+
* Return the head of an IDX.
|
|
65
|
+
*
|
|
66
|
+
* @param count - Number of words to include
|
|
67
|
+
*
|
|
68
|
+
* @param ellipsis - Append an ellipsis at the end
|
|
69
|
+
*
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
export const head = (idx, count, ellipsis = false) => {
|
|
73
|
+
if (count >= IDX_WORDS_COUNT) return idx;
|
|
74
|
+
const words = split(idx).slice(0, count);
|
|
75
|
+
return `${words.join("-")}${ellipsis ? "-…" : ""}`;
|
|
76
|
+
};
|
package/web/json.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
import { TypePredicate } from "./types/types.js";
|
|
17
|
+
/**
|
|
18
|
+
* All the primitive types supported in a JSON file.
|
|
19
|
+
*
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export type JSONPrimitiveType = null | string | number | boolean;
|
|
23
|
+
/**
|
|
24
|
+
* All possible value type in a JSON file.
|
|
25
|
+
*
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export type JSONValueType = JSONPrimitiveType | JSONArray | JSONObject;
|
|
29
|
+
/**
|
|
30
|
+
* All supported arrays in a JSON files.
|
|
31
|
+
*
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export type JSONArray = Array<JSONValueType>;
|
|
35
|
+
/**
|
|
36
|
+
* All supported objects in a JSON files.
|
|
37
|
+
*
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export interface JSONObject {
|
|
41
|
+
[key: string]: JSONValueType;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* JSON.parse() with typed return.
|
|
45
|
+
*
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
export declare const jsonParse: <T>(value: unknown, predicate?: TypePredicate<T>) => T;
|
|
49
|
+
/**
|
|
50
|
+
* Do a JSON stringify operation with optional canonization of properties' order.
|
|
51
|
+
*
|
|
52
|
+
* @param order - Sort all properties using their UTF-8 point value.
|
|
53
|
+
* This is roughly alphabetical order when there are no non-ASCII characters.
|
|
54
|
+
*
|
|
55
|
+
* @public
|
|
56
|
+
*/
|
|
57
|
+
export declare const jsonStringify: (source: unknown, order?: boolean, prettyIndent?: boolean | number) => string;
|
package/web/json.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
const JSON_DEFAULT_INDENT = 2;
|
|
17
|
+
/**
|
|
18
|
+
* JSON.parse() with typed return.
|
|
19
|
+
*
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export const jsonParse = (value, predicate) => {
|
|
23
|
+
const res = typeof value === "string" ? JSON.parse(value) : value;
|
|
24
|
+
if (predicate && !predicate(res, {
|
|
25
|
+
raise: true
|
|
26
|
+
})) throw new Error("Unexpected state");
|
|
27
|
+
return res;
|
|
28
|
+
};
|
|
29
|
+
/** Return the expected indentation step */
|
|
30
|
+
const indentStep = prettyIndent => {
|
|
31
|
+
if (prettyIndent === false) return 0;
|
|
32
|
+
if (prettyIndent === true) return JSON_DEFAULT_INDENT;
|
|
33
|
+
return prettyIndent;
|
|
34
|
+
};
|
|
35
|
+
/** Indent a string */
|
|
36
|
+
const indentStr = (level, value) => {
|
|
37
|
+
const lines = value.split("\n");
|
|
38
|
+
const indentedLines = lines.map(c => `${" ".repeat(level)}${c}`);
|
|
39
|
+
return indentedLines.join("\n");
|
|
40
|
+
};
|
|
41
|
+
/** Helper for Array.map() */
|
|
42
|
+
const indenter = level => value => indentStr(level, value);
|
|
43
|
+
const processArray = (source, prettyIndent) => {
|
|
44
|
+
const outputElem = source.map(c => {
|
|
45
|
+
if (c === undefined) return "null";
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
47
|
+
return jsonStringify(c, true, prettyIndent);
|
|
48
|
+
});
|
|
49
|
+
if (outputElem.length === 0) return "[]";
|
|
50
|
+
if (prettyIndent === false) return `[${outputElem.join(",")}]`;
|
|
51
|
+
return `[\n${outputElem.map(indenter(indentStep(prettyIndent))).map((c, id) => id === outputElem.length - 1 ? `${c}\n` : `${c},\n`).join("")}]`;
|
|
52
|
+
};
|
|
53
|
+
const utf8Sort = (a, b) => {
|
|
54
|
+
const maxLen = Math.max(a.length, b.length);
|
|
55
|
+
for (let cursor = 0; cursor < maxLen; ++cursor) {
|
|
56
|
+
const aCode = a.charCodeAt(cursor);
|
|
57
|
+
const bCode = b.charCodeAt(cursor);
|
|
58
|
+
if (isNaN(aCode) || aCode < bCode) return -1;
|
|
59
|
+
if (isNaN(bCode) || aCode > bCode) return 1;
|
|
60
|
+
}
|
|
61
|
+
return 0;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Do a JSON stringify operation with optional canonization of properties' order.
|
|
65
|
+
*
|
|
66
|
+
* @param order - Sort all properties using their UTF-8 point value.
|
|
67
|
+
* This is roughly alphabetical order when there are no non-ASCII characters.
|
|
68
|
+
*
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
export const jsonStringify = (source, order = false, prettyIndent = false) => {
|
|
72
|
+
if (!order) {
|
|
73
|
+
if (prettyIndent) {
|
|
74
|
+
return JSON.stringify(source, null, typeof prettyIndent === "boolean" ? JSON_DEFAULT_INDENT : prettyIndent);
|
|
75
|
+
}
|
|
76
|
+
return JSON.stringify(source);
|
|
77
|
+
}
|
|
78
|
+
if (Array.isArray(source)) return processArray(source, prettyIndent);
|
|
79
|
+
const sourceType = typeof source;
|
|
80
|
+
if (["string", "number", "boolean"].includes(sourceType) || source === null) {
|
|
81
|
+
return JSON.stringify(source);
|
|
82
|
+
}
|
|
83
|
+
if (sourceType !== "object") throw new Error("Invalid object input");
|
|
84
|
+
const casted = source;
|
|
85
|
+
const outputElem = [];
|
|
86
|
+
for (const key of Object.keys(casted).sort(utf8Sort)) {
|
|
87
|
+
const value = casted[key];
|
|
88
|
+
if (value === undefined) continue;
|
|
89
|
+
if (prettyIndent === false) {
|
|
90
|
+
outputElem.push(`${JSON.stringify(key)}:${jsonStringify(value, true, prettyIndent)}`);
|
|
91
|
+
} else {
|
|
92
|
+
outputElem.push(`${JSON.stringify(key)}: ${jsonStringify(value, true, prettyIndent)}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (outputElem.length === 0) return "{}";
|
|
96
|
+
if (prettyIndent === false) return `{${outputElem.join(",")}}`;
|
|
97
|
+
return `{\n${outputElem.map(indenter(indentStep(prettyIndent))).map((c, id) => id === outputElem.length - 1 ? `${c}\n` : `${c},\n`).join("")}}`;
|
|
98
|
+
};
|