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