@opentripplanner/core-utils 6.0.0 → 7.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/esm/itinerary.js +74 -11
- package/esm/itinerary.js.map +1 -1
- package/esm/map.js +19 -16
- package/esm/map.js.map +1 -1
- package/esm/query.js +14 -11
- package/esm/query.js.map +1 -1
- package/esm/time.js +5 -56
- package/esm/time.js.map +1 -1
- package/lib/itinerary.d.ts +14 -7
- package/lib/itinerary.d.ts.map +1 -1
- package/lib/itinerary.js +79 -65
- package/lib/itinerary.js.map +1 -1
- package/lib/map.d.ts +3 -3
- package/lib/map.d.ts.map +1 -1
- package/lib/map.js +18 -32
- package/lib/map.js.map +1 -1
- package/lib/query.js +11 -15
- package/lib/query.js.map +1 -1
- package/lib/time.d.ts +1 -16
- package/lib/time.d.ts.map +1 -1
- package/lib/time.js +4 -72
- package/lib/time.js.map +1 -1
- package/package.json +4 -5
- package/src/__tests__/__snapshots__/itinerary.js.snap +2 -38
- package/src/__tests__/__snapshots__/time.js.snap +7 -13
- package/src/__tests__/itinerary.js +6 -29
- package/src/__tests__/time.js +7 -27
- package/src/itinerary.ts +72 -27
- package/src/map.ts +17 -22
- package/src/query.js +19 -16
- package/src/time.ts +11 -76
- package/tsconfig.tsbuildinfo +1346 -1299
- package/esm/deprecated-with-types.js +0 -47
- package/esm/deprecated-with-types.js.map +0 -1
- package/esm/deprecated.js +0 -325
- package/esm/deprecated.js.map +0 -1
- package/lib/deprecated-with-types.d.ts +0 -23
- package/lib/deprecated-with-types.d.ts.map +0 -1
- package/lib/deprecated-with-types.js +0 -61
- package/lib/deprecated-with-types.js.map +0 -1
- package/lib/deprecated.js +0 -355
- package/lib/deprecated.js.map +0 -1
- package/src/__tests__/__mocks__/multi-currency-itinerary.json +0 -1728
- package/src/deprecated-with-types.ts +0 -62
- package/src/deprecated.js +0 -334
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/* eslint-disable import/no-cycle */
|
|
2
|
-
import { TimeOptions } from "@opentripplanner/types";
|
|
3
|
-
import { format } from "date-fns";
|
|
4
|
-
|
|
5
|
-
import { logDeprecationWarning } from "./deprecated";
|
|
6
|
-
import {
|
|
7
|
-
formatDurationLikeMoment,
|
|
8
|
-
offsetTime,
|
|
9
|
-
OTP_API_TIME_FORMAT
|
|
10
|
-
} from "./time";
|
|
11
|
-
|
|
12
|
-
// time.ts
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Formats a time value for display in narrative
|
|
16
|
-
* TODO: internationalization/timezone
|
|
17
|
-
* @param {number} ms epoch time value in milliseconds
|
|
18
|
-
* @returns {string} formatted text representation
|
|
19
|
-
*/
|
|
20
|
-
export function formatTime(ms: number, options: TimeOptions): string {
|
|
21
|
-
logDeprecationWarning("formatTime", "formatjs");
|
|
22
|
-
|
|
23
|
-
return format(
|
|
24
|
-
offsetTime(ms, options),
|
|
25
|
-
options?.format || OTP_API_TIME_FORMAT
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Formats an elapsed time duration for display in narrative.
|
|
31
|
-
* TODO: internationalization
|
|
32
|
-
* @param {number} seconds duration in seconds
|
|
33
|
-
* @returns {string} formatted text representation
|
|
34
|
-
*/
|
|
35
|
-
// TS TODO: region as type?
|
|
36
|
-
export function formatDuration(seconds: number, region: string): string {
|
|
37
|
-
logDeprecationWarning("formatDuration", "formatjs");
|
|
38
|
-
|
|
39
|
-
return formatDurationLikeMoment(seconds, false, {
|
|
40
|
-
enabled: true,
|
|
41
|
-
code: region
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Formats an elapsed time in seconds, minutes, hours duration for display in narrative
|
|
47
|
-
* @param {number} seconds duration in seconds
|
|
48
|
-
* @param {object} region an object that allows internationalization of the time
|
|
49
|
-
* @returns {string} formatted text representation
|
|
50
|
-
*/
|
|
51
|
-
// TS TODO: region as type?
|
|
52
|
-
export function formatDurationWithSeconds(
|
|
53
|
-
seconds: number,
|
|
54
|
-
region: string
|
|
55
|
-
): string {
|
|
56
|
-
logDeprecationWarning("formatDurationWithSeconds", "formatjs");
|
|
57
|
-
|
|
58
|
-
return formatDurationLikeMoment(seconds, true, {
|
|
59
|
-
enabled: true,
|
|
60
|
-
code: region
|
|
61
|
-
});
|
|
62
|
-
}
|
package/src/deprecated.js
DELETED
|
@@ -1,334 +0,0 @@
|
|
|
1
|
-
import moment from "moment";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* To disable cyclic dependency resolution we need to require() within methods
|
|
5
|
-
* This is a good reason to disable this eslint-rule
|
|
6
|
-
*/
|
|
7
|
-
/* eslint-disable global-require */
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Generates a warning to tell developer that they are using deprecated methods!
|
|
11
|
-
*/
|
|
12
|
-
export function logDeprecationWarning(method, alternative) {
|
|
13
|
-
console.warn(
|
|
14
|
-
`${method ||
|
|
15
|
-
"This method"} is deprecated and will be removed in a future otp-ui release. All language functionality should be handled using react-intl.
|
|
16
|
-
${
|
|
17
|
-
alternative
|
|
18
|
-
? `
|
|
19
|
-
|
|
20
|
-
Use ${alternative} instead, which provides a new interface that doesn't return English strings.`
|
|
21
|
-
: ""
|
|
22
|
-
}`
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// itinerary.js
|
|
27
|
-
|
|
28
|
-
export function getStepDirection(step) {
|
|
29
|
-
logDeprecationWarning("getStepDirection");
|
|
30
|
-
|
|
31
|
-
switch (step.relativeDirection) {
|
|
32
|
-
case "DEPART":
|
|
33
|
-
return `Head ${step.absoluteDirection.toLowerCase()}`;
|
|
34
|
-
case "LEFT":
|
|
35
|
-
return "Left";
|
|
36
|
-
case "HARD_LEFT":
|
|
37
|
-
return "Hard left";
|
|
38
|
-
case "SLIGHTLY_LEFT":
|
|
39
|
-
return "Slight left";
|
|
40
|
-
case "CONTINUE":
|
|
41
|
-
return "Continue";
|
|
42
|
-
case "SLIGHTLY_RIGHT":
|
|
43
|
-
return "Slight right";
|
|
44
|
-
case "RIGHT":
|
|
45
|
-
return "Right";
|
|
46
|
-
case "HARD_RIGHT":
|
|
47
|
-
return "Hard right";
|
|
48
|
-
case "CIRCLE_CLOCKWISE":
|
|
49
|
-
return "Follow circle clockwise";
|
|
50
|
-
case "CIRCLE_COUNTERCLOCKWISE":
|
|
51
|
-
return "Follow circle counterclockwise";
|
|
52
|
-
case "ELEVATOR":
|
|
53
|
-
return "Take elevator";
|
|
54
|
-
case "UTURN_LEFT":
|
|
55
|
-
return "Left U-turn";
|
|
56
|
-
case "UTURN_RIGHT":
|
|
57
|
-
return "Right U-turn";
|
|
58
|
-
default:
|
|
59
|
-
return step.relativeDirection;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function getStepInstructions(step) {
|
|
64
|
-
logDeprecationWarning("getStepInstructions");
|
|
65
|
-
|
|
66
|
-
const conjunction = step.relativeDirection === "ELEVATOR" ? "to" : "on";
|
|
67
|
-
return `${getStepDirection(step)} ${conjunction} ${step.streetName}`;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function getStepStreetName(step) {
|
|
71
|
-
logDeprecationWarning("getStepStreetName");
|
|
72
|
-
|
|
73
|
-
if (step.streetName === "road") return "Unnamed Road";
|
|
74
|
-
if (step.streetName === "path") return "Unnamed Path";
|
|
75
|
-
return step.streetName;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function getLegModeLabel(leg) {
|
|
79
|
-
logDeprecationWarning("getLegModeLabel");
|
|
80
|
-
|
|
81
|
-
switch (leg.mode) {
|
|
82
|
-
case "BICYCLE_RENT":
|
|
83
|
-
return "Biketown";
|
|
84
|
-
case "CAR":
|
|
85
|
-
return leg.hailedCar ? "Ride" : "Drive";
|
|
86
|
-
case "GONDOLA":
|
|
87
|
-
return "Aerial Tram";
|
|
88
|
-
case "TRAM":
|
|
89
|
-
if (leg.routeLongName.toLowerCase().indexOf("streetcar") !== -1)
|
|
90
|
-
return "Streetcar";
|
|
91
|
-
return "Light Rail";
|
|
92
|
-
case "MICROMOBILITY":
|
|
93
|
-
case "SCOOTER":
|
|
94
|
-
return "Ride";
|
|
95
|
-
default:
|
|
96
|
-
return require("./itinerary").toSentenceCase(leg.mode);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Returns mode name by checking the vertex type (VertexType class in OTP) for
|
|
102
|
-
* the provided place. NOTE: this is currently only intended for vehicles at
|
|
103
|
-
* the moment (not transit or walking).
|
|
104
|
-
*
|
|
105
|
-
* @param {string} place place from itinerary leg
|
|
106
|
-
*/
|
|
107
|
-
export function getModeForPlace(place) {
|
|
108
|
-
logDeprecationWarning("getModeForPlace");
|
|
109
|
-
|
|
110
|
-
switch (place.vertexType) {
|
|
111
|
-
case "CARSHARE":
|
|
112
|
-
return "car";
|
|
113
|
-
case "VEHICLERENTAL":
|
|
114
|
-
return "E-scooter";
|
|
115
|
-
// TODO: Should the type change depending on bike vertex type?
|
|
116
|
-
case "BIKESHARE":
|
|
117
|
-
case "BIKEPARK":
|
|
118
|
-
return "bike";
|
|
119
|
-
// If company offers more than one mode, default to `vehicle` string.
|
|
120
|
-
default:
|
|
121
|
-
return "vehicle";
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export function getPlaceName(place, companies) {
|
|
126
|
-
logDeprecationWarning("getPlaceName");
|
|
127
|
-
|
|
128
|
-
// If address is provided (i.e. for carshare station, use it)
|
|
129
|
-
if (place.address) return place.address.split(",")[0];
|
|
130
|
-
if (place.networks && place.vertexType === "VEHICLERENTAL") {
|
|
131
|
-
// For vehicle rental pick up, do not use the place name. Rather, use
|
|
132
|
-
// company name + vehicle type (e.g., SPIN E-scooter). Place name is often just
|
|
133
|
-
// a UUID that has no relevance to the actual vehicle. For bikeshare, however,
|
|
134
|
-
// there are often hubs or bikes that have relevant names to the user.
|
|
135
|
-
const company = require("./itinerary").getCompanyForNetwork(
|
|
136
|
-
place.networks[0],
|
|
137
|
-
companies
|
|
138
|
-
);
|
|
139
|
-
if (company) {
|
|
140
|
-
return `${company.label} ${getModeForPlace(place)}`;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
// Default to place name
|
|
144
|
-
return place.name;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* For a given fare component (either total fare or component parts), returns
|
|
149
|
-
* an object with string formatters and the fare value (in cents).
|
|
150
|
-
*/
|
|
151
|
-
export function getTransitFare(fareComponent) {
|
|
152
|
-
logDeprecationWarning("getTransitFare", "the fare object and getTncFare");
|
|
153
|
-
|
|
154
|
-
// Default values (if fare component is not valid).
|
|
155
|
-
let digits = 2;
|
|
156
|
-
let transitFare = 0;
|
|
157
|
-
let symbol = "$";
|
|
158
|
-
let currencyCode = "USD";
|
|
159
|
-
if (fareComponent) {
|
|
160
|
-
// Assign values without declaration. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#assignment_without_declaration
|
|
161
|
-
({
|
|
162
|
-
currencyCode,
|
|
163
|
-
defaultFractionDigits: digits,
|
|
164
|
-
symbol
|
|
165
|
-
} = fareComponent.currency);
|
|
166
|
-
transitFare = fareComponent.cents;
|
|
167
|
-
}
|
|
168
|
-
// For cents to string conversion, use digits from fare component.
|
|
169
|
-
const centsToString = cents => {
|
|
170
|
-
const dollars = (cents / 10 ** digits).toFixed(digits);
|
|
171
|
-
return `${symbol}${dollars}`;
|
|
172
|
-
};
|
|
173
|
-
// For dollars to string conversion, assume we're rounding to two digits.
|
|
174
|
-
const dollarsToString = dollars => `${symbol}${dollars.toFixed(2)}`;
|
|
175
|
-
return {
|
|
176
|
-
centsToString,
|
|
177
|
-
currencyCode,
|
|
178
|
-
dollarsToString,
|
|
179
|
-
transitFare
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* For an itinerary, calculates the transit/TNC fares and returns an object with
|
|
185
|
-
* these values, currency info, as well as string formatters.
|
|
186
|
-
* It is assumed that the same currency is used for transit and TNC legs.
|
|
187
|
-
*
|
|
188
|
-
* multiple being set to true will change the output behavior:
|
|
189
|
-
* - dollarsToString and centsToString will be returned as part of each fare
|
|
190
|
-
* - currencyCode will be returned separately for each fare
|
|
191
|
-
* - tnc currency code will be returned separately
|
|
192
|
-
* - each fare type will be returned separately within a new transitFares property
|
|
193
|
-
*
|
|
194
|
-
* FIXME: a new approach to fare calculation must be found:
|
|
195
|
-
* the current approach is not sustainable, as centsToString and DollarsToString
|
|
196
|
-
* must be replaced by i18n anyway.
|
|
197
|
-
*
|
|
198
|
-
* However, the current behavior should ideally be kept to avoid a breaking change.
|
|
199
|
-
* The "multiple" mode is helpful, but only prevents tnc fare calculation from being duplicated.
|
|
200
|
-
* This method could be split out into a new one, along with tnc fare calculation.
|
|
201
|
-
* If this is done, the individual fare calculation should also be modified to support
|
|
202
|
-
* a default fare not being called "regular". However, this again would be a breaking change.
|
|
203
|
-
* This breaking change is avoided by adding the "multiple" parameter.
|
|
204
|
-
*
|
|
205
|
-
* When centsToString and dollarsToString are removed, this method should be split into
|
|
206
|
-
* individual fare calculation on a variable fare key, fare calculation of an entire leg,
|
|
207
|
-
* which will get fares for every fare key in the leg, and a method to calculate the fare of
|
|
208
|
-
* a tnc ride within the leg. This will make typescripting easier, as the types will be cleaner.
|
|
209
|
-
*/
|
|
210
|
-
export function calculateFares(itinerary, multiple = false) {
|
|
211
|
-
logDeprecationWarning("calculateFares", "the fare object and getTncFare");
|
|
212
|
-
|
|
213
|
-
// Process any TNC fares
|
|
214
|
-
let minTNCFare = 0;
|
|
215
|
-
let maxTNCFare = 0;
|
|
216
|
-
let tncCurrencyCode;
|
|
217
|
-
itinerary.legs.forEach(leg => {
|
|
218
|
-
if (leg.mode === "CAR" && leg.hailedCar && leg.tncData) {
|
|
219
|
-
const { currency, maxCost, minCost } = leg.tncData;
|
|
220
|
-
// TODO: Support non-USD
|
|
221
|
-
minTNCFare += minCost;
|
|
222
|
-
maxTNCFare += maxCost;
|
|
223
|
-
tncCurrencyCode = currency;
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
if (multiple) {
|
|
228
|
-
// Return object of fares
|
|
229
|
-
const transitFares = {};
|
|
230
|
-
if (itinerary && itinerary.fare && itinerary.fare.fare) {
|
|
231
|
-
Object.keys(itinerary.fare.fare).forEach(fareKey => {
|
|
232
|
-
const fareComponent = itinerary.fare.fare[fareKey];
|
|
233
|
-
transitFares[fareKey] = getTransitFare(fareComponent);
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
return {
|
|
238
|
-
maxTNCFare,
|
|
239
|
-
minTNCFare,
|
|
240
|
-
tncCurrencyCode,
|
|
241
|
-
transitFares
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// Extract fare total from itinerary fares.
|
|
246
|
-
const fareComponent =
|
|
247
|
-
itinerary.fare && itinerary.fare.fare && itinerary.fare.fare.regular;
|
|
248
|
-
// Get string formatters and itinerary fare.
|
|
249
|
-
const {
|
|
250
|
-
centsToString,
|
|
251
|
-
currencyCode: transitCurrencyCode,
|
|
252
|
-
dollarsToString,
|
|
253
|
-
transitFare
|
|
254
|
-
} = getTransitFare(fareComponent);
|
|
255
|
-
|
|
256
|
-
return {
|
|
257
|
-
centsToString,
|
|
258
|
-
currencyCode: transitCurrencyCode || tncCurrencyCode,
|
|
259
|
-
dollarsToString,
|
|
260
|
-
maxTNCFare,
|
|
261
|
-
minTNCFare,
|
|
262
|
-
transitFare
|
|
263
|
-
};
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
// map.js
|
|
267
|
-
|
|
268
|
-
export function latlngToString(latlng) {
|
|
269
|
-
logDeprecationWarning("latlngToString", "the latlng object");
|
|
270
|
-
|
|
271
|
-
return (
|
|
272
|
-
latlng &&
|
|
273
|
-
`${latlng.lat.toFixed(5)}, ${(latlng.lng || latlng.lon).toFixed(5)}`
|
|
274
|
-
);
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
export function coordsToString(coords) {
|
|
278
|
-
logDeprecationWarning("coordsToString", "the coords object");
|
|
279
|
-
|
|
280
|
-
return coords.length && coords.map(c => (+c).toFixed(5)).join(", ");
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
export function getDetailText(location) {
|
|
284
|
-
let detailText;
|
|
285
|
-
if (location.type === "home" || location.type === "work") {
|
|
286
|
-
detailText = location.name;
|
|
287
|
-
}
|
|
288
|
-
if (location.type === "stop") {
|
|
289
|
-
detailText = location.id;
|
|
290
|
-
} else if (location.type === "recent" && location.timestamp) {
|
|
291
|
-
detailText = moment(location.timestamp).fromNow();
|
|
292
|
-
}
|
|
293
|
-
return detailText;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// query.js
|
|
297
|
-
|
|
298
|
-
export function summarizeQuery(query, locations = []) {
|
|
299
|
-
logDeprecationWarning("summarizeQuery");
|
|
300
|
-
|
|
301
|
-
function findLocationType(
|
|
302
|
-
location,
|
|
303
|
-
ls = [],
|
|
304
|
-
types = ["home", "work", "suggested"]
|
|
305
|
-
) {
|
|
306
|
-
const match = ls.find(l => require("./map").matchLatLon(l, location));
|
|
307
|
-
return match && types.indexOf(match.type) !== -1 ? match.type : null;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
const from =
|
|
311
|
-
findLocationType(query.from, locations) || query.from.name.split(",")[0];
|
|
312
|
-
const to =
|
|
313
|
-
findLocationType(query.to, locations) || query.to.name.split(",")[0];
|
|
314
|
-
const mode = require("./itinerary").hasTransit(query.mode)
|
|
315
|
-
? "Transit"
|
|
316
|
-
: require("./itinerary").toSentenceCase(query.mode);
|
|
317
|
-
return `${mode} from ${from} to ${to}`;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
export function getTimeZoneOffset(itinerary) {
|
|
321
|
-
logDeprecationWarning("getTimeZoneOffset");
|
|
322
|
-
|
|
323
|
-
if (!itinerary.legs || !itinerary.legs.length) return 0;
|
|
324
|
-
|
|
325
|
-
// Determine if there is a DST offset between now and the itinerary start date
|
|
326
|
-
const dstOffset =
|
|
327
|
-
new Date(itinerary.startTime).getTimezoneOffset() -
|
|
328
|
-
new Date().getTimezoneOffset();
|
|
329
|
-
|
|
330
|
-
return (
|
|
331
|
-
itinerary.legs[0].agencyTimeZoneOffset +
|
|
332
|
-
(new Date().getTimezoneOffset() + dstOffset) * 60000
|
|
333
|
-
);
|
|
334
|
-
}
|