Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix deepkeys for sparsed arrays #110

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,12 @@ export function escapePath(path) {

// The keys returned by Object.entries() for arrays are strings
function entries(value) {
const result = Object.entries(value);
if (Array.isArray(value)) {
// We use `[...value]` to convert sparse entries to normal ones.
return [...value].map((value, index) => [index, value]);
return result.map(([key, value]) => [Number(key), value]);
}

return Object.entries(value);
return result;
}

function stringifyPath(pathSegments) {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ for (const property of deepKeys(user)) {
}
```

Sparse arrays are supported, but holes are filled. In general, [avoid using sparse arrays](https://github.com/sindresorhus/dot-prop/issues/109#issuecomment-1614819869).
Sparse arrays are supported. In general, [avoid using sparse arrays](https://github.com/sindresorhus/dot-prop/issues/109#issuecomment-1614819869).

#### object

Expand Down
1 change: 0 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,6 @@ test('deepKeys - does not throw on sparse array', t => {

t.deepEqual(keys, [
'sparse[0]',
'sparse[1]',
'sparse[2]',
]);
});
Expand Down