Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement waypoint.silent to set getDirections waypoints #458

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 45 additions & 0 deletions services/__tests__/directions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,34 @@ 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',
profile: 'driving'
},
query: {
waypoints: '0;2'
}
});
});

test('waypoints.radius can be any of string or number', () => {
directions.getDirections({
waypoints: [
Expand Down Expand Up @@ -221,4 +249,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/);
});
});
38 changes: 34 additions & 4 deletions services/directions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
)
),
Expand Down Expand Up @@ -129,7 +130,9 @@ Directions.getDirections = function(config) {
approach: [],
bearing: [],
radius: [],
waypointName: []
waypointName: [],
waypoints: [],
silent: []
};

var waypointCount = config.waypoints.length;
Expand All @@ -148,12 +151,27 @@ 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);
}
if (waypoint.hasOwnProperty('silent')) {
path.silent.push(waypoint.silent);
}

// join props which come in pairs
['bearing'].forEach(function(prop) {
if (waypoint.hasOwnProperty(prop) && waypoint[prop] != null) {
Expand Down Expand Up @@ -183,6 +201,17 @@ Directions.getDirections = function(config) {
}
});

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,
annotations: config.annotations,
Expand Down Expand Up @@ -211,7 +240,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({
Expand Down