From 20730e5f1a442e766416951cb88a73d49709e9a7 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 1 Feb 2013 17:24:16 -0500 Subject: [PATCH] Entity#mergeTags --- js/id/graph/entity.js | 14 ++++++++++++++ test/spec/graph/entity.js | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/js/id/graph/entity.js b/js/id/graph/entity.js index f91bb89d9..ae612c268 100644 --- a/js/id/graph/entity.js +++ b/js/id/graph/entity.js @@ -66,6 +66,20 @@ iD.Entity.prototype = { return iD.Entity(this, attrs, {_updated: true}); }, + mergeTags: function(tags) { + var merged = _.clone(this.tags); + for (var k in tags) { + var t1 = merged[k], + t2 = tags[k]; + if (t1 && t1 !== t2) { + merged[k] = t1 + "; " + t2; + } else { + merged[k] = t2; + } + } + return this.update({tags: merged}); + }, + created: function() { return this._updated && this.osmId().charAt(0) === '-'; }, diff --git a/test/spec/graph/entity.js b/test/spec/graph/entity.js index a573f4c8c..088ab5516 100644 --- a/test/spec/graph/entity.js +++ b/test/spec/graph/entity.js @@ -69,6 +69,33 @@ describe('iD.Entity', function () { }); }); + describe("#mergeTags", function () { + it("returns a new Entity", function () { + var a = iD.Entity(), + b = a.mergeTags({}); + expect(b instanceof iD.Entity).to.be.true; + expect(a).not.to.equal(b); + }); + + it("merges tags", function () { + var a = iD.Entity({tags: {a: 'a'}}), + b = a.mergeTags({b: 'b'}); + expect(b.tags).to.eql({a: 'a', b: 'b'}); + }); + + it("combines non-conflicting tags", function () { + var a = iD.Entity({tags: {a: 'a'}}), + b = a.mergeTags({a: 'a'}); + expect(b.tags).to.eql({a: 'a'}); + }); + + it("combines conflicting tags with semicolons", function () { + var a = iD.Entity({tags: {a: 'a'}}), + b = a.mergeTags({a: 'b'}); + expect(b.tags).to.eql({a: 'a; b'}); + }); + }); + describe("#osmId", function () { it("returns an OSM ID as a string", function () { expect(iD.Entity({id: 'w1234'}).osmId()).to.eql('1234');