@stryke/helpers 0.10.13 → 0.10.15
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 +20 -0
- package/dist/deep-clone.cjs +3 -5
- package/dist/deep-clone.mjs +1 -3
- package/dist/deep-clone.mjs.map +1 -1
- package/dist/deep-merge.cjs +6 -6
- package/dist/deep-merge.mjs +3 -3
- package/dist/filter-empty.cjs +2 -2
- package/dist/filter-empty.mjs +1 -1
- package/dist/flatten-object.cjs +2 -3
- package/dist/flatten-object.d.cts +1 -1
- package/dist/flatten-object.d.mts +1 -1
- package/dist/flatten-object.mjs +1 -2
- package/dist/flatten-object.mjs.map +1 -1
- package/dist/get-field.cjs +4 -7
- package/dist/get-field.mjs +2 -5
- package/dist/get-field.mjs.map +1 -1
- package/dist/is-equal.cjs +4 -4
- package/dist/is-equal.mjs +2 -2
- package/dist/set-field.cjs +4 -4
- package/dist/set-field.d.cts +1 -1
- package/dist/set-field.d.mts +1 -1
- package/dist/set-field.mjs +2 -2
- package/dist/to-deep-key.cjs +4 -4
- package/dist/to-deep-key.mjs +2 -2
- package/dist/unflatten-object.d.cts +1 -1
- package/dist/unflatten-object.d.mts +1 -1
- package/package.json +7 -118
- package/dist/convert/src/index.cjs +0 -5
- package/dist/convert/src/index.mjs +0 -7
- package/dist/convert/src/parse-type-definition.cjs +0 -1
- package/dist/convert/src/parse-type-definition.mjs +0 -3
- package/dist/convert/src/string-to-utf8-array.cjs +0 -5
- package/dist/convert/src/string-to-utf8-array.mjs +0 -6
- package/dist/convert/src/string-to-utf8-array.mjs.map +0 -1
- package/dist/convert/src/to-bool.cjs +0 -1
- package/dist/convert/src/to-bool.mjs +0 -3
- package/dist/convert/src/to-string-key.cjs +0 -15
- package/dist/convert/src/to-string-key.mjs +0 -15
- package/dist/convert/src/to-string-key.mjs.map +0 -1
- package/dist/convert/src/utf8-array-to-string.cjs +0 -5
- package/dist/convert/src/utf8-array-to-string.mjs +0 -6
- package/dist/convert/src/utf8-array-to-string.mjs.map +0 -1
- package/dist/type-checks/src/get-object-tag.cjs +0 -15
- package/dist/type-checks/src/get-object-tag.mjs +0 -15
- package/dist/type-checks/src/get-object-tag.mjs.map +0 -1
- package/dist/type-checks/src/index.cjs +0 -21
- package/dist/type-checks/src/index.mjs +0 -23
- package/dist/type-checks/src/is-buffer.cjs +0 -13
- package/dist/type-checks/src/is-buffer.mjs +0 -14
- package/dist/type-checks/src/is-buffer.mjs.map +0 -1
- package/dist/type-checks/src/is-collection.cjs +0 -1
- package/dist/type-checks/src/is-collection.mjs +0 -3
- package/dist/type-checks/src/is-deep-key.cjs +0 -29
- package/dist/type-checks/src/is-deep-key.mjs +0 -29
- package/dist/type-checks/src/is-deep-key.mjs.map +0 -1
- package/dist/type-checks/src/is-empty.cjs +0 -20
- package/dist/type-checks/src/is-empty.mjs +0 -21
- package/dist/type-checks/src/is-empty.mjs.map +0 -1
- package/dist/type-checks/src/is-function.cjs +0 -27
- package/dist/type-checks/src/is-function.mjs +0 -26
- package/dist/type-checks/src/is-function.mjs.map +0 -1
- package/dist/type-checks/src/is-mergeable-object.cjs +0 -14
- package/dist/type-checks/src/is-mergeable-object.mjs +0 -15
- package/dist/type-checks/src/is-mergeable-object.mjs.map +0 -1
- package/dist/type-checks/src/is-non-null-object.cjs +0 -16
- package/dist/type-checks/src/is-non-null-object.mjs +0 -17
- package/dist/type-checks/src/is-non-null-object.mjs.map +0 -1
- package/dist/type-checks/src/is-null.cjs +0 -12
- package/dist/type-checks/src/is-null.mjs +0 -12
- package/dist/type-checks/src/is-null.mjs.map +0 -1
- package/dist/type-checks/src/is-number.cjs +0 -18
- package/dist/type-checks/src/is-number.mjs +0 -18
- package/dist/type-checks/src/is-number.mjs.map +0 -1
- package/dist/type-checks/src/is-object-index.cjs +0 -20
- package/dist/type-checks/src/is-object-index.mjs +0 -20
- package/dist/type-checks/src/is-object-index.mjs.map +0 -1
- package/dist/type-checks/src/is-object.cjs +0 -19
- package/dist/type-checks/src/is-object.mjs +0 -20
- package/dist/type-checks/src/is-object.mjs.map +0 -1
- package/dist/type-checks/src/is-plain-object.cjs +0 -64
- package/dist/type-checks/src/is-plain-object.mjs +0 -64
- package/dist/type-checks/src/is-plain-object.mjs.map +0 -1
- package/dist/type-checks/src/is-primitive.cjs +0 -12
- package/dist/type-checks/src/is-primitive.mjs +0 -12
- package/dist/type-checks/src/is-primitive.mjs.map +0 -1
- package/dist/type-checks/src/is-react-element.cjs +0 -8
- package/dist/type-checks/src/is-react-element.mjs +0 -8
- package/dist/type-checks/src/is-react-element.mjs.map +0 -1
- package/dist/type-checks/src/is-set-string.cjs +0 -20
- package/dist/type-checks/src/is-set-string.mjs +0 -21
- package/dist/type-checks/src/is-set-string.mjs.map +0 -1
- package/dist/type-checks/src/is-set.cjs +0 -19
- package/dist/type-checks/src/is-set.mjs +0 -20
- package/dist/type-checks/src/is-set.mjs.map +0 -1
- package/dist/type-checks/src/is-string.cjs +0 -12
- package/dist/type-checks/src/is-string.mjs +0 -12
- package/dist/type-checks/src/is-string.mjs.map +0 -1
- package/dist/type-checks/src/is-typed-array.cjs +0 -8
- package/dist/type-checks/src/is-typed-array.mjs +0 -8
- package/dist/type-checks/src/is-typed-array.mjs.map +0 -1
- package/dist/type-checks/src/is-undefined.cjs +0 -8
- package/dist/type-checks/src/is-undefined.mjs +0 -8
- package/dist/type-checks/src/is-undefined.mjs.map +0 -1
- package/dist/type-checks/src/property-exists.cjs +0 -31
- package/dist/type-checks/src/property-exists.mjs +0 -31
- package/dist/type-checks/src/property-exists.mjs.map +0 -1
- package/dist/type-checks/src/type-detect.cjs +0 -15
- package/dist/type-checks/src/type-detect.mjs +0 -17
- package/dist/type-checks/src/type-detect.mjs.map +0 -1
- package/dist/types/src/base.d.cts +0 -7
- package/dist/types/src/base.d.cts.map +0 -1
- package/dist/types/src/base.d.mts +0 -7
- package/dist/types/src/base.d.mts.map +0 -1
- package/dist/types/src/json.d.cts +0 -27
- package/dist/types/src/json.d.cts.map +0 -1
- package/dist/types/src/json.d.mts +0 -27
- package/dist/types/src/json.d.mts.map +0 -1
- package/dist/types/src/object.d.cts +0 -124
- package/dist/types/src/object.d.cts.map +0 -1
- package/dist/types/src/object.d.mts +0 -124
- package/dist/types/src/object.d.mts.map +0 -1
- package/dist/types/src/string.d.cts +0 -5
- package/dist/types/src/string.d.cts.map +0 -1
- package/dist/types/src/string.d.mts +0 -5
- package/dist/types/src/string.d.mts.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
# Changelog for Stryke - Helpers
|
|
4
4
|
|
|
5
|
+
## [0.10.15](https://github.com/storm-software/stryke/releases/tag/helpers%400.10.15) (05/08/2026)
|
|
6
|
+
|
|
7
|
+
### Updated Dependencies
|
|
8
|
+
|
|
9
|
+
- Updated **type-checks** to **v0.6.8**
|
|
10
|
+
- Updated **convert** to **v0.7.6**
|
|
11
|
+
- Updated **types** to **v0.12.3**
|
|
12
|
+
|
|
13
|
+
## [0.10.14](https://github.com/storm-software/stryke/releases/tag/helpers%400.10.14) (05/07/2026)
|
|
14
|
+
|
|
15
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
- **monorepo:** Resolve inter-package dependency issues ([e83c91bc](https://github.com/storm-software/stryke/commit/e83c91bc))
|
|
18
|
+
|
|
19
|
+
### Updated Dependencies
|
|
20
|
+
|
|
21
|
+
- Updated **type-checks** to **v0.6.7**
|
|
22
|
+
- Updated **convert** to **v0.7.5**
|
|
23
|
+
- Updated **types** to **v0.12.2**
|
|
24
|
+
|
|
5
25
|
## [0.10.13](https://github.com/storm-software/stryke/releases/tag/helpers%400.10.13) (05/06/2026)
|
|
6
26
|
|
|
7
27
|
### Miscellaneous
|
package/dist/deep-clone.cjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
3
|
-
const require_is_typed_array = require('./type-checks/src/is-typed-array.cjs');
|
|
4
|
-
require('./type-checks/src/index.cjs');
|
|
2
|
+
let _stryke_type_checks = require("@stryke/type-checks");
|
|
5
3
|
|
|
6
4
|
//#region src/deep-clone.ts
|
|
7
5
|
/**
|
|
@@ -52,7 +50,7 @@ require('./type-checks/src/index.cjs');
|
|
|
52
50
|
* @returns A deep clone of the given object.
|
|
53
51
|
*/
|
|
54
52
|
function deepClone(obj) {
|
|
55
|
-
if (
|
|
53
|
+
if ((0, _stryke_type_checks.isPrimitive)(obj)) return obj;
|
|
56
54
|
if (Array.isArray(obj)) return obj.map((item) => deepClone(item));
|
|
57
55
|
if (obj instanceof Date) return new Date(obj.getTime());
|
|
58
56
|
if (obj instanceof RegExp) return new RegExp(obj.source, obj.flags);
|
|
@@ -66,7 +64,7 @@ function deepClone(obj) {
|
|
|
66
64
|
for (const value of obj.values()) result.add(deepClone(value));
|
|
67
65
|
return result;
|
|
68
66
|
}
|
|
69
|
-
if (
|
|
67
|
+
if ((0, _stryke_type_checks.isTypedArray)(obj)) {
|
|
70
68
|
const result = new (Object.getPrototypeOf(obj)).constructor(obj.length);
|
|
71
69
|
for (const [i, element] of obj.entries()) result[i] = deepClone(element);
|
|
72
70
|
return result;
|
package/dist/deep-clone.mjs
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { isPrimitive } from "
|
|
2
|
-
import { isTypedArray } from "./type-checks/src/is-typed-array.mjs";
|
|
3
|
-
import "./type-checks/src/index.mjs";
|
|
1
|
+
import { isPrimitive, isTypedArray } from "@stryke/type-checks";
|
|
4
2
|
|
|
5
3
|
//#region src/deep-clone.ts
|
|
6
4
|
/**
|
package/dist/deep-clone.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deep-clone.mjs","names":[],"sources":["../src/deep-clone.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\nimport { isPrimitive, isTypedArray } from \"@stryke/type-checks\";\n\nexport type Resolved<T> =\n Equal<T, ResolvedMain<T>> extends true ? T : ResolvedMain<T>;\n\ntype Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;\n\ntype ResolvedMain<T> = T extends [never]\n ? never // (special trick for jsonable | null) type\n : ValueOf<T> extends boolean | number | bigint | string\n ? ValueOf<T>\n : T extends (...args: any[]) => any\n ? never\n : T extends object\n ? ResolvedObject<T>\n : ValueOf<T>;\n\ntype ResolvedObject<T extends object> = T extends (infer U)[]\n ? IsTuple<T> extends true\n ? ResolvedTuple<T>\n : ResolvedMain<U>[]\n : T extends Set<infer U>\n ? Set<ResolvedMain<U>>\n : T extends Map<infer K, infer V>\n ? Map<ResolvedMain<K>, ResolvedMain<V>>\n : T extends WeakSet<any> | WeakMap<any, any>\n ? never\n : T extends\n | Date\n | Uint8Array\n | Uint8ClampedArray\n | Uint16Array\n | Uint32Array\n | BigUint64Array\n | Int8Array\n | Int16Array\n | Int32Array\n | BigInt64Array\n | Float32Array\n | Float64Array\n | ArrayBuffer\n | SharedArrayBuffer\n | DataView\n | Blob\n | File\n ? T\n : {\n [P in keyof T]: ResolvedMain<T[P]>;\n };\n\ntype ResolvedTuple<T extends readonly any[]> = T extends []\n ? []\n : T extends [infer F]\n ? [ResolvedMain<F>]\n : T extends [infer F, ...infer Rest extends readonly any[]]\n ? [ResolvedMain<F>, ...ResolvedTuple<Rest>]\n : T extends [(infer F)?]\n ? [ResolvedMain<F>?]\n : T extends [(infer F)?, ...infer Rest extends readonly any[]]\n ? [ResolvedMain<F>?, ...ResolvedTuple<Rest>]\n : [];\n\ntype IsTuple<T extends readonly any[] | { length: number }> = [T] extends [\n never\n]\n ? false\n : T extends readonly any[]\n ? number extends T[\"length\"]\n ? false\n : true\n : false;\n\ntype ValueOf<Instance> =\n IsValueOf<Instance, boolean> extends true\n ? boolean\n : IsValueOf<Instance, number> extends true\n ? number\n : IsValueOf<Instance, string> extends true\n ? string\n : Instance;\n\ntype IsValueOf<Instance, O extends ValueOfInterface<any>> = Instance extends O\n ? O extends ValueOfInterface<infer Primitive>\n ? Instance extends Primitive\n ? false\n : true // not Primitive, but Object\n : false // cannot be\n : false;\n\ninterface ValueOfInterface<T> {\n valueOf: () => T;\n}\n\n/**\n * Creates a deep clone of the given object.\n *\n * @remarks\n * This function creates a deep clone of the given object, including nested objects and arrays. The resulting output will be of type `Resolved<T>`, which is a type that resolves to the most specific type possible for the given input type `T`. **If you are just looking for a way to copy an object deeply, use {@link deepCopy} instead.**\n *\n * @example\n * ```typescript\n * // Clone a primitive values\n * const num = 29;\n * const clonedNum = clone(num);\n * console.log(clonedNum); // 29\n * console.log(clonedNum === num) ; // true\n *\n * // Clone an array\n * const arr = [1, 2, 3];\n * const clonedArr = clone(arr);\n * console.log(clonedArr); // [1, 2, 3]\n * console.log(clonedArr === arr); // false\n *\n * // Clone an array with nested objects\n * const arr = [1, { a: 1 }, [1, 2, 3]];\n * const clonedArr = clone(arr);\n * arr[1].a = 2;\n * console.log(arr); // [2, { a: 2 }, [1, 2, 3]]\n * console.log(clonedArr); // [1, { a: 1 }, [1, 2, 3]]\n * console.log(clonedArr === arr); // false\n *\n * // Clone an object\n * const obj = { a: 1, b: 'es-toolkit', c: [1, 2, 3] };\n * const clonedObj = clone(obj);\n * console.log(clonedObj); // { a: 1, b: 'es-toolkit', c: [1, 2, 3] }\n * console.log(clonedObj === obj); // false\n *\n *\n * // Clone an object with nested objects\n * const obj = { a: 1, b: { c: 1 } };\n * const clonedObj = clone(obj);\n * obj.b.c = 2;\n * console.log(obj); // { a: 1, b: { c: 2 } }\n * console.log(clonedObj); // { a: 1, b: { c: 1 } }\n * console.log(clonedObj === obj); // false\n * ```\n *\n * @param obj - The object to clone.\n * @returns A deep clone of the given object.\n */\nexport function deepClone<T>(obj: T): Resolved<T> {\n if (isPrimitive(obj)) {\n return obj as Resolved<T>;\n }\n\n if (Array.isArray(obj)) {\n return obj.map(item => deepClone(item)) as Resolved<T>;\n }\n\n if (obj instanceof Date) {\n return new Date(obj.getTime()) as Resolved<T>;\n }\n\n if (obj instanceof RegExp) {\n return new RegExp(obj.source, obj.flags) as Resolved<T>;\n }\n\n if (obj instanceof Map) {\n const result = new Map();\n for (const [key, value] of obj.entries()) {\n result.set(key, deepClone(value));\n }\n return result as Resolved<T>;\n }\n\n if (obj instanceof Set) {\n const result = new Set();\n for (const value of obj.values()) {\n result.add(deepClone(value));\n }\n return result as Resolved<T>;\n }\n\n if (isTypedArray(obj)) {\n // eslint-disable-next-line ts/no-unsafe-call\n const result = new (Object.getPrototypeOf(obj).constructor)(obj.length);\n\n for (const [i, element] of obj.entries()) {\n result[i] = deepClone(element);\n }\n return result as Resolved<T>;\n }\n\n if (\n obj instanceof ArrayBuffer ||\n (typeof SharedArrayBuffer !== \"undefined\" &&\n obj instanceof SharedArrayBuffer)\n ) {\n return [...(obj as any)] as Resolved<T>;\n }\n\n if (obj instanceof DataView) {\n const result = new DataView([...(obj.buffer as any)] as any);\n cloneDeepHelper(obj, result);\n return result as Resolved<T>;\n }\n\n // For legacy NodeJS support\n if (typeof File !== \"undefined\" && obj instanceof File) {\n const result = new File([obj], obj.name, { type: obj.type });\n cloneDeepHelper(obj, result);\n return result as Resolved<T>;\n }\n\n if (obj instanceof Blob) {\n const result = new Blob([obj], { type: obj.type });\n cloneDeepHelper(obj, result);\n return result as Resolved<T>;\n }\n\n if (obj instanceof Error) {\n const result = new (obj.constructor as new () => Error)();\n result.message = obj.message;\n result.name = obj.name;\n result.stack = obj.stack;\n result.cause = obj.cause;\n cloneDeepHelper(obj, result);\n return result as Resolved<T>;\n }\n\n if (typeof obj === \"object\" && obj !== null) {\n const result = {};\n cloneDeepHelper(obj, result);\n return result as Resolved<T>;\n }\n\n return obj as Resolved<T>;\n}\n\nfunction cloneDeepHelper(obj: any, clonedObj: any): void {\n const keys = Object.keys(obj);\n\n for (const key of keys) {\n const descriptor = Object.getOwnPropertyDescriptor(obj, key);\n\n if (descriptor?.writable || descriptor?.set) {\n clonedObj[key] = deepClone(obj[key]);\n }\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"deep-clone.mjs","names":[],"sources":["../src/deep-clone.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\nimport { isPrimitive, isTypedArray } from \"@stryke/type-checks\";\n\nexport type Resolved<T> =\n Equal<T, ResolvedMain<T>> extends true ? T : ResolvedMain<T>;\n\ntype Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;\n\ntype ResolvedMain<T> = T extends [never]\n ? never // (special trick for jsonable | null) type\n : ValueOf<T> extends boolean | number | bigint | string\n ? ValueOf<T>\n : T extends (...args: any[]) => any\n ? never\n : T extends object\n ? ResolvedObject<T>\n : ValueOf<T>;\n\ntype ResolvedObject<T extends object> = T extends (infer U)[]\n ? IsTuple<T> extends true\n ? ResolvedTuple<T>\n : ResolvedMain<U>[]\n : T extends Set<infer U>\n ? Set<ResolvedMain<U>>\n : T extends Map<infer K, infer V>\n ? Map<ResolvedMain<K>, ResolvedMain<V>>\n : T extends WeakSet<any> | WeakMap<any, any>\n ? never\n : T extends\n | Date\n | Uint8Array\n | Uint8ClampedArray\n | Uint16Array\n | Uint32Array\n | BigUint64Array\n | Int8Array\n | Int16Array\n | Int32Array\n | BigInt64Array\n | Float32Array\n | Float64Array\n | ArrayBuffer\n | SharedArrayBuffer\n | DataView\n | Blob\n | File\n ? T\n : {\n [P in keyof T]: ResolvedMain<T[P]>;\n };\n\ntype ResolvedTuple<T extends readonly any[]> = T extends []\n ? []\n : T extends [infer F]\n ? [ResolvedMain<F>]\n : T extends [infer F, ...infer Rest extends readonly any[]]\n ? [ResolvedMain<F>, ...ResolvedTuple<Rest>]\n : T extends [(infer F)?]\n ? [ResolvedMain<F>?]\n : T extends [(infer F)?, ...infer Rest extends readonly any[]]\n ? [ResolvedMain<F>?, ...ResolvedTuple<Rest>]\n : [];\n\ntype IsTuple<T extends readonly any[] | { length: number }> = [T] extends [\n never\n]\n ? false\n : T extends readonly any[]\n ? number extends T[\"length\"]\n ? false\n : true\n : false;\n\ntype ValueOf<Instance> =\n IsValueOf<Instance, boolean> extends true\n ? boolean\n : IsValueOf<Instance, number> extends true\n ? number\n : IsValueOf<Instance, string> extends true\n ? string\n : Instance;\n\ntype IsValueOf<Instance, O extends ValueOfInterface<any>> = Instance extends O\n ? O extends ValueOfInterface<infer Primitive>\n ? Instance extends Primitive\n ? false\n : true // not Primitive, but Object\n : false // cannot be\n : false;\n\ninterface ValueOfInterface<T> {\n valueOf: () => T;\n}\n\n/**\n * Creates a deep clone of the given object.\n *\n * @remarks\n * This function creates a deep clone of the given object, including nested objects and arrays. The resulting output will be of type `Resolved<T>`, which is a type that resolves to the most specific type possible for the given input type `T`. **If you are just looking for a way to copy an object deeply, use {@link deepCopy} instead.**\n *\n * @example\n * ```typescript\n * // Clone a primitive values\n * const num = 29;\n * const clonedNum = clone(num);\n * console.log(clonedNum); // 29\n * console.log(clonedNum === num) ; // true\n *\n * // Clone an array\n * const arr = [1, 2, 3];\n * const clonedArr = clone(arr);\n * console.log(clonedArr); // [1, 2, 3]\n * console.log(clonedArr === arr); // false\n *\n * // Clone an array with nested objects\n * const arr = [1, { a: 1 }, [1, 2, 3]];\n * const clonedArr = clone(arr);\n * arr[1].a = 2;\n * console.log(arr); // [2, { a: 2 }, [1, 2, 3]]\n * console.log(clonedArr); // [1, { a: 1 }, [1, 2, 3]]\n * console.log(clonedArr === arr); // false\n *\n * // Clone an object\n * const obj = { a: 1, b: 'es-toolkit', c: [1, 2, 3] };\n * const clonedObj = clone(obj);\n * console.log(clonedObj); // { a: 1, b: 'es-toolkit', c: [1, 2, 3] }\n * console.log(clonedObj === obj); // false\n *\n *\n * // Clone an object with nested objects\n * const obj = { a: 1, b: { c: 1 } };\n * const clonedObj = clone(obj);\n * obj.b.c = 2;\n * console.log(obj); // { a: 1, b: { c: 2 } }\n * console.log(clonedObj); // { a: 1, b: { c: 1 } }\n * console.log(clonedObj === obj); // false\n * ```\n *\n * @param obj - The object to clone.\n * @returns A deep clone of the given object.\n */\nexport function deepClone<T>(obj: T): Resolved<T> {\n if (isPrimitive(obj)) {\n return obj as Resolved<T>;\n }\n\n if (Array.isArray(obj)) {\n return obj.map(item => deepClone(item)) as Resolved<T>;\n }\n\n if (obj instanceof Date) {\n return new Date(obj.getTime()) as Resolved<T>;\n }\n\n if (obj instanceof RegExp) {\n return new RegExp(obj.source, obj.flags) as Resolved<T>;\n }\n\n if (obj instanceof Map) {\n const result = new Map();\n for (const [key, value] of obj.entries()) {\n result.set(key, deepClone(value));\n }\n return result as Resolved<T>;\n }\n\n if (obj instanceof Set) {\n const result = new Set();\n for (const value of obj.values()) {\n result.add(deepClone(value));\n }\n return result as Resolved<T>;\n }\n\n if (isTypedArray(obj)) {\n // eslint-disable-next-line ts/no-unsafe-call\n const result = new (Object.getPrototypeOf(obj).constructor)(obj.length);\n\n for (const [i, element] of obj.entries()) {\n result[i] = deepClone(element);\n }\n return result as Resolved<T>;\n }\n\n if (\n obj instanceof ArrayBuffer ||\n (typeof SharedArrayBuffer !== \"undefined\" &&\n obj instanceof SharedArrayBuffer)\n ) {\n return [...(obj as any)] as Resolved<T>;\n }\n\n if (obj instanceof DataView) {\n const result = new DataView([...(obj.buffer as any)] as any);\n cloneDeepHelper(obj, result);\n return result as Resolved<T>;\n }\n\n // For legacy NodeJS support\n if (typeof File !== \"undefined\" && obj instanceof File) {\n const result = new File([obj], obj.name, { type: obj.type });\n cloneDeepHelper(obj, result);\n return result as Resolved<T>;\n }\n\n if (obj instanceof Blob) {\n const result = new Blob([obj], { type: obj.type });\n cloneDeepHelper(obj, result);\n return result as Resolved<T>;\n }\n\n if (obj instanceof Error) {\n const result = new (obj.constructor as new () => Error)();\n result.message = obj.message;\n result.name = obj.name;\n result.stack = obj.stack;\n result.cause = obj.cause;\n cloneDeepHelper(obj, result);\n return result as Resolved<T>;\n }\n\n if (typeof obj === \"object\" && obj !== null) {\n const result = {};\n cloneDeepHelper(obj, result);\n return result as Resolved<T>;\n }\n\n return obj as Resolved<T>;\n}\n\nfunction cloneDeepHelper(obj: any, clonedObj: any): void {\n const keys = Object.keys(obj);\n\n for (const key of keys) {\n const descriptor = Object.getOwnPropertyDescriptor(obj, key);\n\n if (descriptor?.writable || descriptor?.set) {\n clonedObj[key] = deepClone(obj[key]);\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8JA,SAAgB,UAAa,KAAqB;AAChD,KAAI,YAAY,IAAI,CAClB,QAAO;AAGT,KAAI,MAAM,QAAQ,IAAI,CACpB,QAAO,IAAI,KAAI,SAAQ,UAAU,KAAK,CAAC;AAGzC,KAAI,eAAe,KACjB,QAAO,IAAI,KAAK,IAAI,SAAS,CAAC;AAGhC,KAAI,eAAe,OACjB,QAAO,IAAI,OAAO,IAAI,QAAQ,IAAI,MAAM;AAG1C,KAAI,eAAe,KAAK;EACtB,MAAM,yBAAS,IAAI,KAAK;AACxB,OAAK,MAAM,CAAC,KAAK,UAAU,IAAI,SAAS,CACtC,QAAO,IAAI,KAAK,UAAU,MAAM,CAAC;AAEnC,SAAO;;AAGT,KAAI,eAAe,KAAK;EACtB,MAAM,yBAAS,IAAI,KAAK;AACxB,OAAK,MAAM,SAAS,IAAI,QAAQ,CAC9B,QAAO,IAAI,UAAU,MAAM,CAAC;AAE9B,SAAO;;AAGT,KAAI,aAAa,IAAI,EAAE;EAErB,MAAM,SAAS,KAAK,OAAO,eAAe,IAAI,EAAC,YAAa,IAAI,OAAO;AAEvE,OAAK,MAAM,CAAC,GAAG,YAAY,IAAI,SAAS,CACtC,QAAO,KAAK,UAAU,QAAQ;AAEhC,SAAO;;AAGT,KACE,eAAe,eACd,OAAO,sBAAsB,eAC5B,eAAe,kBAEjB,QAAO,CAAC,GAAI,IAAY;AAG1B,KAAI,eAAe,UAAU;EAC3B,MAAM,SAAS,IAAI,SAAS,CAAC,GAAI,IAAI,OAAe,CAAQ;AAC5D,kBAAgB,KAAK,OAAO;AAC5B,SAAO;;AAIT,KAAI,OAAO,SAAS,eAAe,eAAe,MAAM;EACtD,MAAM,SAAS,IAAI,KAAK,CAAC,IAAI,EAAE,IAAI,MAAM,EAAE,MAAM,IAAI,MAAM,CAAC;AAC5D,kBAAgB,KAAK,OAAO;AAC5B,SAAO;;AAGT,KAAI,eAAe,MAAM;EACvB,MAAM,SAAS,IAAI,KAAK,CAAC,IAAI,EAAE,EAAE,MAAM,IAAI,MAAM,CAAC;AAClD,kBAAgB,KAAK,OAAO;AAC5B,SAAO;;AAGT,KAAI,eAAe,OAAO;EACxB,MAAM,SAAS,IAAK,IAAI,aAAiC;AACzD,SAAO,UAAU,IAAI;AACrB,SAAO,OAAO,IAAI;AAClB,SAAO,QAAQ,IAAI;AACnB,SAAO,QAAQ,IAAI;AACnB,kBAAgB,KAAK,OAAO;AAC5B,SAAO;;AAGT,KAAI,OAAO,QAAQ,YAAY,QAAQ,MAAM;EAC3C,MAAM,SAAS,EAAE;AACjB,kBAAgB,KAAK,OAAO;AAC5B,SAAO;;AAGT,QAAO;;AAGT,SAAS,gBAAgB,KAAU,WAAsB;CACvD,MAAM,OAAO,OAAO,KAAK,IAAI;AAE7B,MAAK,MAAM,OAAO,MAAM;EACtB,MAAM,aAAa,OAAO,yBAAyB,KAAK,IAAI;AAE5D,MAAI,YAAY,YAAY,YAAY,IACtC,WAAU,OAAO,UAAU,IAAI,KAAK"}
|
package/dist/deep-merge.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
let _stryke_type_checks_is_function = require("@stryke/type-checks/is-function");
|
|
3
|
+
let _stryke_type_checks_is_mergeable_object = require("@stryke/type-checks/is-mergeable-object");
|
|
4
|
+
let _stryke_type_checks_property_exists = require("@stryke/type-checks/property-exists");
|
|
5
5
|
|
|
6
6
|
//#region src/deep-merge.ts
|
|
7
7
|
const emptyTarget = (val) => {
|
|
@@ -18,7 +18,7 @@ const defaultArrayMerge = (target, source, options) => {
|
|
|
18
18
|
const getMergeFunction = (key, options) => {
|
|
19
19
|
if (!options.customMerge) return deepMerge;
|
|
20
20
|
const customMerge = options.customMerge(key);
|
|
21
|
-
return
|
|
21
|
+
return (0, _stryke_type_checks_is_function.isFunction)(customMerge) ? customMerge : deepMerge;
|
|
22
22
|
};
|
|
23
23
|
const getKeys = (target) => {
|
|
24
24
|
return [...Object.keys(target), ...Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(target).filter((symbol) => {
|
|
@@ -28,7 +28,7 @@ const getKeys = (target) => {
|
|
|
28
28
|
const mergeObject = (target, source, options) => {
|
|
29
29
|
const destination = {};
|
|
30
30
|
if (options.isMergeableObject(target)) for (const key of getKeys(target)) destination[key] = cloneUnlessOtherwiseSpecified(target[key], options);
|
|
31
|
-
for (const key of getKeys(source)) destination[key] =
|
|
31
|
+
for (const key of getKeys(source)) destination[key] = (0, _stryke_type_checks_property_exists.propertyUnsafe)(target, key) && options.isMergeableObject(source[key]) ? getMergeFunction(key, options)(target[key], source[key], options) : cloneUnlessOtherwiseSpecified(source[key], options);
|
|
32
32
|
return destination;
|
|
33
33
|
};
|
|
34
34
|
/**
|
|
@@ -43,7 +43,7 @@ function deepMerge(target, source, options) {
|
|
|
43
43
|
if (!target || !source) return target || source;
|
|
44
44
|
const opts = options ?? {};
|
|
45
45
|
opts.arrayMerge ??= defaultArrayMerge;
|
|
46
|
-
opts.isMergeableObject ??=
|
|
46
|
+
opts.isMergeableObject ??= _stryke_type_checks_is_mergeable_object.isMergeableObject;
|
|
47
47
|
opts.cloneUnlessOtherwiseSpecified ??= cloneUnlessOtherwiseSpecified;
|
|
48
48
|
const sourceIsArray = Array.isArray(source);
|
|
49
49
|
if (!(sourceIsArray === Array.isArray(target))) return cloneUnlessOtherwiseSpecified(source, opts);
|
package/dist/deep-merge.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { isFunction } from "
|
|
2
|
-
import { isMergeableObject } from "
|
|
3
|
-
import { propertyUnsafe } from "
|
|
1
|
+
import { isFunction } from "@stryke/type-checks/is-function";
|
|
2
|
+
import { isMergeableObject } from "@stryke/type-checks/is-mergeable-object";
|
|
3
|
+
import { propertyUnsafe } from "@stryke/type-checks/property-exists";
|
|
4
4
|
|
|
5
5
|
//#region src/deep-merge.ts
|
|
6
6
|
const emptyTarget = (val) => {
|
package/dist/filter-empty.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
2
|
+
let _stryke_type_checks_is_empty = require("@stryke/type-checks/is-empty");
|
|
3
3
|
|
|
4
4
|
//#region src/filter-empty.ts
|
|
5
|
-
const filterEmpty = (values = []) => values.filter((value) => !
|
|
5
|
+
const filterEmpty = (values = []) => values.filter((value) => !(0, _stryke_type_checks_is_empty.isEmpty)(value));
|
|
6
6
|
|
|
7
7
|
//#endregion
|
|
8
8
|
exports.filterEmpty = filterEmpty;
|
package/dist/filter-empty.mjs
CHANGED
package/dist/flatten-object.cjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
3
|
-
require('./type-checks/src/index.cjs');
|
|
2
|
+
let _stryke_type_checks = require("@stryke/type-checks");
|
|
4
3
|
|
|
5
4
|
//#region src/flatten-object.ts
|
|
6
5
|
/**
|
|
@@ -39,7 +38,7 @@ function flattenObjectImpl(object, prefix = "") {
|
|
|
39
38
|
for (const key of keys) {
|
|
40
39
|
const value = object[key];
|
|
41
40
|
const prefixedKey = prefix ? `${prefix}.${key}` : key;
|
|
42
|
-
if (
|
|
41
|
+
if ((0, _stryke_type_checks.isPlainObject)(value) && Object.keys(value).length > 0) Object.assign(result, flattenObjectImpl(value, prefixedKey));
|
|
43
42
|
else if (Array.isArray(value)) for (const [index, element] of value.entries()) result[`${prefixedKey}.${index}`] = element;
|
|
44
43
|
else result[prefixedKey] = value;
|
|
45
44
|
}
|
package/dist/flatten-object.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flatten-object.mjs","names":[],"sources":["../src/flatten-object.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\nimport { isPlainObject } from \"@stryke/type-checks\";\nimport type { DeepKey, DeepValue } from \"@stryke/types\";\n\n/**\n * Flattens a nested object into a single level object with dot-separated keys.\n *\n * @example\n * ```typescript\n * const nestedObject = {\n * a: {\n * b: {\n * c: 1\n * }\n * },\n * d: [2, 3]\n * };\n *\n * const flattened = flattenObject(nestedObject);\n * console.log(flattened);\n * // Output:\n * // {\n * // 'a.b.c': 1,\n * // 'd.0': 2,\n * // 'd.1': 3\n * // }\n * ```\n *\n * @param object - The object to flatten.\n * @returns - The flattened object.\n */\nexport function flattenObject<\n TObject extends Record<string, any> = Record<string, any>,\n TDeepKeyObject extends {\n [TKey in DeepKey<TObject>]: DeepValue<TObject, TKey>;\n } = { [TKey in DeepKey<TObject>]: DeepValue<TObject, TKey> }\n>(object: TObject): TDeepKeyObject {\n return flattenObjectImpl<TObject, TDeepKeyObject>(object);\n}\n\nfunction flattenObjectImpl<\n TObject extends Record<string, any> = Record<string, any>,\n TDeepKeyObject extends {\n [TKey in DeepKey<TObject>]: DeepValue<TObject, TKey>;\n } = { [TKey in DeepKey<TObject>]: DeepValue<TObject, TKey> }\n>(object: TObject, prefix = \"\"): TDeepKeyObject {\n const result = {} as TDeepKeyObject;\n const keys = Object.keys(object);\n\n for (const key of keys) {\n const value = (object as any)[key];\n\n const prefixedKey = prefix ? `${prefix}.${key}` : key;\n\n if (isPlainObject(value) && Object.keys(value).length > 0) {\n Object.assign(\n result,\n flattenObjectImpl<typeof value>(value, prefixedKey)\n );\n } else if (Array.isArray(value)) {\n for (const [index, element] of value.entries()) {\n (result as any)[`${prefixedKey}.${index}` as DeepKey<TObject>] =\n element;\n }\n } else {\n (result as any)[prefixedKey as DeepKey<TObject>] = value;\n }\n }\n\n return result;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"flatten-object.mjs","names":[],"sources":["../src/flatten-object.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\nimport { isPlainObject } from \"@stryke/type-checks\";\nimport type { DeepKey, DeepValue } from \"@stryke/types\";\n\n/**\n * Flattens a nested object into a single level object with dot-separated keys.\n *\n * @example\n * ```typescript\n * const nestedObject = {\n * a: {\n * b: {\n * c: 1\n * }\n * },\n * d: [2, 3]\n * };\n *\n * const flattened = flattenObject(nestedObject);\n * console.log(flattened);\n * // Output:\n * // {\n * // 'a.b.c': 1,\n * // 'd.0': 2,\n * // 'd.1': 3\n * // }\n * ```\n *\n * @param object - The object to flatten.\n * @returns - The flattened object.\n */\nexport function flattenObject<\n TObject extends Record<string, any> = Record<string, any>,\n TDeepKeyObject extends {\n [TKey in DeepKey<TObject>]: DeepValue<TObject, TKey>;\n } = { [TKey in DeepKey<TObject>]: DeepValue<TObject, TKey> }\n>(object: TObject): TDeepKeyObject {\n return flattenObjectImpl<TObject, TDeepKeyObject>(object);\n}\n\nfunction flattenObjectImpl<\n TObject extends Record<string, any> = Record<string, any>,\n TDeepKeyObject extends {\n [TKey in DeepKey<TObject>]: DeepValue<TObject, TKey>;\n } = { [TKey in DeepKey<TObject>]: DeepValue<TObject, TKey> }\n>(object: TObject, prefix = \"\"): TDeepKeyObject {\n const result = {} as TDeepKeyObject;\n const keys = Object.keys(object);\n\n for (const key of keys) {\n const value = (object as any)[key];\n\n const prefixedKey = prefix ? `${prefix}.${key}` : key;\n\n if (isPlainObject(value) && Object.keys(value).length > 0) {\n Object.assign(\n result,\n flattenObjectImpl<typeof value>(value, prefixedKey)\n );\n } else if (Array.isArray(value)) {\n for (const [index, element] of value.entries()) {\n (result as any)[`${prefixedKey}.${index}` as DeepKey<TObject>] =\n element;\n }\n } else {\n (result as any)[prefixedKey as DeepKey<TObject>] = value;\n }\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,cAKd,QAAiC;AACjC,QAAO,kBAA2C,OAAO;;AAG3D,SAAS,kBAKP,QAAiB,SAAS,IAAoB;CAC9C,MAAM,SAAS,EAAE;CACjB,MAAM,OAAO,OAAO,KAAK,OAAO;AAEhC,MAAK,MAAM,OAAO,MAAM;EACtB,MAAM,QAAS,OAAe;EAE9B,MAAM,cAAc,SAAS,GAAG,OAAO,GAAG,QAAQ;AAElD,MAAI,cAAc,MAAM,IAAI,OAAO,KAAK,MAAM,CAAC,SAAS,EACtD,QAAO,OACL,QACA,kBAAgC,OAAO,YAAY,CACpD;WACQ,MAAM,QAAQ,MAAM,CAC7B,MAAK,MAAM,CAAC,OAAO,YAAY,MAAM,SAAS,CAC5C,CAAC,OAAe,GAAG,YAAY,GAAG,WAChC;MAGJ,CAAC,OAAe,eAAmC;;AAIvD,QAAO"}
|
package/dist/get-field.cjs
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_is_number = require('./type-checks/src/is-number.cjs');
|
|
3
|
-
const require_is_deep_key = require('./type-checks/src/is-deep-key.cjs');
|
|
4
|
-
require('./type-checks/src/index.cjs');
|
|
5
|
-
const require_to_string_key = require('./convert/src/to-string-key.cjs');
|
|
6
|
-
require('./convert/src/index.cjs');
|
|
7
2
|
const require_to_path = require('./to-path.cjs');
|
|
3
|
+
let _stryke_type_checks = require("@stryke/type-checks");
|
|
4
|
+
let _stryke_convert = require("@stryke/convert");
|
|
8
5
|
|
|
9
6
|
//#region src/get-field.ts
|
|
10
7
|
/**
|
|
@@ -21,14 +18,14 @@ function getField(object, path, defaultValue) {
|
|
|
21
18
|
case "string": {
|
|
22
19
|
const result = object[path];
|
|
23
20
|
if (result === void 0) {
|
|
24
|
-
if (
|
|
21
|
+
if ((0, _stryke_type_checks.isDeepKey)(path)) return getField(object, require_to_path.toPath(path), defaultValue);
|
|
25
22
|
return defaultValue;
|
|
26
23
|
}
|
|
27
24
|
return result;
|
|
28
25
|
}
|
|
29
26
|
case "number":
|
|
30
27
|
case "symbol": {
|
|
31
|
-
if (
|
|
28
|
+
if ((0, _stryke_type_checks.isNumber)(path)) path = (0, _stryke_convert.toStringKey)(path);
|
|
32
29
|
const result = Array.isArray(path) ? void 0 : object[path];
|
|
33
30
|
if (result === void 0) return defaultValue;
|
|
34
31
|
return result;
|
package/dist/get-field.mjs
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import { isNumber } from "./type-checks/src/is-number.mjs";
|
|
2
|
-
import { isDeepKey } from "./type-checks/src/is-deep-key.mjs";
|
|
3
|
-
import "./type-checks/src/index.mjs";
|
|
4
|
-
import { toStringKey } from "./convert/src/to-string-key.mjs";
|
|
5
|
-
import "./convert/src/index.mjs";
|
|
6
1
|
import { toPath } from "./to-path.mjs";
|
|
2
|
+
import { isDeepKey, isNumber } from "@stryke/type-checks";
|
|
3
|
+
import { toStringKey } from "@stryke/convert";
|
|
7
4
|
|
|
8
5
|
//#region src/get-field.ts
|
|
9
6
|
/**
|
package/dist/get-field.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-field.mjs","names":[],"sources":["../src/get-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 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\nimport { toStringKey } from \"@stryke/convert\";\nimport { isDeepKey, isNumber } from \"@stryke/type-checks\";\nimport { toPath } from \"./to-path\";\n\n/**\n * See the definition of `@types/lodash`.\n */\ntype GetIndexedField<T, K> = K extends keyof T\n ? T[K]\n : K extends `${number}`\n ? \"length\" extends keyof T\n ? number extends T[\"length\"]\n ? number extends keyof T\n ? T[number]\n : undefined\n : undefined\n : undefined\n : undefined;\n\ntype FieldWithPossiblyUndefined<T, Key> =\n | GetField<Exclude<T, undefined>, Key>\n | Extract<T, undefined>;\n\ntype IndexedFieldWithPossiblyUndefined<T, Key> =\n | GetIndexedField<Exclude<T, undefined>, Key>\n | Extract<T, undefined>;\n\nexport type GetField<T, P> = P extends `${infer Left}.${infer Right}`\n ? Left extends keyof Exclude<T, undefined>\n ?\n | FieldWithPossiblyUndefined<Exclude<T, undefined>[Left], Right>\n | Extract<T, undefined>\n : Left extends `${infer FieldKey}[${infer IndexKey}]`\n ? FieldKey extends keyof T\n ? FieldWithPossiblyUndefined<\n IndexedFieldWithPossiblyUndefined<T[FieldKey], IndexKey>,\n Right\n >\n : undefined\n : undefined\n : P extends keyof T\n ? T[P]\n : P extends `${infer FieldKey}[${infer IndexKey}]`\n ? FieldKey extends keyof T\n ? IndexedFieldWithPossiblyUndefined<T[FieldKey], IndexKey>\n : undefined\n : IndexedFieldWithPossiblyUndefined<T, P>;\n\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K - The type of the key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value.\n */\nexport function getField<T extends object, K extends keyof T>(\n object: T,\n path: K | readonly [K]\n): T[K];\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K - The type of the key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value or undefined.\n */\nexport function getField<T extends object, K extends keyof T>(\n object: T | null | undefined,\n path: K | readonly [K]\n): T[K] | undefined;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K - The type of the key in the object.\n * @typeParam D - The type of the default value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value or default value.\n */\nexport function getField<T extends object, K extends keyof T, D>(\n object: T | null | undefined,\n path: K | readonly [K],\n defaultValue: D\n): Exclude<T[K], undefined> | D;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1]\n>(object: T, path: readonly [K1, K2]): T[K1][K2];\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value or undefined.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1]\n>(object: T | null | undefined, path: readonly [K1, K2]): T[K1][K2] | undefined;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n * @typeParam D - The type of the default value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value or default value.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1],\n D\n>(\n object: T | null | undefined,\n path: readonly [K1, K2],\n defaultValue: D\n): Exclude<T[K1][K2], undefined> | D;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n * @typeParam K3 - The type of the third key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1],\n K3 extends keyof T[K1][K2]\n>(object: T, path: readonly [K1, K2, K3]): T[K1][K2][K3];\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n * @typeParam K3 - The type of the third key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value or undefined.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1],\n K3 extends keyof T[K1][K2]\n>(\n object: T | null | undefined,\n path: readonly [K1, K2, K3]\n): T[K1][K2][K3] | undefined;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n * @typeParam K3 - The type of the third key in the object.\n * @typeParam D - The type of the default value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value or default value.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1],\n K3 extends keyof T[K1][K2],\n D\n>(\n object: T | null | undefined,\n path: readonly [K1, K2, K3],\n defaultValue: D\n): Exclude<T[K1][K2][K3], undefined> | D;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n * @typeParam K3 - The type of the third key in the object.\n * @typeParam K4 - The type of the fourth key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1],\n K3 extends keyof T[K1][K2],\n K4 extends keyof T[K1][K2][K3]\n>(object: T, path: readonly [K1, K2, K3, K4]): T[K1][K2][K3][K4];\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n * @typeParam K3 - The type of the third key in the object.\n * @typeParam K4 - The type of the fourth key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value or undefined.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1],\n K3 extends keyof T[K1][K2],\n K4 extends keyof T[K1][K2][K3]\n>(\n object: T | null | undefined,\n path: readonly [K1, K2, K3, K4]\n): T[K1][K2][K3][K4] | undefined;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n * @typeParam K3 - The type of the third key in the object.\n * @typeParam K4 - The type of the fourth key in the object.\n * @typeParam D - The type of the default value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value or default value.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1],\n K3 extends keyof T[K1][K2],\n K4 extends keyof T[K1][K2][K3],\n D\n>(\n object: T | null | undefined,\n path: readonly [K1, K2, K3, K4],\n defaultValue: D\n): Exclude<T[K1][K2][K3][K4], undefined> | D;\n/**\n * Retrieves the value at a given path from an object with numeric keys. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value.\n */\nexport function getField<T>(object: Record<number, T>, path: number): T;\n/**\n * Retrieves the value at a given path from an object with numeric keys. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value or undefined.\n */\nexport function getField<T>(\n object: Record<number, T> | null | undefined,\n path: number\n): T | undefined;\n/**\n * Retrieves the value at a given path from an object with numeric keys. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the value.\n * @typeParam D - The type of the default value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value or default value.\n */\nexport function getField<T, D>(\n object: Record<number, T> | null | undefined,\n path: number,\n defaultValue: D\n): T | D;\n/**\n * Retrieves the value at a given path from a null or undefined object, returning the default value.\n *\n * @typeParam D - The type of the default value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The default value.\n */\nexport function getField<D>(\n object: null | undefined,\n path: PropertyKey,\n defaultValue: D\n): D;\n\n/**\n * Retrieves the value at a given path from a null or undefined object, returning undefined.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n */\nexport function getField(\n object: null | undefined,\n path: PropertyKey\n): undefined;\n\n/**\n * Retrieves the value at a given path from a string-keyed object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam P - The type of the path.\n *\n * @param data - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value, or any if path is a general string.\n */\nexport function getField<T, P extends string>(\n data: T,\n path: P\n): string extends P ? any : GetField<T, P>;\n/**\n * Retrieves the value at a given path from a string-keyed object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam P - The type of the path.\n * @typeParam D - The type of the default value.\n *\n * @param data - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value or default value.\n */\nexport function getField<T, P extends string, D = GetField<T, P>>(\n data: T,\n path: P,\n defaultValue: D\n): Exclude<GetField<T, P>, null | undefined> | D;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value.\n */\nexport function getField(\n object: unknown,\n path: PropertyKey | readonly PropertyKey[],\n defaultValue?: unknown\n): any;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value.\n */\nexport function getField(\n object: any,\n path: PropertyKey | readonly PropertyKey[],\n defaultValue?: any\n): any {\n if (object === null) {\n return defaultValue;\n }\n\n switch (typeof path) {\n case \"string\": {\n const result = object[path];\n\n if (result === undefined) {\n if (isDeepKey(path)) {\n return getField(object, toPath(path), defaultValue);\n }\n return defaultValue;\n }\n\n return result;\n }\n case \"number\":\n case \"symbol\": {\n if (isNumber(path)) {\n path = toStringKey(path);\n }\n\n const result = Array.isArray(path)\n ? undefined\n : object[path as PropertyKey];\n if (result === undefined) {\n return defaultValue;\n }\n\n return result;\n }\n case \"bigint\":\n case \"boolean\":\n case \"undefined\":\n case \"object\":\n case \"function\":\n default: {\n if (Array.isArray(path)) {\n return getWithPath(object, path, defaultValue);\n }\n\n path = Object.is(path?.valueOf(), -0) ? \"-0\" : String(path);\n\n const result = object[path];\n\n if (result === undefined) {\n return defaultValue;\n }\n\n return result;\n }\n }\n}\n\nfunction getWithPath(\n object: any,\n path: readonly PropertyKey[],\n defaultValue?: any\n): any {\n if (path.length === 0) {\n return defaultValue;\n }\n\n let current = object;\n\n for (const element of path) {\n if (current === null) {\n return defaultValue;\n }\n\n current = current[element];\n }\n\n if (current === undefined) {\n return defaultValue;\n }\n\n return current;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAkaA,SAAgB,SACd,QACA,MACA,cACK;AACL,KAAI,WAAW,KACb,QAAO;AAGT,SAAQ,OAAO,MAAf;EACE,KAAK,UAAU;GACb,MAAM,SAAS,OAAO;AAEtB,OAAI,WAAW,QAAW;AACxB,QAAI,UAAU,KAAK,CACjB,QAAO,SAAS,QAAQ,OAAO,KAAK,EAAE,aAAa;AAErD,WAAO;;AAGT,UAAO;;EAET,KAAK;EACL,KAAK,UAAU;AACb,OAAI,SAAS,KAAK,CAChB,QAAO,YAAY,KAAK;GAG1B,MAAM,SAAS,MAAM,QAAQ,KAAK,GAC9B,SACA,OAAO;AACX,OAAI,WAAW,OACb,QAAO;AAGT,UAAO;;EAOT,SAAS;AACP,OAAI,MAAM,QAAQ,KAAK,CACrB,QAAO,YAAY,QAAQ,MAAM,aAAa;AAGhD,UAAO,OAAO,GAAG,MAAM,SAAS,EAAE,GAAG,GAAG,OAAO,OAAO,KAAK;GAE3D,MAAM,SAAS,OAAO;AAEtB,OAAI,WAAW,OACb,QAAO;AAGT,UAAO;;;;AAKb,SAAS,YACP,QACA,MACA,cACK;AACL,KAAI,KAAK,WAAW,EAClB,QAAO;CAGT,IAAI,UAAU;AAEd,MAAK,MAAM,WAAW,MAAM;AAC1B,MAAI,YAAY,KACd,QAAO;AAGT,YAAU,QAAQ;;AAGpB,KAAI,YAAY,OACd,QAAO;AAGT,QAAO"}
|
|
1
|
+
{"version":3,"file":"get-field.mjs","names":[],"sources":["../src/get-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 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\nimport { toStringKey } from \"@stryke/convert\";\nimport { isDeepKey, isNumber } from \"@stryke/type-checks\";\nimport { toPath } from \"./to-path\";\n\n/**\n * See the definition of `@types/lodash`.\n */\ntype GetIndexedField<T, K> = K extends keyof T\n ? T[K]\n : K extends `${number}`\n ? \"length\" extends keyof T\n ? number extends T[\"length\"]\n ? number extends keyof T\n ? T[number]\n : undefined\n : undefined\n : undefined\n : undefined;\n\ntype FieldWithPossiblyUndefined<T, Key> =\n | GetField<Exclude<T, undefined>, Key>\n | Extract<T, undefined>;\n\ntype IndexedFieldWithPossiblyUndefined<T, Key> =\n | GetIndexedField<Exclude<T, undefined>, Key>\n | Extract<T, undefined>;\n\nexport type GetField<T, P> = P extends `${infer Left}.${infer Right}`\n ? Left extends keyof Exclude<T, undefined>\n ?\n | FieldWithPossiblyUndefined<Exclude<T, undefined>[Left], Right>\n | Extract<T, undefined>\n : Left extends `${infer FieldKey}[${infer IndexKey}]`\n ? FieldKey extends keyof T\n ? FieldWithPossiblyUndefined<\n IndexedFieldWithPossiblyUndefined<T[FieldKey], IndexKey>,\n Right\n >\n : undefined\n : undefined\n : P extends keyof T\n ? T[P]\n : P extends `${infer FieldKey}[${infer IndexKey}]`\n ? FieldKey extends keyof T\n ? IndexedFieldWithPossiblyUndefined<T[FieldKey], IndexKey>\n : undefined\n : IndexedFieldWithPossiblyUndefined<T, P>;\n\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K - The type of the key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value.\n */\nexport function getField<T extends object, K extends keyof T>(\n object: T,\n path: K | readonly [K]\n): T[K];\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K - The type of the key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value or undefined.\n */\nexport function getField<T extends object, K extends keyof T>(\n object: T | null | undefined,\n path: K | readonly [K]\n): T[K] | undefined;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K - The type of the key in the object.\n * @typeParam D - The type of the default value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value or default value.\n */\nexport function getField<T extends object, K extends keyof T, D>(\n object: T | null | undefined,\n path: K | readonly [K],\n defaultValue: D\n): Exclude<T[K], undefined> | D;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1]\n>(object: T, path: readonly [K1, K2]): T[K1][K2];\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value or undefined.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1]\n>(object: T | null | undefined, path: readonly [K1, K2]): T[K1][K2] | undefined;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n * @typeParam D - The type of the default value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value or default value.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1],\n D\n>(\n object: T | null | undefined,\n path: readonly [K1, K2],\n defaultValue: D\n): Exclude<T[K1][K2], undefined> | D;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n * @typeParam K3 - The type of the third key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1],\n K3 extends keyof T[K1][K2]\n>(object: T, path: readonly [K1, K2, K3]): T[K1][K2][K3];\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n * @typeParam K3 - The type of the third key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value or undefined.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1],\n K3 extends keyof T[K1][K2]\n>(\n object: T | null | undefined,\n path: readonly [K1, K2, K3]\n): T[K1][K2][K3] | undefined;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n * @typeParam K3 - The type of the third key in the object.\n * @typeParam D - The type of the default value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value or default value.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1],\n K3 extends keyof T[K1][K2],\n D\n>(\n object: T | null | undefined,\n path: readonly [K1, K2, K3],\n defaultValue: D\n): Exclude<T[K1][K2][K3], undefined> | D;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n * @typeParam K3 - The type of the third key in the object.\n * @typeParam K4 - The type of the fourth key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1],\n K3 extends keyof T[K1][K2],\n K4 extends keyof T[K1][K2][K3]\n>(object: T, path: readonly [K1, K2, K3, K4]): T[K1][K2][K3][K4];\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n * @typeParam K3 - The type of the third key in the object.\n * @typeParam K4 - The type of the fourth key in the object.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value or undefined.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1],\n K3 extends keyof T[K1][K2],\n K4 extends keyof T[K1][K2][K3]\n>(\n object: T | null | undefined,\n path: readonly [K1, K2, K3, K4]\n): T[K1][K2][K3][K4] | undefined;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam K1 - The type of the first key in the object.\n * @typeParam K2 - The type of the second key in the object.\n * @typeParam K3 - The type of the third key in the object.\n * @typeParam K4 - The type of the fourth key in the object.\n * @typeParam D - The type of the default value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value or default value.\n */\nexport function getField<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T[K1],\n K3 extends keyof T[K1][K2],\n K4 extends keyof T[K1][K2][K3],\n D\n>(\n object: T | null | undefined,\n path: readonly [K1, K2, K3, K4],\n defaultValue: D\n): Exclude<T[K1][K2][K3][K4], undefined> | D;\n/**\n * Retrieves the value at a given path from an object with numeric keys. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value.\n */\nexport function getField<T>(object: Record<number, T>, path: number): T;\n/**\n * Retrieves the value at a given path from an object with numeric keys. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value or undefined.\n */\nexport function getField<T>(\n object: Record<number, T> | null | undefined,\n path: number\n): T | undefined;\n/**\n * Retrieves the value at a given path from an object with numeric keys. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the value.\n * @typeParam D - The type of the default value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value or default value.\n */\nexport function getField<T, D>(\n object: Record<number, T> | null | undefined,\n path: number,\n defaultValue: D\n): T | D;\n/**\n * Retrieves the value at a given path from a null or undefined object, returning the default value.\n *\n * @typeParam D - The type of the default value.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The default value.\n */\nexport function getField<D>(\n object: null | undefined,\n path: PropertyKey,\n defaultValue: D\n): D;\n\n/**\n * Retrieves the value at a given path from a null or undefined object, returning undefined.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n */\nexport function getField(\n object: null | undefined,\n path: PropertyKey\n): undefined;\n\n/**\n * Retrieves the value at a given path from a string-keyed object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam P - The type of the path.\n *\n * @param data - The object to query.\n * @param path - The path of the property to get.\n * @returns The resolved value, or any if path is a general string.\n */\nexport function getField<T, P extends string>(\n data: T,\n path: P\n): string extends P ? any : GetField<T, P>;\n/**\n * Retrieves the value at a given path from a string-keyed object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @typeParam T - The type of the object.\n * @typeParam P - The type of the path.\n * @typeParam D - The type of the default value.\n *\n * @param data - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value or default value.\n */\nexport function getField<T, P extends string, D = GetField<T, P>>(\n data: T,\n path: P,\n defaultValue: D\n): Exclude<GetField<T, P>, null | undefined> | D;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value.\n */\nexport function getField(\n object: unknown,\n path: PropertyKey | readonly PropertyKey[],\n defaultValue?: unknown\n): any;\n/**\n * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.\n *\n * @param object - The object to query.\n * @param path - The path of the property to get.\n * @param defaultValue - The value returned if the resolved value is undefined.\n * @returns The resolved value.\n */\nexport function getField(\n object: any,\n path: PropertyKey | readonly PropertyKey[],\n defaultValue?: any\n): any {\n if (object === null) {\n return defaultValue;\n }\n\n switch (typeof path) {\n case \"string\": {\n const result = object[path];\n\n if (result === undefined) {\n if (isDeepKey(path)) {\n return getField(object, toPath(path), defaultValue);\n }\n return defaultValue;\n }\n\n return result;\n }\n case \"number\":\n case \"symbol\": {\n if (isNumber(path)) {\n path = toStringKey(path);\n }\n\n const result = Array.isArray(path)\n ? undefined\n : object[path as PropertyKey];\n if (result === undefined) {\n return defaultValue;\n }\n\n return result;\n }\n case \"bigint\":\n case \"boolean\":\n case \"undefined\":\n case \"object\":\n case \"function\":\n default: {\n if (Array.isArray(path)) {\n return getWithPath(object, path, defaultValue);\n }\n\n path = Object.is(path?.valueOf(), -0) ? \"-0\" : String(path);\n\n const result = object[path];\n\n if (result === undefined) {\n return defaultValue;\n }\n\n return result;\n }\n }\n}\n\nfunction getWithPath(\n object: any,\n path: readonly PropertyKey[],\n defaultValue?: any\n): any {\n if (path.length === 0) {\n return defaultValue;\n }\n\n let current = object;\n\n for (const element of path) {\n if (current === null) {\n return defaultValue;\n }\n\n current = current[element];\n }\n\n if (current === undefined) {\n return defaultValue;\n }\n\n return current;\n}\n"],"mappings":";;;;;;;;;;;;;AAkaA,SAAgB,SACd,QACA,MACA,cACK;AACL,KAAI,WAAW,KACb,QAAO;AAGT,SAAQ,OAAO,MAAf;EACE,KAAK,UAAU;GACb,MAAM,SAAS,OAAO;AAEtB,OAAI,WAAW,QAAW;AACxB,QAAI,UAAU,KAAK,CACjB,QAAO,SAAS,QAAQ,OAAO,KAAK,EAAE,aAAa;AAErD,WAAO;;AAGT,UAAO;;EAET,KAAK;EACL,KAAK,UAAU;AACb,OAAI,SAAS,KAAK,CAChB,QAAO,YAAY,KAAK;GAG1B,MAAM,SAAS,MAAM,QAAQ,KAAK,GAC9B,SACA,OAAO;AACX,OAAI,WAAW,OACb,QAAO;AAGT,UAAO;;EAOT,SAAS;AACP,OAAI,MAAM,QAAQ,KAAK,CACrB,QAAO,YAAY,QAAQ,MAAM,aAAa;AAGhD,UAAO,OAAO,GAAG,MAAM,SAAS,EAAE,GAAG,GAAG,OAAO,OAAO,KAAK;GAE3D,MAAM,SAAS,OAAO;AAEtB,OAAI,WAAW,OACb,QAAO;AAGT,UAAO;;;;AAKb,SAAS,YACP,QACA,MACA,cACK;AACL,KAAI,KAAK,WAAW,EAClB,QAAO;CAGT,IAAI,UAAU;AAEd,MAAK,MAAM,WAAW,MAAM;AAC1B,MAAI,YAAY,KACd,QAAO;AAGT,YAAU,QAAQ;;AAGpB,KAAI,YAAY,OACd,QAAO;AAGT,QAAO"}
|
package/dist/is-equal.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
let _stryke_type_checks_is_set = require("@stryke/type-checks/is-set");
|
|
3
|
+
let _stryke_type_checks_is_set_string = require("@stryke/type-checks/is-set-string");
|
|
4
4
|
|
|
5
5
|
//#region src/is-equal.ts
|
|
6
6
|
const hasMap = typeof Map === "function";
|
|
@@ -46,10 +46,10 @@ function equal(a, b) {
|
|
|
46
46
|
const keys = Object.keys(a);
|
|
47
47
|
length = keys.length;
|
|
48
48
|
if (length !== Object.keys(b).length) return false;
|
|
49
|
-
for (let i = length; i-- !== 0;) if (!
|
|
49
|
+
for (let i = length; i-- !== 0;) if (!(0, _stryke_type_checks_is_set.isSet)(i) || !(0, _stryke_type_checks_is_set_string.isSetString)(keys[i]) || !Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
|
|
50
50
|
for (let i = length; i-- !== 0;) {
|
|
51
51
|
if (Array.isArray(keys) && (keys[i] === "_owner" || keys[i] === "__v" || keys[i] === "__o") && a.$$typeof) continue;
|
|
52
|
-
if (!
|
|
52
|
+
if (!(0, _stryke_type_checks_is_set.isSet)(i) || !(0, _stryke_type_checks_is_set_string.isSetString)(keys[i]) || !equal(a[keys[i]], b[keys[i]])) return false;
|
|
53
53
|
}
|
|
54
54
|
return true;
|
|
55
55
|
}
|
package/dist/is-equal.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { isSet } from "
|
|
2
|
-
import { isSetString } from "
|
|
1
|
+
import { isSet } from "@stryke/type-checks/is-set";
|
|
2
|
+
import { isSetString } from "@stryke/type-checks/is-set-string";
|
|
3
3
|
|
|
4
4
|
//#region src/is-equal.ts
|
|
5
5
|
const hasMap = typeof Map === "function";
|
package/dist/set-field.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
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
2
|
const require_to_path = require('./to-path.cjs');
|
|
3
|
+
let _stryke_type_checks_is_object_index = require("@stryke/type-checks/is-object-index");
|
|
4
|
+
let _stryke_type_checks_is_string = require("@stryke/type-checks/is-string");
|
|
5
5
|
|
|
6
6
|
//#region src/set-field.ts
|
|
7
7
|
/**
|
|
@@ -13,13 +13,13 @@ const require_to_path = require('./to-path.cjs');
|
|
|
13
13
|
* @returns The object with the value set at the given deep path.
|
|
14
14
|
*/
|
|
15
15
|
function setField(object, path, value) {
|
|
16
|
-
const resolvedPath = Array.isArray(path) ? path :
|
|
16
|
+
const resolvedPath = Array.isArray(path) ? path : (0, _stryke_type_checks_is_string.isString)(path) ? require_to_path.toPath(path) : [path];
|
|
17
17
|
for (const key of resolvedPath) if (key === "__proto__" || key === "constructor" || key === "prototype") throw new Error(`Invalid key in path: ${key}`);
|
|
18
18
|
let current = object;
|
|
19
19
|
for (let i = 0; i < resolvedPath.length - 1; i++) {
|
|
20
20
|
const key = resolvedPath[i];
|
|
21
21
|
const nextKey = resolvedPath[i + 1];
|
|
22
|
-
if (current[key] === null) current[key] =
|
|
22
|
+
if (current[key] === null) current[key] = (0, _stryke_type_checks_is_object_index.isObjectIndex)(nextKey) ? [] : {};
|
|
23
23
|
current = current[key];
|
|
24
24
|
}
|
|
25
25
|
const lastKey = resolvedPath.at(-1);
|
package/dist/set-field.d.cts
CHANGED
package/dist/set-field.d.mts
CHANGED
package/dist/set-field.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { isString } from "./type-checks/src/is-string.mjs";
|
|
2
|
-
import { isObjectIndex } from "./type-checks/src/is-object-index.mjs";
|
|
3
1
|
import { toPath } from "./to-path.mjs";
|
|
2
|
+
import { isObjectIndex } from "@stryke/type-checks/is-object-index";
|
|
3
|
+
import { isString } from "@stryke/type-checks/is-string";
|
|
4
4
|
|
|
5
5
|
//#region src/set-field.ts
|
|
6
6
|
/**
|
package/dist/to-deep-key.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
let _stryke_type_checks_is_set_string = require("@stryke/type-checks/is-set-string");
|
|
3
|
+
let _stryke_type_checks_is_number = require("@stryke/type-checks/is-number");
|
|
4
4
|
|
|
5
5
|
//#region src/to-deep-key.ts
|
|
6
6
|
/**
|
|
@@ -45,8 +45,8 @@ function toDeepKey(path) {
|
|
|
45
45
|
* @returns A new deep key string.
|
|
46
46
|
*/
|
|
47
47
|
function addPathToDeepKey(deepKey, path) {
|
|
48
|
-
if (
|
|
49
|
-
if (
|
|
48
|
+
if ((0, _stryke_type_checks_is_number.isNumber)(path) || Number.isInteger(path)) return `${deepKey}[${path}]`;
|
|
49
|
+
if ((0, _stryke_type_checks_is_set_string.isSetString)(path)) return `${deepKey}.${path}`;
|
|
50
50
|
return deepKey;
|
|
51
51
|
}
|
|
52
52
|
|
package/dist/to-deep-key.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { isSetString } from "@stryke/type-checks/is-set-string";
|
|
2
|
+
import { isNumber } from "@stryke/type-checks/is-number";
|
|
3
3
|
|
|
4
4
|
//#region src/to-deep-key.ts
|
|
5
5
|
/**
|