@omnific/utils 0.0.2 → 0.0.3

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.
@@ -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,4 @@
1
+ //#region @types/Many.d.ts
2
+ type Many<T> = T | readonly T[];
3
+ //#endregion
4
+ export { Many };
@@ -0,0 +1,6 @@
1
+ import { Many } from "./Many.js";
2
+
3
+ //#region @types/PropertyPath.d.ts
4
+ type PropertyPath = Many<PropertyKey>;
5
+ //#endregion
6
+ export { PropertyPath };
@@ -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 };
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,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 };
@@ -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 };
@@ -0,0 +1,28 @@
1
+ //#region isBoolean.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
+ function isBoolean(x) {
25
+ return typeof x === "boolean";
26
+ }
27
+ //#endregion
28
+ export { isBoolean };
@@ -0,0 +1,17 @@
1
+ //#region isFunction.d.ts
2
+ /**
3
+ * Checks if `value` is a function.
4
+ *
5
+ * @param {any} value The value to check.
6
+ * @returns {boolean} Returns `true` if `value` is a function, else `false`.
7
+ *
8
+ * @example
9
+ * isFunction(Array.prototype.slice); // true
10
+ * isFunction(async function () {}); // true
11
+ * isFunction(function* () {}); // true
12
+ * isFunction(Proxy); // true
13
+ * isFunction(Int8Array); // true
14
+ */
15
+ declare function isFunction(value: any): value is (...args: any[]) => any;
16
+ //#endregion
17
+ export { isFunction };
@@ -0,0 +1,19 @@
1
+ //#region isFunction.ts
2
+ /**
3
+ * Checks if `value` is a function.
4
+ *
5
+ * @param {any} value The value to check.
6
+ * @returns {boolean} Returns `true` if `value` is a function, else `false`.
7
+ *
8
+ * @example
9
+ * isFunction(Array.prototype.slice); // true
10
+ * isFunction(async function () {}); // true
11
+ * isFunction(function* () {}); // true
12
+ * isFunction(Proxy); // true
13
+ * isFunction(Int8Array); // true
14
+ */
15
+ function isFunction(value) {
16
+ return typeof value === "function";
17
+ }
18
+ //#endregion
19
+ export { isFunction };
@@ -0,0 +1,23 @@
1
+ //#region isNil.d.ts
2
+ /**
3
+ * Checks if a given value is null or undefined.
4
+ *
5
+ * This function tests whether the provided value is either `null` or `undefined`.
6
+ * It returns `true` if the value is `null` or `undefined`, and `false` otherwise.
7
+ *
8
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `null` or `undefined`.
9
+ *
10
+ * @param {unknown} x - The value to test for null or undefined.
11
+ * @returns {boolean} `true` if the value is null or undefined, `false` otherwise.
12
+ *
13
+ * @example
14
+ * const value1 = null;
15
+ * const value2 = undefined;
16
+ * const value3 = 42;
17
+ * const result1 = isNil(value1); // true
18
+ * const result2 = isNil(value2); // true
19
+ * const result3 = isNil(value3); // false
20
+ */
21
+ declare function isNil(x: unknown): x is null | undefined;
22
+ //#endregion
23
+ export { isNil };