@opentripplanner/core-utils 12.0.2 → 13.0.0-alpha.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.
Files changed (73) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/esm/graphql.d.js.map +1 -1
  4. package/esm/index.js.map +1 -1
  5. package/esm/itinerary.js +78 -114
  6. package/esm/itinerary.js.map +1 -1
  7. package/esm/map.js +3 -3
  8. package/esm/map.js.map +1 -1
  9. package/esm/otpSchema.json +12 -0
  10. package/esm/profile.js +18 -19
  11. package/esm/profile.js.map +1 -1
  12. package/esm/query-gen.js +35 -45
  13. package/esm/query-gen.js.map +1 -1
  14. package/esm/query.js +139 -191
  15. package/esm/query.js.map +1 -1
  16. package/esm/route.js +74 -97
  17. package/esm/route.js.map +1 -1
  18. package/esm/storage.js +10 -7
  19. package/esm/storage.js.map +1 -1
  20. package/esm/suspense.js +1 -3
  21. package/esm/suspense.js.map +1 -1
  22. package/esm/time.js +13 -17
  23. package/esm/time.js.map +1 -1
  24. package/esm/ui.js +0 -5
  25. package/esm/ui.js.map +1 -1
  26. package/lib/graphql.d.js.map +1 -1
  27. package/lib/index.js +3 -19
  28. package/lib/index.js.map +1 -1
  29. package/lib/itinerary.d.ts.map +1 -1
  30. package/lib/itinerary.js +151 -247
  31. package/lib/itinerary.js.map +1 -1
  32. package/lib/map.d.ts +1 -1
  33. package/lib/map.d.ts.map +1 -1
  34. package/lib/map.js +9 -20
  35. package/lib/map.js.map +1 -1
  36. package/lib/otpSchema.json +12 -0
  37. package/lib/profile.js +19 -21
  38. package/lib/profile.js.map +1 -1
  39. package/lib/query-gen.d.ts +4 -4
  40. package/lib/query-gen.d.ts.map +1 -1
  41. package/lib/query-gen.js +35 -47
  42. package/lib/query-gen.js.map +1 -1
  43. package/lib/query-params.jsx +893 -0
  44. package/lib/query.js +72 -122
  45. package/lib/query.js.map +1 -1
  46. package/lib/route.js +73 -101
  47. package/lib/route.js.map +1 -1
  48. package/lib/storage.d.ts.map +1 -1
  49. package/lib/storage.js +12 -13
  50. package/lib/storage.js.map +1 -1
  51. package/lib/suspense.d.ts +1 -1
  52. package/lib/suspense.d.ts.map +1 -1
  53. package/lib/suspense.js +3 -9
  54. package/lib/suspense.js.map +1 -1
  55. package/lib/time.js +21 -44
  56. package/lib/time.js.map +1 -1
  57. package/lib/ui.js +1 -8
  58. package/lib/ui.js.map +1 -1
  59. package/package.json +7 -8
  60. package/src/core-utils.story.tsx +1 -1
  61. package/src/itinerary.ts +2 -0
  62. package/src/otpSchema.json +12 -0
  63. package/src/profile.js +1 -1
  64. package/src/query-gen.ts +1 -1
  65. package/src/query-params.jsx +893 -0
  66. package/src/query.js +4 -1
  67. package/src/storage.ts +9 -5
  68. package/tsconfig.tsbuildinfo +1 -1
  69. package/esm/query-params.js +0 -786
  70. package/esm/query-params.js.map +0 -1
  71. package/lib/query-params.js +0 -756
  72. package/lib/query-params.js.map +0 -1
  73. /package/{src/query-params.js → esm/query-params.jsx} +0 -0
package/esm/itinerary.js CHANGED
@@ -1,15 +1,14 @@
1
1
  import _typeof from "@babel/runtime/helpers/typeof";
2
2
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
3
  import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
4
-
5
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
6
-
7
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
8
-
4
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
5
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
9
6
  import polyline from "@mapbox/polyline";
10
- import turfAlong from "@turf/along"; // All OTP transit modes
7
+ import turfAlong from "@turf/along";
11
8
 
9
+ // All OTP transit modes
12
10
  export var transitModes = ["TRAM", "TROLLEYBUS", "BUS", "SUBWAY", "FERRY", "RAIL", "GONDOLA"];
11
+
13
12
  /**
14
13
  * @param {config} config OTP-RR configuration object
15
14
  * @return {Array} List of all transit modes defined in config; otherwise default mode list
@@ -27,12 +26,12 @@ export function isTransitLeg(leg) {
27
26
  export function isTransit(mode) {
28
27
  return transitModes.includes(mode) || mode === "TRANSIT";
29
28
  }
29
+
30
30
  /**
31
31
  * Returns true if the leg pickup rules enabled which require
32
32
  * calling ahead for the service to run. "mustPhone" is the only
33
33
  * property which encodes this info.
34
34
  */
35
-
36
35
  export function isReservationRequired(leg) {
37
36
  return (leg === null || leg === void 0 ? void 0 : leg.boardRule) === "mustPhone" || (leg === null || leg === void 0 ? void 0 : leg.alightRule) === "mustPhone";
38
37
  }
@@ -41,14 +40,12 @@ export function isReservationRequired(leg) {
41
40
  * or if the user must flag the driver down for pickup.
42
41
  * "coordinateWithDriver" in board/alight rule encodes this info.
43
42
  */
44
-
45
43
  export function isCoordinationRequired(leg) {
46
44
  return (leg === null || leg === void 0 ? void 0 : leg.boardRule) === "coordinateWithDriver" || (leg === null || leg === void 0 ? void 0 : leg.alightRule) === "coordinateWithDriver";
47
45
  }
48
46
  export function containsGeometry(place) {
49
47
  var _place$stop, _place$stop2;
50
-
51
- return (place === null || place === void 0 ? void 0 : (_place$stop = place.stop) === null || _place$stop === void 0 ? void 0 : _place$stop.geometries) !== null && (place === null || place === void 0 ? void 0 : (_place$stop2 = place.stop) === null || _place$stop2 === void 0 ? void 0 : _place$stop2.geometries) !== undefined;
48
+ return (place === null || place === void 0 || (_place$stop = place.stop) === null || _place$stop === void 0 ? void 0 : _place$stop.geometries) !== null && (place === null || place === void 0 || (_place$stop2 = place.stop) === null || _place$stop2 === void 0 ? void 0 : _place$stop2.geometries) !== undefined;
52
49
  }
53
50
  export function endsWithGeometry(leg) {
54
51
  return containsGeometry(leg === null || leg === void 0 ? void 0 : leg.to);
@@ -61,24 +58,24 @@ export function legContainsGeometry(leg) {
61
58
  }
62
59
  export function isAdvanceBookingRequired(info) {
63
60
  var _info$latestBookingTi;
64
-
65
- return (info === null || info === void 0 ? void 0 : (_info$latestBookingTi = info.latestBookingTime) === null || _info$latestBookingTi === void 0 ? void 0 : _info$latestBookingTi.daysPrior) > 0;
61
+ return (info === null || info === void 0 || (_info$latestBookingTi = info.latestBookingTime) === null || _info$latestBookingTi === void 0 ? void 0 : _info$latestBookingTi.daysPrior) > 0;
66
62
  }
67
63
  export function legDropoffRequiresAdvanceBooking(leg) {
68
64
  return isAdvanceBookingRequired(leg === null || leg === void 0 ? void 0 : leg.dropOffBookingInfo);
69
65
  }
66
+
70
67
  /**
71
68
  * The two rules checked by the above two functions are the only values
72
69
  * returned by OTP when a leg is a flex leg.
73
70
  */
74
-
75
71
  export function isFlex(leg) {
76
72
  return isReservationRequired(leg) || isCoordinationRequired(leg) || legDropoffRequiresAdvanceBooking(leg) || isAdvanceBookingRequired(leg === null || leg === void 0 ? void 0 : leg.pickupBookingInfo) || legContainsGeometry(leg);
77
73
  }
78
- export function isRideshareLeg(leg) {
79
- var _leg$rideHailingEstim, _leg$rideHailingEstim2;
80
74
 
81
- return !!((_leg$rideHailingEstim = leg.rideHailingEstimate) !== null && _leg$rideHailingEstim !== void 0 && (_leg$rideHailingEstim2 = _leg$rideHailingEstim.provider) !== null && _leg$rideHailingEstim2 !== void 0 && _leg$rideHailingEstim2.id);
75
+ // alpha-only comment
76
+ export function isRideshareLeg(leg) {
77
+ var _leg$rideHailingEstim;
78
+ return !!((_leg$rideHailingEstim = leg.rideHailingEstimate) !== null && _leg$rideHailingEstim !== void 0 && (_leg$rideHailingEstim = _leg$rideHailingEstim.provider) !== null && _leg$rideHailingEstim !== void 0 && _leg$rideHailingEstim.id);
82
79
  }
83
80
  export function isWalk(mode) {
84
81
  if (!mode) return false;
@@ -103,61 +100,61 @@ export function isMicromobility(mode) {
103
100
  export function isAccessMode(mode) {
104
101
  return isWalk(mode) || isBicycle(mode) || isBicycleRent(mode) || isCar(mode) || isMicromobility(mode);
105
102
  }
103
+
106
104
  /**
107
105
  * @param {string} modesStr a comma-separated list of OTP modes
108
106
  * @return {boolean} whether any of the modes are transit modes
109
107
  */
110
-
111
108
  export function hasTransit(modesStr) {
112
109
  return modesStr.split(",").some(function (mode) {
113
110
  return isTransit(mode);
114
111
  });
115
112
  }
113
+
116
114
  /**
117
115
  * @param {string} modesStr a comma-separated list of OTP modes
118
116
  * @return {boolean} whether any of the modes are car-based modes
119
117
  */
120
-
121
118
  export function hasCar(modesStr) {
122
119
  return modesStr.split(",").some(function (mode) {
123
120
  return isCar(mode);
124
121
  });
125
122
  }
123
+
126
124
  /**
127
125
  * @param {string} modesStr a comma-separated list of OTP modes
128
126
  * @return {boolean} whether any of the modes are bicycle-based modes
129
127
  */
130
-
131
128
  export function hasBike(modesStr) {
132
129
  return modesStr.split(",").some(function (mode) {
133
130
  return isBicycle(mode) || isBicycleRent(mode);
134
131
  });
135
132
  }
133
+
136
134
  /**
137
135
  * @param {string} modesStr a comma-separated list of OTP modes
138
136
  * @return {boolean} whether any of the modes are micromobility-based modes
139
137
  */
140
-
141
138
  export function hasMicromobility(modesStr) {
142
139
  return modesStr.split(",").some(function (mode) {
143
140
  return isMicromobility(mode);
144
141
  });
145
142
  }
143
+
146
144
  /**
147
145
  * @param {string} modesStr a comma-separated list of OTP modes
148
146
  * @return {boolean} whether any of the modes is a hailing mode
149
147
  */
150
-
151
148
  export function hasHail(modesStr) {
152
149
  return modesStr.split(",").some(function (mode) {
153
150
  return mode.indexOf("_HAIL") > -1;
154
151
  });
155
152
  }
153
+
156
154
  /**
157
155
  * @param {string} modesStr a comma-separated list of OTP modes
158
156
  * @return {boolean} whether any of the modes is a rental mode
159
157
  */
160
-
161
158
  export function hasRental(modesStr) {
162
159
  return modesStr.split(",").some(function (mode) {
163
160
  return mode.indexOf("_RENT") > -1;
@@ -181,43 +178,36 @@ export function toSentenceCase(str) {
181
178
  if (str == null) {
182
179
  return "";
183
180
  }
184
-
185
181
  str = String(str);
186
182
  return str.charAt(0).toUpperCase() + str.substr(1).toLowerCase();
187
183
  }
184
+
188
185
  /**
189
186
  * Derive the company string based on mode and network associated with leg.
190
187
  */
191
-
192
188
  export function getCompanyFromLeg(leg) {
193
189
  if (!leg) return null;
194
190
  var from = leg.from,
195
- mode = leg.mode,
196
- rentedBike = leg.rentedBike,
197
- rentedCar = leg.rentedCar,
198
- rentedVehicle = leg.rentedVehicle,
199
- rideHailingEstimate = leg.rideHailingEstimate;
200
-
191
+ mode = leg.mode,
192
+ rentedBike = leg.rentedBike,
193
+ rentedCar = leg.rentedCar,
194
+ rentedVehicle = leg.rentedVehicle,
195
+ rideHailingEstimate = leg.rideHailingEstimate;
201
196
  if (mode === "CAR" && rentedCar) {
202
197
  return from.networks[0];
203
198
  }
204
-
205
199
  if (mode === "CAR" && rideHailingEstimate) {
206
200
  return rideHailingEstimate.provider.id;
207
201
  }
208
-
209
202
  if (mode === "BICYCLE" && rentedBike && from.networks) {
210
203
  return from.networks[0];
211
204
  }
212
-
213
205
  if (from.rentalVehicle) {
214
206
  return from.rentalVehicle.network;
215
207
  }
216
-
217
208
  if ((mode === "MICROMOBILITY" || mode === "SCOOTER") && rentedVehicle && from.networks) {
218
209
  return from.networks[0];
219
210
  }
220
-
221
211
  return null;
222
212
  }
223
213
  export function getItineraryBounds(itinerary) {
@@ -230,57 +220,55 @@ export function getItineraryBounds(itinerary) {
230
220
  });
231
221
  return coords;
232
222
  }
223
+
233
224
  /**
234
225
  * Return a coords object that encloses the given leg's geometry.
235
226
  */
236
-
237
227
  export function getLegBounds(leg) {
238
228
  var coords = polyline.toGeoJSON(leg.legGeometry.points).coordinates.map(function (c) {
239
229
  return [c[1], c[0]];
240
- }); // in certain cases, there might be zero-length coordinates in the leg
230
+ });
231
+
232
+ // in certain cases, there might be zero-length coordinates in the leg
241
233
  // geometry. In these cases, build us an array of coordinates using the from
242
234
  // and to data of the leg.
243
-
244
235
  if (coords.length === 0) {
245
236
  coords.push([leg.from.lat, leg.from.lon], [leg.to.lat, leg.to.lon]);
246
237
  }
247
-
248
238
  return coords;
249
239
  }
240
+
250
241
  /* Returns an interpolated lat-lon at a specified distance along a leg */
251
242
 
252
243
  export function legLocationAtDistance(leg, distance) {
253
244
  if (!leg.legGeometry) return null;
254
-
255
245
  try {
256
246
  var line = polyline.toGeoJSON(leg.legGeometry.points);
257
247
  var pt = turfAlong(line, distance, {
258
248
  units: "meters"
259
249
  });
260
-
261
250
  if (pt && pt.geometry && pt.geometry.coordinates) {
262
251
  return [pt.geometry.coordinates[1], pt.geometry.coordinates[0]];
263
252
  }
264
- } catch (e) {// FIXME handle error!
253
+ } catch (e) {
254
+ // FIXME handle error!
265
255
  }
266
-
267
256
  return null;
268
257
  }
258
+
269
259
  /* Returns an interpolated elevation at a specified distance along a leg */
270
260
 
271
261
  export function legElevationAtDistance(points, distance) {
272
262
  // Iterate through the combined elevation profile
273
- var traversed = 0; // If first point distance is not zero, insert starting point at zero with
263
+ var traversed = 0;
264
+ // If first point distance is not zero, insert starting point at zero with
274
265
  // null elevation. Encountering this value should trigger the warning below.
275
-
276
266
  if (points[0][0] > 0) {
277
267
  points.unshift([0, null]);
278
268
  }
279
-
280
269
  for (var i = 1; i < points.length; i++) {
281
270
  var start = points[i - 1];
282
271
  var elevDistanceSpan = points[i][0] - start[0];
283
-
284
272
  if (distance >= traversed && distance <= traversed + elevDistanceSpan) {
285
273
  // Distance falls within this point and the previous one;
286
274
  // compute & return interpolated elevation value
@@ -288,15 +276,12 @@ export function legElevationAtDistance(points, distance) {
288
276
  console.warn("Elevation value does not exist for distance.", distance, traversed);
289
277
  return null;
290
278
  }
291
-
292
279
  var pct = (distance - traversed) / elevDistanceSpan;
293
280
  var elevSpan = points[i][1] - start[1];
294
281
  return start[1] + elevSpan * pct;
295
282
  }
296
-
297
283
  traversed += elevDistanceSpan;
298
284
  }
299
-
300
285
  console.warn("Elevation value does not exist for distance.", distance, traversed);
301
286
  return null;
302
287
  }
@@ -305,9 +290,10 @@ export function mapOldElevationComponentToNew(oldElev) {
305
290
  distance: oldElev.first,
306
291
  elevation: oldElev.second
307
292
  };
308
- } // Iterate through the steps, building the array of elevation points and
309
- // keeping track of the minimum and maximum elevations reached
293
+ }
310
294
 
295
+ // Iterate through the steps, building the array of elevation points and
296
+ // keeping track of the minimum and maximum elevations reached
311
297
  export function getElevationProfile(steps) {
312
298
  var unitConversion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
313
299
  var minElev = 100000;
@@ -319,38 +305,32 @@ export function getElevationProfile(steps) {
319
305
  var points = [];
320
306
  steps.forEach(function (step) {
321
307
  var _step$elevation;
322
-
323
308
  // Support for old REST response data (in step.elevation)
324
309
  var stepElevationProfile = step.elevationProfile || Array.isArray(step.elevation) && ((_step$elevation = step.elevation) === null || _step$elevation === void 0 ? void 0 : _step$elevation.map(mapOldElevationComponentToNew));
325
-
326
310
  if (!stepElevationProfile || stepElevationProfile.length === 0) {
327
311
  traversed += step.distance;
328
312
  return;
329
313
  }
330
-
331
314
  for (var i = 0; i < stepElevationProfile.length; i++) {
332
315
  var elev = stepElevationProfile[i];
333
-
334
316
  if (previous) {
335
317
  var diff = (elev.elevation - previous.elevation) * unitConversion;
336
318
  if (diff > 0) gain += diff;else loss += diff;
337
319
  }
338
-
339
- if (i === 0 && elev.distance !== 0) {// console.warn(`No elevation data available for step ${stepIndex}-${i} at beginning of segment`, elev)
320
+ if (i === 0 && elev.distance !== 0) {
321
+ // console.warn(`No elevation data available for step ${stepIndex}-${i} at beginning of segment`, elev)
340
322
  }
341
-
342
323
  var convertedElevation = elev.elevation * unitConversion;
343
324
  if (convertedElevation < minElev) minElev = convertedElevation;
344
325
  if (convertedElevation > maxElev) maxElev = convertedElevation;
345
- points.push([traversed + elev.distance, elev.elevation]); // Insert "filler" point if the last point in elevation profile does not
326
+ points.push([traversed + elev.distance, elev.elevation]);
327
+ // Insert "filler" point if the last point in elevation profile does not
346
328
  // reach the full distance of the step.
347
-
348
- if (i === stepElevationProfile.length - 1 && elev.distance !== step.distance) {// points.push([traversed + step.distance, elev.second])
329
+ if (i === stepElevationProfile.length - 1 && elev.distance !== step.distance) {
330
+ // points.push([traversed + step.distance, elev.second])
349
331
  }
350
-
351
332
  previous = elev;
352
333
  }
353
-
354
334
  traversed += step.distance;
355
335
  });
356
336
  return {
@@ -362,6 +342,7 @@ export function getElevationProfile(steps) {
362
342
  loss: loss
363
343
  };
364
344
  }
345
+
365
346
  /**
366
347
  * Uses canvas.measureText to compute and return the width of the given text of given font in pixels.
367
348
  *
@@ -370,9 +351,8 @@ export function getElevationProfile(steps) {
370
351
  *
371
352
  * @see https://stackoverflow.com/questions/118241/calculate-text-width-with-javascript/21015393#21015393
372
353
  */
373
-
374
354
  export function getTextWidth(text) {
375
- var font = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "22px Arial";
355
+ var font = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "22px Arial"; // Create custom type for function including reused canvas object
376
356
  // reuse canvas object for better performance
377
357
  var canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas"));
378
358
  var context = canvas.getContext("2d");
@@ -380,23 +360,22 @@ export function getTextWidth(text) {
380
360
  var metrics = context.measureText(text);
381
361
  return metrics.width;
382
362
  }
363
+
383
364
  /**
384
365
  * Get the configured company object for the given network string if the company
385
366
  * has been defined in the provided companies array config.
386
367
  */
387
-
388
368
  export function getCompanyForNetwork(networkString) {
389
369
  var companies = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
390
370
  var company = companies.find(function (co) {
391
371
  return co.id === networkString;
392
372
  });
393
-
394
373
  if (!company) {
395
374
  console.warn("No company found in config.yml that matches rented vehicle network: ".concat(networkString), companies);
396
375
  }
397
-
398
376
  return company;
399
377
  }
378
+
400
379
  /**
401
380
  * Get a string label to display from a list of vehicle rental networks.
402
381
  *
@@ -404,7 +383,6 @@ export function getCompanyForNetwork(networkString) {
404
383
  * @param {Array<object>} [companies=[]] An optional list of the companies config.
405
384
  * @return {string} A label for use in presentation on a website.
406
385
  */
407
-
408
386
  export function getCompaniesLabelFromNetworks(networks) {
409
387
  var companies = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
410
388
  return (Array.isArray(networks) ? networks : [networks]).map(function (network) {
@@ -433,21 +411,21 @@ export function calculatePhysicalActivity(itinerary) {
433
411
  walkDuration: walkDuration
434
412
  };
435
413
  }
414
+
436
415
  /**
437
416
  * For an itinerary, calculates the TNC fares and returns an object with
438
417
  * these values and currency info.
439
418
  * It is assumed that the same currency is used for all TNC legs.
440
419
  */
441
-
442
420
  export function calculateTncFares(itinerary) {
443
421
  return itinerary.legs.filter(function (leg) {
444
422
  return leg.mode === "CAR" && leg.rideHailingEstimate;
445
423
  }).reduce(function (_ref, _ref2) {
446
424
  var maxTNCFare = _ref.maxTNCFare,
447
- minTNCFare = _ref.minTNCFare;
425
+ minTNCFare = _ref.minTNCFare;
448
426
  var rideHailingEstimate = _ref2.rideHailingEstimate;
449
427
  var minPrice = rideHailingEstimate.minPrice,
450
- maxPrice = rideHailingEstimate.maxPrice;
428
+ maxPrice = rideHailingEstimate.maxPrice;
451
429
  return {
452
430
  // Assumes a single currency for entire itinerary.
453
431
  currencyCode: minPrice.currency.code,
@@ -460,6 +438,7 @@ export function calculateTncFares(itinerary) {
460
438
  minTNCFare: 0
461
439
  });
462
440
  }
441
+
463
442
  /**
464
443
  * Sources:
465
444
  * - https://www.itf-oecd.org/sites/default/files/docs/environmental-performance-new-mobility.pdf
@@ -467,7 +446,6 @@ export function calculateTncFares(itinerary) {
467
446
  * - https://www.itf-oecd.org/sites/default/files/life-cycle-assessment-calculations-2020.xlsx
468
447
  * Other values extrapolated.
469
448
  */
470
-
471
449
  var CARBON_INTENSITY_DEFAULTS = {
472
450
  walk: 0.026,
473
451
  bicycle: 0.017,
@@ -486,56 +464,49 @@ var CARBON_INTENSITY_DEFAULTS = {
486
464
  airplane: 0.382,
487
465
  micromobility: 0.095
488
466
  };
467
+
489
468
  /**
490
469
  * @param {itinerary} itinerary OTP trip itinierary, only legs is required.
491
470
  * @param {carbonIntensity} carbonIntensity carbon intensity by mode in grams/meter
492
471
  * @param {units} units units to be used in return value
493
472
  * @return Amount of carbon in chosen unit
494
473
  */
495
-
496
- export function calculateEmissions( // This type makes all the properties from Itinerary optional except legs.
474
+ export function calculateEmissions(
475
+ // This type makes all the properties from Itinerary optional except legs.
497
476
  itinerary) {
498
477
  var _itinerary$legs;
499
-
500
478
  var carbonIntensity = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
501
479
  var units = arguments.length > 2 ? arguments[2] : undefined;
502
-
503
480
  // Apply defaults for any values that we don't have.
504
- var carbonIntensityWithDefaults = _objectSpread(_objectSpread({}, CARBON_INTENSITY_DEFAULTS), carbonIntensity); // Distance is in meters, totalCarbon is in grams
505
-
481
+ var carbonIntensityWithDefaults = _objectSpread(_objectSpread({}, CARBON_INTENSITY_DEFAULTS), carbonIntensity);
506
482
 
507
- var totalCarbon = (itinerary === null || itinerary === void 0 ? void 0 : (_itinerary$legs = itinerary.legs) === null || _itinerary$legs === void 0 ? void 0 : _itinerary$legs.reduce(function (total, leg) {
483
+ // Distance is in meters, totalCarbon is in grams
484
+ var totalCarbon = (itinerary === null || itinerary === void 0 || (_itinerary$legs = itinerary.legs) === null || _itinerary$legs === void 0 ? void 0 : _itinerary$legs.reduce(function (total, leg) {
508
485
  return (leg.distance * carbonIntensityWithDefaults[leg.mode.toLowerCase()] || 0) + total;
509
486
  }, 0)) || 0;
510
-
511
487
  switch (units) {
512
488
  case "ounce":
513
489
  return totalCarbon / 28.35;
514
-
515
490
  case "kilogram":
516
491
  return totalCarbon / 1000;
517
-
518
492
  case "pound":
519
493
  return totalCarbon / 454;
520
-
521
494
  case "gram":
522
495
  default:
523
496
  return totalCarbon;
524
497
  }
525
498
  }
499
+
526
500
  /**
527
501
  * Returns the user-facing stop id to display for a stop or place, using the following priority:
528
502
  * 1. stop code,
529
503
  * 2. stop id without the agency id portion, if stop id contains an agency portion,
530
504
  * 3. stop id, whether null or not (this is the fallback case).
531
505
  */
532
-
533
506
  export function getDisplayedStopId(placeOrStop) {
534
507
  var _stopId;
535
-
536
508
  var stopId;
537
509
  var stopCode;
538
-
539
510
  if ("stopId" in placeOrStop) {
540
511
  stopCode = placeOrStop.stopCode;
541
512
  stopId = placeOrStop.stopId;
@@ -543,9 +514,9 @@ export function getDisplayedStopId(placeOrStop) {
543
514
  stopCode = placeOrStop.code;
544
515
  stopId = placeOrStop.id;
545
516
  }
546
-
547
517
  return stopCode || ((_stopId = stopId) === null || _stopId === void 0 ? void 0 : _stopId.split(":")[1]) || stopId;
548
518
  }
519
+
549
520
  /**
550
521
  * Extracts useful data from the fare products on a leg, such as the leg cost and transfer info.
551
522
  * @param leg Leg with fare products (must have used getLegsWithFares)
@@ -553,7 +524,6 @@ export function getDisplayedStopId(placeOrStop) {
553
524
  * @param container Fare container (cash, electronic)
554
525
  * @returns Object containing price as well as the transfer discount amount, if a transfer was used.
555
526
  */
556
-
557
527
  export function getLegCost(leg, mediumId, riderCategoryId) {
558
528
  if (!leg.fareProducts) return {
559
529
  price: undefined
@@ -563,8 +533,9 @@ export function getLegCost(leg, mediumId, riderCategoryId) {
563
533
  // riderCategory and medium can be specifically defined as null to handle
564
534
  // generic GTFS based fares from OTP when there is no fare model
565
535
  return (product.riderCategory === null ? null : product.riderCategory.id) === riderCategoryId && (product.medium === null ? null : product.medium.id) === mediumId;
566
- }); // Custom fare models return "rideCost", generic GTFS fares return "regular"
536
+ });
567
537
 
538
+ // Custom fare models return "rideCost", generic GTFS fares return "regular"
568
539
  var totalCostProduct = relevantFareProducts.find(function (fp) {
569
540
  return fp.product.name === "rideCost" || fp.product.name === "regular";
570
541
  });
@@ -577,6 +548,7 @@ export function getLegCost(leg, mediumId, riderCategoryId) {
577
548
  productUseId: totalCostProduct === null || totalCostProduct === void 0 ? void 0 : totalCostProduct.id
578
549
  };
579
550
  }
551
+
580
552
  /**
581
553
  * Returns the total itinerary cost for a given set of legs.
582
554
  * @param legs Itinerary legs with fare products (must have used getLegsWithFares)
@@ -584,19 +556,20 @@ export function getLegCost(leg, mediumId, riderCategoryId) {
584
556
  * @param container Fare container (cash, electronic)
585
557
  * @returns Money object for the total itinerary cost.
586
558
  */
587
-
588
559
  export function getItineraryCost(legs, mediumId, riderCategoryId) {
589
- var legCosts = legs // Only legs with fares (no walking legs)
560
+ var legCosts = legs
561
+ // Only legs with fares (no walking legs)
590
562
  .filter(function (leg) {
591
563
  var _leg$fareProducts;
592
-
593
564
  return ((_leg$fareProducts = leg.fareProducts) === null || _leg$fareProducts === void 0 ? void 0 : _leg$fareProducts.length) > 0;
594
- }) // Get the leg cost object of each leg
565
+ })
566
+ // Get the leg cost object of each leg
595
567
  .map(function (leg) {
596
568
  return getLegCost(leg, mediumId, riderCategoryId);
597
569
  }).filter(function (cost) {
598
570
  return cost.price !== undefined;
599
- }) // Filter out duplicate use IDs
571
+ })
572
+ // Filter out duplicate use IDs
600
573
  // One fare product can be used on multiple legs,
601
574
  // and we don't want to count it more than once.
602
575
  .reduce(function (prev, cur) {
@@ -608,16 +581,14 @@ export function getItineraryCost(legs, mediumId, riderCategoryId) {
608
581
  price: cur.price
609
582
  });
610
583
  }
611
-
612
584
  return prev;
613
585
  }, []).map(function (productUse) {
614
586
  return productUse.price;
615
587
  });
616
- if (legCosts.length === 0) return undefined; // Calculate the total
617
-
588
+ if (legCosts.length === 0) return undefined;
589
+ // Calculate the total
618
590
  return legCosts.reduce(function (prev, cur) {
619
591
  var _prev$currency;
620
-
621
592
  return {
622
593
  amount: prev.amount + (cur === null || cur === void 0 ? void 0 : cur.amount) || 0,
623
594
  currency: (_prev$currency = prev.currency) !== null && _prev$currency !== void 0 ? _prev$currency : cur === null || cur === void 0 ? void 0 : cur.currency
@@ -627,29 +598,22 @@ export function getItineraryCost(legs, mediumId, riderCategoryId) {
627
598
  currency: null
628
599
  });
629
600
  }
630
-
631
601
  var pickupDropoffTypeToOtp1 = function pickupDropoffTypeToOtp1(otp2Type) {
632
602
  switch (otp2Type) {
633
603
  case "COORDINATE_WITH_DRIVER":
634
604
  return "coordinateWithDriver";
635
-
636
605
  case "CALL_AGENCY":
637
606
  return "mustPhone";
638
-
639
607
  case "SCHEDULED":
640
608
  return "scheduled";
641
-
642
609
  case "NONE":
643
610
  return "none";
644
-
645
611
  default:
646
612
  return null;
647
613
  }
648
614
  };
649
-
650
615
  export var convertGraphQLResponseToLegacy = function convertGraphQLResponseToLegacy(leg) {
651
616
  var _leg$agency, _leg$agency2, _leg$agency3, _leg$agency4, _leg$from$stop, _leg$from$stop2, _leg$route, _leg$route2, _leg$route3, _leg$route4, _leg$route5, _leg$route6, _leg$to$stop, _leg$to$stop2, _leg$trip, _leg$trip2;
652
-
653
617
  return _objectSpread(_objectSpread({}, leg), {}, {
654
618
  agencyBrandingUrl: (_leg$agency = leg.agency) === null || _leg$agency === void 0 ? void 0 : _leg$agency.url,
655
619
  agencyId: (_leg$agency2 = leg.agency) === null || _leg$agency2 === void 0 ? void 0 : _leg$agency2.id,
@@ -678,27 +642,27 @@ export var convertGraphQLResponseToLegacy = function convertGraphQLResponseToLeg
678
642
  tripId: (_leg$trip2 = leg.trip) === null || _leg$trip2 === void 0 ? void 0 : _leg$trip2.gtfsId
679
643
  });
680
644
  };
681
- /** Extracts the route number for a leg returned from OTP1 or OTP2. */
682
645
 
646
+ /** Extracts the route number for a leg returned from OTP1 or OTP2. */
683
647
  export var getLegRouteShortName = function getLegRouteShortName(leg) {
684
648
  var route = leg.route,
685
- routeShortName = leg.routeShortName; // typeof route === "object" denotes newer OTP2 responses. routeShortName and route as string is OTP1.
686
-
649
+ routeShortName = leg.routeShortName;
650
+ // typeof route === "object" denotes newer OTP2 responses. routeShortName and route as string is OTP1.
687
651
  return _typeof(route) === "object" ? route === null || route === void 0 ? void 0 : route.shortName : routeShortName || route;
688
652
  };
689
- /** Extract the route long name for a leg returned from OTP1 or OTP2. */
690
653
 
654
+ /** Extract the route long name for a leg returned from OTP1 or OTP2. */
691
655
  export var getLegRouteLongName = function getLegRouteLongName(leg) {
692
656
  var route = leg.route,
693
- routeLongName = leg.routeLongName; // typeof route === "object" denotes newer OTP2 responses. routeLongName is OTP1.
694
-
657
+ routeLongName = leg.routeLongName;
658
+ // typeof route === "object" denotes newer OTP2 responses. routeLongName is OTP1.
695
659
  return _typeof(route) === "object" ? route === null || route === void 0 ? void 0 : route.longName : routeLongName;
696
660
  };
661
+
697
662
  /**
698
663
  * Returns the route short name, or the route long name if no short name is provided.
699
664
  * This is happens with Seattle area streetcars and ferries.
700
665
  */
701
-
702
666
  export var getLegRouteName = function getLegRouteName(leg) {
703
667
  return getLegRouteShortName(leg) || getLegRouteLongName(leg);
704
668
  };