diff --git a/index.js b/index.js index abd5e30..11d9895 100644 --- a/index.js +++ b/index.js @@ -219,12 +219,10 @@ export function setProperty(object, path, value) { assertNotStringIndex(object, key); - if (!isObject(object[key])) { - object[key] = typeof pathArray[index + 1] === 'number' ? [] : {}; - } - if (index === pathArray.length - 1) { object[key] = value; + } else if (!isObject(object[key])) { + object[key] = typeof pathArray[index + 1] === 'number' ? [] : {}; } object = object[key]; diff --git a/test.js b/test.js index 0430e58..5db7cc7 100644 --- a/test.js +++ b/test.js @@ -245,6 +245,11 @@ test('setProperty', t => { bar: true, }], }); + + const fixture7 = {foo: ['bar', 'baz']}; + setProperty(fixture7, 'foo.length', 1); + t.is(fixture7.foo.length, 1); + t.deepEqual(fixture7, {foo: ['bar']}); }); test('deleteProperty', t => {