Fixing the object diff so it only changes add and changed keys

This commit is contained in:
Chris Cowan 2014-08-11 12:10:22 -07:00
parent a7a139ce5a
commit d9b7b8ed28
2 changed files with 12 additions and 2 deletions

View file

@ -49,8 +49,10 @@ define(function (require) {
delete target[key];
});
// Assign the source to the target
_.assign(target, _.pick(source, sourceKeys));
// Assign the changed to the source to the target
_.assign(target, _.pick(source, diff.changed));
// Assign the added to the source to the target
_.assign(target, _.pick(source, diff.added));
return diff;

View file

@ -64,5 +64,13 @@ define(function (require) {
expect(results.changed).to.be.empty();
});
it('should only change keys that actually changed', function () {
var obj = { 'message': 'foo' };
var target = { obj: obj, message: 'foo' };
var source = { obj: _.cloneDeep(obj), message: 'test' };
var results = diff(target, source);
expect(target.obj).to.be(obj);
});
});
});