Skip to content

Commit f21b68e

Browse files
committedJun 29, 2023
Fix deepKeys to not throw on sparse arrays
Fixes #108
1 parent bf6224b commit f21b68e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed
 

Diff for: ‎index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ export function escapePath(path) {
296296
// The keys returned by Object.entries() for arrays are strings
297297
function entries(value) {
298298
if (Array.isArray(value)) {
299-
return value.map((value, index) => [index, value]);
299+
// We use `[...value]` to convert sparse entries to normal ones.
300+
return [...value].map((value, index) => [index, value]);
300301
}
301302

302303
return Object.entries(value);

Diff for: ‎test.js

+15
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ test('deepKeys', t => {
436436
a: 0,
437437
},
438438
};
439+
439440
const keys = deepKeys(object);
440441

441442
t.deepEqual(keys, [
@@ -463,6 +464,20 @@ test('deepKeys', t => {
463464
t.deepEqual(deepKeys(0), []);
464465
});
465466

467+
test('deepKeys - does not throw on sparse array', t => {
468+
const object = {
469+
sparse: [1,,3], // eslint-disable-line no-sparse-arrays, comma-spacing
470+
};
471+
472+
const keys = deepKeys(object);
473+
474+
t.deepEqual(keys, [
475+
'sparse[0]',
476+
'sparse[1]',
477+
'sparse[2]',
478+
]);
479+
});
480+
466481
test('prevent setting/getting `__proto__`', t => {
467482
setProperty({}, '__proto__.unicorn', '🦄');
468483
t.not({}.unicorn, '🦄'); // eslint-disable-line no-use-extend-native/no-use-extend-native

0 commit comments

Comments
 (0)
Please sign in to comment.