Skip to content

Commit

Permalink
Merge pull request #176 from aneilbaboo/aneil/setSingleKey
Browse files Browse the repository at this point in the history
Allow setting a single attribute, as per documentation
  • Loading branch information
zolaemil committed Aug 8, 2019
2 parents 0a6281c + 718a1dc commit e7fd869
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ Item.prototype.get = function (key) {
}
};

Item.prototype.set = function (params) {
this.attrs = _.merge({}, this.attrs, params);
Item.prototype.set = function (paramsOrKey, value) {
if (_.isString(paramsOrKey)) {
this.attrs[paramsOrKey] = value;
} else {
this.attrs = _.merge({}, this.attrs, paramsOrKey);
}

return this;
};
Expand Down
16 changes: 16 additions & 0 deletions test/item-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,20 @@ describe('item', () => {
});
});
});

describe('#set', () => {
it('should set attributes', () => {
const item = new Item({});
item.set({ num: 1, name: 'foo' });
expect(item.get('num')).to.equal(1);
expect(item.get('name')).to.equal('foo');
});

it('should set a single key when provided a string and value', () => {
const item = new Item({});
item.set('num', 123);
expect(item.get('num')).to.equal(123);
});
});
});

0 comments on commit e7fd869

Please sign in to comment.