Skip to content

Commit

Permalink
Fix handling of sparse arrays (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
AuthorProxy authored Jul 18, 2023
1 parent b58cb21 commit e314e80
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 5 deletions.
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

0 comments on commit e314e80

Please sign in to comment.