Skip to content

Commit

Permalink
fix(shape): Fix issue with setting option by deep path
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaiklor committed Dec 7, 2015
1 parent 4c4cb28 commit 6b9a26a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ export default class Shape {
_options = {};

/**
* Constructor is responsible for initializing base properties.
* Don't forgot to call `super(...args)` when extending from this class.
*
* @constructor
* @param {Object} [options]
* @param {String} [options.text] Text that will be rendered in shape
* @param {String} [options.text] Text that will be rendered in the shape
* @param {Number} [options.width] Shape width
* @param {Number} [options.height] Shape height
* @param {Number} [options.x] Absolute coordinate X
Expand Down Expand Up @@ -56,12 +53,22 @@ export default class Shape {
/**
* Set new option value.
*
* @param {String} path
* @param {String} path Path can be set with dot-notation
* @param {*} value
* @returns {Shape}
*/
set(path, value) {
this._options[path] = value;
let obj = this._options;
let tags = path.split('.');
let len = tags.length - 1;

for (let i = 0; i < len; i++) {
if (typeof obj[tags[i]] === 'undefined') obj[tags[i]] = {};
obj = obj[tags[i]];
}

obj[tags[len]] = value;

return this;
}

Expand Down
9 changes: 9 additions & 0 deletions test/unit/Basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ describe('Shape', () => {
assert.equal(shape.getText(), 'test');
});

it('Should properly get/set from options object', () => {
let shape = new Shape();
assert.equal(shape.get('text'), '');
assert.instanceOf(shape.set('animation.name', 'print'), Shape);
assert.equal(shape.get('animation.name'), 'print');
assert.instanceOf(shape.set('animation.name', 'test'), Shape);
assert.equal(shape.get('animation.name'), 'test');
});

it('Should properly get/set text', () => {
let shape = new Shape();
assert.equal(shape.getText(), '');
Expand Down

0 comments on commit 6b9a26a

Please sign in to comment.