Entity#mergeTags

This commit is contained in:
John Firebaugh
2013-02-01 17:24:16 -05:00
parent 9120f33aa7
commit 20730e5f1a
2 changed files with 41 additions and 0 deletions
+14
View File
@@ -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) === '-';
},
+27
View File
@@ -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');