@stryke/helpers 0.9.34 → 0.9.36
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/CHANGELOG.md +16 -0
- package/dist/arg-identity.cjs +17 -1
- package/dist/arg-identity.mjs +16 -1
- package/dist/arg-identity.mjs.map +1 -1
- package/dist/convert/src/to-string-key.cjs +15 -1
- package/dist/convert/src/to-string-key.mjs +14 -1
- package/dist/convert/src/to-string-key.mjs.map +1 -1
- package/dist/debounce.cjs +62 -1
- package/dist/debounce.mjs +61 -1
- package/dist/debounce.mjs.map +1 -1
- package/dist/deep-clone.cjs +113 -1
- package/dist/deep-clone.mjs +113 -1
- package/dist/deep-clone.mjs.map +1 -1
- package/dist/deep-merge.cjs +60 -1
- package/dist/deep-merge.mjs +60 -1
- package/dist/deep-merge.mjs.map +1 -1
- package/dist/delay.cjs +90 -1
- package/dist/delay.mjs +89 -1
- package/dist/delay.mjs.map +1 -1
- package/dist/errors.cjs +24 -1
- package/dist/errors.mjs +22 -1
- package/dist/errors.mjs.map +1 -1
- package/dist/filter-empty.cjs +7 -1
- package/dist/filter-empty.mjs +7 -1
- package/dist/filter-empty.mjs.map +1 -1
- package/dist/flatten-object.cjs +48 -1
- package/dist/flatten-object.mjs +48 -1
- package/dist/flatten-object.mjs.map +1 -1
- package/dist/get-field.cjs +54 -1
- package/dist/get-field.mjs +54 -1
- package/dist/get-field.mjs.map +1 -1
- package/dist/get-ordered-by.cjs +54 -1
- package/dist/get-ordered-by.mjs +53 -1
- package/dist/get-ordered-by.mjs.map +1 -1
- package/dist/get-unique.cjs +37 -1
- package/dist/get-unique.mjs +35 -1
- package/dist/get-unique.mjs.map +1 -1
- package/dist/identity.cjs +21 -1
- package/dist/identity.mjs +20 -1
- package/dist/identity.mjs.map +1 -1
- package/dist/index.cjs +71 -1
- package/dist/index.mjs +33 -1
- package/dist/is-equal.cjs +87 -1
- package/dist/is-equal.mjs +87 -1
- package/dist/is-equal.mjs.map +1 -1
- package/dist/lru-cache.cjs +190 -1
- package/dist/lru-cache.mjs +189 -1
- package/dist/lru-cache.mjs.map +1 -1
- package/dist/match-sorter.cjs +264 -1
- package/dist/match-sorter.mjs +262 -1
- package/dist/match-sorter.mjs.map +1 -1
- package/dist/memoize.cjs +20 -1
- package/dist/memoize.mjs +19 -1
- package/dist/memoize.mjs.map +1 -1
- package/dist/mutex.cjs +80 -1
- package/dist/mutex.mjs +80 -1
- package/dist/mutex.mjs.map +1 -1
- package/dist/noop.cjs +24 -1
- package/dist/noop.mjs +22 -1
- package/dist/noop.mjs.map +1 -1
- package/dist/omit.cjs +30 -1
- package/dist/omit.mjs +29 -1
- package/dist/omit.mjs.map +1 -1
- package/dist/once.cjs +27 -1
- package/dist/once.mjs +25 -1
- package/dist/once.mjs.map +1 -1
- package/dist/pick.cjs +30 -1
- package/dist/pick.mjs +29 -1
- package/dist/pick.mjs.map +1 -1
- package/dist/remove-accents.cjs +416 -1
- package/dist/remove-accents.mjs +414 -1
- package/dist/remove-accents.mjs.map +1 -1
- package/dist/remove-empty-items.cjs +12 -1
- package/dist/remove-empty-items.mjs +11 -1
- package/dist/remove-empty-items.mjs.map +1 -1
- package/dist/semaphore.cjs +105 -1
- package/dist/semaphore.mjs +104 -1
- package/dist/semaphore.mjs.map +1 -1
- package/dist/set-field.cjs +30 -1
- package/dist/set-field.mjs +30 -1
- package/dist/set-field.mjs.map +1 -1
- package/dist/throttle.cjs +43 -1
- package/dist/throttle.mjs +42 -1
- package/dist/throttle.mjs.map +1 -1
- package/dist/timeout.cjs +18 -1
- package/dist/timeout.mjs +18 -1
- package/dist/timeout.mjs.map +1 -1
- package/dist/to-deep-key.cjs +54 -1
- package/dist/to-deep-key.mjs +53 -1
- package/dist/to-deep-key.mjs.map +1 -1
- package/dist/to-path.cjs +43 -1
- package/dist/to-path.mjs +42 -1
- package/dist/to-path.mjs.map +1 -1
- package/dist/type-checks/src/get-object-tag.cjs +15 -1
- package/dist/type-checks/src/get-object-tag.mjs +14 -1
- package/dist/type-checks/src/get-object-tag.mjs.map +1 -1
- package/dist/type-checks/src/is-deep-key.cjs +28 -1
- package/dist/type-checks/src/is-deep-key.mjs +27 -1
- package/dist/type-checks/src/is-deep-key.mjs.map +1 -1
- package/dist/type-checks/src/is-empty.cjs +20 -1
- package/dist/type-checks/src/is-empty.mjs +20 -1
- package/dist/type-checks/src/is-empty.mjs.map +1 -1
- package/dist/type-checks/src/is-function.cjs +25 -1
- package/dist/type-checks/src/is-function.mjs +25 -1
- package/dist/type-checks/src/is-function.mjs.map +1 -1
- package/dist/type-checks/src/is-mergeable-object.cjs +14 -1
- package/dist/type-checks/src/is-mergeable-object.mjs +14 -1
- package/dist/type-checks/src/is-mergeable-object.mjs.map +1 -1
- package/dist/type-checks/src/is-non-null-object.cjs +16 -1
- package/dist/type-checks/src/is-non-null-object.mjs +16 -1
- package/dist/type-checks/src/is-non-null-object.mjs.map +1 -1
- package/dist/type-checks/src/is-null.cjs +12 -1
- package/dist/type-checks/src/is-null.mjs +11 -1
- package/dist/type-checks/src/is-null.mjs.map +1 -1
- package/dist/type-checks/src/is-number.cjs +18 -1
- package/dist/type-checks/src/is-number.mjs +17 -1
- package/dist/type-checks/src/is-number.mjs.map +1 -1
- package/dist/type-checks/src/is-object-index.cjs +19 -1
- package/dist/type-checks/src/is-object-index.mjs +18 -1
- package/dist/type-checks/src/is-object-index.mjs.map +1 -1
- package/dist/type-checks/src/is-object.cjs +19 -1
- package/dist/type-checks/src/is-object.mjs +19 -1
- package/dist/type-checks/src/is-object.mjs.map +1 -1
- package/dist/type-checks/src/is-plain-object.cjs +63 -1
- package/dist/type-checks/src/is-plain-object.mjs +63 -1
- package/dist/type-checks/src/is-plain-object.mjs.map +1 -1
- package/dist/type-checks/src/is-primitive.cjs +12 -1
- package/dist/type-checks/src/is-primitive.mjs +11 -1
- package/dist/type-checks/src/is-primitive.mjs.map +1 -1
- package/dist/type-checks/src/is-react-element.cjs +8 -1
- package/dist/type-checks/src/is-react-element.mjs +7 -1
- package/dist/type-checks/src/is-react-element.mjs.map +1 -1
- package/dist/type-checks/src/is-set-string.cjs +20 -1
- package/dist/type-checks/src/is-set-string.mjs +20 -1
- package/dist/type-checks/src/is-set-string.mjs.map +1 -1
- package/dist/type-checks/src/is-set.cjs +19 -1
- package/dist/type-checks/src/is-set.mjs +19 -1
- package/dist/type-checks/src/is-set.mjs.map +1 -1
- package/dist/type-checks/src/is-string.cjs +12 -1
- package/dist/type-checks/src/is-string.mjs +11 -1
- package/dist/type-checks/src/is-string.mjs.map +1 -1
- package/dist/type-checks/src/is-typed-array.cjs +8 -1
- package/dist/type-checks/src/is-typed-array.mjs +7 -1
- package/dist/type-checks/src/is-typed-array.mjs.map +1 -1
- package/dist/type-checks/src/is-undefined.cjs +8 -1
- package/dist/type-checks/src/is-undefined.mjs +7 -1
- package/dist/type-checks/src/is-undefined.mjs.map +1 -1
- package/dist/type-checks/src/property-exists.cjs +30 -1
- package/dist/type-checks/src/property-exists.mjs +30 -1
- package/dist/type-checks/src/property-exists.mjs.map +1 -1
- package/dist/unflatten-object.cjs +38 -1
- package/dist/unflatten-object.mjs +38 -1
- package/dist/unflatten-object.mjs.map +1 -1
- package/dist/union.cjs +28 -1
- package/dist/union.mjs +28 -1
- package/dist/union.mjs.map +1 -1
- package/dist/with-timeout.cjs +28 -1
- package/dist/with-timeout.mjs +28 -1
- package/dist/with-timeout.mjs.map +1 -1
- package/package.json +2 -2
package/dist/semaphore.mjs
CHANGED
|
@@ -1,2 +1,105 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/semaphore.ts
|
|
2
|
+
/**
|
|
3
|
+
* A counting semaphore for async functions that manages available permits.
|
|
4
|
+
* Semaphores are mainly used to limit the number of concurrent async tasks.
|
|
5
|
+
*
|
|
6
|
+
* Each `acquire` operation takes a permit or waits until one is available.
|
|
7
|
+
* Each `release` operation adds a permit, potentially allowing a waiting task to proceed.
|
|
8
|
+
*
|
|
9
|
+
* The semaphore ensures fairness by maintaining a FIFO (First In, First Out) order for acquirers.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const sema = new Semaphore(2);
|
|
13
|
+
*
|
|
14
|
+
* async function task() {
|
|
15
|
+
* await sema.acquire();
|
|
16
|
+
* try {
|
|
17
|
+
* // This code can only be executed by two tasks at the same time
|
|
18
|
+
* } finally {
|
|
19
|
+
* sema.release();
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* task();
|
|
24
|
+
* task();
|
|
25
|
+
* task(); // This task will wait until one of the previous tasks releases the semaphore.
|
|
26
|
+
*/
|
|
27
|
+
var Semaphore = class {
|
|
28
|
+
/**
|
|
29
|
+
* The maximum number of concurrent operations allowed.
|
|
30
|
+
*/
|
|
31
|
+
capacity;
|
|
32
|
+
/**
|
|
33
|
+
* The number of available permits.
|
|
34
|
+
*/
|
|
35
|
+
available;
|
|
36
|
+
deferredTasks = [];
|
|
37
|
+
/**
|
|
38
|
+
* Creates an instance of Semaphore.
|
|
39
|
+
* @param capacity - The maximum number of concurrent operations allowed.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const sema = new Semaphore(3); // Allows up to 3 concurrent operations.
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
constructor(capacity) {
|
|
47
|
+
this.capacity = capacity;
|
|
48
|
+
this.available = capacity;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Acquires a semaphore, blocking if necessary until one is available.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const sema = new Semaphore(1);
|
|
56
|
+
*
|
|
57
|
+
* async function criticalSection() {
|
|
58
|
+
* await sema.acquire();
|
|
59
|
+
* try {
|
|
60
|
+
* // This code section cannot be executed simultaneously
|
|
61
|
+
* } finally {
|
|
62
|
+
* sema.release();
|
|
63
|
+
* }
|
|
64
|
+
* }
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* @returns A promise that resolves when the semaphore is acquired.
|
|
68
|
+
*/
|
|
69
|
+
async acquire() {
|
|
70
|
+
if (this.available > 0) {
|
|
71
|
+
this.available--;
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
return new Promise((resolve) => {
|
|
75
|
+
this.deferredTasks.push(resolve);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Releases a semaphore, allowing one more operation to proceed.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* const sema = new Semaphore(1);
|
|
83
|
+
*
|
|
84
|
+
* async function task() {
|
|
85
|
+
* await sema.acquire();
|
|
86
|
+
* try {
|
|
87
|
+
* // This code can only be executed by two tasks at the same time
|
|
88
|
+
* } finally {
|
|
89
|
+
* sema.release(); // Allows another waiting task to proceed.
|
|
90
|
+
* }
|
|
91
|
+
* }
|
|
92
|
+
*/
|
|
93
|
+
release() {
|
|
94
|
+
const deferredTask = this.deferredTasks.shift();
|
|
95
|
+
if (deferredTask != null) {
|
|
96
|
+
deferredTask();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (this.available < this.capacity) this.available++;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
//#endregion
|
|
104
|
+
export { Semaphore };
|
|
2
105
|
//# sourceMappingURL=semaphore.mjs.map
|
package/dist/semaphore.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"semaphore.mjs","names":[],"sources":["../src/semaphore.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 License, and is\n free for commercial and private use. For more information, please visit\n our licensing page.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://stormsoftware.com/projects/stryke/docs\n Contact: https://stormsoftware.com/contact\n License: https://stormsoftware.com/projects/stryke/license\n\n ------------------------------------------------------------------- */\n\n/**\n * A counting semaphore for async functions that manages available permits.\n * Semaphores are mainly used to limit the number of concurrent async tasks.\n *\n * Each `acquire` operation takes a permit or waits until one is available.\n * Each `release` operation adds a permit, potentially allowing a waiting task to proceed.\n *\n * The semaphore ensures fairness by maintaining a FIFO (First In, First Out) order for acquirers.\n *\n * @example\n * const sema = new Semaphore(2);\n *\n * async function task() {\n * await sema.acquire();\n * try {\n * // This code can only be executed by two tasks at the same time\n * } finally {\n * sema.release();\n * }\n * }\n *\n * task();\n * task();\n * task(); // This task will wait until one of the previous tasks releases the semaphore.\n */\nexport class Semaphore {\n /**\n * The maximum number of concurrent operations allowed.\n */\n public capacity: number;\n\n /**\n * The number of available permits.\n */\n public available: number;\n\n private deferredTasks: Array<() => void> = [];\n\n /**\n * Creates an instance of Semaphore.\n * @param capacity - The maximum number of concurrent operations allowed.\n *\n * @example\n * ```ts\n * const sema = new Semaphore(3); // Allows up to 3 concurrent operations.\n * ```\n */\n constructor(capacity: number) {\n this.capacity = capacity;\n this.available = capacity;\n }\n\n /**\n * Acquires a semaphore, blocking if necessary until one is available.\n *\n * @example\n * ```ts\n * const sema = new Semaphore(1);\n *\n * async function criticalSection() {\n * await sema.acquire();\n * try {\n * // This code section cannot be executed simultaneously\n * } finally {\n * sema.release();\n * }\n * }\n * ```\n *\n * @returns A promise that resolves when the semaphore is acquired.\n */\n async acquire(): Promise<void> {\n if (this.available > 0) {\n this.available--;\n return;\n }\n\n return new Promise<void>(resolve => {\n this.deferredTasks.push(resolve);\n });\n }\n\n /**\n * Releases a semaphore, allowing one more operation to proceed.\n *\n * @example\n * const sema = new Semaphore(1);\n *\n * async function task() {\n * await sema.acquire();\n * try {\n * // This code can only be executed by two tasks at the same time\n * } finally {\n * sema.release(); // Allows another waiting task to proceed.\n * }\n * }\n */\n release(): void {\n const deferredTask = this.deferredTasks.shift();\n\n if (deferredTask != null) {\n deferredTask();\n return;\n }\n\n if (this.available < this.capacity) {\n this.available++;\n }\n }\n}\n"],"mappings":"AA0CA,IAAa,
|
|
1
|
+
{"version":3,"file":"semaphore.mjs","names":[],"sources":["../src/semaphore.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 License, and is\n free for commercial and private use. For more information, please visit\n our licensing page.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://stormsoftware.com/projects/stryke/docs\n Contact: https://stormsoftware.com/contact\n License: https://stormsoftware.com/projects/stryke/license\n\n ------------------------------------------------------------------- */\n\n/**\n * A counting semaphore for async functions that manages available permits.\n * Semaphores are mainly used to limit the number of concurrent async tasks.\n *\n * Each `acquire` operation takes a permit or waits until one is available.\n * Each `release` operation adds a permit, potentially allowing a waiting task to proceed.\n *\n * The semaphore ensures fairness by maintaining a FIFO (First In, First Out) order for acquirers.\n *\n * @example\n * const sema = new Semaphore(2);\n *\n * async function task() {\n * await sema.acquire();\n * try {\n * // This code can only be executed by two tasks at the same time\n * } finally {\n * sema.release();\n * }\n * }\n *\n * task();\n * task();\n * task(); // This task will wait until one of the previous tasks releases the semaphore.\n */\nexport class Semaphore {\n /**\n * The maximum number of concurrent operations allowed.\n */\n public capacity: number;\n\n /**\n * The number of available permits.\n */\n public available: number;\n\n private deferredTasks: Array<() => void> = [];\n\n /**\n * Creates an instance of Semaphore.\n * @param capacity - The maximum number of concurrent operations allowed.\n *\n * @example\n * ```ts\n * const sema = new Semaphore(3); // Allows up to 3 concurrent operations.\n * ```\n */\n constructor(capacity: number) {\n this.capacity = capacity;\n this.available = capacity;\n }\n\n /**\n * Acquires a semaphore, blocking if necessary until one is available.\n *\n * @example\n * ```ts\n * const sema = new Semaphore(1);\n *\n * async function criticalSection() {\n * await sema.acquire();\n * try {\n * // This code section cannot be executed simultaneously\n * } finally {\n * sema.release();\n * }\n * }\n * ```\n *\n * @returns A promise that resolves when the semaphore is acquired.\n */\n async acquire(): Promise<void> {\n if (this.available > 0) {\n this.available--;\n return;\n }\n\n return new Promise<void>(resolve => {\n this.deferredTasks.push(resolve);\n });\n }\n\n /**\n * Releases a semaphore, allowing one more operation to proceed.\n *\n * @example\n * const sema = new Semaphore(1);\n *\n * async function task() {\n * await sema.acquire();\n * try {\n * // This code can only be executed by two tasks at the same time\n * } finally {\n * sema.release(); // Allows another waiting task to proceed.\n * }\n * }\n */\n release(): void {\n const deferredTask = this.deferredTasks.shift();\n\n if (deferredTask != null) {\n deferredTask();\n return;\n }\n\n if (this.available < this.capacity) {\n this.available++;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,IAAa,YAAb,MAAuB;;;;CAIrB,AAAO;;;;CAKP,AAAO;CAEP,AAAQ,gBAAmC,EAAE;;;;;;;;;;CAW7C,YAAY,UAAkB;AAC5B,OAAK,WAAW;AAChB,OAAK,YAAY;;;;;;;;;;;;;;;;;;;;;CAsBnB,MAAM,UAAyB;AAC7B,MAAI,KAAK,YAAY,GAAG;AACtB,QAAK;AACL;;AAGF,SAAO,IAAI,SAAc,YAAW;AAClC,QAAK,cAAc,KAAK,QAAQ;IAChC;;;;;;;;;;;;;;;;;CAkBJ,UAAgB;EACd,MAAM,eAAe,KAAK,cAAc,OAAO;AAE/C,MAAI,gBAAgB,MAAM;AACxB,iBAAc;AACd;;AAGF,MAAI,KAAK,YAAY,KAAK,SACxB,MAAK"}
|
package/dist/set-field.cjs
CHANGED
|
@@ -1 +1,30 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_to_path = require('./to-path.cjs');
|
|
2
|
+
const require_is_string = require('./type-checks/src/is-string.cjs');
|
|
3
|
+
const require_is_object_index = require('./type-checks/src/is-object-index.cjs');
|
|
4
|
+
|
|
5
|
+
//#region src/set-field.ts
|
|
6
|
+
/**
|
|
7
|
+
* Sets a value at a given deep path in an object.
|
|
8
|
+
*
|
|
9
|
+
* @param object - The object to set the value in.
|
|
10
|
+
* @param path - The deep path to set the value at.
|
|
11
|
+
* @param value - The value to set.
|
|
12
|
+
* @returns The object with the value set at the given deep path.
|
|
13
|
+
*/
|
|
14
|
+
function setField(object, path, value) {
|
|
15
|
+
const resolvedPath = Array.isArray(path) ? path : require_is_string.isString(path) ? require_to_path.toPath(path) : [path];
|
|
16
|
+
for (const key of resolvedPath) if (key === "__proto__" || key === "constructor" || key === "prototype") throw new Error(`Invalid key in path: ${key}`);
|
|
17
|
+
let current = object;
|
|
18
|
+
for (let i = 0; i < resolvedPath.length - 1; i++) {
|
|
19
|
+
const key = resolvedPath[i];
|
|
20
|
+
const nextKey = resolvedPath[i + 1];
|
|
21
|
+
if (current[key] === null) current[key] = require_is_object_index.isObjectIndex(nextKey) ? [] : {};
|
|
22
|
+
current = current[key];
|
|
23
|
+
}
|
|
24
|
+
const lastKey = resolvedPath.at(-1);
|
|
25
|
+
if (lastKey) current[lastKey] = value;
|
|
26
|
+
return object;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
exports.setField = setField;
|
package/dist/set-field.mjs
CHANGED
|
@@ -1,2 +1,31 @@
|
|
|
1
|
-
import{toPath
|
|
1
|
+
import { toPath } from "./to-path.mjs";
|
|
2
|
+
import { isString } from "./type-checks/src/is-string.mjs";
|
|
3
|
+
import { isObjectIndex } from "./type-checks/src/is-object-index.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/set-field.ts
|
|
6
|
+
/**
|
|
7
|
+
* Sets a value at a given deep path in an object.
|
|
8
|
+
*
|
|
9
|
+
* @param object - The object to set the value in.
|
|
10
|
+
* @param path - The deep path to set the value at.
|
|
11
|
+
* @param value - The value to set.
|
|
12
|
+
* @returns The object with the value set at the given deep path.
|
|
13
|
+
*/
|
|
14
|
+
function setField(object, path, value) {
|
|
15
|
+
const resolvedPath = Array.isArray(path) ? path : isString(path) ? toPath(path) : [path];
|
|
16
|
+
for (const key of resolvedPath) if (key === "__proto__" || key === "constructor" || key === "prototype") throw new Error(`Invalid key in path: ${key}`);
|
|
17
|
+
let current = object;
|
|
18
|
+
for (let i = 0; i < resolvedPath.length - 1; i++) {
|
|
19
|
+
const key = resolvedPath[i];
|
|
20
|
+
const nextKey = resolvedPath[i + 1];
|
|
21
|
+
if (current[key] === null) current[key] = isObjectIndex(nextKey) ? [] : {};
|
|
22
|
+
current = current[key];
|
|
23
|
+
}
|
|
24
|
+
const lastKey = resolvedPath.at(-1);
|
|
25
|
+
if (lastKey) current[lastKey] = value;
|
|
26
|
+
return object;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
export { setField };
|
|
2
31
|
//# sourceMappingURL=set-field.mjs.map
|
package/dist/set-field.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"set-field.mjs","names":["current: any"],"sources":["../src/set-field.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 License, and is\n free for commercial and private use. For more information, please visit\n our licensing page.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://stormsoftware.com/projects/stryke/docs\n Contact: https://stormsoftware.com/contact\n License: https://stormsoftware.com/projects/stryke/license\n\n ------------------------------------------------------------------- */\n\nimport { isObjectIndex } from \"@stryke/type-checks/is-object-index\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport type { DeepKey } from \"@stryke/types/object\";\nimport { toPath } from \"./to-path\";\n\n/**\n * Sets a value at a given deep path in an object.\n *\n * @param object - The object to set the value in.\n * @param path - The deep path to set the value at.\n * @param value - The value to set.\n * @returns The object with the value set at the given deep path.\n */\nexport function setField<\n TObject extends Record<string, any> = Record<string, any>\n>(object: TObject, path: DeepKey<TObject>, value: unknown): TObject {\n const resolvedPath = Array.isArray(path)\n ? path\n : isString(path)\n ? toPath(path)\n : [path];\n\n // Validate resolvedPath to prevent prototype pollution\n for (const key of resolvedPath) {\n if (key === \"__proto__\" || key === \"constructor\" || key === \"prototype\") {\n throw new Error(`Invalid key in path: ${key}`);\n }\n }\n\n let current: any = object;\n for (let i = 0; i < resolvedPath.length - 1; i++) {\n const key = resolvedPath[i];\n const nextKey = resolvedPath[i + 1];\n\n if (current[key] === null) {\n current[key] = isObjectIndex(nextKey) ? [] : {};\n }\n\n current = current[key];\n }\n\n const lastKey = resolvedPath.at(-1);\n if (lastKey) {\n current[lastKey] = value;\n }\n\n return object;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"set-field.mjs","names":["current: any"],"sources":["../src/set-field.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 License, and is\n free for commercial and private use. For more information, please visit\n our licensing page.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://stormsoftware.com/projects/stryke/docs\n Contact: https://stormsoftware.com/contact\n License: https://stormsoftware.com/projects/stryke/license\n\n ------------------------------------------------------------------- */\n\nimport { isObjectIndex } from \"@stryke/type-checks/is-object-index\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport type { DeepKey } from \"@stryke/types/object\";\nimport { toPath } from \"./to-path\";\n\n/**\n * Sets a value at a given deep path in an object.\n *\n * @param object - The object to set the value in.\n * @param path - The deep path to set the value at.\n * @param value - The value to set.\n * @returns The object with the value set at the given deep path.\n */\nexport function setField<\n TObject extends Record<string, any> = Record<string, any>\n>(object: TObject, path: DeepKey<TObject>, value: unknown): TObject {\n const resolvedPath = Array.isArray(path)\n ? path\n : isString(path)\n ? toPath(path)\n : [path];\n\n // Validate resolvedPath to prevent prototype pollution\n for (const key of resolvedPath) {\n if (key === \"__proto__\" || key === \"constructor\" || key === \"prototype\") {\n throw new Error(`Invalid key in path: ${key}`);\n }\n }\n\n let current: any = object;\n for (let i = 0; i < resolvedPath.length - 1; i++) {\n const key = resolvedPath[i];\n const nextKey = resolvedPath[i + 1];\n\n if (current[key] === null) {\n current[key] = isObjectIndex(nextKey) ? [] : {};\n }\n\n current = current[key];\n }\n\n const lastKey = resolvedPath.at(-1);\n if (lastKey) {\n current[lastKey] = value;\n }\n\n return object;\n}\n"],"mappings":";;;;;;;;;;;;;AA8BA,SAAgB,SAEd,QAAiB,MAAwB,OAAyB;CAClE,MAAM,eAAe,MAAM,QAAQ,KAAK,GACpC,OACA,SAAS,KAAK,GACZ,OAAO,KAAK,GACZ,CAAC,KAAK;AAGZ,MAAK,MAAM,OAAO,aAChB,KAAI,QAAQ,eAAe,QAAQ,iBAAiB,QAAQ,YAC1D,OAAM,IAAI,MAAM,wBAAwB,MAAM;CAIlD,IAAIA,UAAe;AACnB,MAAK,IAAI,IAAI,GAAG,IAAI,aAAa,SAAS,GAAG,KAAK;EAChD,MAAM,MAAM,aAAa;EACzB,MAAM,UAAU,aAAa,IAAI;AAEjC,MAAI,QAAQ,SAAS,KACnB,SAAQ,OAAO,cAAc,QAAQ,GAAG,EAAE,GAAG,EAAE;AAGjD,YAAU,QAAQ;;CAGpB,MAAM,UAAU,aAAa,GAAG,GAAG;AACnC,KAAI,QACF,SAAQ,WAAW;AAGrB,QAAO"}
|
package/dist/throttle.cjs
CHANGED
|
@@ -1 +1,43 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region src/throttle.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates a throttled function that only invokes the provided function at most once
|
|
5
|
+
* per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
|
6
|
+
* within the wait time will not trigger the execution of the original function.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const throttledFunction = throttle(() => {
|
|
11
|
+
* console.log('Function executed');
|
|
12
|
+
* }, 1000);
|
|
13
|
+
*
|
|
14
|
+
* // Will log 'Function executed' immediately
|
|
15
|
+
* throttledFunction();
|
|
16
|
+
*
|
|
17
|
+
* // Will not log anything as it is within the throttle time
|
|
18
|
+
* throttledFunction();
|
|
19
|
+
*
|
|
20
|
+
* // After 1 second
|
|
21
|
+
* setTimeout(() => {
|
|
22
|
+
* throttledFunction(); // Will log 'Function executed'
|
|
23
|
+
* }, 1000);
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @param func - The function to throttle.
|
|
27
|
+
* @param throttleMs - The number of milliseconds to throttle executions to.
|
|
28
|
+
* @returns A new throttled function that accepts the same parameters as the original function.
|
|
29
|
+
*/
|
|
30
|
+
function throttle(func, throttleMs) {
|
|
31
|
+
let lastCallTime;
|
|
32
|
+
const throttledFunction = ((...args) => {
|
|
33
|
+
const now = Date.now();
|
|
34
|
+
if (lastCallTime == null || now - lastCallTime >= throttleMs) {
|
|
35
|
+
lastCallTime = now;
|
|
36
|
+
func(...args);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
return throttledFunction;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
exports.throttle = throttle;
|
package/dist/throttle.mjs
CHANGED
|
@@ -1,2 +1,43 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/throttle.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates a throttled function that only invokes the provided function at most once
|
|
4
|
+
* per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
|
5
|
+
* within the wait time will not trigger the execution of the original function.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const throttledFunction = throttle(() => {
|
|
10
|
+
* console.log('Function executed');
|
|
11
|
+
* }, 1000);
|
|
12
|
+
*
|
|
13
|
+
* // Will log 'Function executed' immediately
|
|
14
|
+
* throttledFunction();
|
|
15
|
+
*
|
|
16
|
+
* // Will not log anything as it is within the throttle time
|
|
17
|
+
* throttledFunction();
|
|
18
|
+
*
|
|
19
|
+
* // After 1 second
|
|
20
|
+
* setTimeout(() => {
|
|
21
|
+
* throttledFunction(); // Will log 'Function executed'
|
|
22
|
+
* }, 1000);
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @param func - The function to throttle.
|
|
26
|
+
* @param throttleMs - The number of milliseconds to throttle executions to.
|
|
27
|
+
* @returns A new throttled function that accepts the same parameters as the original function.
|
|
28
|
+
*/
|
|
29
|
+
function throttle(func, throttleMs) {
|
|
30
|
+
let lastCallTime;
|
|
31
|
+
const throttledFunction = ((...args) => {
|
|
32
|
+
const now = Date.now();
|
|
33
|
+
if (lastCallTime == null || now - lastCallTime >= throttleMs) {
|
|
34
|
+
lastCallTime = now;
|
|
35
|
+
func(...args);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return throttledFunction;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
export { throttle };
|
|
2
43
|
//# sourceMappingURL=throttle.mjs.map
|
package/dist/throttle.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throttle.mjs","names":["lastCallTime: number | null"],"sources":["../src/throttle.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 License, and is\n free for commercial and private use. For more information, please visit\n our licensing page.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://stormsoftware.com/projects/stryke/docs\n Contact: https://stormsoftware.com/contact\n License: https://stormsoftware.com/projects/stryke/license\n\n ------------------------------------------------------------------- */\n\n/**\n * Creates a throttled function that only invokes the provided function at most once\n * per every `throttleMs` milliseconds. Subsequent calls to the throttled function\n * within the wait time will not trigger the execution of the original function.\n *\n * @example\n * ```typescript\n * const throttledFunction = throttle(() => {\n * console.log('Function executed');\n * }, 1000);\n *\n * // Will log 'Function executed' immediately\n * throttledFunction();\n *\n * // Will not log anything as it is within the throttle time\n * throttledFunction();\n *\n * // After 1 second\n * setTimeout(() => {\n * throttledFunction(); // Will log 'Function executed'\n * }, 1000);\n * ```\n *\n * @param func - The function to throttle.\n * @param throttleMs - The number of milliseconds to throttle executions to.\n * @returns A new throttled function that accepts the same parameters as the original function.\n */\nexport function throttle<F extends (...args: any[]) => void>(\n func: F,\n throttleMs: number\n): F {\n let lastCallTime: number | null;\n\n const throttledFunction = ((...args: Parameters<F>) => {\n const now = Date.now();\n\n if (lastCallTime == null || now - lastCallTime >= throttleMs) {\n lastCallTime = now;\n func(...args);\n }\n }) as F;\n\n return throttledFunction;\n}\n"],"mappings":"AA4CA,SAAgB,
|
|
1
|
+
{"version":3,"file":"throttle.mjs","names":["lastCallTime: number | null"],"sources":["../src/throttle.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 License, and is\n free for commercial and private use. For more information, please visit\n our licensing page.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://stormsoftware.com/projects/stryke/docs\n Contact: https://stormsoftware.com/contact\n License: https://stormsoftware.com/projects/stryke/license\n\n ------------------------------------------------------------------- */\n\n/**\n * Creates a throttled function that only invokes the provided function at most once\n * per every `throttleMs` milliseconds. Subsequent calls to the throttled function\n * within the wait time will not trigger the execution of the original function.\n *\n * @example\n * ```typescript\n * const throttledFunction = throttle(() => {\n * console.log('Function executed');\n * }, 1000);\n *\n * // Will log 'Function executed' immediately\n * throttledFunction();\n *\n * // Will not log anything as it is within the throttle time\n * throttledFunction();\n *\n * // After 1 second\n * setTimeout(() => {\n * throttledFunction(); // Will log 'Function executed'\n * }, 1000);\n * ```\n *\n * @param func - The function to throttle.\n * @param throttleMs - The number of milliseconds to throttle executions to.\n * @returns A new throttled function that accepts the same parameters as the original function.\n */\nexport function throttle<F extends (...args: any[]) => void>(\n func: F,\n throttleMs: number\n): F {\n let lastCallTime: number | null;\n\n const throttledFunction = ((...args: Parameters<F>) => {\n const now = Date.now();\n\n if (lastCallTime == null || now - lastCallTime >= throttleMs) {\n lastCallTime = now;\n func(...args);\n }\n }) as F;\n\n return throttledFunction;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CA,SAAgB,SACd,MACA,YACG;CACH,IAAIA;CAEJ,MAAM,sBAAsB,GAAG,SAAwB;EACrD,MAAM,MAAM,KAAK,KAAK;AAEtB,MAAI,gBAAgB,QAAQ,MAAM,gBAAgB,YAAY;AAC5D,kBAAe;AACf,QAAK,GAAG,KAAK;;;AAIjB,QAAO"}
|
package/dist/timeout.cjs
CHANGED
|
@@ -1 +1,18 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_errors = require('./errors.cjs');
|
|
2
|
+
const require_delay = require('./delay.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/timeout.ts
|
|
5
|
+
/**
|
|
6
|
+
* Returns a promise that rejects with a `TimeoutError` after a specified delay.
|
|
7
|
+
*
|
|
8
|
+
* @param ms - The delay duration in milliseconds.
|
|
9
|
+
* @returns A promise that rejects with a `TimeoutError` after the specified delay.
|
|
10
|
+
* @throws Throws a `TimeoutError` after the specified delay.
|
|
11
|
+
*/
|
|
12
|
+
async function timeout(ms) {
|
|
13
|
+
await require_delay.delay(ms);
|
|
14
|
+
throw new require_errors.TimeoutError();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
exports.timeout = timeout;
|
package/dist/timeout.mjs
CHANGED
|
@@ -1,2 +1,19 @@
|
|
|
1
|
-
import{TimeoutError
|
|
1
|
+
import { TimeoutError } from "./errors.mjs";
|
|
2
|
+
import { delay } from "./delay.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/timeout.ts
|
|
5
|
+
/**
|
|
6
|
+
* Returns a promise that rejects with a `TimeoutError` after a specified delay.
|
|
7
|
+
*
|
|
8
|
+
* @param ms - The delay duration in milliseconds.
|
|
9
|
+
* @returns A promise that rejects with a `TimeoutError` after the specified delay.
|
|
10
|
+
* @throws Throws a `TimeoutError` after the specified delay.
|
|
11
|
+
*/
|
|
12
|
+
async function timeout(ms) {
|
|
13
|
+
await delay(ms);
|
|
14
|
+
throw new TimeoutError();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { timeout };
|
|
2
19
|
//# sourceMappingURL=timeout.mjs.map
|
package/dist/timeout.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timeout.mjs","names":[],"sources":["../src/timeout.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 License, and is\n free for commercial and private use. For more information, please visit\n our licensing page.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://stormsoftware.com/projects/stryke/docs\n Contact: https://stormsoftware.com/contact\n License: https://stormsoftware.com/projects/stryke/license\n\n ------------------------------------------------------------------- */\n\nimport { delay } from \"./delay\";\nimport { TimeoutError } from \"./errors\";\n\n/**\n * Returns a promise that rejects with a `TimeoutError` after a specified delay.\n *\n * @param ms - The delay duration in milliseconds.\n * @returns A promise that rejects with a `TimeoutError` after the specified delay.\n * @throws Throws a `TimeoutError` after the specified delay.\n */\nexport async function timeout(ms: number): Promise<never> {\n await delay(ms);\n throw new TimeoutError();\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"timeout.mjs","names":[],"sources":["../src/timeout.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 License, and is\n free for commercial and private use. For more information, please visit\n our licensing page.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://stormsoftware.com/projects/stryke/docs\n Contact: https://stormsoftware.com/contact\n License: https://stormsoftware.com/projects/stryke/license\n\n ------------------------------------------------------------------- */\n\nimport { delay } from \"./delay\";\nimport { TimeoutError } from \"./errors\";\n\n/**\n * Returns a promise that rejects with a `TimeoutError` after a specified delay.\n *\n * @param ms - The delay duration in milliseconds.\n * @returns A promise that rejects with a `TimeoutError` after the specified delay.\n * @throws Throws a `TimeoutError` after the specified delay.\n */\nexport async function timeout(ms: number): Promise<never> {\n await delay(ms);\n throw new TimeoutError();\n}\n"],"mappings":";;;;;;;;;;;AA2BA,eAAsB,QAAQ,IAA4B;AACxD,OAAM,MAAM,GAAG;AACf,OAAM,IAAI,cAAc"}
|
package/dist/to-deep-key.cjs
CHANGED
|
@@ -1 +1,54 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_is_number = require('./type-checks/src/is-number.cjs');
|
|
2
|
+
const require_is_set_string = require('./type-checks/src/is-set-string.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/to-deep-key.ts
|
|
5
|
+
/**
|
|
6
|
+
* Converts an array of path segments into a deep key string.
|
|
7
|
+
*
|
|
8
|
+
* This function takes an array of strings and numbers representing path segments and combines them into a deep key string.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* toDeepKey(['a', 'b', 'c']) // Returns 'a.b.c'
|
|
12
|
+
* toDeepKey(['a', 0, 'c']) // Returns 'a[0].c'
|
|
13
|
+
* toDeepKey(['', 'a', 'b', 'c']) // Returns '.a.b.c'
|
|
14
|
+
* toDeepKey(['a', 'b.c', 'd']) // Returns 'a.b.c.d'
|
|
15
|
+
* toDeepKey([]) // Returns ''
|
|
16
|
+
* toDeepKey(['', 'a', 'b', 'c', 'd', 'e', 'f.g', 'h']) // Returns '.a.b.c.d.e.f.g.h'
|
|
17
|
+
*
|
|
18
|
+
* @param path - An array of strings and numbers representing path segments.
|
|
19
|
+
* @returns A deep key string.
|
|
20
|
+
*/
|
|
21
|
+
function toDeepKey(path) {
|
|
22
|
+
return path.reduce((ret, segment) => {
|
|
23
|
+
return addPathToDeepKey(ret, segment);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Adds a path segment to a deep key string.
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* This function takes a deep key string and a path segment and combines them into a new deep key string.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* addPathToDeepKey('a.b', 'c') // Returns 'a.b.c'
|
|
35
|
+
* addPathToDeepKey('a[0]', 'c') // Returns 'a[0].c'
|
|
36
|
+
* addPathToDeepKey('.a.b', 'c') // Returns '.a.b.c'
|
|
37
|
+
* addPathToDeepKey('a.b', 'b.c') // Returns 'a.b.b.c'
|
|
38
|
+
* addPathToDeepKey('', 'a') // Returns 'a'
|
|
39
|
+
* addPathToDeepKey('.a.b', 'c.d') // Returns '.a.b.c.d'
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @param deepKey - The deep key string to add the path segment to.
|
|
43
|
+
* @param path - The path segment to add to the deep key string.
|
|
44
|
+
* @returns A new deep key string.
|
|
45
|
+
*/
|
|
46
|
+
function addPathToDeepKey(deepKey, path) {
|
|
47
|
+
if (require_is_number.isNumber(path) || Number.isInteger(path)) return `${deepKey}[${path}]`;
|
|
48
|
+
if (require_is_set_string.isSetString(path)) return `${deepKey}.${path}`;
|
|
49
|
+
return deepKey;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
//#endregion
|
|
53
|
+
exports.addPathToDeepKey = addPathToDeepKey;
|
|
54
|
+
exports.toDeepKey = toDeepKey;
|
package/dist/to-deep-key.mjs
CHANGED
|
@@ -1,2 +1,54 @@
|
|
|
1
|
-
import{isNumber
|
|
1
|
+
import { isNumber } from "./type-checks/src/is-number.mjs";
|
|
2
|
+
import { isSetString } from "./type-checks/src/is-set-string.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/to-deep-key.ts
|
|
5
|
+
/**
|
|
6
|
+
* Converts an array of path segments into a deep key string.
|
|
7
|
+
*
|
|
8
|
+
* This function takes an array of strings and numbers representing path segments and combines them into a deep key string.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* toDeepKey(['a', 'b', 'c']) // Returns 'a.b.c'
|
|
12
|
+
* toDeepKey(['a', 0, 'c']) // Returns 'a[0].c'
|
|
13
|
+
* toDeepKey(['', 'a', 'b', 'c']) // Returns '.a.b.c'
|
|
14
|
+
* toDeepKey(['a', 'b.c', 'd']) // Returns 'a.b.c.d'
|
|
15
|
+
* toDeepKey([]) // Returns ''
|
|
16
|
+
* toDeepKey(['', 'a', 'b', 'c', 'd', 'e', 'f.g', 'h']) // Returns '.a.b.c.d.e.f.g.h'
|
|
17
|
+
*
|
|
18
|
+
* @param path - An array of strings and numbers representing path segments.
|
|
19
|
+
* @returns A deep key string.
|
|
20
|
+
*/
|
|
21
|
+
function toDeepKey(path) {
|
|
22
|
+
return path.reduce((ret, segment) => {
|
|
23
|
+
return addPathToDeepKey(ret, segment);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Adds a path segment to a deep key string.
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* This function takes a deep key string and a path segment and combines them into a new deep key string.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* addPathToDeepKey('a.b', 'c') // Returns 'a.b.c'
|
|
35
|
+
* addPathToDeepKey('a[0]', 'c') // Returns 'a[0].c'
|
|
36
|
+
* addPathToDeepKey('.a.b', 'c') // Returns '.a.b.c'
|
|
37
|
+
* addPathToDeepKey('a.b', 'b.c') // Returns 'a.b.b.c'
|
|
38
|
+
* addPathToDeepKey('', 'a') // Returns 'a'
|
|
39
|
+
* addPathToDeepKey('.a.b', 'c.d') // Returns '.a.b.c.d'
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @param deepKey - The deep key string to add the path segment to.
|
|
43
|
+
* @param path - The path segment to add to the deep key string.
|
|
44
|
+
* @returns A new deep key string.
|
|
45
|
+
*/
|
|
46
|
+
function addPathToDeepKey(deepKey, path) {
|
|
47
|
+
if (isNumber(path) || Number.isInteger(path)) return `${deepKey}[${path}]`;
|
|
48
|
+
if (isSetString(path)) return `${deepKey}.${path}`;
|
|
49
|
+
return deepKey;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
//#endregion
|
|
53
|
+
export { addPathToDeepKey, toDeepKey };
|
|
2
54
|
//# sourceMappingURL=to-deep-key.mjs.map
|
package/dist/to-deep-key.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-deep-key.mjs","names":[],"sources":["../src/to-deep-key.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 License, and is\n free for commercial and private use. For more information, please visit\n our licensing page.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://stormsoftware.com/projects/stryke/docs\n Contact: https://stormsoftware.com/contact\n License: https://stormsoftware.com/projects/stryke/license\n\n ------------------------------------------------------------------- */\n\nimport { isNumber } from \"@stryke/type-checks/is-number\";\nimport { isSetString } from \"@stryke/type-checks/is-set-string\";\n\n/**\n * Converts an array of path segments into a deep key string.\n *\n * This function takes an array of strings and numbers representing path segments and combines them into a deep key string.\n *\n * @example\n * toDeepKey(['a', 'b', 'c']) // Returns 'a.b.c'\n * toDeepKey(['a', 0, 'c']) // Returns 'a[0].c'\n * toDeepKey(['', 'a', 'b', 'c']) // Returns '.a.b.c'\n * toDeepKey(['a', 'b.c', 'd']) // Returns 'a.b.c.d'\n * toDeepKey([]) // Returns ''\n * toDeepKey(['', 'a', 'b', 'c', 'd', 'e', 'f.g', 'h']) // Returns '.a.b.c.d.e.f.g.h'\n *\n * @param path - An array of strings and numbers representing path segments.\n * @returns A deep key string.\n */\nexport function toDeepKey(path: string[]): string {\n return path.reduce((ret, segment) => {\n return addPathToDeepKey(ret, segment);\n });\n}\n\n/**\n * Adds a path segment to a deep key string.\n *\n * @remarks\n * This function takes a deep key string and a path segment and combines them into a new deep key string.\n *\n * @example\n * ```ts\n * addPathToDeepKey('a.b', 'c') // Returns 'a.b.c'\n * addPathToDeepKey('a[0]', 'c') // Returns 'a[0].c'\n * addPathToDeepKey('.a.b', 'c') // Returns '.a.b.c'\n * addPathToDeepKey('a.b', 'b.c') // Returns 'a.b.b.c'\n * addPathToDeepKey('', 'a') // Returns 'a'\n * addPathToDeepKey('.a.b', 'c.d') // Returns '.a.b.c.d'\n * ```\n *\n * @param deepKey - The deep key string to add the path segment to.\n * @param path - The path segment to add to the deep key string.\n * @returns A new deep key string.\n */\nexport function addPathToDeepKey(\n deepKey: string,\n path: string | number\n): string {\n if (isNumber(path) || Number.isInteger(path)) {\n return `${deepKey}[${path}]`;\n }\n if (isSetString(path)) {\n return `${deepKey}.${path}`;\n }\n\n return deepKey;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"to-deep-key.mjs","names":[],"sources":["../src/to-deep-key.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 License, and is\n free for commercial and private use. For more information, please visit\n our licensing page.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://stormsoftware.com/projects/stryke/docs\n Contact: https://stormsoftware.com/contact\n License: https://stormsoftware.com/projects/stryke/license\n\n ------------------------------------------------------------------- */\n\nimport { isNumber } from \"@stryke/type-checks/is-number\";\nimport { isSetString } from \"@stryke/type-checks/is-set-string\";\n\n/**\n * Converts an array of path segments into a deep key string.\n *\n * This function takes an array of strings and numbers representing path segments and combines them into a deep key string.\n *\n * @example\n * toDeepKey(['a', 'b', 'c']) // Returns 'a.b.c'\n * toDeepKey(['a', 0, 'c']) // Returns 'a[0].c'\n * toDeepKey(['', 'a', 'b', 'c']) // Returns '.a.b.c'\n * toDeepKey(['a', 'b.c', 'd']) // Returns 'a.b.c.d'\n * toDeepKey([]) // Returns ''\n * toDeepKey(['', 'a', 'b', 'c', 'd', 'e', 'f.g', 'h']) // Returns '.a.b.c.d.e.f.g.h'\n *\n * @param path - An array of strings and numbers representing path segments.\n * @returns A deep key string.\n */\nexport function toDeepKey(path: string[]): string {\n return path.reduce((ret, segment) => {\n return addPathToDeepKey(ret, segment);\n });\n}\n\n/**\n * Adds a path segment to a deep key string.\n *\n * @remarks\n * This function takes a deep key string and a path segment and combines them into a new deep key string.\n *\n * @example\n * ```ts\n * addPathToDeepKey('a.b', 'c') // Returns 'a.b.c'\n * addPathToDeepKey('a[0]', 'c') // Returns 'a[0].c'\n * addPathToDeepKey('.a.b', 'c') // Returns '.a.b.c'\n * addPathToDeepKey('a.b', 'b.c') // Returns 'a.b.b.c'\n * addPathToDeepKey('', 'a') // Returns 'a'\n * addPathToDeepKey('.a.b', 'c.d') // Returns '.a.b.c.d'\n * ```\n *\n * @param deepKey - The deep key string to add the path segment to.\n * @param path - The path segment to add to the deep key string.\n * @returns A new deep key string.\n */\nexport function addPathToDeepKey(\n deepKey: string,\n path: string | number\n): string {\n if (isNumber(path) || Number.isInteger(path)) {\n return `${deepKey}[${path}]`;\n }\n if (isSetString(path)) {\n return `${deepKey}.${path}`;\n }\n\n return deepKey;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAoCA,SAAgB,UAAU,MAAwB;AAChD,QAAO,KAAK,QAAQ,KAAK,YAAY;AACnC,SAAO,iBAAiB,KAAK,QAAQ;GACrC;;;;;;;;;;;;;;;;;;;;;;AAuBJ,SAAgB,iBACd,SACA,MACQ;AACR,KAAI,SAAS,KAAK,IAAI,OAAO,UAAU,KAAK,CAC1C,QAAO,GAAG,QAAQ,GAAG,KAAK;AAE5B,KAAI,YAAY,KAAK,CACnB,QAAO,GAAG,QAAQ,GAAG;AAGvB,QAAO"}
|
package/dist/to-path.cjs
CHANGED
|
@@ -1 +1,43 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region src/to-path.ts
|
|
3
|
+
const DOTS_KEY = /^[\w.]+$/g;
|
|
4
|
+
const ESCAPE_REGEXP = /\\(?<temp1>\\)?/g;
|
|
5
|
+
const PROPERTY_REGEXP = new RegExp(`${String.raw`[^.[\]]+`}|${String.raw`\[(?:`}([^"'][^[]*)|${String.raw`(["'])((?:(?!\2)[^\\]|\\.)*?)\2`}${String.raw`)\]`}|${String.raw`(?=(?:\.|\[\])(?:\.|\[\]|$))`}`, "g");
|
|
6
|
+
/**
|
|
7
|
+
* Converts a deep key string into an array of path segments.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* This function takes a string representing a deep key (e.g., 'a.b.c' or 'a[b][c]') and breaks it down into an array of strings, each representing a segment of the path.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* toPath('a.b.c') // Returns ['a', 'b', 'c']
|
|
15
|
+
* toPath('a[b][c]') // Returns ['a', 'b', 'c']
|
|
16
|
+
* toPath('.a.b.c') // Returns ['', 'a', 'b', 'c']
|
|
17
|
+
* toPath('a["b.c"].d') // Returns ['a', 'b.c', 'd']
|
|
18
|
+
* toPath('') // Returns []
|
|
19
|
+
* toPath('.a[b].c.d[e]["f.g"].h') // Returns ['', 'a', 'b', 'c', 'd', 'e', 'f.g', 'h']
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @param deepKey - The deep key string to convert.
|
|
23
|
+
* @returns An array of strings, each representing a segment of the path.
|
|
24
|
+
*/
|
|
25
|
+
function toPath(deepKey) {
|
|
26
|
+
if (DOTS_KEY.test(deepKey)) return deepKey.split(".");
|
|
27
|
+
const result = [];
|
|
28
|
+
if (deepKey[0] === ".") result.push("");
|
|
29
|
+
const matches = deepKey.matchAll(PROPERTY_REGEXP);
|
|
30
|
+
for (const match of matches) {
|
|
31
|
+
let key = match[0];
|
|
32
|
+
const expr = match[1];
|
|
33
|
+
const quote = match[2];
|
|
34
|
+
const substr = match[3];
|
|
35
|
+
if (quote && substr) key = substr.replace(ESCAPE_REGEXP, "$1");
|
|
36
|
+
else if (expr) key = expr;
|
|
37
|
+
result.push(key);
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
exports.toPath = toPath;
|
package/dist/to-path.mjs
CHANGED
|
@@ -1,2 +1,43 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/to-path.ts
|
|
2
|
+
const DOTS_KEY = /^[\w.]+$/g;
|
|
3
|
+
const ESCAPE_REGEXP = /\\(?<temp1>\\)?/g;
|
|
4
|
+
const PROPERTY_REGEXP = new RegExp(`${String.raw`[^.[\]]+`}|${String.raw`\[(?:`}([^"'][^[]*)|${String.raw`(["'])((?:(?!\2)[^\\]|\\.)*?)\2`}${String.raw`)\]`}|${String.raw`(?=(?:\.|\[\])(?:\.|\[\]|$))`}`, "g");
|
|
5
|
+
/**
|
|
6
|
+
* Converts a deep key string into an array of path segments.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* This function takes a string representing a deep key (e.g., 'a.b.c' or 'a[b][c]') and breaks it down into an array of strings, each representing a segment of the path.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* toPath('a.b.c') // Returns ['a', 'b', 'c']
|
|
14
|
+
* toPath('a[b][c]') // Returns ['a', 'b', 'c']
|
|
15
|
+
* toPath('.a.b.c') // Returns ['', 'a', 'b', 'c']
|
|
16
|
+
* toPath('a["b.c"].d') // Returns ['a', 'b.c', 'd']
|
|
17
|
+
* toPath('') // Returns []
|
|
18
|
+
* toPath('.a[b].c.d[e]["f.g"].h') // Returns ['', 'a', 'b', 'c', 'd', 'e', 'f.g', 'h']
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @param deepKey - The deep key string to convert.
|
|
22
|
+
* @returns An array of strings, each representing a segment of the path.
|
|
23
|
+
*/
|
|
24
|
+
function toPath(deepKey) {
|
|
25
|
+
if (DOTS_KEY.test(deepKey)) return deepKey.split(".");
|
|
26
|
+
const result = [];
|
|
27
|
+
if (deepKey[0] === ".") result.push("");
|
|
28
|
+
const matches = deepKey.matchAll(PROPERTY_REGEXP);
|
|
29
|
+
for (const match of matches) {
|
|
30
|
+
let key = match[0];
|
|
31
|
+
const expr = match[1];
|
|
32
|
+
const quote = match[2];
|
|
33
|
+
const substr = match[3];
|
|
34
|
+
if (quote && substr) key = substr.replace(ESCAPE_REGEXP, "$1");
|
|
35
|
+
else if (expr) key = expr;
|
|
36
|
+
result.push(key);
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
export { toPath };
|
|
2
43
|
//# sourceMappingURL=to-path.mjs.map
|
package/dist/to-path.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-path.mjs","names":["result: string[]"],"sources":["../src/to-path.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nconst DOTS_KEY = /^[\\w.]+$/g;\n\nconst ESCAPE_REGEXP = /\\\\(?<temp1>\\\\)?/g;\nconst PROPERTY_REGEXP = new RegExp(\n // Match anything that isn't a dot or bracket.\n `${String.raw`[^.[\\]]+`}|${\n // Or match property names within brackets.\n String.raw`\\[(?:`\n // Match a non-string expression.\n }([^\"'][^[]*)` +\n `|${\n // Or match strings (supports escaping characters).\n String.raw`([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2`\n }${String.raw`)\\]`}|${\n // Or match \"\" as the space between consecutive dots or empty brackets.\n String.raw`(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))`\n }`,\n \"g\"\n);\n\n/**\n * Converts a deep key string into an array of path segments.\n *\n * @remarks\n * This function takes a string representing a deep key (e.g., 'a.b.c' or 'a[b][c]') and breaks it down into an array of strings, each representing a segment of the path.\n *\n * @example\n * ```ts\n * toPath('a.b.c') // Returns ['a', 'b', 'c']\n * toPath('a[b][c]') // Returns ['a', 'b', 'c']\n * toPath('.a.b.c') // Returns ['', 'a', 'b', 'c']\n * toPath('a[\"b.c\"].d') // Returns ['a', 'b.c', 'd']\n * toPath('') // Returns []\n * toPath('.a[b].c.d[e][\"f.g\"].h') // Returns ['', 'a', 'b', 'c', 'd', 'e', 'f.g', 'h']\n * ```\n *\n * @param deepKey - The deep key string to convert.\n * @returns An array of strings, each representing a segment of the path.\n */\nexport function toPath(deepKey: string): string[] {\n if (DOTS_KEY.test(deepKey)) {\n return deepKey.split(\".\");\n }\n\n const result: string[] = [];\n\n if (deepKey[0] === \".\") {\n result.push(\"\");\n }\n\n const matches = deepKey.matchAll(PROPERTY_REGEXP);\n\n for (const match of matches) {\n let key = match[0];\n const expr = match[1];\n const quote = match[2];\n const substr = match[3];\n\n if (quote && substr) {\n key = substr.replace(ESCAPE_REGEXP, \"$1\");\n } else if (expr) {\n key = expr;\n }\n\n result.push(key);\n }\n\n return result;\n}\n"],"mappings":"AAkBA,MAAM,
|
|
1
|
+
{"version":3,"file":"to-path.mjs","names":["result: string[]"],"sources":["../src/to-path.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nconst DOTS_KEY = /^[\\w.]+$/g;\n\nconst ESCAPE_REGEXP = /\\\\(?<temp1>\\\\)?/g;\nconst PROPERTY_REGEXP = new RegExp(\n // Match anything that isn't a dot or bracket.\n `${String.raw`[^.[\\]]+`}|${\n // Or match property names within brackets.\n String.raw`\\[(?:`\n // Match a non-string expression.\n }([^\"'][^[]*)` +\n `|${\n // Or match strings (supports escaping characters).\n String.raw`([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2`\n }${String.raw`)\\]`}|${\n // Or match \"\" as the space between consecutive dots or empty brackets.\n String.raw`(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))`\n }`,\n \"g\"\n);\n\n/**\n * Converts a deep key string into an array of path segments.\n *\n * @remarks\n * This function takes a string representing a deep key (e.g., 'a.b.c' or 'a[b][c]') and breaks it down into an array of strings, each representing a segment of the path.\n *\n * @example\n * ```ts\n * toPath('a.b.c') // Returns ['a', 'b', 'c']\n * toPath('a[b][c]') // Returns ['a', 'b', 'c']\n * toPath('.a.b.c') // Returns ['', 'a', 'b', 'c']\n * toPath('a[\"b.c\"].d') // Returns ['a', 'b.c', 'd']\n * toPath('') // Returns []\n * toPath('.a[b].c.d[e][\"f.g\"].h') // Returns ['', 'a', 'b', 'c', 'd', 'e', 'f.g', 'h']\n * ```\n *\n * @param deepKey - The deep key string to convert.\n * @returns An array of strings, each representing a segment of the path.\n */\nexport function toPath(deepKey: string): string[] {\n if (DOTS_KEY.test(deepKey)) {\n return deepKey.split(\".\");\n }\n\n const result: string[] = [];\n\n if (deepKey[0] === \".\") {\n result.push(\"\");\n }\n\n const matches = deepKey.matchAll(PROPERTY_REGEXP);\n\n for (const match of matches) {\n let key = match[0];\n const expr = match[1];\n const quote = match[2];\n const substr = match[3];\n\n if (quote && substr) {\n key = substr.replace(ESCAPE_REGEXP, \"$1\");\n } else if (expr) {\n key = expr;\n }\n\n result.push(key);\n }\n\n return result;\n}\n"],"mappings":";AAkBA,MAAM,WAAW;AAEjB,MAAM,gBAAgB;AACtB,MAAM,kBAAkB,IAAI,OAE1B,GAAG,OAAO,GAAG,WAAW,GAEtB,OAAO,GAAG,QAEX,eAGG,OAAO,GAAG,oCACT,OAAO,GAAG,MAAM,GAEjB,OAAO,GAAG,kCAEd,IACD;;;;;;;;;;;;;;;;;;;;AAqBD,SAAgB,OAAO,SAA2B;AAChD,KAAI,SAAS,KAAK,QAAQ,CACxB,QAAO,QAAQ,MAAM,IAAI;CAG3B,MAAMA,SAAmB,EAAE;AAE3B,KAAI,QAAQ,OAAO,IACjB,QAAO,KAAK,GAAG;CAGjB,MAAM,UAAU,QAAQ,SAAS,gBAAgB;AAEjD,MAAK,MAAM,SAAS,SAAS;EAC3B,IAAI,MAAM,MAAM;EAChB,MAAM,OAAO,MAAM;EACnB,MAAM,QAAQ,MAAM;EACpB,MAAM,SAAS,MAAM;AAErB,MAAI,SAAS,OACX,OAAM,OAAO,QAAQ,eAAe,KAAK;WAChC,KACT,OAAM;AAGR,SAAO,KAAK,IAAI;;AAGlB,QAAO"}
|
|
@@ -1 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region ../type-checks/src/get-object-tag.ts
|
|
3
|
+
/**
|
|
4
|
+
* Gets the `toStringTag` of `obj`.
|
|
5
|
+
*
|
|
6
|
+
* @param value - The obj to query.
|
|
7
|
+
* @returns Returns the `toStringTag`.
|
|
8
|
+
*/
|
|
9
|
+
const getObjectTag = (value) => {
|
|
10
|
+
if (value == null) return value === void 0 ? "[object Undefined]" : "[object Null]";
|
|
11
|
+
return Object.prototype.toString.call(value);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
exports.getObjectTag = getObjectTag;
|