Skip to content

Commit

Permalink
Basic changeset application
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeneware committed Jun 10, 2013
1 parent db60fe1 commit 159750b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
9 changes: 7 additions & 2 deletions index.js
Expand Up @@ -49,8 +49,13 @@ function compare(path, old, new_) {
}

module.exports.apply = apply;
function apply(changes, a) {
var obj = JSON.parse(JSON.stringify(a));
function apply(changes, target) {
var obj;
try {
obj = JSON.parse(JSON.stringify(target));
} catch (err) {
obj = undefined;
}
changes.forEach(function (ch) {
var ptr, keys, len;
switch (ch.type) {
Expand Down
37 changes: 37 additions & 0 deletions test/index.js
Expand Up @@ -110,9 +110,46 @@ describe('changeset', function () {
};

var changes = diff(a, b);
var b_ = diff.apply(changes, a);
expect(b_).to.deep.equals(b);
done();
});

it('should be able to apply a changeset to a value', function (done) {
var a = 'Eugene';
var b = 'Susan';

var changes = diff(a, b);
var b_ = diff.apply(changes, a);
expect(b_).to.deep.equals(b);
done();
});

it('should be able to apply a changeset with nulls', function (done) {
var changes, b_;

changes = diff(null, 'Susan');
b_ = diff.apply(changes, null);
expect(b_).to.deep.equals('Susan');

changes = diff('Eugene', null);
b_ = diff.apply(changes, 'Eugene');
expect(b_).to.deep.equals(null);

done();
});

it('should be able to apply a changeset with undefined', function (done) {
var changes, b_;

changes = diff(undefined, 'Susan');
b_ = diff.apply(changes, undefined);
expect(b_).to.deep.equals('Susan');

changes = diff('Eugene', undefined);
b_ = diff.apply(changes, 'Eugene');
expect(b_).to.deep.equals(null);

done();
});
});

0 comments on commit 159750b

Please sign in to comment.