Skip to content

Commit

Permalink
Prevent duplicate consecutive nodes (#1296)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Apr 12, 2013
1 parent 5dda54e commit 94e2c8d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
11 changes: 9 additions & 2 deletions js/id/core/way.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,17 @@ _.extend(iD.Way.prototype, {
},

removeNode: function(id) {
var nodes = _.without(this.nodes, id);
var nodes = [];

for (var i = 0; i < this.nodes.length; i++) {
var node = this.nodes[i];
if (node != id && nodes[nodes.length - 1] != node) {
nodes.push(node);
}
}

// Preserve circularity
if (this.nodes.length > 1 && this.first() === id && this.last() === id) {
if (this.nodes.length > 1 && this.first() === id && this.last() === id && nodes[nodes.length - 1] != nodes[0]) {
nodes.push(nodes[0]);
}

Expand Down
19 changes: 19 additions & 0 deletions test/spec/core/way.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ describe('iD.Way', function() {
expect(w.removeNode('a').nodes).to.eql([]);
});

it("prevents duplicate consecutive nodes", function () {
var a = iD.Node({id: 'a'}),
b = iD.Node({id: 'b'}),
c = iD.Node({id: 'c'}),
w = iD.Way({nodes: ['a', 'b', 'c', 'b']});

expect(w.removeNode('c').nodes).to.eql(['a', 'b']);
});

it("preserves circularity", function () {
var a = iD.Node({id: 'a'}),
b = iD.Node({id: 'b'}),
Expand All @@ -230,6 +239,16 @@ describe('iD.Way', function() {

expect(w.removeNode('a').nodes).to.eql(['b', 'c', 'd', 'b']);
});

it("prevents duplicate consecutive nodes when preserving circularity", function () {
var a = iD.Node({id: 'a'}),
b = iD.Node({id: 'b'}),
c = iD.Node({id: 'c'}),
d = iD.Node({id: 'd'}),
w = iD.Way({nodes: ['a', 'b', 'c', 'd', 'b', 'a']});

expect(w.removeNode('a').nodes).to.eql(['b', 'c', 'd', 'b']);
});
});

describe("#asJXON", function () {
Expand Down

0 comments on commit 94e2c8d

Please sign in to comment.