From 64bcc9235f761bfd5867a5456fc54d0ffdc3b87d Mon Sep 17 00:00:00 2001 From: whytro Date: Thu, 16 Mar 2023 00:10:24 +0900 Subject: [PATCH] Require radiuses for bearings in nodejs bindings --- docs/nodejs/api.md | 3 ++- include/nodejs/node_osrm_support.hpp | 5 +++++ src/nodejs/node_osrm.cpp | 3 ++- test/nodejs/route.js | 15 ++++++++++++++- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/docs/nodejs/api.md b/docs/nodejs/api.md index c4d389023..992fc9689 100644 --- a/docs/nodejs/api.md +++ b/docs/nodejs/api.md @@ -111,7 +111,8 @@ var osrm = new OSRM('network.osrm'); var options = { coordinates: [[13.388860,52.517037]], number: 3, - bearings: [[0,20]] + bearings: [[0,20]], + radiuses: [null] }; osrm.nearest(options, function(err, response) { console.log(response.waypoints); // array of Waypoint objects diff --git a/include/nodejs/node_osrm_support.hpp b/include/nodejs/node_osrm_support.hpp index a1d7b4473..d6c60e8ec 100644 --- a/include/nodejs/node_osrm_support.hpp +++ b/include/nodejs/node_osrm_support.hpp @@ -522,6 +522,11 @@ inline bool argumentsToParameter(const Napi::CallbackInfo &args, if (bearings.IsEmpty()) return false; + if (!obj.Has("radiuses")) { + ThrowError(args.Env(), "Bearings must be accompanied with radiuses"); + return false; + } + if (!bearings.IsArray()) { ThrowError(args.Env(), "Bearings must be an array of arrays of numbers"); diff --git a/src/nodejs/node_osrm.cpp b/src/nodejs/node_osrm.cpp index 1537ab4d5..e866b0a52 100644 --- a/src/nodejs/node_osrm.cpp +++ b/src/nodejs/node_osrm.cpp @@ -349,7 +349,8 @@ Napi::Value Engine::route(const Napi::CallbackInfo &info) * var options = { * coordinates: [[13.388860,52.517037]], * number: 3, - * bearings: [[0,20]] + * bearings: [[0,20]], + * radiuses: [null] * }; * osrm.nearest(options, function(err, response) { * console.log(response.waypoints); // array of Waypoint objects diff --git a/test/nodejs/route.js b/test/nodejs/route.js index d0f1ddf49..6ddf95f51 100644 --- a/test/nodejs/route.js +++ b/test/nodejs/route.js @@ -424,6 +424,7 @@ test('route: integer bearing values no longer supported', function(assert) { var options = { coordinates: two_test_coordinates, bearings: [200, 250], + radiuses: [null], }; assert.throws(function() { osrm.route(options, function(err, route) {}); }, /Bearing must be an array of \[bearing, range\] or null/); @@ -435,6 +436,7 @@ test('route: valid bearing values', function(assert) { var options = { coordinates: two_test_coordinates, bearings: [[200, 180], [250, 180]], + radiuses: [null, null], }; osrm.route(options, function(err, route) { assert.ifError(err); @@ -448,38 +450,49 @@ test('route: valid bearing values', function(assert) { }); test('route: invalid bearing values', function(assert) { - assert.plan(6); + assert.plan(7); var osrm = new OSRM(monaco_path); assert.throws(function() { osrm.route({ coordinates: two_test_coordinates, bearings: [[400, 180], [-250, 180]], + radiuses: [null, null], }, function(err, route) {}) }, /Bearing values need to be in range 0..360, 0..180/); assert.throws(function() { osrm.route({ coordinates: two_test_coordinates, bearings: [[200], [250, 180]], + radiuses: [null, null], }, function(err, route) {}) }, /Bearing must be an array of/); assert.throws(function() { osrm.route({ coordinates: two_test_coordinates, bearings: [[400, 109], [100, 720]], + radiuses: [null, null], }, function(err, route) {}) }, /Bearing values need to be in range 0..360, 0..180/); assert.throws(function() { osrm.route({ coordinates: two_test_coordinates, bearings: 400, + radiuses: [null], }, function(err, route) {}) }, /Bearings must be an array of arrays of numbers/); assert.throws(function() { osrm.route({ coordinates: two_test_coordinates, bearings: [[100, 100]], + radiuses: [null, null], }, function(err, route) {}) }, /Bearings array must have the same length as coordinates array/); assert.throws(function() { osrm.route({ coordinates: two_test_coordinates, bearings: [Infinity, Infinity], + radiuses: [null, null], }, function(err, route) {}) }, /Bearing must be an array of \[bearing, range\] or null/); + assert.throws(function() { osrm.route({ + coordinates: two_test_coordinates, + bearings: [[0, 180], [0, 180]], + }, function(err, route) {}) }, + /Bearings must be accompanied with radiuses/); }); test('route: routes Monaco with hints', function(assert) {