Comparing version 0.0.6 to 0.1.0
19
index.js
var _ = require('underscore'); | ||
// a = old, b = new | ||
module.exports = diff; | ||
@@ -9,2 +9,3 @@ function diff(old, new_) { | ||
comparing = []; | ||
return changes; | ||
@@ -21,5 +22,10 @@ } | ||
var comparing = []; | ||
function compare(path, old, new_) { | ||
var changes = []; | ||
if (old !== null && new_ !== null && typeof old === 'object') { | ||
if (old !== null && new_ !== null && | ||
typeof old === 'object' && | ||
!_.contains(comparing, old)) { | ||
comparing.push(old); | ||
var oldKeys = Object.keys(old); | ||
@@ -54,11 +60,8 @@ var newKeys = Object.keys(new_); | ||
function apply(changes, target, modify) { | ||
var obj; | ||
var obj, clone; | ||
if (modify) { | ||
obj = target; | ||
} else { | ||
try { | ||
obj = JSON.parse(JSON.stringify(target)); | ||
} catch (err) { | ||
obj = undefined; | ||
} | ||
clone = require("udc"); | ||
obj = clone(target); | ||
} | ||
@@ -65,0 +68,0 @@ changes.forEach(function (ch) { |
{ | ||
"name": "changeset", | ||
"version": "0.0.6", | ||
"version": "0.1.0", | ||
"description": "Library to diff JSON objects into atomic put and delete operations, and apply change sets to objects. Useful with Levelup/LevelDB object synchronization.", | ||
@@ -29,4 +29,5 @@ "main": "index.js", | ||
"dependencies": { | ||
"udc": "^1.0.0", | ||
"underscore": "~1.4.4" | ||
} | ||
} |
@@ -5,2 +5,4 @@ # changeset | ||
Handles circular references of Objects and Arrays. | ||
[![build status](https://secure.travis-ci.org/eugeneware/changeset.png)](http://travis-ci.org/eugeneware/changeset) | ||
@@ -94,3 +96,3 @@ | ||
``` | ||
``` js | ||
// apply the changes to a and modify a | ||
@@ -97,0 +99,0 @@ var b_ = diff.apply(changes, a, true); |
@@ -21,2 +21,5 @@ var expect = require('chai').expect | ||
a.self = a; | ||
a.scoresAgain = a.scores; | ||
var b = { | ||
@@ -33,2 +36,5 @@ name: 'Susan', | ||
b.friend = a; | ||
b.self = b; | ||
var changes = diff(a, b); | ||
@@ -42,3 +48,6 @@ expect(changes).to.deep.equal([ | ||
{ type: 'put', key: ['scores', 'zelda'], value: 3000 }, | ||
{ type: 'put', key: ['age'], value: 37 } | ||
{ type: 'put', key: ['self'], value: b }, | ||
{ type: 'del', key: ['scoresAgain'], }, | ||
{ type: 'put', key: ['age'], value: 37 }, | ||
{ type: 'put', key: ['friend'], value: a } | ||
]); | ||
@@ -104,2 +113,5 @@ | ||
a.self = a; | ||
a.scoresAgain = a.scores; | ||
var b = { | ||
@@ -116,5 +128,12 @@ name: 'Susan', | ||
b.friend = a; | ||
b.self = b; | ||
var clone = require("udc"); | ||
var bClone = clone(b); | ||
var changes = diff(a, b); | ||
var b_ = diff.apply(changes, a); | ||
expect(b_).to.deep.equals(b); | ||
expect(b).to.deep.equals(bClone); // Target did not change. | ||
done(); | ||
@@ -173,2 +192,5 @@ }); | ||
a.self = a; | ||
a.scoresAgain = a.scores; | ||
var b = { | ||
@@ -185,2 +207,5 @@ name: 'Susan', | ||
b.friend = a; | ||
b.self = b; | ||
var changes = diff(a, b); | ||
@@ -192,2 +217,11 @@ var b_ = diff.apply(changes, a, true); | ||
}); | ||
it('should be able to self-modify and replace an entire object', | ||
function(done) { | ||
var data = { name: 'Eugene', number: 43 }; | ||
var change = [ { type: 'put', key: [], value: 'xxx' } ]; | ||
var obj = diff.apply(change, data, true); | ||
expect(obj).to.equal('xxx'); | ||
done(); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12323
276
101
2
+ Addedudc@^1.0.0
+ Addedudc@1.0.1(transitive)