Skip to content
Open
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
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export function unflatten (target, opts) {
// safely ensure that the key is
// an integer.
function getkey (key) {
// `Number('')` is 0 (not NaN), so an empty-string key would be coerced to
// the numeric index 0 and renamed; keep it as-is.
if (key === '') return key

const parsedKey = Number(key)

return (
Expand Down
6 changes: 6 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,12 @@ describe('Arrays', function () {
}))
})

test('Should keep an empty-string key instead of renaming it to 0', function () {
assert.deepStrictEqual({ '': 1, x: 2 }, unflatten({ '': 1, x: 2 }))
assert.deepStrictEqual({ a: { '': 1 } }, unflatten({ 'a.': 1 }))
assert.deepStrictEqual({ '': 1, x: 2 }, unflatten(flatten({ '': 1, x: 2 })))
})

test('Array typed objects should be restored by unflatten', function () {
assert.strictEqual(
Object.prototype.toString.call(['foo', 'bar'])
Expand Down