From cc7328d1c941a0ff0eb42c93842b55688b2e9503 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Mon, 26 Nov 2012 21:48:40 -0400 Subject: [PATCH] Extract Entity#created and Entity#modified --- js/iD/actions/modes.js | 4 ++-- js/iD/graph/Entity.js | 10 +++++++++- js/iD/graph/Graph.js | 9 ++------- test/spec/Entity.js | 28 ++++++++++++++++++++++++++-- test/spec/Graph.js | 18 ++++++++++++++++++ 5 files changed, 57 insertions(+), 12 deletions(-) diff --git a/js/iD/actions/modes.js b/js/iD/actions/modes.js index 85580294e..b463402d9 100644 --- a/js/iD/actions/modes.js +++ b/js/iD/actions/modes.js @@ -64,7 +64,7 @@ iD.modes.AddRoad = { tags: { highway: 'residential' }, - modified: true, + _updated: true, id: iD.Util.id('way') }); }, @@ -184,7 +184,7 @@ iD.modes.AddArea = { tags: { building: 'yes' }, - modified: true, + _updated: true, id: iD.Util.id('way') }); }, diff --git a/js/iD/graph/Entity.js b/js/iD/graph/Entity.js index ff3208473..37a0f8a20 100644 --- a/js/iD/graph/Entity.js +++ b/js/iD/graph/Entity.js @@ -4,7 +4,7 @@ iD.Entity = function(a, b) { _.extend(this, {tags: {}}, a, b); if (b) { - this.modified = true; + this._updated = true; } if (iD.debug) { @@ -16,5 +16,13 @@ iD.Entity = function(a, b) { iD.Entity.prototype = { update: function(attrs) { return iD.Entity(this, attrs); + }, + + created: function() { + return this._updated && +this.id.slice(1) < 0; + }, + + modified: function() { + return this._updated && +this.id.slice(1) > 0; } }; diff --git a/js/iD/graph/Graph.js b/js/iD/graph/Graph.js index 1708218ec..1fcb69245 100644 --- a/js/iD/graph/Graph.js +++ b/js/iD/graph/Graph.js @@ -91,15 +91,10 @@ iD.Graph.prototype = { }, modifications: function() { - return _.filter(this.entities, function(entity) { - return ((+entity.id.slice(1) > 0)) && entity.modified; - }); + return _.filter(this.entities, function(entity) { return entity.modified(); }); }, creations: function() { - return _.filter(this.entities, function(entity) { - return ((+entity.id.slice(1)) < 0) && entity.modified; - }); + return _.filter(this.entities, function(entity) { return entity.created(); }); } - }; diff --git a/test/spec/Entity.js b/test/spec/Entity.js index 5b46ef564..62a022969 100644 --- a/test/spec/Entity.js +++ b/test/spec/Entity.js @@ -12,9 +12,33 @@ describe('Entity', function () { e = iD.Entity().update({tags: tags}); expect(e.tags).toBe(tags); }); + }); - it("returns a modified Entity", function () { - expect(iD.Entity().update({}).modified).toBeTruthy(); + describe("#created", function () { + it("returns false for an unmodified Entity", function () { + expect(iD.Entity().created()).toBeFalsy(); + }); + + it("returns false for a modified Entity with positive ID", function () { + expect(iD.Entity({id: 'w1234'}).update({}).created()).toBeFalsy(); + }); + + it("returns true for a modified Entity with negative ID", function () { + expect(iD.Entity({id: 'w-1234'}).update({}).created()).toBeTruthy(); + }); + }); + + describe("#modified", function () { + it("returns false for an unmodified Entity", function () { + expect(iD.Entity().modified()).toBeFalsy(); + }); + + it("returns true for a modified Entity with positive ID", function () { + expect(iD.Entity({id: 'w1234'}).update({}).modified()).toBeTruthy(); + }); + + it("returns false for a modified Entity with negative ID", function () { + expect(iD.Entity({id: 'w-1234'}).update({}).modified()).toBeFalsy(); }); }); }); diff --git a/test/spec/Graph.js b/test/spec/Graph.js index c17382ba7..fc5554321 100644 --- a/test/spec/Graph.js +++ b/test/spec/Graph.js @@ -53,4 +53,22 @@ describe('Graph', function() { expect(g2.entity('n-1').lat).toEqual(40); }); }); + + describe("#modifications", function () { + it("filters entities by modified", function () { + var a = {modified: function () { return true; }}, + b = {modified: function () { return false; }}, + graph = iD.Graph([a, b]); + expect(graph.modifications()).toEqual([a]); + }); + }); + + describe("#creations", function () { + it("filters entities by created", function () { + var a = {created: function () { return true; }}, + b = {created: function () { return false; }}, + graph = iD.Graph([a, b]); + expect(graph.creations()).toEqual([a]); + }); + }) });