@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.
Files changed (183) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +105 -0
  3. package/lib/array.d.ts +28 -0
  4. package/lib/array.js +36 -0
  5. package/lib/arraybuffer.d.ts +138 -0
  6. package/lib/arraybuffer.js +141 -0
  7. package/lib/async/asynctrigger.d.ts +50 -0
  8. package/lib/async/asynctrigger.js +108 -0
  9. package/lib/async/deferredpromise.d.ts +32 -0
  10. package/lib/async/deferredpromise.js +66 -0
  11. package/lib/async/keycache.d.ts +56 -0
  12. package/lib/async/keycache.js +103 -0
  13. package/lib/async/queues.d.ts +69 -0
  14. package/lib/async/queues.js +135 -0
  15. package/lib/async/timecache.d.ts +58 -0
  16. package/lib/async/timecache.js +118 -0
  17. package/lib/base58.d.ts +27 -0
  18. package/lib/base58.js +83 -0
  19. package/lib/base64.d.ts +51 -0
  20. package/lib/base64.js +126 -0
  21. package/lib/benchmark.d.ts +126 -0
  22. package/lib/benchmark.js +177 -0
  23. package/lib/bits/arraybuffer.d.ts +35 -0
  24. package/lib/bits/arraybuffer.js +64 -0
  25. package/lib/bits/base64.d.ts +35 -0
  26. package/lib/bits/base64.js +70 -0
  27. package/lib/bits/hex.d.ts +17 -0
  28. package/lib/bits/hex.js +30 -0
  29. package/lib/bits/uint8array.d.ts +28 -0
  30. package/lib/bits/uint8array.js +42 -0
  31. package/lib/bytebuffer.d.ts +27 -0
  32. package/lib/bytebuffer.js +29 -0
  33. package/lib/consts.d.ts +33 -0
  34. package/lib/consts.js +33 -0
  35. package/lib/cron/logger.d.ts +22 -0
  36. package/lib/cron/logger.js +31 -0
  37. package/lib/cron/scheduledtask.d.ts +71 -0
  38. package/lib/cron/scheduledtask.js +137 -0
  39. package/lib/cron/types.d.ts +53 -0
  40. package/lib/cron/types.js +31 -0
  41. package/lib/cron.d.ts +29 -0
  42. package/lib/cron.js +47 -0
  43. package/lib/dict.d.ts +56 -0
  44. package/lib/dict.js +74 -0
  45. package/lib/error.d.ts +25 -0
  46. package/lib/error.js +41 -0
  47. package/lib/global.d.ts +27 -0
  48. package/lib/global.js +53 -0
  49. package/lib/hex.d.ts +32 -0
  50. package/lib/hex.js +58 -0
  51. package/lib/idx.d.ts +51 -0
  52. package/lib/idx.js +81 -0
  53. package/lib/json.d.ts +57 -0
  54. package/lib/json.js +116 -0
  55. package/lib/marshalling/marshaller.d.ts +51 -0
  56. package/lib/marshalling/marshaller.js +155 -0
  57. package/lib/marshalling/unmarshaller.d.ts +53 -0
  58. package/lib/marshalling/unmarshaller.js +124 -0
  59. package/lib/marshalling/util.d.ts +25 -0
  60. package/lib/marshalling/util.js +25 -0
  61. package/lib/number.d.ts +17 -0
  62. package/lib/number.js +21 -0
  63. package/lib/path.d.ts +25 -0
  64. package/lib/path.js +29 -0
  65. package/lib/promise.d.ts +42 -0
  66. package/lib/promise.js +78 -0
  67. package/lib/starttime.d.ts +23 -0
  68. package/lib/starttime.js +29 -0
  69. package/lib/string.d.ts +65 -0
  70. package/lib/string.js +108 -0
  71. package/lib/types/array.d.ts +34 -0
  72. package/lib/types/array.js +64 -0
  73. package/lib/types/enum.d.ts +30 -0
  74. package/lib/types/enum.js +44 -0
  75. package/lib/types/predicateerror.d.ts +40 -0
  76. package/lib/types/predicateerror.js +107 -0
  77. package/lib/types/primitive.d.ts +23 -0
  78. package/lib/types/primitive.js +34 -0
  79. package/lib/types/record.d.ts +67 -0
  80. package/lib/types/record.js +235 -0
  81. package/lib/types/types.d.ts +64 -0
  82. package/lib/types/types.js +115 -0
  83. package/lib/types/utils.d.ts +18 -0
  84. package/lib/types/utils.js +67 -0
  85. package/lib/uint8array.d.ts +176 -0
  86. package/lib/uint8array.js +438 -0
  87. package/lib/units.d.ts +159 -0
  88. package/lib/units.js +290 -0
  89. package/lib/utils/buffer.d.ts +49 -0
  90. package/lib/utils/buffer.js +79 -0
  91. package/lib/utils/fourbytes.d.ts +29 -0
  92. package/lib/utils/fourbytes.js +45 -0
  93. package/package.json +1 -0
  94. package/web/array.d.ts +28 -0
  95. package/web/array.js +34 -0
  96. package/web/arraybuffer.d.ts +138 -0
  97. package/web/arraybuffer.js +141 -0
  98. package/web/async/asynctrigger.d.ts +50 -0
  99. package/web/async/asynctrigger.js +106 -0
  100. package/web/async/deferredpromise.d.ts +32 -0
  101. package/web/async/deferredpromise.js +65 -0
  102. package/web/async/keycache.d.ts +56 -0
  103. package/web/async/keycache.js +97 -0
  104. package/web/async/queues.d.ts +69 -0
  105. package/web/async/queues.js +131 -0
  106. package/web/async/timecache.d.ts +58 -0
  107. package/web/async/timecache.js +107 -0
  108. package/web/base58.d.ts +27 -0
  109. package/web/base58.js +78 -0
  110. package/web/base64.d.ts +51 -0
  111. package/web/base64.js +136 -0
  112. package/web/benchmark.d.ts +126 -0
  113. package/web/benchmark.js +183 -0
  114. package/web/bits/arraybuffer.d.ts +35 -0
  115. package/web/bits/arraybuffer.js +59 -0
  116. package/web/bits/base64.d.ts +35 -0
  117. package/web/bits/base64.js +67 -0
  118. package/web/bits/hex.d.ts +17 -0
  119. package/web/bits/hex.js +27 -0
  120. package/web/bits/uint8array.d.ts +28 -0
  121. package/web/bits/uint8array.js +41 -0
  122. package/web/bytebuffer.d.ts +27 -0
  123. package/web/bytebuffer.js +29 -0
  124. package/web/consts.d.ts +33 -0
  125. package/web/consts.js +33 -0
  126. package/web/cron/logger.d.ts +22 -0
  127. package/web/cron/logger.js +30 -0
  128. package/web/cron/scheduledtask.d.ts +71 -0
  129. package/web/cron/scheduledtask.js +136 -0
  130. package/web/cron/types.d.ts +53 -0
  131. package/web/cron/types.js +31 -0
  132. package/web/cron.d.ts +29 -0
  133. package/web/cron.js +47 -0
  134. package/web/dict.d.ts +56 -0
  135. package/web/dict.js +67 -0
  136. package/web/error.d.ts +25 -0
  137. package/web/error.js +39 -0
  138. package/web/global.d.ts +27 -0
  139. package/web/global.js +49 -0
  140. package/web/hex.d.ts +32 -0
  141. package/web/hex.js +52 -0
  142. package/web/idx.d.ts +51 -0
  143. package/web/idx.js +76 -0
  144. package/web/json.d.ts +57 -0
  145. package/web/json.js +98 -0
  146. package/web/marshalling/marshaller.d.ts +51 -0
  147. package/web/marshalling/marshaller.js +150 -0
  148. package/web/marshalling/unmarshaller.d.ts +53 -0
  149. package/web/marshalling/unmarshaller.js +115 -0
  150. package/web/marshalling/util.d.ts +25 -0
  151. package/web/marshalling/util.js +25 -0
  152. package/web/number.d.ts +17 -0
  153. package/web/number.js +21 -0
  154. package/web/path.d.ts +25 -0
  155. package/web/path.js +26 -0
  156. package/web/promise.d.ts +42 -0
  157. package/web/promise.js +74 -0
  158. package/web/starttime.d.ts +23 -0
  159. package/web/starttime.js +29 -0
  160. package/web/string.d.ts +65 -0
  161. package/web/string.js +101 -0
  162. package/web/types/array.d.ts +34 -0
  163. package/web/types/array.js +63 -0
  164. package/web/types/enum.d.ts +30 -0
  165. package/web/types/enum.js +40 -0
  166. package/web/types/predicateerror.d.ts +40 -0
  167. package/web/types/predicateerror.js +128 -0
  168. package/web/types/primitive.d.ts +23 -0
  169. package/web/types/primitive.js +33 -0
  170. package/web/types/record.d.ts +67 -0
  171. package/web/types/record.js +213 -0
  172. package/web/types/types.d.ts +64 -0
  173. package/web/types/types.js +123 -0
  174. package/web/types/utils.d.ts +18 -0
  175. package/web/types/utils.js +30 -0
  176. package/web/uint8array.d.ts +176 -0
  177. package/web/uint8array.js +412 -0
  178. package/web/units.d.ts +159 -0
  179. package/web/units.js +312 -0
  180. package/web/utils/buffer.d.ts +49 -0
  181. package/web/utils/buffer.js +76 -0
  182. package/web/utils/fourbytes.d.ts +29 -0
  183. package/web/utils/fourbytes.js +45 -0
@@ -0,0 +1,53 @@
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
+ * Function to execute a single task
18
+ *
19
+ * If it returns a promise, the task is considerred running as long as the
20
+ * promise didn't complete
21
+ */
22
+ export type TaskFunction = () => Promise<void> | void;
23
+ export type LoggerFunction = (error: Error) => void;
24
+ /**
25
+ * Behavior when a task is scheduled to start while it's already running
26
+ */
27
+ export declare enum Overrun {
28
+ /**
29
+ * Skip this start
30
+ */
31
+ SKIP = "skip",
32
+ /**
33
+ * Immediately restart the task once it finish
34
+ */
35
+ AFTER = "after"
36
+ }
37
+ /**
38
+ * Options when scheduling a task
39
+ */
40
+ export interface TaskOptions {
41
+ /**
42
+ * Behavior when the task is scheduled to restart before it have completed.
43
+ *
44
+ * Default to IGNORE when scheduling a task.
45
+ */
46
+ overrun?: Overrun;
47
+ }
48
+ /**
49
+ * Same as TaskOptions, but all members are required
50
+ */
51
+ export interface CompleteTaskOptions {
52
+ overrun: Overrun;
53
+ }
@@ -0,0 +1,31 @@
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
+ * Behavior when a task is scheduled to start while it's already running
18
+ */
19
+ export var Overrun;
20
+ (function (Overrun) {
21
+ /**
22
+ * Skip this start
23
+ */
24
+ // eslint-disable-next-line @typescript-eslint/naming-convention
25
+ Overrun["SKIP"] = "skip";
26
+ /**
27
+ * Immediately restart the task once it finish
28
+ */
29
+ // eslint-disable-next-line @typescript-eslint/naming-convention
30
+ Overrun["AFTER"] = "after";
31
+ })(Overrun || (Overrun = {}));
package/lib/cron.d.ts ADDED
@@ -0,0 +1,29 @@
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 ScheduledTask from "./cron/scheduledtask.js";
17
+ import { TaskFunction, TaskOptions, Overrun, CompleteTaskOptions } from "./cron/types.js";
18
+ export { Overrun };
19
+ export type { ScheduledTask, TaskFunction, TaskOptions, CompleteTaskOptions };
20
+ export declare const setLoggerFunction: (logFunc?: import("./cron/types.js").LoggerFunction) => void;
21
+ /**
22
+ * Schedule a task for execution
23
+ *
24
+ * @param cronDefinition - CRON timing for the task
25
+ * @param task - The task function to run at the scheduled time
26
+ * @param taskOptions - The behavior of the task
27
+ */
28
+ export declare const schedule: (cronDefinition: string, task: TaskFunction, taskOptions?: TaskOptions) => ScheduledTask;
29
+ export declare const stop: () => void;
package/lib/cron.js ADDED
@@ -0,0 +1,47 @@
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 { setLoggerFunction as bitsSetLoggerFunction } from "./cron/logger.js";
17
+ import ScheduledTask from "./cron/scheduledtask.js";
18
+ import { Overrun } from "./cron/types.js";
19
+ const scheduledTasks = [];
20
+ export { Overrun };
21
+ export const setLoggerFunction = bitsSetLoggerFunction;
22
+ /**
23
+ * Set default values for task options
24
+ */
25
+ const getDefaultTaskOptions = (taskOptions) => {
26
+ const result = taskOptions ?? {};
27
+ return {
28
+ overrun: Overrun.SKIP,
29
+ ...result,
30
+ };
31
+ };
32
+ /**
33
+ * Schedule a task for execution
34
+ *
35
+ * @param cronDefinition - CRON timing for the task
36
+ * @param task - The task function to run at the scheduled time
37
+ * @param taskOptions - The behavior of the task
38
+ */
39
+ export const schedule = (cronDefinition, task, taskOptions) => {
40
+ const newTask = new ScheduledTask(cronDefinition, task, getDefaultTaskOptions(taskOptions));
41
+ scheduledTasks.push(newTask);
42
+ return newTask;
43
+ };
44
+ export const stop = () => {
45
+ scheduledTasks.forEach((task) => task.cancel());
46
+ scheduledTasks.length = 0;
47
+ };
package/lib/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/lib/dict.js ADDED
@@ -0,0 +1,74 @@
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
31
+ ? (Object.keys(secondary).forEach((propName) => {
32
+ primary[propName] = copier(secondary[propName]);
33
+ }),
34
+ primary)
35
+ : Object.assign(primary, secondary);
36
+ /**
37
+ * Merge two dictionaries.
38
+ *
39
+ * If both op1 and op2 have properties with the same key, values from op2 will erase values from
40
+ * op1.
41
+ *
42
+ * @param copier - Function to create copy of each property values.
43
+ * By default create a shallow copy.
44
+ * The function accept one argument: the value to copy.
45
+ *
46
+ * @public
47
+ */
48
+ export const merge = (op1, op2, copier) => update(update({}, op1, copier), op2, copier);
49
+ /**
50
+ * Perform a deep copy of a dictionary with only primitive types (string, number, boolean) and other
51
+ * objects and arrays of such types.
52
+ *
53
+ * This is not strongly typed because lazy.
54
+ *
55
+ * @public
56
+ */
57
+ export const deepCopyPrimitive = (source) => {
58
+ if (typeof source === "string")
59
+ return source;
60
+ if (typeof source === "number")
61
+ return source;
62
+ if (typeof source === "boolean")
63
+ return source;
64
+ if (Array.isArray(source))
65
+ return source.map(deepCopyPrimitive);
66
+ if (typeof source === "object" && source) {
67
+ const res = {};
68
+ const rec = source;
69
+ for (const key of Object.keys(rec)) {
70
+ res[key] = deepCopyPrimitive(rec[key]);
71
+ }
72
+ return res;
73
+ }
74
+ };
package/lib/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/lib/error.js ADDED
@@ -0,0 +1,41 @@
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)
27
+ return e;
28
+ if (typeof e === "string")
29
+ return new Error(e);
30
+ const cast = e;
31
+ if (cast.toString && typeof cast.toString === "function") {
32
+ const str = cast.toString();
33
+ if (str !== "[object Object]")
34
+ return new Error(cast.toString());
35
+ }
36
+ try {
37
+ return new Error(JSON.stringify(e));
38
+ }
39
+ catch { }
40
+ return new Error("Unknown error", { cause: e });
41
+ };
@@ -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/lib/global.js ADDED
@@ -0,0 +1,53 @@
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")
18
+ return window;
19
+ if (typeof global !== "undefined")
20
+ return global;
21
+ throw new Error("No global object available");
22
+ };
23
+ const globalKey = "kx_global_holder";
24
+ /**
25
+ * Retrieve a value stored at a global level in the current execution environment.
26
+ *
27
+ * @public
28
+ */
29
+ export const getGlobalValue = (key) => {
30
+ const globalObject = getGlobalObject();
31
+ if (!(globalKey in globalObject))
32
+ return;
33
+ const globalHolder = globalObject[globalKey];
34
+ return globalHolder[key];
35
+ };
36
+ /**
37
+ * Store a value at a global level in the current execution environment.
38
+ *
39
+ * @public
40
+ */
41
+ export const setGlobalValue = (key, value) => {
42
+ const globalObject = getGlobalObject();
43
+ // eslint-disable-next-line @typescript-eslint/init-declarations
44
+ let globalHolder;
45
+ if (globalKey in globalObject) {
46
+ globalHolder = globalObject[globalKey];
47
+ }
48
+ else {
49
+ globalHolder = {};
50
+ globalObject[globalKey] = globalHolder;
51
+ }
52
+ globalHolder[key] = value;
53
+ };
package/lib/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/lib/hex.js ADDED
@@ -0,0 +1,58 @@
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";
17
+ import { OutputFormat } from "@keeex/bubble_babble";
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)
25
+ return value.slice(HEX_PREFIX.length);
26
+ if (!value.startsWith(HEX_PREFIX) && prefix)
27
+ return `0x${value}`;
28
+ return value;
29
+ };
30
+ /** Check that a string is an hexadecimal string */
31
+ export const isHexString = (value) => /^(?:0x)?[0-9a-fA-F]+$/u.test(value);
32
+ /**
33
+ * Convert source data to hexadecimal representation.
34
+ *
35
+ * @param src - The source value.
36
+ * Accepts many type of input, handled in reasonable ways:
37
+ *
38
+ * - `number` and `bigint`: converted to their hexadecimal representation
39
+ * - `string`: can be either an hexadecimal, base64, or IDX string
40
+ * - `Uint8Array` and `Array<number>`: converted to the hexadecimal representation of their content
41
+ *
42
+ * @param withPrefix - Whether to prefix the result with `0x`.
43
+ */
44
+ export const toHex = (src, withPrefix = false) => {
45
+ if (typeof src === "number" || typeof src === "bigint") {
46
+ const bi = BigInt(src);
47
+ return prependPrefix(bi.toString(HEXA_BASE), withPrefix);
48
+ }
49
+ if (src instanceof Uint8Array)
50
+ return buf82hex(src, withPrefix);
51
+ if (Array.isArray(src))
52
+ return buf82hex(new Uint8Array(src), withPrefix);
53
+ if (check(src))
54
+ return toHex(bubble.decode(src, OutputFormat.Uint8Array), withPrefix);
55
+ if (isHexString(src))
56
+ return prependPrefix(src, withPrefix);
57
+ return toHex(b642buf8(src, true), withPrefix);
58
+ };
package/lib/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/lib/idx.js ADDED
@@ -0,0 +1,81 @@
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";
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) &&
31
+ bubbleDecode(changeExtremNoCheck(candidate, "x")).byteLength === SHA256_BYTE_LENGTH);
32
+ }
33
+ catch {
34
+ return false;
35
+ }
36
+ };
37
+ /**
38
+ * Raise an exception if the value isn't a valid IDX.
39
+ *
40
+ * @public
41
+ */
42
+ export const checkException = (candidate) => {
43
+ if (!check(candidate))
44
+ throw new Error(`Invalid IDX: "${candidate}"`);
45
+ };
46
+ /**
47
+ * Change first and last letter of an IDX.
48
+ *
49
+ * @public
50
+ */
51
+ export const changeExtrem = (candidate, extrems = "x") => {
52
+ if (extrems.length !== 1)
53
+ throw new Error(`Invalid extrems value "${extrems}"`);
54
+ checkException(candidate);
55
+ return changeExtremNoCheck(candidate, extrems);
56
+ };
57
+ /**
58
+ * Split an IDX into its words.
59
+ *
60
+ * @public
61
+ */
62
+ export const split = (idx) => {
63
+ checkException(idx);
64
+ return idx.split("-");
65
+ };
66
+ const IDX_WORDS_COUNT = 17;
67
+ /**
68
+ * Return the head of an IDX.
69
+ *
70
+ * @param count - Number of words to include
71
+ *
72
+ * @param ellipsis - Append an ellipsis at the end
73
+ *
74
+ * @public
75
+ */
76
+ export const head = (idx, count, ellipsis = false) => {
77
+ if (count >= IDX_WORDS_COUNT)
78
+ return idx;
79
+ const words = split(idx).slice(0, count);
80
+ return `${words.join("-")}${ellipsis ? "-…" : ""}`;
81
+ };