Skip to content

Commit

Permalink
fix deepkeys to work with empty arrays and objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Kostyukov committed Mar 6, 2023
1 parent 7cfa81f commit 8f27bed
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const isObject = value => {
return value !== null && (type === 'object' || type === 'function');
};

const isEmptyObject = value => isObject(value) && Object.keys(value).length === 0;

const disallowedKeys = new Set([
'__proto__',
'prototype',
Expand Down Expand Up @@ -326,6 +328,10 @@ function * deepKeysIterator(object, currentPath = []) {

for (const [key, value] of entries(object)) {
yield * deepKeysIterator(value, [...currentPath, key]);

if (isEmptyObject(value)) {
yield key;
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ test('deepKeys', t => {
}],
e: '🦄',
f: 0,
h: {},
i: [],
},
'': {
a: 0,
Expand All @@ -437,6 +439,8 @@ test('deepKeys', t => {
'a\\.b.c.d[2].g',
'a\\.b.c.e',
'a\\.b.c.f',
'h',
'i',
'a\\.b..a',
'.a',
]);
Expand Down

0 comments on commit 8f27bed

Please sign in to comment.