Skip to content

Commit

Permalink
Check if the target is an object instead of an array to avoid deletin…
Browse files Browse the repository at this point in the history
…g properties

fixes jonschlinkert#35
  • Loading branch information
snowbldr committed Mar 26, 2022
1 parent 56a44ed commit 2a24ab2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
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
10 changes: 8 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,15 @@ describe('set-value', () => {
});

it('should not delete number properties from an object', ()=>{
const o = {a: {0: 'foo', 1: 'bar'}}
const o = { a: { 0: 'foo', 1: 'bar' } }
set(o, 'a.0', 'baz')
assert.deepEqual(o, {a: {0: 'baz', 1: 'bar'}})
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 a deeply nested property if it does not already exist', () => {
Expand Down

0 comments on commit 2a24ab2

Please sign in to comment.