Skip to content

Commit 6b9a26a

Browse files
committed
fix(shape): Fix issue with setting option by deep path
1 parent 4c4cb28 commit 6b9a26a

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

src/Basic.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ export default class Shape {
1717
_options = {};
1818

1919
/**
20-
* Constructor is responsible for initializing base properties.
21-
* Don't forgot to call `super(...args)` when extending from this class.
22-
*
2320
* @constructor
2421
* @param {Object} [options]
25-
* @param {String} [options.text] Text that will be rendered in shape
22+
* @param {String} [options.text] Text that will be rendered in the shape
2623
* @param {Number} [options.width] Shape width
2724
* @param {Number} [options.height] Shape height
2825
* @param {Number} [options.x] Absolute coordinate X
@@ -56,12 +53,22 @@ export default class Shape {
5653
/**
5754
* Set new option value.
5855
*
59-
* @param {String} path
56+
* @param {String} path Path can be set with dot-notation
6057
* @param {*} value
6158
* @returns {Shape}
6259
*/
6360
set(path, value) {
64-
this._options[path] = value;
61+
let obj = this._options;
62+
let tags = path.split('.');
63+
let len = tags.length - 1;
64+
65+
for (let i = 0; i < len; i++) {
66+
if (typeof obj[tags[i]] === 'undefined') obj[tags[i]] = {};
67+
obj = obj[tags[i]];
68+
}
69+
70+
obj[tags[len]] = value;
71+
6572
return this;
6673
}
6774

test/unit/Basic.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ describe('Shape', () => {
1414
assert.equal(shape.getText(), 'test');
1515
});
1616

17+
it('Should properly get/set from options object', () => {
18+
let shape = new Shape();
19+
assert.equal(shape.get('text'), '');
20+
assert.instanceOf(shape.set('animation.name', 'print'), Shape);
21+
assert.equal(shape.get('animation.name'), 'print');
22+
assert.instanceOf(shape.set('animation.name', 'test'), Shape);
23+
assert.equal(shape.get('animation.name'), 'test');
24+
});
25+
1726
it('Should properly get/set text', () => {
1827
let shape = new Shape();
1928
assert.equal(shape.getText(), '');

0 commit comments

Comments
 (0)