Graph#difference

This commit is contained in:
John Firebaugh
2012-12-07 12:24:38 -05:00
parent 255e3aaabd
commit ea4b93d88b
2 changed files with 37 additions and 0 deletions
+18
View File
@@ -81,6 +81,24 @@ iD.Graph.prototype = {
return iD.Entity(entity, {nodes: nodes});
},
difference: function (graph) {
var result = [];
_.each(this.entities, function(entity, id) {
if (entity !== graph.entities[id]) {
result.push(id);
}
});
_.each(graph.entities, function(entity, id) {
if (entity && !this.entities.hasOwnProperty(id)) {
result.push(id);
}
}, this);
return result.sort();
},
modified: function() {
var result = [];
_.each(this.entities, function(entity, id) {
+19
View File
@@ -93,6 +93,25 @@ describe('iD.Graph', function() {
});
});
describe("#difference", function () {
it("returns an Array of ids of changed entities", function () {
var initial = iD.Node({id: "n1"}),
updated = initial.update({}),
created = iD.Node(),
deleted = iD.Node({id: 'n2'}),
graph1 = iD.Graph([initial, deleted]),
graph2 = graph1.replace(updated).replace(created).remove(deleted);
expect(graph2.difference(graph1)).to.eql([created.id, updated.id, deleted.id]);
});
it("includes created entities that were subsequently deleted", function () {
var node = iD.Node(),
graph1 = iD.Graph([node]),
graph2 = graph1.remove(node);
expect(graph2.difference(graph1)).to.eql([node.id]);
});
});
describe("#modified", function () {
it("returns an Array of ids of modified entities", function () {
var node1 = iD.Node({id: 'n1', _updated: true}),