Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Item.set(string, value) and fix object merging bug #175

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = _.extend({}, this.attrs, paramsOrKey);
}

return this;
};
Expand Down
25 changes: 24 additions & 1 deletion test/item-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ describe('item', () => {
hashKey: 'num',
schema: {
num: Joi.number(),
name: Joi.string()
name: Joi.string(),
obj: Joi.object()
}
};

Expand Down Expand Up @@ -97,4 +98,26 @@ 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);
});


it('should not merge object attributes', () => {
const item = new Item({ obj: { a: 1 } });
item.set({ obj: { b: 2 } });
expect(item.get('obj')).to.eql({ b: 2 });
});
});
});