From bd713fccbc0b8f15b809bdb33363396379175aad Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Wed, 9 Nov 2022 12:21:32 +1100 Subject: [PATCH 1/2] implement waypoint.silent to set getDirections waypoints --- docs/services.md | 1 + services/__tests__/directions.test.js | 44 +++++++++++++++++++++++++++ services/directions.js | 44 ++++++++++++++++++--------- 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/docs/services.md b/docs/services.md index 148562aa..b4259d65 100644 --- a/docs/services.md +++ b/docs/services.md @@ -2183,6 +2183,7 @@ Type: [Object][200] and the second is the range of degrees the angle can deviate by. * `radius` **([number][205] | `"unlimited"`)?** Maximum distance in meters that the coordinate is allowed to move when snapped to a nearby road segment. * `waypointName` **[string][201]?** Custom name for the waypoint used for the arrival instruction in banners and voice instructions. +* `silent` **[boolean][202]?** Used to indicate if the waypoint should be used to split a route into distinct legs. The first and last waypoints can't be set as silent. ### MapMatchingPoint diff --git a/services/__tests__/directions.test.js b/services/__tests__/directions.test.js index 313b95b3..14e5b466 100644 --- a/services/__tests__/directions.test.js +++ b/services/__tests__/directions.test.js @@ -167,6 +167,33 @@ describe('getDirections', () => { }); }); + test('it works with optional waypoints.silent', () => { + directions.getDirections({ + waypoints: [ + { + coordinates: [2.2, 1.1] + }, + { + coordinates: [2.2, 1.1], + silent: true + }, + { + coordinates: [2.2, 1.1] + } + ] + }); + expect(tu.requestConfig(directions)).toEqual({ + path: '/directions/v5/mapbox/:profile/:coordinates', + method: 'GET', + params: { + coordinates: '2.2,1.1;2.2,1.1;2.2,1.1', + waypoints: '0;2', + profile: 'driving' + }, + query: {} + }); + }); + test('waypoints.radius can be any of string or number', () => { directions.getDirections({ waypoints: [ @@ -221,4 +248,21 @@ describe('getDirections', () => { }); }).toThrowError(/between 2 and 25/); }); + + test('errors if first or last waypoints are silent', () => { + expect(() => { + directions.getDirections({ + waypoints: [ + { + coordinates: [2.2, 1.1], + silent: true + }, + { + coordinates: [2.2, 1.1], + silent: true + } + ] + }); + }).toThrowError(/first and last waypoints cannot be silent/); + }); }); diff --git a/services/directions.js b/services/directions.js index 797f99ff..1389bec4 100644 --- a/services/directions.js +++ b/services/directions.js @@ -129,7 +129,8 @@ Directions.getDirections = function(config) { approach: [], bearing: [], radius: [], - waypointName: [] + waypointName: [], + waypoints: [] }; var waypointCount = config.waypoints.length; @@ -148,12 +149,24 @@ Directions.getDirections = function(config) { * and the second is the range of degrees the angle can deviate by. * @property {number|'unlimited'} [radius] - Maximum distance in meters that the coordinate is allowed to move when snapped to a nearby road segment. * @property {string} [waypointName] - Custom name for the waypoint used for the arrival instruction in banners and voice instructions. + * @property {boolean} [silent=false] - Used to indicate if the waypoint should be used to split a route into distinct legs. The first and last waypoints can't be set as silent. */ - config.waypoints.forEach(function(waypoint) { + config.waypoints.forEach(function(waypoint, index) { path.coordinates.push( waypoint.coordinates[0] + ',' + waypoint.coordinates[1] ); + if ( + (index === 0 && waypoint.silent) || + (index === config.waypoints.length - 1 && waypoint.silent) + ) { + throw new Error('first and last waypoints cannot be silent'); + } + + if (waypoint.silent) { + path.waypoints.push(index); + } + // join props which come in pairs ['bearing'].forEach(function(prop) { if (waypoint.hasOwnProperty(prop) && waypoint[prop] != null) { @@ -170,18 +183,20 @@ Directions.getDirections = function(config) { }); }); - ['approach', 'bearing', 'radius', 'waypointName'].forEach(function(prop) { - // avoid sending params which are all `;` - if ( - path[prop].every(function(char) { - return char === ''; - }) - ) { - delete path[prop]; - } else { - path[prop] = path[prop].join(';'); + ['approach', 'bearing', 'radius', 'waypointName', 'waypoints'].forEach( + function(prop) { + // avoid sending params which are all `;` + if ( + path[prop].every(function(char) { + return char === ''; + }) + ) { + delete path[prop]; + } else { + path[prop] = path[prop].join(';'); + } } - }); + ); var query = stringifyBooleans({ alternatives: config.alternatives, @@ -211,7 +226,8 @@ Directions.getDirections = function(config) { ev_max_ac_charging_power: config.ev_max_ac_charging_power, ev_min_charge_at_destination: config.ev_min_charge_at_destination, ev_min_charge_at_charging_station: config.ev_min_charge_at_charging_station, - auxiliary_consumption: config.auxiliary_consumption + auxiliary_consumption: config.auxiliary_consumption, + waypoints: path.waypoints }); return this.client.createRequest({ From 71c151d67b57e42409a36b8c893a1388d7906263 Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Wed, 9 Nov 2022 13:45:48 +1100 Subject: [PATCH 2/2] fix --- services/__tests__/directions.test.js | 5 +-- services/directions.js | 46 +++++++++++++++++---------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/services/__tests__/directions.test.js b/services/__tests__/directions.test.js index 14e5b466..cccb8637 100644 --- a/services/__tests__/directions.test.js +++ b/services/__tests__/directions.test.js @@ -187,10 +187,11 @@ describe('getDirections', () => { method: 'GET', params: { coordinates: '2.2,1.1;2.2,1.1;2.2,1.1', - waypoints: '0;2', profile: 'driving' }, - query: {} + query: { + waypoints: '0;2' + } }); }); diff --git a/services/directions.js b/services/directions.js index 1389bec4..7b313556 100644 --- a/services/directions.js +++ b/services/directions.js @@ -81,7 +81,8 @@ Directions.getDirections = function(config) { approach: v.oneOf('unrestricted', 'curb'), bearing: v.arrayOf(v.range([0, 360])), radius: v.oneOfType(v.number, v.equal('unlimited')), - waypointName: v.string + waypointName: v.string, + silent: v.boolean }) ) ), @@ -130,7 +131,8 @@ Directions.getDirections = function(config) { bearing: [], radius: [], waypointName: [], - waypoints: [] + waypoints: [], + silent: [] }; var waypointCount = config.waypoints.length; @@ -163,9 +165,12 @@ Directions.getDirections = function(config) { throw new Error('first and last waypoints cannot be silent'); } - if (waypoint.silent) { + if (!waypoint.silent) { path.waypoints.push(index); } + if (waypoint.hasOwnProperty('silent')) { + path.silent.push(waypoint.silent); + } // join props which come in pairs ['bearing'].forEach(function(prop) { @@ -183,20 +188,29 @@ Directions.getDirections = function(config) { }); }); - ['approach', 'bearing', 'radius', 'waypointName', 'waypoints'].forEach( - function(prop) { - // avoid sending params which are all `;` - if ( - path[prop].every(function(char) { - return char === ''; - }) - ) { - delete path[prop]; - } else { - path[prop] = path[prop].join(';'); - } + ['approach', 'bearing', 'radius', 'waypointName'].forEach(function(prop) { + // avoid sending params which are all `;` + if ( + path[prop].every(function(char) { + return char === ''; + }) + ) { + delete path[prop]; + } else { + path[prop] = path[prop].join(';'); } - ); + }); + + if ( + path.silent.every(function(v) { + return !v; + }) + ) { + delete path.waypoints; + } else { + path.waypoints = path.waypoints.join(';'); + } + delete path.silent; var query = stringifyBooleans({ alternatives: config.alternatives,