@naturalcycles/js-lib 14.110.0 → 14.111.0
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/object/object.util.d.ts +11 -11
- package/dist/object/object.util.js +12 -13
- package/dist-esm/object/object.util.js +12 -13
- package/package.json +1 -1
- package/src/object/object.util.ts +14 -20
|
@@ -135,19 +135,19 @@ export declare function _invert<T extends AnyObject>(o: T): {
|
|
|
135
135
|
};
|
|
136
136
|
export declare function _invertMap<K, V>(m: ReadonlyMap<K, V>): Map<V, K>;
|
|
137
137
|
/**
|
|
138
|
-
* Gets the property value at path of object.
|
|
139
|
-
* in its place.
|
|
138
|
+
* Gets the property value at path of object.
|
|
140
139
|
*
|
|
141
|
-
* @
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
140
|
+
* @example
|
|
141
|
+
* const obj = {a: 'a', b: 'b', c: { cc: 'cc' }}
|
|
142
|
+
* _get(obj, 'a') // 'a'
|
|
143
|
+
* _get(obj, 'c.cc') // 'cc'
|
|
144
|
+
* _get(obj, 'c[cc]') // 'cc'
|
|
145
|
+
* _get(obj, 'unknown.path') // undefined
|
|
145
146
|
*/
|
|
146
|
-
export declare function _get<T extends AnyObject>(obj?: T, path?: string
|
|
147
|
+
export declare function _get<T extends AnyObject>(obj?: T, path?: string): unknown;
|
|
147
148
|
/**
|
|
148
149
|
* Sets the value at path of object. If a portion of path doesn’t exist it’s created. Arrays are created for
|
|
149
|
-
* missing index properties while objects are created for all other missing properties.
|
|
150
|
-
* customize path creation.
|
|
150
|
+
* missing index properties while objects are created for all other missing properties.
|
|
151
151
|
*
|
|
152
152
|
* @param obj The object to modify.
|
|
153
153
|
* @param path The path of the property to set.
|
|
@@ -156,7 +156,7 @@ export declare function _get<T extends AnyObject>(obj?: T, path?: string, def?:
|
|
|
156
156
|
*
|
|
157
157
|
* Based on: https://stackoverflow.com/a/54733755/4919972
|
|
158
158
|
*/
|
|
159
|
-
export declare function _set<
|
|
159
|
+
export declare function _set<T extends AnyObject>(obj: T, path: PropertyPath, value: any): T;
|
|
160
160
|
/**
|
|
161
161
|
* Checks if `path` is a direct property of `object` (not null, not undefined).
|
|
162
162
|
*
|
|
@@ -181,4 +181,4 @@ export declare function _set<IN extends AnyObject, OUT = IN>(obj: IN, path: Prop
|
|
|
181
181
|
* _.has(other, 'a');
|
|
182
182
|
* // => false
|
|
183
183
|
*/
|
|
184
|
-
export declare function _has<T extends AnyObject>(obj: T, path
|
|
184
|
+
export declare function _has<T extends AnyObject>(obj: T, path: string): boolean;
|
|
@@ -286,26 +286,25 @@ function _invertMap(m) {
|
|
|
286
286
|
}
|
|
287
287
|
exports._invertMap = _invertMap;
|
|
288
288
|
/**
|
|
289
|
-
* Gets the property value at path of object.
|
|
290
|
-
* in its place.
|
|
289
|
+
* Gets the property value at path of object.
|
|
291
290
|
*
|
|
292
|
-
* @
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
291
|
+
* @example
|
|
292
|
+
* const obj = {a: 'a', b: 'b', c: { cc: 'cc' }}
|
|
293
|
+
* _get(obj, 'a') // 'a'
|
|
294
|
+
* _get(obj, 'c.cc') // 'cc'
|
|
295
|
+
* _get(obj, 'c[cc]') // 'cc'
|
|
296
|
+
* _get(obj, 'unknown.path') // undefined
|
|
296
297
|
*/
|
|
297
|
-
function _get(obj = {}, path = ''
|
|
298
|
-
|
|
298
|
+
function _get(obj = {}, path = '') {
|
|
299
|
+
return path
|
|
299
300
|
.replace(/\[([^\]]+)]/g, '.$1')
|
|
300
301
|
.split('.')
|
|
301
|
-
.reduce((o, p) => o[p], obj);
|
|
302
|
-
return res === undefined ? def : res;
|
|
302
|
+
.reduce((o, p) => o?.[p], obj);
|
|
303
303
|
}
|
|
304
304
|
exports._get = _get;
|
|
305
305
|
/**
|
|
306
306
|
* Sets the value at path of object. If a portion of path doesn’t exist it’s created. Arrays are created for
|
|
307
|
-
* missing index properties while objects are created for all other missing properties.
|
|
308
|
-
* customize path creation.
|
|
307
|
+
* missing index properties while objects are created for all other missing properties.
|
|
309
308
|
*
|
|
310
309
|
* @param obj The object to modify.
|
|
311
310
|
* @param path The path of the property to set.
|
|
@@ -336,7 +335,7 @@ function _set(obj, path, value) {
|
|
|
336
335
|
? [] // Yes: assign a new array object
|
|
337
336
|
: {}), // No: assign a new plain object
|
|
338
337
|
obj)[path[path.length - 1]] = value; // Finally assign the value to the last key
|
|
339
|
-
return obj; //
|
|
338
|
+
return obj; // allow chaining
|
|
340
339
|
}
|
|
341
340
|
exports._set = _set;
|
|
342
341
|
/**
|
|
@@ -263,25 +263,24 @@ export function _invertMap(m) {
|
|
|
263
263
|
return inv;
|
|
264
264
|
}
|
|
265
265
|
/**
|
|
266
|
-
* Gets the property value at path of object.
|
|
267
|
-
* in its place.
|
|
266
|
+
* Gets the property value at path of object.
|
|
268
267
|
*
|
|
269
|
-
* @
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
268
|
+
* @example
|
|
269
|
+
* const obj = {a: 'a', b: 'b', c: { cc: 'cc' }}
|
|
270
|
+
* _get(obj, 'a') // 'a'
|
|
271
|
+
* _get(obj, 'c.cc') // 'cc'
|
|
272
|
+
* _get(obj, 'c[cc]') // 'cc'
|
|
273
|
+
* _get(obj, 'unknown.path') // undefined
|
|
273
274
|
*/
|
|
274
|
-
export function _get(obj = {}, path = ''
|
|
275
|
-
|
|
275
|
+
export function _get(obj = {}, path = '') {
|
|
276
|
+
return path
|
|
276
277
|
.replace(/\[([^\]]+)]/g, '.$1')
|
|
277
278
|
.split('.')
|
|
278
|
-
.reduce((o, p) => o[p], obj);
|
|
279
|
-
return res === undefined ? def : res;
|
|
279
|
+
.reduce((o, p) => o === null || o === void 0 ? void 0 : o[p], obj);
|
|
280
280
|
}
|
|
281
281
|
/**
|
|
282
282
|
* Sets the value at path of object. If a portion of path doesn’t exist it’s created. Arrays are created for
|
|
283
|
-
* missing index properties while objects are created for all other missing properties.
|
|
284
|
-
* customize path creation.
|
|
283
|
+
* missing index properties while objects are created for all other missing properties.
|
|
285
284
|
*
|
|
286
285
|
* @param obj The object to modify.
|
|
287
286
|
* @param path The path of the property to set.
|
|
@@ -312,7 +311,7 @@ export function _set(obj, path, value) {
|
|
|
312
311
|
? [] // Yes: assign a new array object
|
|
313
312
|
: {}), // No: assign a new plain object
|
|
314
313
|
obj)[path[path.length - 1]] = value; // Finally assign the value to the last key
|
|
315
|
-
return obj; //
|
|
314
|
+
return obj; // allow chaining
|
|
316
315
|
}
|
|
317
316
|
/**
|
|
318
317
|
* Checks if `path` is a direct property of `object` (not null, not undefined).
|
package/package.json
CHANGED
|
@@ -311,27 +311,25 @@ export function _invertMap<K, V>(m: ReadonlyMap<K, V>): Map<V, K> {
|
|
|
311
311
|
}
|
|
312
312
|
|
|
313
313
|
/**
|
|
314
|
-
* Gets the property value at path of object.
|
|
315
|
-
* in its place.
|
|
314
|
+
* Gets the property value at path of object.
|
|
316
315
|
*
|
|
317
|
-
* @
|
|
318
|
-
*
|
|
319
|
-
*
|
|
320
|
-
*
|
|
316
|
+
* @example
|
|
317
|
+
* const obj = {a: 'a', b: 'b', c: { cc: 'cc' }}
|
|
318
|
+
* _get(obj, 'a') // 'a'
|
|
319
|
+
* _get(obj, 'c.cc') // 'cc'
|
|
320
|
+
* _get(obj, 'c[cc]') // 'cc'
|
|
321
|
+
* _get(obj, 'unknown.path') // undefined
|
|
321
322
|
*/
|
|
322
|
-
export function _get<T extends AnyObject>(obj = {} as T, path = ''
|
|
323
|
-
|
|
323
|
+
export function _get<T extends AnyObject>(obj = {} as T, path = ''): unknown {
|
|
324
|
+
return path
|
|
324
325
|
.replace(/\[([^\]]+)]/g, '.$1')
|
|
325
326
|
.split('.')
|
|
326
|
-
.reduce((o, p) => o[p], obj)
|
|
327
|
-
|
|
328
|
-
return res === undefined ? def : res
|
|
327
|
+
.reduce((o, p) => o?.[p], obj)
|
|
329
328
|
}
|
|
330
329
|
|
|
331
330
|
/**
|
|
332
331
|
* Sets the value at path of object. If a portion of path doesn’t exist it’s created. Arrays are created for
|
|
333
|
-
* missing index properties while objects are created for all other missing properties.
|
|
334
|
-
* customize path creation.
|
|
332
|
+
* missing index properties while objects are created for all other missing properties.
|
|
335
333
|
*
|
|
336
334
|
* @param obj The object to modify.
|
|
337
335
|
* @param path The path of the property to set.
|
|
@@ -340,11 +338,7 @@ export function _get<T extends AnyObject>(obj = {} as T, path = '', def?: any):
|
|
|
340
338
|
*
|
|
341
339
|
* Based on: https://stackoverflow.com/a/54733755/4919972
|
|
342
340
|
*/
|
|
343
|
-
export function _set<
|
|
344
|
-
obj: IN,
|
|
345
|
-
path: PropertyPath,
|
|
346
|
-
value?: any,
|
|
347
|
-
): OUT {
|
|
341
|
+
export function _set<T extends AnyObject>(obj: T, path: PropertyPath, value: any): T {
|
|
348
342
|
if (!obj || Object(obj) !== obj || !path) return obj as any // When obj is not an object
|
|
349
343
|
|
|
350
344
|
// If not yet an array, get the keys from the string-path
|
|
@@ -373,7 +367,7 @@ export function _set<IN extends AnyObject, OUT = IN>(
|
|
|
373
367
|
obj,
|
|
374
368
|
)[path[path.length - 1]!] = value // Finally assign the value to the last key
|
|
375
369
|
|
|
376
|
-
return obj
|
|
370
|
+
return obj // allow chaining
|
|
377
371
|
}
|
|
378
372
|
|
|
379
373
|
/**
|
|
@@ -400,7 +394,7 @@ export function _set<IN extends AnyObject, OUT = IN>(
|
|
|
400
394
|
* _.has(other, 'a');
|
|
401
395
|
* // => false
|
|
402
396
|
*/
|
|
403
|
-
export function _has<T extends AnyObject>(obj: T, path
|
|
397
|
+
export function _has<T extends AnyObject>(obj: T, path: string): boolean {
|
|
404
398
|
const v = _get(obj, path)
|
|
405
399
|
return v !== undefined && v !== null
|
|
406
400
|
}
|