Skip to content

Commit d363922

Browse files
authoredJan 22, 2022
Fix crash when modifying array length (#89)
1 parent 0289e19 commit d363922

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed
 

‎index.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,10 @@ export function setProperty(object, path, value) {
219219

220220
assertNotStringIndex(object, key);
221221

222-
if (!isObject(object[key])) {
223-
object[key] = typeof pathArray[index + 1] === 'number' ? [] : {};
224-
}
225-
226222
if (index === pathArray.length - 1) {
227223
object[key] = value;
224+
} else if (!isObject(object[key])) {
225+
object[key] = typeof pathArray[index + 1] === 'number' ? [] : {};
228226
}
229227

230228
object = object[key];

‎test.js

+5
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ test('setProperty', t => {
245245
bar: true,
246246
}],
247247
});
248+
249+
const fixture7 = {foo: ['bar', 'baz']};
250+
setProperty(fixture7, 'foo.length', 1);
251+
t.is(fixture7.foo.length, 1);
252+
t.deepEqual(fixture7, {foo: ['bar']});
248253
});
249254

250255
test('deleteProperty', t => {

0 commit comments

Comments
 (0)
Please sign in to comment.