File tree 4 files changed +27
-3
lines changed
4 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -133,7 +133,7 @@ console.log(getProperty(object, escapedPath));
133
133
export function escapePath ( path : string ) : string ;
134
134
135
135
/**
136
- Returns an array of every path. Plain objects are deeply recursed and are not themselves included.
136
+ Returns an array of every path. Non-empty plain objects and arrays are deeply recursed and are not themselves included.
137
137
138
138
This can be useful to help flatten an object for an API that only accepts key-value pairs or for a tagged template literal.
139
139
@@ -148,12 +148,16 @@ const user = {
148
148
first: 'Richie',
149
149
last: 'Bendall',
150
150
},
151
+ activeTasks: [],
152
+ currentProject: null
151
153
};
152
154
153
155
for (const property of deepKeys(user)) {
154
156
console.log(`${property}: ${getProperty(user, property)}`);
155
157
//=> name.first: Richie
156
158
//=> name.last: Bendall
159
+ //=> activeTasks: []
160
+ //=> currentProject: null
157
161
}
158
162
```
159
163
*/
Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ const isObject = value => {
3
3
return value !== null && ( type === 'object' || type === 'function' ) ;
4
4
} ;
5
5
6
+ const isEmptyObject = value => isObject ( value ) && Object . keys ( value ) . length === 0 ;
7
+
6
8
const disallowedKeys = new Set ( [
7
9
'__proto__' ,
8
10
'prototype' ,
@@ -316,7 +318,7 @@ function stringifyPath(pathSegments) {
316
318
}
317
319
318
320
function * deepKeysIterator ( object , currentPath = [ ] ) {
319
- if ( ! isObject ( object ) ) {
321
+ if ( ! isObject ( object ) || isEmptyObject ( object ) ) {
320
322
if ( currentPath . length > 0 ) {
321
323
yield stringifyPath ( currentPath ) ;
322
324
}
Original file line number Diff line number Diff line change @@ -110,7 +110,7 @@ console.log(getProperty(object, escapedPath));
110
110
111
111
### deepKeys(object)
112
112
113
- Returns an array of every path. Plain objects are deeply recursed and are not themselves included.
113
+ Returns an array of every path. Non-empty plain objects and arrays are deeply recursed and are not themselves included.
114
114
115
115
This can be useful to help flatten an object for an API that only accepts key-value pairs or for a tagged template literal.
116
116
@@ -122,12 +122,16 @@ const user = {
122
122
first: ' Richie' ,
123
123
last: ' Bendall' ,
124
124
},
125
+ activeTasks: [],
126
+ currentProject: null
125
127
};
126
128
127
129
for (const property of deepKeys (user)) {
128
130
console .log (` ${ property} : ${ getProperty (user, property)} ` );
129
131
// => name.first: Richie
130
132
// => name.last: Bendall
133
+ // => activeTasks: []
134
+ // => currentProject: null
131
135
}
132
136
```
133
137
Original file line number Diff line number Diff line change @@ -413,13 +413,20 @@ test('escapePath', t => {
413
413
414
414
test ( 'deepKeys' , t => {
415
415
const object = {
416
+ eo : { } ,
417
+ ea : [ ] ,
416
418
'a.b' : {
417
419
c : {
418
420
d : [ 1 , 2 , {
419
421
g : 3 ,
420
422
} ] ,
421
423
e : '🦄' ,
422
424
f : 0 ,
425
+ h : { } ,
426
+ i : [ ] ,
427
+ nu : null ,
428
+ na : Number . NaN ,
429
+ un : undefined ,
423
430
} ,
424
431
'' : {
425
432
a : 0 ,
@@ -432,11 +439,18 @@ test('deepKeys', t => {
432
439
const keys = deepKeys ( object ) ;
433
440
434
441
t . deepEqual ( keys , [
442
+ 'eo' ,
443
+ 'ea' ,
435
444
'a\\.b.c.d[0]' ,
436
445
'a\\.b.c.d[1]' ,
437
446
'a\\.b.c.d[2].g' ,
438
447
'a\\.b.c.e' ,
439
448
'a\\.b.c.f' ,
449
+ 'a\\.b.c.h' ,
450
+ 'a\\.b.c.i' ,
451
+ 'a\\.b.c.nu' ,
452
+ 'a\\.b.c.na' ,
453
+ 'a\\.b.c.un' ,
440
454
'a\\.b..a' ,
441
455
'.a' ,
442
456
] ) ;
You can’t perform that action at this time.
0 commit comments