Skip to content

Commit 49e9082

Browse files
authoredApr 22, 2023
Make deepKeys include empty arrays and objects (#105)
1 parent 7cfa81f commit 49e9082

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed
 

‎index.d.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ console.log(getProperty(object, escapedPath));
133133
export function escapePath(path: string): string;
134134

135135
/**
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.
137137
138138
This can be useful to help flatten an object for an API that only accepts key-value pairs or for a tagged template literal.
139139
@@ -148,12 +148,16 @@ const user = {
148148
first: 'Richie',
149149
last: 'Bendall',
150150
},
151+
activeTasks: [],
152+
currentProject: null
151153
};
152154
153155
for (const property of deepKeys(user)) {
154156
console.log(`${property}: ${getProperty(user, property)}`);
155157
//=> name.first: Richie
156158
//=> name.last: Bendall
159+
//=> activeTasks: []
160+
//=> currentProject: null
157161
}
158162
```
159163
*/

‎index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const isObject = value => {
33
return value !== null && (type === 'object' || type === 'function');
44
};
55

6+
const isEmptyObject = value => isObject(value) && Object.keys(value).length === 0;
7+
68
const disallowedKeys = new Set([
79
'__proto__',
810
'prototype',
@@ -316,7 +318,7 @@ function stringifyPath(pathSegments) {
316318
}
317319

318320
function * deepKeysIterator(object, currentPath = []) {
319-
if (!isObject(object)) {
321+
if (!isObject(object) || isEmptyObject(object)) {
320322
if (currentPath.length > 0) {
321323
yield stringifyPath(currentPath);
322324
}

‎readme.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ console.log(getProperty(object, escapedPath));
110110

111111
### deepKeys(object)
112112

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.
114114

115115
This can be useful to help flatten an object for an API that only accepts key-value pairs or for a tagged template literal.
116116

@@ -122,12 +122,16 @@ const user = {
122122
first: 'Richie',
123123
last: 'Bendall',
124124
},
125+
activeTasks: [],
126+
currentProject: null
125127
};
126128

127129
for (const property of deepKeys(user)) {
128130
console.log(`${property}: ${getProperty(user, property)}`);
129131
//=> name.first: Richie
130132
//=> name.last: Bendall
133+
//=> activeTasks: []
134+
//=> currentProject: null
131135
}
132136
```
133137

‎test.js

+14
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,20 @@ test('escapePath', t => {
413413

414414
test('deepKeys', t => {
415415
const object = {
416+
eo: {},
417+
ea: [],
416418
'a.b': {
417419
c: {
418420
d: [1, 2, {
419421
g: 3,
420422
}],
421423
e: '🦄',
422424
f: 0,
425+
h: {},
426+
i: [],
427+
nu: null,
428+
na: Number.NaN,
429+
un: undefined,
423430
},
424431
'': {
425432
a: 0,
@@ -432,11 +439,18 @@ test('deepKeys', t => {
432439
const keys = deepKeys(object);
433440

434441
t.deepEqual(keys, [
442+
'eo',
443+
'ea',
435444
'a\\.b.c.d[0]',
436445
'a\\.b.c.d[1]',
437446
'a\\.b.c.d[2].g',
438447
'a\\.b.c.e',
439448
'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',
440454
'a\\.b..a',
441455
'.a',
442456
]);

0 commit comments

Comments
 (0)
Please sign in to comment.