From ea4b93d88be2aeb1c7772659cca74ef0691923d0 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 7 Dec 2012 12:24:38 -0500 Subject: [PATCH] Graph#difference --- js/id/graph/graph.js | 18 ++++++++++++++++++ test/spec/graph/graph.js | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/js/id/graph/graph.js b/js/id/graph/graph.js index b05129aee..593124927 100644 --- a/js/id/graph/graph.js +++ b/js/id/graph/graph.js @@ -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) { diff --git a/test/spec/graph/graph.js b/test/spec/graph/graph.js index 1341996d8..a1407c09a 100644 --- a/test/spec/graph/graph.js +++ b/test/spec/graph/graph.js @@ -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}),