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 3, 2023
1 parent 7cfa81f commit 40f947c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions index.js
Expand Up @@ -3,6 +3,10 @@ const isObject = value => {
return value !== null && (type === 'object' || type === 'function');
};

const isEmptyObject = (value) => {

Check failure on line 6 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 16

Unexpected parentheses around single function argument.

Check failure on line 6 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 16

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`.

Check failure on line 6 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 14

Unexpected parentheses around single function argument.

Check failure on line 6 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 14

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`.

Check failure on line 6 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 12

Unexpected parentheses around single function argument.

Check failure on line 6 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 12

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`.
return isObject(value) && Object.entries(value).length === 0;
};

const disallowedKeys = new Set([
'__proto__',
'prototype',
Expand Down Expand Up @@ -326,6 +330,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
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 40f947c

Please sign in to comment.