@omnific/utils 0.0.1 → 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 };
package/dist/isNil.js ADDED
@@ -0,0 +1,27 @@
1
+ import { isUndefined } from "./isUndefined.js";
2
+ import { isNull } from "./isNull.js";
3
+ //#region isNil.ts
4
+ /**
5
+ * Checks if a given value is null or undefined.
6
+ *
7
+ * This function tests whether the provided value is either `null` or `undefined`.
8
+ * It returns `true` if the value is `null` or `undefined`, and `false` otherwise.
9
+ *
10
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `null` or `undefined`.
11
+ *
12
+ * @param {unknown} x - The value to test for null or undefined.
13
+ * @returns {boolean} `true` if the value is null or undefined, `false` otherwise.
14
+ *
15
+ * @example
16
+ * const value1 = null;
17
+ * const value2 = undefined;
18
+ * const value3 = 42;
19
+ * const result1 = isNil(value1); // true
20
+ * const result2 = isNil(value2); // true
21
+ * const result3 = isNil(value3); // false
22
+ */
23
+ function isNil(x) {
24
+ return isNull(x) || isUndefined(x);
25
+ }
26
+ //#endregion
27
+ export { isNil };
@@ -0,0 +1,24 @@
1
+ //#region isNull.d.ts
2
+ /**
3
+ * Checks if the given value is null.
4
+ *
5
+ * This function tests whether the provided value is strictly equal to `null`.
6
+ * It returns `true` if the value is `null`, and `false` otherwise.
7
+ *
8
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `null`.
9
+ *
10
+ * @param {unknown} x - The value to test if it is null.
11
+ * @returns {x is null} True if the value is null, false otherwise.
12
+ *
13
+ * @example
14
+ * const value1 = null;
15
+ * const value2 = undefined;
16
+ * const value3 = 42;
17
+ *
18
+ * console.log(isNull(value1)); // true
19
+ * console.log(isNull(value2)); // false
20
+ * console.log(isNull(value3)); // false
21
+ */
22
+ declare function isNull(x: unknown): x is null;
23
+ //#endregion
24
+ export { isNull };
package/dist/isNull.js ADDED
@@ -0,0 +1,26 @@
1
+ //#region isNull.ts
2
+ /**
3
+ * Checks if the given value is null.
4
+ *
5
+ * This function tests whether the provided value is strictly equal to `null`.
6
+ * It returns `true` if the value is `null`, and `false` otherwise.
7
+ *
8
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `null`.
9
+ *
10
+ * @param {unknown} x - The value to test if it is null.
11
+ * @returns {x is null} True if the value is null, false otherwise.
12
+ *
13
+ * @example
14
+ * const value1 = null;
15
+ * const value2 = undefined;
16
+ * const value3 = 42;
17
+ *
18
+ * console.log(isNull(value1)); // true
19
+ * console.log(isNull(value2)); // false
20
+ * console.log(isNull(value3)); // false
21
+ */
22
+ function isNull(x) {
23
+ return x === null;
24
+ }
25
+ //#endregion
26
+ export { isNull };
@@ -0,0 +1,26 @@
1
+ //#region isNumber.d.ts
2
+ /**
3
+ * Checks if the given value is a number.
4
+ *
5
+ * This function tests whether the provided value is strictly a `number`.
6
+ * It returns `true` if the value is a `number`, and `false` otherwise.
7
+ *
8
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `number`.
9
+ *
10
+ * @param {unknown} x - The value to test if it is a number.
11
+ * @returns {x is number} True if the value is a number, false otherwise.
12
+ *
13
+ * @example
14
+ *
15
+ * const value1 = 123;
16
+ * const value2 = 'abc';
17
+ * const value3 = true;
18
+ *
19
+ * console.log(isNumber(value1)); // true
20
+ * console.log(isNumber(value2)); // false
21
+ * console.log(isNumber(value3)); // false
22
+ *
23
+ */
24
+ declare function isNumber(value: unknown): value is number;
25
+ //#endregion
26
+ export { isNumber };
@@ -0,0 +1,28 @@
1
+ //#region isNumber.ts
2
+ /**
3
+ * Checks if the given value is a number.
4
+ *
5
+ * This function tests whether the provided value is strictly a `number`.
6
+ * It returns `true` if the value is a `number`, and `false` otherwise.
7
+ *
8
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `number`.
9
+ *
10
+ * @param {unknown} x - The value to test if it is a number.
11
+ * @returns {x is number} True if the value is a number, false otherwise.
12
+ *
13
+ * @example
14
+ *
15
+ * const value1 = 123;
16
+ * const value2 = 'abc';
17
+ * const value3 = true;
18
+ *
19
+ * console.log(isNumber(value1)); // true
20
+ * console.log(isNumber(value2)); // false
21
+ * console.log(isNumber(value3)); // false
22
+ *
23
+ */
24
+ function isNumber(value) {
25
+ return typeof value === "number" && !Number.isNaN(value);
26
+ }
27
+ //#endregion
28
+ export { isNumber };
@@ -0,0 +1,27 @@
1
+ //#region isObject.d.ts
2
+ /**
3
+ * Checks if the given value is an object. An object is a value that is
4
+ * not a primitive type (string, number, boolean, symbol, null, or undefined).
5
+ *
6
+ * This function tests whether the provided value is an object or not.
7
+ * It returns `true` if the value is an object, and `false` otherwise.
8
+ *
9
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to an object value.
10
+ *
11
+ * @param {any} value - The value to check if it is an object.
12
+ * @returns {value is object} `true` if the value is an object, `false` otherwise.
13
+ *
14
+ * @example
15
+ * const value1 = {};
16
+ * const value2 = [1, 2, 3];
17
+ * const value3 = () => {};
18
+ * const value4 = null;
19
+ *
20
+ * console.log(isObject(value1)); // true
21
+ * console.log(isObject(value2)); // true
22
+ * console.log(isObject(value3)); // true
23
+ * console.log(isObject(value4)); // false
24
+ */
25
+ declare function isObject(value?: any): value is object;
26
+ //#endregion
27
+ export { isObject };
@@ -0,0 +1,29 @@
1
+ //#region isObject.ts
2
+ /**
3
+ * Checks if the given value is an object. An object is a value that is
4
+ * not a primitive type (string, number, boolean, symbol, null, or undefined).
5
+ *
6
+ * This function tests whether the provided value is an object or not.
7
+ * It returns `true` if the value is an object, and `false` otherwise.
8
+ *
9
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to an object value.
10
+ *
11
+ * @param {any} value - The value to check if it is an object.
12
+ * @returns {value is object} `true` if the value is an object, `false` otherwise.
13
+ *
14
+ * @example
15
+ * const value1 = {};
16
+ * const value2 = [1, 2, 3];
17
+ * const value3 = () => {};
18
+ * const value4 = null;
19
+ *
20
+ * console.log(isObject(value1)); // true
21
+ * console.log(isObject(value2)); // true
22
+ * console.log(isObject(value3)); // true
23
+ * console.log(isObject(value4)); // false
24
+ */
25
+ function isObject(value) {
26
+ return value !== null && (typeof value === "object" || typeof value === "function");
27
+ }
28
+ //#endregion
29
+ export { isObject };
@@ -0,0 +1,21 @@
1
+ //#region isPromise.d.ts
2
+ /**
3
+ * Checks if a given value is `Promise`.
4
+ *
5
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `Promise`.
6
+ *
7
+ * @param {unknown} value The value to check if it is a `Promise`.
8
+ * @returns {value is Promise<any>} Returns `true` if `value` is a `Promise`, else `false`.
9
+ *
10
+ * @example
11
+ * const value1 = new Promise((resolve) => resolve());
12
+ * const value2 = {};
13
+ * const value3 = 123;
14
+ *
15
+ * console.log(isPromise(value1)); // true
16
+ * console.log(isPromise(value2)); // false
17
+ * console.log(isPromise(value3)); // false
18
+ */
19
+ declare function isPromise(value: unknown): value is Promise<any>;
20
+ //#endregion
21
+ export { isPromise };
@@ -0,0 +1,25 @@
1
+ import { isFunction } from "./isFunction.js";
2
+ import { isObject } from "./isObject.js";
3
+ //#region isPromise.ts
4
+ /**
5
+ * Checks if a given value is `Promise`.
6
+ *
7
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `Promise`.
8
+ *
9
+ * @param {unknown} value The value to check if it is a `Promise`.
10
+ * @returns {value is Promise<any>} Returns `true` if `value` is a `Promise`, else `false`.
11
+ *
12
+ * @example
13
+ * const value1 = new Promise((resolve) => resolve());
14
+ * const value2 = {};
15
+ * const value3 = 123;
16
+ *
17
+ * console.log(isPromise(value1)); // true
18
+ * console.log(isPromise(value2)); // false
19
+ * console.log(isPromise(value3)); // false
20
+ */
21
+ function isPromise(value) {
22
+ return value !== null && (isObject(value) || isFunction(value)) && typeof value.then === "function";
23
+ }
24
+ //#endregion
25
+ export { isPromise };
@@ -0,0 +1,21 @@
1
+ //#region isString.d.ts
2
+ /**
3
+ * Checks if a given value is string.
4
+ *
5
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `string`.
6
+ *
7
+ * @param {unknown} value The value to check if it is string.
8
+ * @returns {value is string} Returns `true` if `value` is a string, else `false`.
9
+ *
10
+ * @example
11
+ * const value1 = 'abc';
12
+ * const value2 = 123;
13
+ * const value3 = true;
14
+ *
15
+ * console.log(isString(value1)); // true
16
+ * console.log(isString(value2)); // false
17
+ * console.log(isString(value3)); // false
18
+ */
19
+ declare function isString(value: unknown): value is string;
20
+ //#endregion
21
+ export { isString };
@@ -0,0 +1,23 @@
1
+ //#region isString.ts
2
+ /**
3
+ * Checks if a given value is string.
4
+ *
5
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `string`.
6
+ *
7
+ * @param {unknown} value The value to check if it is string.
8
+ * @returns {value is string} Returns `true` if `value` is a string, else `false`.
9
+ *
10
+ * @example
11
+ * const value1 = 'abc';
12
+ * const value2 = 123;
13
+ * const value3 = true;
14
+ *
15
+ * console.log(isString(value1)); // true
16
+ * console.log(isString(value2)); // false
17
+ * console.log(isString(value3)); // false
18
+ */
19
+ function isString(value) {
20
+ return typeof value === "string";
21
+ }
22
+ //#endregion
23
+ export { isString };
@@ -0,0 +1,27 @@
1
+ //#region isSymbol.d.ts
2
+ /**
3
+ * Check whether a value is a symbol.
4
+ *
5
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `symbol`.
6
+ *
7
+ * @param {unknown} value The value to check.
8
+ * @returns {value is symbol} Returns `true` if `value` is a symbol, else `false`.
9
+ *
10
+ * @example
11
+ * import { isSymbol } from 'es-toolkit/predicate';
12
+ *
13
+ * isSymbol(Symbol('a')); // true
14
+ * isSymbol(Symbol.for('a')); // true
15
+ * isSymbol(Symbol.iterator); // true
16
+ *
17
+ * isSymbol(null); // false
18
+ * isSymbol(undefined); // false
19
+ * isSymbol('123'); // false
20
+ * isSymbol(false); // false
21
+ * isSymbol(123n); // false
22
+ * isSymbol({}); // false
23
+ * isSymbol([1, 2, 3]); // false
24
+ */
25
+ declare function isSymbol(value: unknown): value is symbol;
26
+ //#endregion
27
+ export { isSymbol };
@@ -0,0 +1,29 @@
1
+ //#region isSymbol.ts
2
+ /**
3
+ * Check whether a value is a symbol.
4
+ *
5
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `symbol`.
6
+ *
7
+ * @param {unknown} value The value to check.
8
+ * @returns {value is symbol} Returns `true` if `value` is a symbol, else `false`.
9
+ *
10
+ * @example
11
+ * import { isSymbol } from 'es-toolkit/predicate';
12
+ *
13
+ * isSymbol(Symbol('a')); // true
14
+ * isSymbol(Symbol.for('a')); // true
15
+ * isSymbol(Symbol.iterator); // true
16
+ *
17
+ * isSymbol(null); // false
18
+ * isSymbol(undefined); // false
19
+ * isSymbol('123'); // false
20
+ * isSymbol(false); // false
21
+ * isSymbol(123n); // false
22
+ * isSymbol({}); // false
23
+ * isSymbol([1, 2, 3]); // false
24
+ */
25
+ function isSymbol(value) {
26
+ return typeof value === "symbol";
27
+ }
28
+ //#endregion
29
+ export { isSymbol };
@@ -0,0 +1,24 @@
1
+ //#region isUndefined.d.ts
2
+ /**
3
+ * Checks if the given value is undefined.
4
+ *
5
+ * This function tests whether the provided value is strictly equal to `undefined`.
6
+ * It returns `true` if the value is `undefined`, and `false` otherwise.
7
+ *
8
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `undefined`.
9
+ *
10
+ * @param {unknown} value - The value to test if it is undefined.
11
+ * @returns {value is undefined} True if the value is undefined, false otherwise.
12
+ *
13
+ * @example
14
+ * const value1 = undefined;
15
+ * const value2 = null;
16
+ * const value3 = 42;
17
+ *
18
+ * console.log(isUndefined(value1)); // true
19
+ * console.log(isUndefined(value2)); // false
20
+ * console.log(isUndefined(value3)); // false
21
+ */
22
+ declare const isUndefined: (value: unknown) => value is undefined;
23
+ //#endregion
24
+ export { isUndefined };
@@ -0,0 +1,26 @@
1
+ //#region isUndefined.ts
2
+ /**
3
+ * Checks if the given value is undefined.
4
+ *
5
+ * This function tests whether the provided value is strictly equal to `undefined`.
6
+ * It returns `true` if the value is `undefined`, and `false` otherwise.
7
+ *
8
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `undefined`.
9
+ *
10
+ * @param {unknown} value - The value to test if it is undefined.
11
+ * @returns {value is undefined} True if the value is undefined, false otherwise.
12
+ *
13
+ * @example
14
+ * const value1 = undefined;
15
+ * const value2 = null;
16
+ * const value3 = 42;
17
+ *
18
+ * console.log(isUndefined(value1)); // true
19
+ * console.log(isUndefined(value2)); // false
20
+ * console.log(isUndefined(value3)); // false
21
+ */
22
+ const isUndefined = (value) => {
23
+ return value === void 0;
24
+ };
25
+ //#endregion
26
+ export { isUndefined };
package/dist/main.d.ts CHANGED
@@ -1,23 +1,13 @@
1
- //#region isArray.d.ts
2
- declare const isArray: <T = unknown>(value: unknown) => value is T[];
3
- //#endregion
4
- //#region isBoolean.d.ts
5
- declare const isBoolean: (value: unknown) => value is boolean;
6
- //#endregion
7
- //#region isFunction.d.ts
8
- type UnknownFunction<TArgs extends unknown[] = unknown[], TResult = unknown> = (...args: TArgs) => TResult;
9
- declare const isFunction: <TArgs extends unknown[] = unknown[], TResult = unknown>(value: unknown) => value is UnknownFunction<TArgs, TResult>;
10
- //#endregion
11
- //#region isNil.d.ts
12
- declare const isNil: (value: unknown) => value is null | undefined;
13
- //#endregion
14
- //#region isNumber.d.ts
15
- declare const isNumber: (value: unknown) => value is number;
16
- //#endregion
17
- //#region isString.d.ts
18
- declare const isString: (value: unknown) => value is string;
19
- //#endregion
20
- //#region isUndefined.d.ts
21
- declare const isUndefined: (value: unknown) => value is undefined;
22
- //#endregion
23
- export { isArray, isBoolean, isFunction, isNil, isNumber, isString, isUndefined };
1
+ import { get } from "./get.js";
2
+ import { isArray } from "./isArray.js";
3
+ import { isBoolean } from "./isBoolean.js";
4
+ import { isFunction } from "./isFunction.js";
5
+ import { isNil } from "./isNil.js";
6
+ import { isNull } from "./isNull.js";
7
+ import { isNumber } from "./isNumber.js";
8
+ import { isObject } from "./isObject.js";
9
+ import { isPromise } from "./isPromise.js";
10
+ import { isString } from "./isString.js";
11
+ import { isSymbol } from "./isSymbol.js";
12
+ import { isUndefined } from "./isUndefined.js";
13
+ export { get, isArray, isBoolean, isFunction, isNil, isNull, isNumber, isObject, isPromise, isString, isSymbol, isUndefined };
package/dist/main.js CHANGED
@@ -1,36 +1,13 @@
1
- //#region isArray.ts
2
- const isArray = (value) => {
3
- return Array.isArray(value);
4
- };
5
- //#endregion
6
- //#region isBoolean.ts
7
- const isBoolean = (value) => {
8
- return typeof value === "boolean";
9
- };
10
- //#endregion
11
- //#region isFunction.ts
12
- const isFunction = (value) => {
13
- return typeof value === "function";
14
- };
15
- //#endregion
16
- //#region isNil.ts
17
- const isNil = (value) => {
18
- return value === null || value === void 0;
19
- };
20
- //#endregion
21
- //#region isNumber.ts
22
- const isNumber = (value) => {
23
- return typeof value === "number" && !Number.isNaN(value);
24
- };
25
- //#endregion
26
- //#region isString.ts
27
- const isString = (value) => {
28
- return typeof value === "string";
29
- };
30
- //#endregion
31
- //#region isUndefined.ts
32
- const isUndefined = (value) => {
33
- return value === void 0;
34
- };
35
- //#endregion
36
- export { isArray, isBoolean, isFunction, isNil, isNumber, isString, isUndefined };
1
+ import { isArray } from "./isArray.js";
2
+ import { isBoolean } from "./isBoolean.js";
3
+ import { isFunction } from "./isFunction.js";
4
+ import { isUndefined } from "./isUndefined.js";
5
+ import { isNull } from "./isNull.js";
6
+ import { isNil } from "./isNil.js";
7
+ import { isNumber } from "./isNumber.js";
8
+ import { isString } from "./isString.js";
9
+ import { isObject } from "./isObject.js";
10
+ import { isPromise } from "./isPromise.js";
11
+ import { isSymbol } from "./isSymbol.js";
12
+ import { get } from "./get.js";
13
+ export { get, isArray, isBoolean, isFunction, isNil, isNull, isNumber, isObject, isPromise, isString, isSymbol, isUndefined };
@@ -0,0 +1,67 @@
1
+ import { toKey } from "../_internal/toKey.js";
2
+ import { toString } from "./toString.js";
3
+ //#region utils/toPath.ts
4
+ /**
5
+ * Converts a deep key string into an array of path segments.
6
+ *
7
+ * This function takes a string representing a deep key (e.g., 'a.b.c' or 'a[b][c]') and breaks it down into an array of strings, each representing a segment of the path.
8
+ *
9
+ * @param {any} deepKey - The deep key string to convert.
10
+ * @returns {string[]} An array of strings, each representing a segment of the path.
11
+ *
12
+ * Examples:
13
+ *
14
+ * toPath('a.b.c') // Returns ['a', 'b', 'c']
15
+ * toPath('a[b][c]') // Returns ['a', 'b', 'c']
16
+ * toPath('.a.b.c') // Returns ['', 'a', 'b', 'c']
17
+ * toPath('a["b.c"].d') // Returns ['a', 'b.c', 'd']
18
+ * toPath('') // Returns []
19
+ * toPath('.a[b].c.d[e]["f.g"].h') // Returns ['', 'a', 'b', 'c', 'd', 'e', 'f.g', 'h']
20
+ */
21
+ function toPath(deepKey) {
22
+ if (Array.isArray(deepKey)) return deepKey.map((item) => toKey(item));
23
+ if (typeof deepKey === "symbol") return [deepKey];
24
+ deepKey = toString(deepKey);
25
+ const result = [];
26
+ const length = deepKey.length;
27
+ if (length === 0) return result;
28
+ let index = 0;
29
+ let key = "";
30
+ let quoteChar = "";
31
+ let bracket = false;
32
+ if (deepKey.codePointAt(0) === 46) {
33
+ result.push("");
34
+ index++;
35
+ }
36
+ while (index < length) {
37
+ const char = deepKey[index];
38
+ if (quoteChar) if (char === "\\" && index + 1 < length) {
39
+ index++;
40
+ key += deepKey[index];
41
+ } else if (char === quoteChar) quoteChar = "";
42
+ else key += char;
43
+ else if (bracket) if (char === "\"" || char === "'") quoteChar = char;
44
+ else if (char === "]") {
45
+ bracket = false;
46
+ result.push(key);
47
+ key = "";
48
+ } else key += char;
49
+ else if (char === "[") {
50
+ bracket = true;
51
+ if (key) {
52
+ result.push(key);
53
+ key = "";
54
+ }
55
+ } else if (char === ".") {
56
+ if (key) {
57
+ result.push(key);
58
+ key = "";
59
+ }
60
+ } else key += char;
61
+ index++;
62
+ }
63
+ if (key) result.push(key);
64
+ return result;
65
+ }
66
+ //#endregion
67
+ export { toPath };
@@ -0,0 +1,28 @@
1
+ import { isNil } from "../isNil.js";
2
+ import { isString } from "../isString.js";
3
+ import { normalizeKey } from "../_internal/toKey.js";
4
+ //#region utils/toString.ts
5
+ /**
6
+ * Converts `value` to a string.
7
+ *
8
+ * An empty string is returned for `null` and `undefined` values.
9
+ * The sign of `-0` is preserved.
10
+ *
11
+ * @param {any} value - The value to convert.
12
+ * @returns {string} Returns the converted string.
13
+ *
14
+ * @example
15
+ * toString(null) // returns ''
16
+ * toString(undefined) // returns ''
17
+ * toString(-0) // returns '-0'
18
+ * toString([1, 2, -0]) // returns '1,2,-0'
19
+ * toString([Symbol('a'), Symbol('b')]) // returns 'Symbol(a),Symbol(b)'
20
+ */
21
+ function toString(value) {
22
+ if (isNil(value)) return "";
23
+ if (isString(value)) return value;
24
+ if (Array.isArray(value)) return value.map((item) => toString(item)).join(",");
25
+ return normalizeKey(value);
26
+ }
27
+ //#endregion
28
+ export { toString };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnific/utils",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "Shared utility predicates for Omnific packages",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -24,8 +24,13 @@
24
24
  "exports": {
25
25
  ".": {
26
26
  "types": "./dist/main.d.ts",
27
- "default": "./dist/main.js",
28
- "import": "./dist/main.js"
27
+ "import": "./dist/main.js",
28
+ "default": "./dist/main.js"
29
+ },
30
+ "./*": {
31
+ "types": "./dist/*.d.ts",
32
+ "import": "./dist/*.js",
33
+ "default": "./dist/*.js"
29
34
  }
30
35
  },
31
36
  "sideEffects": false,