From da252c75970dcaad3b12fa8503cb270a3b5d7006 Mon Sep 17 00:00:00 2001 From: Patrick Niklaus Date: Wed, 16 Aug 2017 21:35:02 +0000 Subject: [PATCH] Add node binding integration --- include/nodejs/node_osrm_support.hpp | 33 ++++++++++++++++++++++++++++ test/nodejs/match.js | 15 +++++++++++++ test/nodejs/nearest.js | 14 ++++++++++++ test/nodejs/route.js | 14 ++++++++++++ test/nodejs/table.js | 15 +++++++++++++ test/nodejs/trip.js | 16 ++++++++++++++ 6 files changed, 107 insertions(+) diff --git a/include/nodejs/node_osrm_support.hpp b/include/nodejs/node_osrm_support.hpp index fc67f2087..26b83472c 100644 --- a/include/nodejs/node_osrm_support.hpp +++ b/include/nodejs/node_osrm_support.hpp @@ -585,6 +585,39 @@ inline bool argumentsToParameter(const Nan::FunctionCallbackInfo &arg params->generate_hints = generate_hints->BooleanValue(); } + if (obj->Has(Nan::New("exclude").ToLocalChecked())) + { + v8::Local exclude = obj->Get(Nan::New("exclude").ToLocalChecked()); + if (exclude.IsEmpty()) + return false; + + if (!exclude->IsArray()) + { + Nan::ThrowError("Exclude must be an array of strings or empty"); + return false; + } + + v8::Local exclude_array = v8::Local::Cast(exclude); + + for (uint32_t i = 0; i < exclude_array->Length(); ++i) + { + v8::Local class_name = exclude_array->Get(i); + if (class_name.IsEmpty()) + return false; + + if (class_name->IsString()) + { + std::string class_name_str = *v8::String::Utf8Value(class_name); + params->exclude.emplace_back(class_name_str); + } + else + { + Nan::ThrowError("Exclude must be an array of strings or empty"); + return false; + } + } + } + return true; } diff --git a/test/nodejs/match.js b/test/nodejs/match.js index 5b61ed86f..1dbbfa471 100644 --- a/test/nodejs/match.js +++ b/test/nodejs/match.js @@ -1,6 +1,7 @@ var OSRM = require('../../'); var test = require('tape'); var data_path = require('./constants').data_path; +var mld_data_path = require('./constants').mld_data_path; var three_test_coordinates = require('./constants').three_test_coordinates; var two_test_coordinates = require('./constants').two_test_coordinates; @@ -223,3 +224,17 @@ test('match: throws on invalid tidy param', function(assert) { assert.throws(function() { osrm.match(options, function(err, response) {}) }, /tidy must be of type Boolean/); }); + +test('match: match in Monaco without motorways', function(assert) { + assert.plan(3); + var osrm = new OSRM({path: mld_data_path, algorithm: 'MLD'}); + var options = { + coordinates: three_test_coordinates, + exclude: ['motorway'] + }; + osrm.match(options, function(err, response) { + assert.ifError(err); + assert.equal(response.tracepoints.length, 3); + assert.equal(response.matchings.length, 1); + }); +}); diff --git a/test/nodejs/nearest.js b/test/nodejs/nearest.js index 69835f90d..4a038dbe5 100644 --- a/test/nodejs/nearest.js +++ b/test/nodejs/nearest.js @@ -1,6 +1,7 @@ var OSRM = require('../../'); var test = require('tape'); var data_path = require('./constants').data_path; +var mld_data_path = require('./constants').mld_data_path; var three_test_coordinates = require('./constants').three_test_coordinates; var two_test_coordinates = require('./constants').two_test_coordinates; @@ -52,3 +53,16 @@ test('nearest: throws on invalid args', function(assert) { assert.throws(function() { osrm.nearest(options, function(err, res) {}); }, /Number must be an integer greater than or equal to 1/); }); + +test('nearest: nearest in Monaco without motorways', function(assert) { + assert.plan(2); + var osrm = new OSRM({path: mld_data_path, algorithm: 'MLD'}); + var options = { + coordinates: [two_test_coordinates[0]], + exclude: ['motorway'] + }; + osrm.nearest(options, function(err, response) { + assert.ifError(err); + assert.equal(response.waypoints.length, 1); + }); +}); diff --git a/test/nodejs/route.js b/test/nodejs/route.js index e638bde97..ae28f3762 100644 --- a/test/nodejs/route.js +++ b/test/nodejs/route.js @@ -576,3 +576,17 @@ test('route: in Monaco with custom limits on MLD', function(assert) { }); }); +test('route: route in Monaco without motorways', function(assert) { + assert.plan(3); + var osrm = new OSRM({path: monaco_mld_path, algorithm: 'MLD'}); + var options = { + coordinates: two_test_coordinates, + exclude: ['motorway'] + }; + osrm.route(options, function(err, response) { + assert.ifError(err); + assert.equal(response.waypoints.length, 2); + assert.equal(response.routes.length, 1); + }); +}); + diff --git a/test/nodejs/table.js b/test/nodejs/table.js index 753599843..972550614 100644 --- a/test/nodejs/table.js +++ b/test/nodejs/table.js @@ -1,6 +1,7 @@ var OSRM = require('../../'); var test = require('tape'); var data_path = require('./constants').data_path; +var mld_data_path = require('./constants').mld_data_path; var three_test_coordinates = require('./constants').three_test_coordinates; var two_test_coordinates = require('./constants').two_test_coordinates; @@ -166,3 +167,17 @@ test('table: distance table in Monaco without hints', function(assert) { table.destinations.map(assertHasNoHints); }); }); + +test('table: table in Monaco without motorways', function(assert) { + assert.plan(2); + var osrm = new OSRM({path: mld_data_path, algorithm: 'MLD'}); + var options = { + coordinates: two_test_coordinates, + exclude: ['motorway'] + }; + osrm.table(options, function(err, response) { + assert.ifError(err); + assert.equal(response.durations.length, 2); + }); +}); + diff --git a/test/nodejs/trip.js b/test/nodejs/trip.js index b506426d6..ff2294712 100644 --- a/test/nodejs/trip.js +++ b/test/nodejs/trip.js @@ -1,6 +1,7 @@ var OSRM = require('../../'); var test = require('tape'); var data_path = require('./constants').data_path; +var mld_data_path = require('./constants').mld_data_path; var three_test_coordinates = require('./constants').three_test_coordinates; var two_test_coordinates = require('./constants').two_test_coordinates; @@ -325,3 +326,18 @@ test('trip: fixed start and end combinations', function(assert) { assert.end(); }); + +test('trip: trip in Monaco without motorways', function(assert) { + assert.plan(3); + var osrm = new OSRM({path: mld_data_path, algorithm: 'MLD'}); + var options = { + coordinates: two_test_coordinates, + exclude: ['motorway'] + }; + osrm.trip(options, function(err, response) { + assert.ifError(err); + assert.equal(response.waypoints.length, 2); + assert.equal(response.trips.length, 1); + }); +}); +