@omnific/utils 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/@types/GetFieldType.d.ts +13 -0
- package/dist/@types/Many.d.ts +4 -0
- package/dist/@types/PropertyPath.d.ts +6 -0
- package/dist/_internal/isDeepKey.js +25 -0
- package/dist/_internal/isUnsafeProperty.js +16 -0
- package/dist/_internal/toKey.js +19 -0
- package/dist/createChainedFunction.d.ts +19 -0
- package/dist/createChainedFunction.js +23 -0
- package/dist/get.d.ts +328 -0
- package/dist/get.js +58 -0
- package/dist/hasValue.d.ts +20 -0
- package/dist/hasValue.js +23 -0
- package/dist/isArray.d.ts +24 -0
- package/dist/isArray.js +26 -0
- package/dist/isBoolean.d.ts +26 -0
- package/dist/isBoolean.js +28 -0
- package/dist/isFunction.d.ts +17 -0
- package/dist/isFunction.js +19 -0
- package/dist/isNil.d.ts +23 -0
- package/dist/isNil.js +27 -0
- package/dist/isNull.d.ts +24 -0
- package/dist/isNull.js +26 -0
- package/dist/isNumber.d.ts +26 -0
- package/dist/isNumber.js +28 -0
- package/dist/isObject.d.ts +27 -0
- package/dist/isObject.js +29 -0
- package/dist/isPromise.d.ts +21 -0
- package/dist/isPromise.js +25 -0
- package/dist/isString.d.ts +21 -0
- package/dist/isString.js +23 -0
- package/dist/isSymbol.d.ts +27 -0
- package/dist/isSymbol.js +29 -0
- package/dist/isUndefined.d.ts +24 -0
- package/dist/isUndefined.js +26 -0
- package/dist/main.d.ts +16 -155
- package/dist/main.js +16 -194
- package/dist/resolveValue.d.ts +17 -0
- package/dist/resolveValue.js +21 -0
- package/dist/utils/toPath.js +67 -0
- package/dist/utils/toString.js +28 -0
- package/package.json +8 -3
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region @types/GetFieldType.d.ts
|
|
2
|
+
type GetFieldTypeOfArrayLikeByKey<T extends unknown[], K> = K extends number ? T[K] : K extends `${infer N extends number}` ? T[N] : K extends keyof T ? T[K] : undefined;
|
|
3
|
+
type GetFieldTypeOfStringByKey<T extends string, K> = K extends number ? T[K] : K extends `${infer N extends number}` ? T[N] : K extends keyof T ? T[K] : undefined;
|
|
4
|
+
type GetFieldTypeOfNarrowedByKey<T, K> = T extends unknown[] ? GetFieldTypeOfArrayLikeByKey<T, K> : T extends string ? GetFieldTypeOfStringByKey<T, K> : K extends keyof T ? T[K] : K extends number ? `${K}` extends keyof T ? T[`${K}`] : undefined : K extends `${infer N extends number}` ? N extends keyof T ? T[N] : undefined : undefined;
|
|
5
|
+
type GetFieldTypeOfNarrowedByDotPath<T, P> = P extends `${infer L}.${infer R}` ? GetFieldType<GetFieldTypeOfNarrowedByKey<T, L>, R, 'DotPath'> : GetFieldTypeOfNarrowedByKey<T, P>;
|
|
6
|
+
type GetFieldTypeOfNarrowedByLcKR<T, Lc, K, R> = '' extends R ? GetFieldType<GetFieldTypeOfNarrowedByDotPath<T, Lc>, K, 'Key'> : R extends `.${infer Rc}` ? GetFieldType<GetFieldType<GetFieldTypeOfNarrowedByDotPath<T, Lc>, K, 'Key'>, Rc> : GetFieldType<GetFieldType<GetFieldTypeOfNarrowedByDotPath<T, Lc>, K, 'Key'>, R>;
|
|
7
|
+
type GetFieldTypeOfNarrowedByLKR<T, L, K, R> = '' extends L ? '' extends R ? GetFieldTypeOfNarrowedByKey<T, K> : R extends `.${infer Rc}` ? GetFieldType<GetFieldTypeOfNarrowedByKey<T, K>, Rc> : GetFieldType<GetFieldTypeOfNarrowedByKey<T, K>, R> : L extends `${infer Lc}.` ? GetFieldTypeOfNarrowedByLcKR<T, Lc, K, R> : GetFieldTypeOfNarrowedByLcKR<T, L, K, R>;
|
|
8
|
+
type GetFieldTypeOfNarrowed<T, X, XT extends 'DotPath' | 'Key' | 'Path'> = XT extends 'Key' ? GetFieldTypeOfNarrowedByKey<T, X> : XT extends 'DotPath' ? GetFieldTypeOfNarrowedByDotPath<T, X> : X extends `${infer L}['${infer K}']${infer R}` ? GetFieldTypeOfNarrowedByLKR<T, L, K, R> : X extends `${infer L}["${infer K}"]${infer R}` ? GetFieldTypeOfNarrowedByLKR<T, L, K, R> : X extends `${infer L}[${infer K}]${infer R}` ? GetFieldTypeOfNarrowedByLKR<T, L, K, R> : GetFieldTypeOfNarrowedByDotPath<T, X>;
|
|
9
|
+
type GetFieldTypeOfObject<T, X, XT extends 'DotPath' | 'Key' | 'Path'> = Extract<T, unknown[]> extends never ? GetFieldTypeOfNarrowed<T, X, XT> : GetFieldTypeOfNarrowed<Exclude<T, unknown[]>, X, XT> | GetFieldTypeOfNarrowed<Extract<T, unknown[]>, X, XT>;
|
|
10
|
+
type GetFieldTypeOfPrimitive<T, X, XT extends 'DotPath' | 'Key' | 'Path'> = Extract<T, string> extends never ? T extends never ? never : undefined : (Exclude<T, string> extends never ? never : undefined) | GetFieldTypeOfNarrowed<Extract<T, string>, X, XT>;
|
|
11
|
+
type GetFieldType<T, X, XT extends 'DotPath' | 'Key' | 'Path' = 'Path'> = Extract<T, object> extends never ? GetFieldTypeOfPrimitive<T, X, XT> : GetFieldTypeOfPrimitive<Exclude<T, object>, X, XT> | GetFieldTypeOfObject<Extract<T, object>, X, XT>;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { GetFieldType };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isString } from "../isString.js";
|
|
2
|
+
//#region _internal/isDeepKey.ts
|
|
3
|
+
/**
|
|
4
|
+
* Checks if a given key is a deep key.
|
|
5
|
+
*
|
|
6
|
+
* A deep key is a string that contains a dot (.) or square brackets with a property accessor.
|
|
7
|
+
*
|
|
8
|
+
* @param {PropertyKey} key - The key to check.
|
|
9
|
+
* @returns {boolean} - Returns true if the key is a deep key, otherwise false.
|
|
10
|
+
*
|
|
11
|
+
* Examples:
|
|
12
|
+
*
|
|
13
|
+
* isDeepKey('a.b') // true
|
|
14
|
+
* isDeepKey('a[b]') // true
|
|
15
|
+
* isDeepKey('a') // false
|
|
16
|
+
* isDeepKey(123) // false
|
|
17
|
+
* isDeepKey('a.b.c') // true
|
|
18
|
+
* isDeepKey('a[b][c]') // true
|
|
19
|
+
*/
|
|
20
|
+
function isDeepKey(key) {
|
|
21
|
+
if (isString(key)) return key.includes(".") || key.includes("[") || key.includes("]");
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { isDeepKey };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region _internal/isUnsafeProperty.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks if a property key is unsafe to modify directly.
|
|
4
|
+
*
|
|
5
|
+
* This function is used in functions like `merge` to prevent prototype pollution attacks
|
|
6
|
+
* by identifying property keys that could modify the object's prototype chain or constructor.
|
|
7
|
+
*
|
|
8
|
+
* @param key - The property key to check
|
|
9
|
+
* @returns `true` if the property is unsafe to modify directly, `false` otherwise
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
function isUnsafeProperty(key) {
|
|
13
|
+
return key === "__proto__";
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { isUnsafeProperty };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { isString } from "../isString.js";
|
|
2
|
+
import { isSymbol } from "../isSymbol.js";
|
|
3
|
+
//#region _internal/toKey.ts
|
|
4
|
+
function normalizeKey(path) {
|
|
5
|
+
return Object.is(path?.valueOf?.(), -0) ? "-0" : String(path);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Converts `value` to a string key if it's not a string or symbol.
|
|
9
|
+
*
|
|
10
|
+
* @private
|
|
11
|
+
* @param {*} value The value to inspect.
|
|
12
|
+
* @returns {string|symbol} Returns the key.
|
|
13
|
+
*/
|
|
14
|
+
function toKey(value) {
|
|
15
|
+
if (isString(value) || isSymbol(value)) return value;
|
|
16
|
+
return normalizeKey(value);
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { normalizeKey, toKey };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region createChainedFunction.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A function with a bound `this` context and variadic arguments.
|
|
4
|
+
*/
|
|
5
|
+
type ChainedFunction<TArgs extends any[], TThis> = (this: TThis, ...args: TArgs) => void;
|
|
6
|
+
/**
|
|
7
|
+
* Composes multiple callbacks into a single callback while preserving `this`.
|
|
8
|
+
*
|
|
9
|
+
* Nullish callbacks are ignored. When no valid callback is provided, a no-op
|
|
10
|
+
* function is returned so the consumer can call the result unconditionally.
|
|
11
|
+
*
|
|
12
|
+
* @template TArgs
|
|
13
|
+
* @template TThis
|
|
14
|
+
* @param {...(ChainedFunction<TArgs, TThis> | null | undefined)[]} funcs - The callbacks to chain.
|
|
15
|
+
* @returns {ChainedFunction<TArgs, TThis>} A single chained callback.
|
|
16
|
+
*/
|
|
17
|
+
declare function createChainedFunction<TArgs extends any[], TThis>(...funcs: Array<ChainedFunction<TArgs, TThis> | null | undefined>): ChainedFunction<TArgs, TThis>;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { ChainedFunction, createChainedFunction, createChainedFunction as default };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { isFunction } from "./isFunction.js";
|
|
2
|
+
//#region createChainedFunction.ts
|
|
3
|
+
/**
|
|
4
|
+
* Composes multiple callbacks into a single callback while preserving `this`.
|
|
5
|
+
*
|
|
6
|
+
* Nullish callbacks are ignored. When no valid callback is provided, a no-op
|
|
7
|
+
* function is returned so the consumer can call the result unconditionally.
|
|
8
|
+
*
|
|
9
|
+
* @template TArgs
|
|
10
|
+
* @template TThis
|
|
11
|
+
* @param {...(ChainedFunction<TArgs, TThis> | null | undefined)[]} funcs - The callbacks to chain.
|
|
12
|
+
* @returns {ChainedFunction<TArgs, TThis>} A single chained callback.
|
|
13
|
+
*/
|
|
14
|
+
function createChainedFunction(...funcs) {
|
|
15
|
+
const validFuncs = funcs.filter((fn) => isFunction(fn));
|
|
16
|
+
if (validFuncs.length === 0) return function() {};
|
|
17
|
+
if (validFuncs.length === 1) return validFuncs[0];
|
|
18
|
+
return function(...args) {
|
|
19
|
+
for (const fn of validFuncs) fn.apply(this, args);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { createChainedFunction, createChainedFunction as default };
|
package/dist/get.d.ts
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { GetFieldType } from "./@types/GetFieldType.js";
|
|
2
|
+
import { PropertyPath } from "./@types/PropertyPath.js";
|
|
3
|
+
|
|
4
|
+
//#region get.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
|
|
7
|
+
*
|
|
8
|
+
* @template TObject
|
|
9
|
+
* @template TKey
|
|
10
|
+
* @param {TObject} object - The object to query.
|
|
11
|
+
* @param {TKey | [TKey]} path - The path of the property to get.
|
|
12
|
+
* @returns {TObject[TKey]} Returns the resolved value.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const object = { 'a': [{ 'b': { 'c': 3 } }] };
|
|
16
|
+
* get(object, 'a[0].b.c');
|
|
17
|
+
* // => 3
|
|
18
|
+
*/
|
|
19
|
+
declare function get<TObject extends object, TKey extends keyof TObject>(object: TObject, path: TKey | [TKey]): TObject[TKey];
|
|
20
|
+
/**
|
|
21
|
+
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
|
|
22
|
+
*
|
|
23
|
+
* @template TObject
|
|
24
|
+
* @template TKey
|
|
25
|
+
* @param {TObject | null | undefined} object - The object to query.
|
|
26
|
+
* @param {TKey | [TKey]} path - The path of the property to get.
|
|
27
|
+
* @returns {TObject[TKey] | undefined} Returns the resolved value.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* const object = { 'a': [{ 'b': { 'c': 3 } }] };
|
|
31
|
+
* get(object, 'a[0].b.c');
|
|
32
|
+
* // => 3
|
|
33
|
+
*/
|
|
34
|
+
declare function get<TObject extends object, TKey extends keyof TObject>(object: TObject | null | undefined, path: TKey | [TKey]): TObject[TKey] | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
|
|
37
|
+
*
|
|
38
|
+
* @template TObject
|
|
39
|
+
* @template TKey
|
|
40
|
+
* @template TDefault
|
|
41
|
+
* @param {TObject | null | undefined} object - The object to query.
|
|
42
|
+
* @param {TKey | [TKey]} path - The path of the property to get.
|
|
43
|
+
* @param {TDefault} defaultValue - The value returned if the resolved value is undefined.
|
|
44
|
+
* @returns {Exclude<TObject[TKey], undefined> | TDefault} Returns the resolved value.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* const object = { 'a': [{ 'b': { 'c': 3 } }] };
|
|
48
|
+
* get(object, 'a[0].b.c', 'default');
|
|
49
|
+
* // => 3
|
|
50
|
+
*/
|
|
51
|
+
declare function get<TObject extends object, TKey extends keyof TObject, TDefault>(object: TObject | null | undefined, path: TKey | [TKey], defaultValue: TDefault): Exclude<TObject[TKey], undefined> | TDefault;
|
|
52
|
+
/**
|
|
53
|
+
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
|
|
54
|
+
*
|
|
55
|
+
* @template TObject
|
|
56
|
+
* @template TKey1
|
|
57
|
+
* @template TKey2
|
|
58
|
+
* @param {TObject} object - The object to query.
|
|
59
|
+
* @param {[TKey1, TKey2]} path - The path of the property to get.
|
|
60
|
+
* @returns {TObject[TKey1][TKey2]} Returns the resolved value.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* const object = { 'a': { 'b': 2 } };
|
|
64
|
+
* get(object, ['a', 'b']);
|
|
65
|
+
* // => 2
|
|
66
|
+
*/
|
|
67
|
+
declare function get<TObject extends object, TKey1 extends keyof TObject, TKey2 extends keyof TObject[TKey1]>(object: TObject, path: [TKey1, TKey2]): TObject[TKey1][TKey2];
|
|
68
|
+
/**
|
|
69
|
+
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
|
|
70
|
+
*
|
|
71
|
+
* @template TObject
|
|
72
|
+
* @template TKey1
|
|
73
|
+
* @template TKey2
|
|
74
|
+
* @param {TObject | null | undefined} object - The object to query.
|
|
75
|
+
* @param {[TKey1, TKey2]} path - The path of the property to get.
|
|
76
|
+
* @returns {NonNullable<TObject[TKey1]>[TKey2] | undefined} Returns the resolved value.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* const object = { 'a': { 'b': 2 } };
|
|
80
|
+
* get(object, ['a', 'b']);
|
|
81
|
+
* // => 2
|
|
82
|
+
*/
|
|
83
|
+
declare function get<TObject extends object, TKey1 extends keyof TObject, TKey2 extends keyof NonNullable<TObject[TKey1]>>(object: TObject | null | undefined, path: [TKey1, TKey2]): NonNullable<TObject[TKey1]>[TKey2] | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
|
|
86
|
+
*
|
|
87
|
+
* @template TObject
|
|
88
|
+
* @template TKey1
|
|
89
|
+
* @template TKey2
|
|
90
|
+
* @template TDefault
|
|
91
|
+
* @param {TObject | null | undefined} object - The object to query.
|
|
92
|
+
* @param {[TKey1, TKey2]} path - The path of the property to get.
|
|
93
|
+
* @param {TDefault} defaultValue - The value returned if the resolved value is undefined.
|
|
94
|
+
* @returns {Exclude<NonNullable<TObject[TKey1]>[TKey2], undefined> | TDefault} Returns the resolved value.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* const object = { 'a': { 'b': 2 } };
|
|
98
|
+
* get(object, ['a', 'b'], 'default');
|
|
99
|
+
* // => 2
|
|
100
|
+
*/
|
|
101
|
+
declare function get<TObject extends object, TKey1 extends keyof TObject, TKey2 extends keyof NonNullable<TObject[TKey1]>, TDefault>(object: TObject | null | undefined, path: [TKey1, TKey2], defaultValue: TDefault): Exclude<NonNullable<TObject[TKey1]>[TKey2], undefined> | TDefault;
|
|
102
|
+
/**
|
|
103
|
+
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
|
|
104
|
+
*
|
|
105
|
+
* @template TObject
|
|
106
|
+
* @template TKey1
|
|
107
|
+
* @template TKey2
|
|
108
|
+
* @template TKey3
|
|
109
|
+
* @param {TObject} object - The object to query.
|
|
110
|
+
* @param {[TKey1, TKey2, TKey3]} path - The path of the property to get.
|
|
111
|
+
* @returns {TObject[TKey1][TKey2][TKey3]} Returns the resolved value.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* const object = { 'a': { 'b': { 'c': 3 } } };
|
|
115
|
+
* get(object, ['a', 'b', 'c']);
|
|
116
|
+
* // => 3
|
|
117
|
+
*/
|
|
118
|
+
declare function get<TObject extends object, TKey1 extends keyof TObject, TKey2 extends keyof TObject[TKey1], TKey3 extends keyof TObject[TKey1][TKey2]>(object: TObject, path: [TKey1, TKey2, TKey3]): TObject[TKey1][TKey2][TKey3];
|
|
119
|
+
/**
|
|
120
|
+
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
|
|
121
|
+
*
|
|
122
|
+
* @template TObject
|
|
123
|
+
* @template TKey1
|
|
124
|
+
* @template TKey2
|
|
125
|
+
* @template TKey3
|
|
126
|
+
* @param {TObject | null | undefined} object - The object to query.
|
|
127
|
+
* @param {[TKey1, TKey2, TKey3]} path - The path of the property to get.
|
|
128
|
+
* @returns {NonNullable<NonNullable<TObject[TKey1]>[TKey2]>[TKey3] | undefined} Returns the resolved value.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* const object = { 'a': { 'b': { 'c': 3 } } };
|
|
132
|
+
* get(object, ['a', 'b', 'c']);
|
|
133
|
+
* // => 3
|
|
134
|
+
*/
|
|
135
|
+
declare function get<TObject extends object, TKey1 extends keyof TObject, TKey2 extends keyof NonNullable<TObject[TKey1]>, TKey3 extends keyof NonNullable<NonNullable<TObject[TKey1]>[TKey2]>>(object: TObject | null | undefined, path: [TKey1, TKey2, TKey3]): NonNullable<NonNullable<TObject[TKey1]>[TKey2]>[TKey3] | undefined;
|
|
136
|
+
/**
|
|
137
|
+
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
|
|
138
|
+
*
|
|
139
|
+
* @template TObject
|
|
140
|
+
* @template TKey1
|
|
141
|
+
* @template TKey2
|
|
142
|
+
* @template TKey3
|
|
143
|
+
* @template TDefault
|
|
144
|
+
* @param {TObject | null | undefined} object - The object to query.
|
|
145
|
+
* @param {[TKey1, TKey2, TKey3]} path - The path of the property to get.
|
|
146
|
+
* @param {TDefault} defaultValue - The value returned if the resolved value is undefined.
|
|
147
|
+
* @returns {Exclude<NonNullable<NonNullable<TObject[TKey1]>[TKey2]>[TKey3], undefined> | TDefault} Returns the resolved value.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* const object = { 'a': { 'b': { 'c': 3 } } };
|
|
151
|
+
* get(object, ['a', 'b', 'c'], 'default');
|
|
152
|
+
* // => 3
|
|
153
|
+
*/
|
|
154
|
+
declare function get<TObject extends object, TKey1 extends keyof TObject, TKey2 extends keyof NonNullable<TObject[TKey1]>, TKey3 extends keyof NonNullable<NonNullable<TObject[TKey1]>[TKey2]>, TDefault>(object: TObject | null | undefined, path: [TKey1, TKey2, TKey3], defaultValue: TDefault): Exclude<NonNullable<NonNullable<TObject[TKey1]>[TKey2]>[TKey3], undefined> | TDefault;
|
|
155
|
+
/**
|
|
156
|
+
* Gets the value at path of object.
|
|
157
|
+
*
|
|
158
|
+
* @template TObject
|
|
159
|
+
* @template TKey1
|
|
160
|
+
* @template TKey2
|
|
161
|
+
* @template TKey3
|
|
162
|
+
* @template TKey4
|
|
163
|
+
* @param {TObject} object - The object to query.
|
|
164
|
+
* @param {[TKey1, TKey2, TKey3, TKey4]} path - The path of the property to get.
|
|
165
|
+
* @returns {TObject[TKey1][TKey2][TKey3][TKey4]} Returns the resolved value.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* const object = { 'a': { 'b': { 'c': { 'd': 4 } } } };
|
|
169
|
+
* get(object, ['a', 'b', 'c', 'd']);
|
|
170
|
+
* // => 4
|
|
171
|
+
*/
|
|
172
|
+
declare function get<TObject extends object, TKey1 extends keyof TObject, TKey2 extends keyof TObject[TKey1], TKey3 extends keyof TObject[TKey1][TKey2], TKey4 extends keyof TObject[TKey1][TKey2][TKey3]>(object: TObject, path: [TKey1, TKey2, TKey3, TKey4]): TObject[TKey1][TKey2][TKey3][TKey4];
|
|
173
|
+
/**
|
|
174
|
+
* Gets the value at path of object. If the resolved value is undefined, undefined is returned.
|
|
175
|
+
*
|
|
176
|
+
* @template TObject
|
|
177
|
+
* @template TKey1
|
|
178
|
+
* @template TKey2
|
|
179
|
+
* @template TKey3
|
|
180
|
+
* @template TKey4
|
|
181
|
+
* @param {TObject | null | undefined} object - The object to query.
|
|
182
|
+
* @param {[TKey1, TKey2, TKey3, TKey4]} path - The path of the property to get.
|
|
183
|
+
* @returns {NonNullable<NonNullable<NonNullable<TObject[TKey1]>[TKey2]>[TKey3]>[TKey4] | undefined} Returns the resolved value.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* const object = { 'a': { 'b': { 'c': { 'd': 4 } } } };
|
|
187
|
+
* get(object, ['a', 'b', 'c', 'd']);
|
|
188
|
+
* // => 4
|
|
189
|
+
*/
|
|
190
|
+
declare function get<TObject extends object, TKey1 extends keyof TObject, TKey2 extends keyof NonNullable<TObject[TKey1]>, TKey3 extends keyof NonNullable<NonNullable<TObject[TKey1]>[TKey2]>, TKey4 extends keyof NonNullable<NonNullable<NonNullable<TObject[TKey1]>[TKey2]>[TKey3]>>(object: TObject | null | undefined, path: [TKey1, TKey2, TKey3, TKey4]): NonNullable<NonNullable<NonNullable<TObject[TKey1]>[TKey2]>[TKey3]>[TKey4] | undefined;
|
|
191
|
+
/**
|
|
192
|
+
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
|
|
193
|
+
*
|
|
194
|
+
* @template TObject
|
|
195
|
+
* @template TKey1
|
|
196
|
+
* @template TKey2
|
|
197
|
+
* @template TKey3
|
|
198
|
+
* @template TKey4
|
|
199
|
+
* @template TDefault
|
|
200
|
+
* @param {TObject | null | undefined} object - The object to query.
|
|
201
|
+
* @param {[TKey1, TKey2, TKey3, TKey4]} path - The path of the property to get.
|
|
202
|
+
* @param {TDefault} defaultValue - The value returned if the resolved value is undefined.
|
|
203
|
+
* @returns {Exclude<NonNullable<NonNullable<NonNullable<TObject[TKey1]>[TKey2]>[TKey3]>[TKey4], undefined> | TDefault} Returns the resolved value.
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* const object = { 'a': { 'b': { 'c': { 'd': 4 } } } };
|
|
207
|
+
* get(object, ['a', 'b', 'c', 'd'], 'default');
|
|
208
|
+
* // => 4
|
|
209
|
+
*/
|
|
210
|
+
declare function get<TObject extends object, TKey1 extends keyof TObject, TKey2 extends keyof NonNullable<TObject[TKey1]>, TKey3 extends keyof NonNullable<NonNullable<TObject[TKey1]>[TKey2]>, TKey4 extends keyof NonNullable<NonNullable<NonNullable<TObject[TKey1]>[TKey2]>[TKey3]>, TDefault>(object: TObject | null | undefined, path: [TKey1, TKey2, TKey3, TKey4], defaultValue: TDefault): Exclude<NonNullable<NonNullable<NonNullable<TObject[TKey1]>[TKey2]>[TKey3]>[TKey4], undefined> | TDefault;
|
|
211
|
+
/**
|
|
212
|
+
* Gets the value at path of object.
|
|
213
|
+
*
|
|
214
|
+
* @template T
|
|
215
|
+
* @param {Record<number, T>} object - The object to query.
|
|
216
|
+
* @param {number} path - The path of the property to get.
|
|
217
|
+
* @returns {T} Returns the resolved value.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* const object = { 0: 'a', 1: 'b', 2: 'c' };
|
|
221
|
+
* get(object, 1);
|
|
222
|
+
* // => 'b'
|
|
223
|
+
*/
|
|
224
|
+
declare function get<T>(object: Record<number, T>, path: number): T;
|
|
225
|
+
/**
|
|
226
|
+
* Gets the value at path of object. If the resolved value is undefined, undefined is returned.
|
|
227
|
+
*
|
|
228
|
+
* @template T
|
|
229
|
+
* @param {Record<number, T> | null | undefined} object - The object to query.
|
|
230
|
+
* @param {number} path - The path of the property to get.
|
|
231
|
+
* @returns {T | undefined} Returns the resolved value.
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* const object = { 0: 'a', 1: 'b', 2: 'c' };
|
|
235
|
+
* get(object, 1);
|
|
236
|
+
* // => 'b'
|
|
237
|
+
*/
|
|
238
|
+
declare function get<T>(object: Record<number, T> | null | undefined, path: number): T | undefined;
|
|
239
|
+
/**
|
|
240
|
+
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
|
|
241
|
+
*
|
|
242
|
+
* @template T
|
|
243
|
+
* @template TDefault
|
|
244
|
+
* @param {Record<number, T> | null | undefined} object - The object to query.
|
|
245
|
+
* @param {number} path - The path of the property to get.
|
|
246
|
+
* @param {TDefault} defaultValue - The value returned if the resolved value is undefined.
|
|
247
|
+
* @returns {T | TDefault} Returns the resolved value.
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* const object = { 0: 'a', 1: 'b', 2: 'c' };
|
|
251
|
+
* get(object, 1, 'default');
|
|
252
|
+
* // => 'b'
|
|
253
|
+
*/
|
|
254
|
+
declare function get<T, TDefault>(object: Record<number, T> | null | undefined, path: number, defaultValue: TDefault): T | TDefault;
|
|
255
|
+
/**
|
|
256
|
+
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
|
|
257
|
+
*
|
|
258
|
+
* @template TDefault
|
|
259
|
+
* @param {null | undefined} object - The object to query.
|
|
260
|
+
* @param {PropertyPath} path - The path of the property to get.
|
|
261
|
+
* @param {TDefault} defaultValue - The value returned if the resolved value is undefined.
|
|
262
|
+
* @returns {TDefault} Returns the default value.
|
|
263
|
+
*
|
|
264
|
+
* @example
|
|
265
|
+
* get(null, 'a.b.c', 'default');
|
|
266
|
+
* // => 'default'
|
|
267
|
+
*/
|
|
268
|
+
declare function get<TDefault>(object: null | undefined, path: PropertyPath, defaultValue: TDefault): TDefault;
|
|
269
|
+
/**
|
|
270
|
+
* Gets the value at path of object. If the resolved value is undefined, undefined is returned.
|
|
271
|
+
*
|
|
272
|
+
* @param {null | undefined} object - The object to query.
|
|
273
|
+
* @param {PropertyPath} path - The path of the property to get.
|
|
274
|
+
* @returns {undefined} Returns undefined.
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* get(null, 'a.b.c');
|
|
278
|
+
* // => undefined
|
|
279
|
+
*/
|
|
280
|
+
declare function get(object: null | undefined, path: PropertyPath): undefined;
|
|
281
|
+
/**
|
|
282
|
+
* Gets the value at path of object using type-safe path.
|
|
283
|
+
*
|
|
284
|
+
* @template TObject
|
|
285
|
+
* @template TPath
|
|
286
|
+
* @param {TObject} data - The object to query.
|
|
287
|
+
* @param {TPath} path - The path of the property to get.
|
|
288
|
+
* @returns {string extends TPath ? any : GetFieldType<TObject, TPath>} Returns the resolved value.
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* const object = { a: { b: { c: 1 } } };
|
|
292
|
+
* get(object, 'a.b.c');
|
|
293
|
+
* // => 1
|
|
294
|
+
*/
|
|
295
|
+
declare function get<TObject, TPath extends string>(data: TObject, path: TPath): string extends TPath ? any : GetFieldType<TObject, TPath>;
|
|
296
|
+
/**
|
|
297
|
+
* Gets the value at path of object using type-safe path. If the resolved value is undefined, the defaultValue is returned.
|
|
298
|
+
*
|
|
299
|
+
* @template TObject
|
|
300
|
+
* @template TPath
|
|
301
|
+
* @template TDefault
|
|
302
|
+
* @param {TObject} data - The object to query.
|
|
303
|
+
* @param {TPath} path - The path of the property to get.
|
|
304
|
+
* @param {TDefault} defaultValue - The value returned if the resolved value is undefined.
|
|
305
|
+
* @returns {Exclude<GetFieldType<TObject, TPath>, null | undefined> | TDefault} Returns the resolved value.
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* const object = { a: { b: { c: 1 } } };
|
|
309
|
+
* get(object, 'a.b.d', 'default');
|
|
310
|
+
* // => 'default'
|
|
311
|
+
*/
|
|
312
|
+
declare function get<TObject, TPath extends string, TDefault = GetFieldType<TObject, TPath>>(data: TObject, path: TPath, defaultValue: TDefault): Exclude<GetFieldType<TObject, TPath>, undefined> | TDefault;
|
|
313
|
+
/**
|
|
314
|
+
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned.
|
|
315
|
+
*
|
|
316
|
+
* @param {any} object - The object to query.
|
|
317
|
+
* @param {PropertyPath} path - The path of the property to get.
|
|
318
|
+
* @param {any} [defaultValue] - The value returned if the resolved value is undefined.
|
|
319
|
+
* @returns {any} Returns the resolved value.
|
|
320
|
+
*
|
|
321
|
+
* @example
|
|
322
|
+
* const object = { a: { b: { c: 1 } } };
|
|
323
|
+
* get(object, 'a.b.c', 'default');
|
|
324
|
+
* // => 1
|
|
325
|
+
*/
|
|
326
|
+
declare function get(object: any, path: PropertyPath, defaultValue?: any): any;
|
|
327
|
+
//#endregion
|
|
328
|
+
export { get };
|
package/dist/get.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { isUndefined } from "./isUndefined.js";
|
|
2
|
+
import { isNil } from "./isNil.js";
|
|
3
|
+
import { isNumber } from "./isNumber.js";
|
|
4
|
+
import { isString } from "./isString.js";
|
|
5
|
+
import { isUnsafeProperty } from "./_internal/isUnsafeProperty.js";
|
|
6
|
+
import { isDeepKey } from "./_internal/isDeepKey.js";
|
|
7
|
+
import { isSymbol } from "./isSymbol.js";
|
|
8
|
+
import { normalizeKey, toKey } from "./_internal/toKey.js";
|
|
9
|
+
import { toPath } from "./utils/toPath.js";
|
|
10
|
+
//#region get.ts
|
|
11
|
+
/**
|
|
12
|
+
* Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
|
|
13
|
+
*
|
|
14
|
+
* @param {any} object - The object to query.
|
|
15
|
+
* @param {PropertyKey | readonly PropertyKey[]} path - The path of the property to get.
|
|
16
|
+
* @param {any} [defaultValue] - The value returned if the resolved value is undefined.
|
|
17
|
+
* @returns {any} Returns the resolved value.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const object = { a: { b: { c: 1 } } };
|
|
21
|
+
* get(object, 'a.b.c');
|
|
22
|
+
* // => 1
|
|
23
|
+
*
|
|
24
|
+
* get(object, ['a', 'b', 'c']);
|
|
25
|
+
* // => 1
|
|
26
|
+
*
|
|
27
|
+
* get(object, 'a.b.d', 'default');
|
|
28
|
+
* // => 'default'
|
|
29
|
+
*/
|
|
30
|
+
function get(object, path, defaultValue) {
|
|
31
|
+
if (isNil(object)) return defaultValue;
|
|
32
|
+
if (Array.isArray(path)) return getWithPath(object, path, defaultValue);
|
|
33
|
+
if (isString(path)) {
|
|
34
|
+
if (isUnsafeProperty(path)) return defaultValue;
|
|
35
|
+
const result = object[path];
|
|
36
|
+
if (isUndefined(result)) return isDeepKey(path) ? getWithPath(object, toPath(path), defaultValue) : defaultValue;
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
if (isNumber(path)) return getDirect(object, toKey(path), defaultValue);
|
|
40
|
+
if (isSymbol(path)) return getDirect(object, path, defaultValue);
|
|
41
|
+
return getDirect(object, normalizeKey(path), defaultValue);
|
|
42
|
+
}
|
|
43
|
+
function getWithPath(object, path, defaultValue) {
|
|
44
|
+
if (path.length === 0) return defaultValue;
|
|
45
|
+
let current = object;
|
|
46
|
+
for (const element of path) {
|
|
47
|
+
if (isNil(current) || isUnsafeProperty(element)) return defaultValue;
|
|
48
|
+
current = current[element];
|
|
49
|
+
}
|
|
50
|
+
return isUndefined(current) ? defaultValue : current;
|
|
51
|
+
}
|
|
52
|
+
function getDirect(object, key, defaultValue) {
|
|
53
|
+
if (isUnsafeProperty(key)) return defaultValue;
|
|
54
|
+
const result = object[key];
|
|
55
|
+
return isUndefined(result) ? defaultValue : result;
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
export { get };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region hasValue.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks whether a value is neither `null` nor `undefined`.
|
|
4
|
+
*
|
|
5
|
+
* This helper acts as a non-nullish type guard, allowing TypeScript to narrow
|
|
6
|
+
* the checked value to `NonNullable<T>`.
|
|
7
|
+
*
|
|
8
|
+
* @template T
|
|
9
|
+
* @param {T} value - The value to check.
|
|
10
|
+
* @returns {value is NonNullable<T>} `true` when the value is present.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const value = maybeValue();
|
|
14
|
+
* if (hasValue(value)) {
|
|
15
|
+
* value;
|
|
16
|
+
* }
|
|
17
|
+
*/
|
|
18
|
+
declare function hasValue<T>(value: T): value is NonNullable<T>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { hasValue as default, hasValue };
|
package/dist/hasValue.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { isNil } from "./isNil.js";
|
|
2
|
+
//#region hasValue.ts
|
|
3
|
+
/**
|
|
4
|
+
* Checks whether a value is neither `null` nor `undefined`.
|
|
5
|
+
*
|
|
6
|
+
* This helper acts as a non-nullish type guard, allowing TypeScript to narrow
|
|
7
|
+
* the checked value to `NonNullable<T>`.
|
|
8
|
+
*
|
|
9
|
+
* @template T
|
|
10
|
+
* @param {T} value - The value to check.
|
|
11
|
+
* @returns {value is NonNullable<T>} `true` when the value is present.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const value = maybeValue();
|
|
15
|
+
* if (hasValue(value)) {
|
|
16
|
+
* value;
|
|
17
|
+
* }
|
|
18
|
+
*/
|
|
19
|
+
function hasValue(value) {
|
|
20
|
+
return !isNil(value);
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { hasValue as default, hasValue };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region isArray.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks if the given value is an array.
|
|
4
|
+
*
|
|
5
|
+
* This function tests whether the provided value is strictly an array.
|
|
6
|
+
* It returns `true` if the value is an array, and `false` otherwise.
|
|
7
|
+
*
|
|
8
|
+
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to an array.
|
|
9
|
+
*
|
|
10
|
+
* @param {unknown} value - The value to test if it is an array.
|
|
11
|
+
* @returns {value is T[]} True if the value is an array, false otherwise.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const value1 = [];
|
|
15
|
+
* const value2 = {};
|
|
16
|
+
* const value3 = 'abc';
|
|
17
|
+
*
|
|
18
|
+
* console.log(isArray(value1)); // true
|
|
19
|
+
* console.log(isArray(value2)); // false
|
|
20
|
+
* console.log(isArray(value3)); // false
|
|
21
|
+
*/
|
|
22
|
+
declare function isArray<T = unknown>(value: unknown): value is T[];
|
|
23
|
+
//#endregion
|
|
24
|
+
export { isArray };
|
package/dist/isArray.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region isArray.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks if the given value is an array.
|
|
4
|
+
*
|
|
5
|
+
* This function tests whether the provided value is strictly an array.
|
|
6
|
+
* It returns `true` if the value is an array, and `false` otherwise.
|
|
7
|
+
*
|
|
8
|
+
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to an array.
|
|
9
|
+
*
|
|
10
|
+
* @param {unknown} value - The value to test if it is an array.
|
|
11
|
+
* @returns {value is T[]} True if the value is an array, false otherwise.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const value1 = [];
|
|
15
|
+
* const value2 = {};
|
|
16
|
+
* const value3 = 'abc';
|
|
17
|
+
*
|
|
18
|
+
* console.log(isArray(value1)); // true
|
|
19
|
+
* console.log(isArray(value2)); // false
|
|
20
|
+
* console.log(isArray(value3)); // false
|
|
21
|
+
*/
|
|
22
|
+
function isArray(value) {
|
|
23
|
+
return Array.isArray(value);
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
export { isArray };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region isBoolean.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks if the given value is boolean.
|
|
4
|
+
*
|
|
5
|
+
* This function tests whether the provided value is strictly `boolean`.
|
|
6
|
+
* It returns `true` if the value is `boolean`, and `false` otherwise.
|
|
7
|
+
*
|
|
8
|
+
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `boolean`.
|
|
9
|
+
*
|
|
10
|
+
* @param {unknown} x - The Value to test if it is boolean.
|
|
11
|
+
* @returns {x is boolean} True if the value is boolean, false otherwise.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* const value1 = true;
|
|
16
|
+
* const value2 = 0;
|
|
17
|
+
* const value3 = 'abc';
|
|
18
|
+
*
|
|
19
|
+
* console.log(isBoolean(value1)); // true
|
|
20
|
+
* console.log(isBoolean(value2)); // false
|
|
21
|
+
* console.log(isBoolean(value3)); // false
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
declare function isBoolean(x: unknown): x is boolean;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { isBoolean };
|