Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c9264a6

Browse filesBrowse files
committed
Expand values into arrays when setting with nestedproperty
1 parent 00355fb commit c9264a6
Copy full SHA for c9264a6

File tree

2 files changed

+25
-2
lines changed
Filter options

2 files changed

+25
-2
lines changed

‎src/lib/nested_property.js

Copy file name to clipboardExpand all lines: src/lib/nested_property.js
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,13 @@ function checkNewContainer(container, part, nextPart, toDelete) {
243243
if(container[part] === undefined) {
244244
if(toDelete) return false;
245245

246-
if(typeof nextPart === 'number') container[part] = [];
247-
else container[part] = {};
246+
if(typeof nextPart === 'number') {
247+
container[part] = [];
248+
} else {
249+
container[part] = {};
250+
}
251+
} else if(!isPlainObject(container[part]) && !isArray(container[part]) && typeof nextPart === 'number') {
252+
container[part] = [container[part]];
248253
}
249254
return true;
250255
}

‎test/jasmine/tests/lib_test.js

Copy file name to clipboardExpand all lines: test/jasmine/tests/lib_test.js
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,24 @@ describe('Test lib.js:', function() {
434434
expect(badProp(i)).toThrow('bad property string');
435435
}
436436
});
437+
438+
it('wraps values when cannot set an array property', function() {
439+
var obj = {x: 4, y: 5, foo: {bar: 6, baz: 7}};
440+
441+
np(obj, 'x[0]').set(10);
442+
np(obj, 'y[2]').set(11);
443+
np(obj, 'foo.bar[0]').set(12);
444+
np(obj, 'foo.baz[2]').set(13);
445+
446+
expect(obj).toEqual({
447+
x: [10],
448+
y: [5, undefined, 11],
449+
foo: {
450+
bar: [12],
451+
baz: [7, undefined, 13]
452+
}
453+
});
454+
});
437455
});
438456

439457
describe('objectFromPath', function() {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.