@furkot/directions 2.1.0 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/directions.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const travelMode = require('./model').travelMode;
2
2
  const { defaults: defaults, withTimeout } = require('./service/util');
3
+ const prepareQuery = require('./profile');
3
4
 
4
5
  module.exports = furkotDirections;
5
6
 
@@ -86,7 +87,7 @@ function furkotDirections(options) {
86
87
  */
87
88
  function directions(query, { signal } = {}) {
88
89
  if (query?.points?.length > 1) {
89
- return requestDirections(query, options.timeout);
90
+ return requestDirections(prepareQuery(query), options.timeout);
90
91
  }
91
92
 
92
93
  async function requestDirections(query, timeout) {
@@ -0,0 +1,15 @@
1
+ const profiles = {
2
+ 5: require('./rv')
3
+ };
4
+
5
+ module.exports = prepareQuery;
6
+
7
+ /**
8
+ * Prepares query depending on the mode.
9
+ * @param {Object} query - The query object.
10
+ * @returns {Object} The prepared query.
11
+ */
12
+ function prepareQuery(query) {
13
+ const { mode } = query;
14
+ return profiles[mode]?.(query) || query;
15
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Default RV dimensions based on Valhalla defaults for truck:
3
+ * https://valhalla.github.io/valhalla/api/turn-by-turn/api-reference/#automobile-and-bus-costing-options
4
+ */
5
+ const defaultRV = {
6
+ axle_load: 9.07,
7
+ hazmat: true,
8
+ height: 4.11,
9
+ length: 21.64,
10
+ weight: 21.77,
11
+ width: 2.6
12
+ };
13
+
14
+ const passengerCar = {
15
+ height: 3.8,
16
+ length: 8,
17
+ weight: 6,
18
+ width: 2.5
19
+ };
20
+
21
+ module.exports = {
22
+ defaultRV,
23
+ passengerCar
24
+ };
@@ -0,0 +1,72 @@
1
+ const { passengerCar, defaultRV } = require("./dimensions");
2
+ const passengerCarEntries = Object.entries(passengerCar);
3
+
4
+ module.exports = prepareQuery;
5
+
6
+ /**
7
+ * Treat RV as passenger car when it doesn't carry hazmat and all its dimensions
8
+ * that are present fall within the passenger car limits.
9
+ * @param {Object} vehicle - The vehicle object.
10
+ * @returns {boolean} Whether to treat RV as passenger car.
11
+ */
12
+ function isPassengerCar(vehicle) {
13
+ return !vehicle.hazmat && passengerCarEntries.every(([k, v]) => vehicle[k] <= v);
14
+ }
15
+
16
+ /**
17
+ * Treat RV as non commercial truck when it doesn't carry hazmat and its
18
+ * weight and length are below truck limits.
19
+ * @param {Object} vehicle - The vehicle object.
20
+ * @returns {boolean} Whether to treat RV as non commercial truck.
21
+ */
22
+ function isNonCommercialTruck(vehicle) {
23
+ return !vehicle.hazmat &&
24
+ vehicle.length < defaultRV.length &&
25
+ vehicle.weight < defaultRV.weight;
26
+ }
27
+
28
+ /**
29
+ * Prepares vehicle size for the query.
30
+ * @param {Object} query - The query object.
31
+ * @returns {Object} The prepared query with vehicle size.
32
+ */
33
+ function prepareVehicle(vehicle) {
34
+ if (!vehicle) {
35
+ // treat as a standard truck
36
+ return {
37
+ vehicle: defaultRV
38
+ };
39
+ }
40
+ if (isPassengerCar(vehicle)) {
41
+ // treat as a regular passenger car
42
+ return {
43
+ mode: 0
44
+ };
45
+ }
46
+ vehicle = Object.assign({}, defaultRV, vehicle);
47
+ let proposedMode;
48
+ if (isNonCommercialTruck(vehicle)) {
49
+ // treat as a passenger car if at least height and width can be enforced
50
+ proposedMode = 0;
51
+ }
52
+ return {
53
+ proposedMode,
54
+ vehicle
55
+ };
56
+ }
57
+
58
+ /**
59
+ * Prepares query.
60
+ * @param {Object} query - The query object.
61
+ * @returns {Object} The prepared query.
62
+ */
63
+ function prepareQuery(query) {
64
+ const {
65
+ mode: initialMode,
66
+ vehicle: initialVehicle
67
+ } = query;
68
+ return Object.assign({
69
+ initialMode,
70
+ initialVehicle
71
+ }, query, prepareVehicle(query.vehicle));
72
+ }
@@ -9,12 +9,11 @@ const util = require('../util');
9
9
  module.exports = init;
10
10
 
11
11
  const vehicle = {
12
- '-1': 'car',
13
- 0: 'car',
14
12
  1: 'bike',
15
13
  2: 'foot',
16
14
  5: 'truck'
17
15
  };
16
+ const defaultVehicle = 'car';
18
17
 
19
18
  const weighting = {
20
19
  true: 'curvature',
@@ -130,9 +129,14 @@ function getStatus(st, response) {
130
129
  }
131
130
  }
132
131
 
132
+ function vehicleType(query) {
133
+ const { mode, proposedMode } = query;
134
+ return vehicle[proposedMode ?? mode] || defaultVehicle;
135
+ }
136
+
133
137
  function vehicleSize(query, options) {
134
- const { mode, vehicle } = query;
135
- if (!(vehicle && mode === travelMode.rv)) {
138
+ const { initialMode, mode, vehicle } = query;
139
+ if (!(vehicle && (initialMode ?? mode) === travelMode.rv)) {
136
140
  return;
137
141
  }
138
142
  const { hazmat } = vehicle;
@@ -142,7 +146,7 @@ function vehicleSize(query, options) {
142
146
  multiply_by: '0.0'
143
147
  });
144
148
  }
145
- ['height', 'width', 'length', 'weight', 'axle_load'].forEach(p => {
149
+ ['height', 'width', 'length', 'weight'].forEach(p => {
146
150
  if (vehicle[p]) {
147
151
  options.push({
148
152
  if: `max_${p} < ${vehicle[p]}`,
@@ -165,7 +169,7 @@ function init(options) {
165
169
  return;
166
170
  }
167
171
  let req = {
168
- vehicle: vehicle[mode] || vehicle[0],
172
+ vehicle: vehicleType(query),
169
173
  points,
170
174
  details: ['road_environment', 'toll']
171
175
  };
@@ -205,6 +205,9 @@ function init(options) {
205
205
  restrictions
206
206
  }
207
207
  };
208
+ if (query.mode === travelMode.rv) {
209
+ req.options.vehicle_type = 'hgv';
210
+ }
208
211
  }
209
212
  const param = { query, req };
210
213
  if ((profile[query.mode] || profile[0]) === 'driving-car') {
@@ -166,9 +166,14 @@ function getStatus(err, response) {
166
166
  }
167
167
  }
168
168
 
169
+ function costingType(query) {
170
+ const { mode, proposedMode } = query;
171
+ return costing[proposedMode ?? mode] || defaultCosting;
172
+ }
173
+
169
174
  function vehicleSize(query, options) {
170
- const { mode, vehicle } = query;
171
- if (!(vehicle && mode === travelMode.rv)) {
175
+ const { initialMode, mode, vehicle } = query;
176
+ if (!(vehicle && (initialMode ?? mode) === travelMode.rv)) {
172
177
  return;
173
178
  }
174
179
  Object.assign(options, vehicle);
@@ -179,7 +184,7 @@ function init(options) {
179
184
  function prepareRequest(query) {
180
185
  let req = {
181
186
  locations: query.points.map(prepareWaypoint),
182
- costing: costing[query.mode] || defaultCosting,
187
+ costing: costingType(query),
183
188
  costing_options: {},
184
189
  directions_options: {
185
190
  units: units[query.units] || units.m
@@ -262,3 +267,4 @@ function init(options) {
262
267
  });
263
268
  return require('..')(options);
264
269
  }
270
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@furkot/directions",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "Directions service for Furkot",
5
5
  "author": {
6
6
  "name": "Damian Krzeminski",