diff --git a/js/id/graph/way.js b/js/id/graph/way.js index 305f12051..9efd4fe6c 100644 --- a/js/id/graph/way.js +++ b/js/id/graph/way.js @@ -14,12 +14,24 @@ iD.Way = iD.Entity.extend({ }); }, + first: function() { + return this.nodes[0]; + }, + + last: function() { + return this.nodes[this.nodes.length - 1]; + }, + + contains: function(node) { + return this.nodes.indexOf(node) >= 0; + }, + isOneWay: function() { return this.tags.oneway === 'yes'; }, isClosed: function() { - return this.nodes.length > 0 && this.nodes[this.nodes.length - 1] === this.nodes[0]; + return this.nodes.length > 0 && this.first() === this.last(); }, // a way is an area if: diff --git a/js/id/modes/draw_area.js b/js/id/modes/draw_area.js index cdb966351..196a4de53 100644 --- a/js/id/modes/draw_area.js +++ b/js/id/modes/draw_area.js @@ -13,7 +13,7 @@ iD.modes.DrawArea = function(wayId) { headId = (way.nodes.length == 1) ? way.nodes[0] : way.nodes[way.nodes.length - 2], - tailId = _.first(way.nodes), + tailId = way.first(), node = iD.Node({loc: map.mouseCoordinates()}); map.dblclickEnable(false) diff --git a/js/id/modes/draw_line.js b/js/id/modes/draw_line.js index 68130d7b8..72a85ae41 100644 --- a/js/id/modes/draw_line.js +++ b/js/id/modes/draw_line.js @@ -12,8 +12,8 @@ iD.modes.DrawLine = function(wayId, direction) { way = history.graph().entity(wayId), node = iD.Node({loc: map.mouseCoordinates()}), index = (direction === 'forward') ? undefined : 0, - headId = (direction === 'forward') ? _.last(way.nodes) : _.first(way.nodes), - tailId = (direction === 'forward') ? _.first(way.nodes) : _.last(way.nodes); + headId = (direction === 'forward') ? way.last() : way.first(), + tailId = (direction === 'forward') ? way.first() : way.last(); iD.behavior.Hover()(surface); diff --git a/test/spec/graph/way.js b/test/spec/graph/way.js index c3773f6b0..37df6b648 100644 --- a/test/spec/graph/way.js +++ b/test/spec/graph/way.js @@ -35,6 +35,28 @@ describe('iD.Way', function() { expect(iD.Way({tags: {foo: 'bar'}}).tags).to.eql({foo: 'bar'}); }); + describe("#first", function () { + it("returns the first node", function () { + expect(iD.Way({nodes: ['a', 'b', 'c']}).first()).to.equal('a'); + }); + }); + + describe("#last", function () { + it("returns the last node", function () { + expect(iD.Way({nodes: ['a', 'b', 'c']}).last()).to.equal('c'); + }); + }); + + describe("#contains", function () { + it("returns true if the way contains the given node", function () { + expect(iD.Way({nodes: ['a', 'b', 'c']}).contains('b')).to.be.true; + }); + + it("returns false if the way does not contain the given node", function () { + expect(iD.Way({nodes: ['a', 'b', 'c']}).contains('d')).to.be.false; + }); + }); + describe("#extent", function () { it("returns the minimal extent containing all member nodes", function () { var node1 = iD.Node({loc: [0, 0]}),