From 2b46440429185936617ec0e1712600882c584b0b Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Mon, 29 Apr 2019 21:39:40 -0400 Subject: [PATCH] Add Relation#hasFromViaTo method and tests --- modules/osm/relation.js | 9 +++++ test/spec/osm/relation.js | 79 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/modules/osm/relation.js b/modules/osm/relation.js index a02f761d6..a01ee2342 100644 --- a/modules/osm/relation.js +++ b/modules/osm/relation.js @@ -256,6 +256,15 @@ Object.assign(osmRelation.prototype, { }, + hasFromViaTo: function() { + return ( + this.members.some(function(m) { return m.role === 'from'; }) && + this.members.some(function(m) { return m.role === 'via'; }) && + this.members.some(function(m) { return m.role === 'to'; }) + ); + }, + + isRestriction: function() { return !!(this.tags.type && this.tags.type.match(/^restriction:?/)); }, diff --git a/test/spec/osm/relation.js b/test/spec/osm/relation.js index 17fcc222c..e0810eef6 100644 --- a/test/spec/osm/relation.js +++ b/test/spec/osm/relation.js @@ -185,6 +185,85 @@ describe('iD.osmRelation', function () { }); }); + describe('#hasFromViaTo', function () { + it('returns true if there is a from, via, and to', function () { + var r = iD.osmRelation({ + id: 'r', + tags: { type: 'manoeuvre' }, + members: [ + { role: 'from', id: 'f', type: 'way' }, + { role: 'via', id: 'v', type: 'node' }, + { role: 'to', id: 't', type: 'way' } + ] + }); + expect(r.hasFromViaTo()).to.be.true; + }); + + it('returns true if there are extra froms, vias, tos', function () { + var r = iD.osmRelation({ + id: 'r', + tags: { type: 'manoeuvre' }, + members: [ + { role: 'from', id: 'f1', type: 'way' }, + { role: 'from', id: 'f2', type: 'way' }, + { role: 'via', id: 'v1', type: 'node' }, + { role: 'via', id: 'v2', type: 'node' }, + { role: 'to', id: 't1', type: 'way' }, + { role: 'to', id: 't2', type: 'way' } + ] + }); + expect(r.hasFromViaTo()).to.be.true; + }); + + it('returns false if from missing', function () { + var r = iD.osmRelation({ + id: 'r', + tags: { type: 'manoeuvre' }, + members: [ + { role: 'via', id: 'v', type: 'node' }, + { role: 'to', id: 't', type: 'way' } + ] + }); + expect(r.hasFromViaTo()).to.be.false; + }); + + it('returns false if via missing', function () { + var r = iD.osmRelation({ + id: 'r', + tags: { type: 'manoeuvre' }, + members: [ + { role: 'from', id: 'f', type: 'way' }, + { role: 'to', id: 't', type: 'way' } + ] + }); + expect(r.hasFromViaTo()).to.be.false; + }); + + it('returns false if to missing', function () { + var r = iD.osmRelation({ + id: 'r', + tags: { type: 'manoeuvre' }, + members: [ + { role: 'from', id: 'f', type: 'way' }, + { role: 'via', id: 'v', type: 'node' } + ] + }); + expect(r.hasFromViaTo()).to.be.false; + }); + + it('returns false if all missing', function () { + var r = iD.osmRelation({ + id: 'r', + tags: { type: 'multipolygon' }, + members: [ + { role: 'inner', id: 'i', type: 'way' }, + { role: 'outer', id: 'o', type: 'way' } + ] + }); + expect(r.hasFromViaTo()).to.be.false; + }); + }); + describe('#isRestriction', function () { it('returns true for \'restriction\' type', function () { expect(iD.osmRelation({tags: {type: 'restriction'}}).isRestriction()).to.be.true;