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

Dont delete number keys #45

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ const setValue = (target, path, value, options) => {
break;
}

if (typeof next === 'number' && !Array.isArray(obj[key])) {
if (typeof next === 'number' && (typeof obj[key] !== 'object' || obj[key] === null)) {
obj = obj[key] = [];
continue;
}
Expand Down
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ describe('set-value', () => {
assert.deepEqual(o.a[2].c, { y: 'z' });
});

it('should not delete number properties from an object', ()=>{
const o = { a: { 0: 'foo', 1: 'bar' } }
set(o, 'a.0', 'baz')
assert.deepEqual(o, { a: { 0: 'baz', 1: 'bar' } })
})

it('should create an array if the target is null', ()=>{
const o = { a: null }
set(o, 'a.0', 'baz')
assert.deepEqual(o, { a: ['baz'] })
})

it('should create an array if the target is not an array or object', ()=>{
const o = { a: false, b: undefined, c: 5 }
set(o, 'a.0', 'foo')
set(o, 'b.0', 'bar')
set(o, 'c.0', 'baz')
assert.deepEqual(o, { a: ['foo'], b: ['bar'], c: ['baz'] })
})

it('should create a deeply nested property if it does not already exist', () => {
const o = {};
set(o, 'a.b.c.d.e', 'c');
Expand Down