8000 Expand values into arrays when setting with nestedproperty · gnarracci/plotly.js@c9264a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit c9264a6

Browse files
committed
Expand values into arrays when setting with nestedproperty
1 parent 00355fb commit c9264a6

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/lib/nested_property.js

Lines 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

Lines 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