@opentripplanner/core-utils 6.0.1-alpha.1 → 7.0.0-alpha.1

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 (45) hide show
  1. package/esm/itinerary.js +74 -11
  2. package/esm/itinerary.js.map +1 -1
  3. package/esm/map.js +19 -16
  4. package/esm/map.js.map +1 -1
  5. package/esm/query.js +14 -11
  6. package/esm/query.js.map +1 -1
  7. package/esm/time.js +5 -56
  8. package/esm/time.js.map +1 -1
  9. package/lib/itinerary.d.ts +14 -7
  10. package/lib/itinerary.d.ts.map +1 -1
  11. package/lib/itinerary.js +79 -65
  12. package/lib/itinerary.js.map +1 -1
  13. package/lib/map.d.ts +3 -3
  14. package/lib/map.d.ts.map +1 -1
  15. package/lib/map.js +18 -32
  16. package/lib/map.js.map +1 -1
  17. package/lib/query.js +11 -15
  18. package/lib/query.js.map +1 -1
  19. package/lib/time.d.ts +1 -16
  20. package/lib/time.d.ts.map +1 -1
  21. package/lib/time.js +4 -72
  22. package/lib/time.js.map +1 -1
  23. package/package.json +4 -6
  24. package/src/__tests__/__snapshots__/itinerary.js.snap +2 -38
  25. package/src/__tests__/__snapshots__/time.js.snap +7 -13
  26. package/src/__tests__/itinerary.js +6 -29
  27. package/src/__tests__/time.js +7 -27
  28. package/src/itinerary.ts +72 -27
  29. package/src/map.ts +17 -22
  30. package/src/query.js +19 -16
  31. package/src/time.ts +11 -76
  32. package/tsconfig.tsbuildinfo +1294 -1311
  33. package/esm/deprecated-with-types.js +0 -47
  34. package/esm/deprecated-with-types.js.map +0 -1
  35. package/esm/deprecated.js +0 -325
  36. package/esm/deprecated.js.map +0 -1
  37. package/lib/deprecated-with-types.d.ts +0 -23
  38. package/lib/deprecated-with-types.d.ts.map +0 -1
  39. package/lib/deprecated-with-types.js +0 -61
  40. package/lib/deprecated-with-types.js.map +0 -1
  41. package/lib/deprecated.js +0 -355
  42. package/lib/deprecated.js.map +0 -1
  43. package/src/__tests__/__mocks__/multi-currency-itinerary.json +0 -1728
  44. package/src/deprecated-with-types.ts +0 -62
  45. package/src/deprecated.js +0 -334
package/src/itinerary.ts CHANGED
@@ -7,33 +7,37 @@ import {
7
7
  Itinerary,
8
8
  LatLngArray,
9
9
  Leg,
10
- Step
10
+ Money,
11
+ Step,
12
+ TncFare
11
13
  } from "@opentripplanner/types";
12
14
  import turfAlong from "@turf/along";
13
15
 
16
+ /*
14
17
  import {
15
- calculateFares,
16
- getLegModeLabel,
17
- getModeForPlace,
18
- getPlaceName,
19
- getStepDirection,
20
- getStepInstructions,
21
- getStepStreetName,
22
- getTimeZoneOffset,
23
- getTransitFare
18
+ // calculateFares,
19
+ // getLegModeLabel,
20
+ // getModeForPlace,
21
+ // getPlaceName,
22
+ // getStepDirection,
23
+ // getStepInstructions,
24
+ // getStepStreetName,
25
+ // getTimeZoneOffset,
26
+ // getTransitFare
24
27
  } from "./deprecated";
25
28
 
26
29
  export {
27
- calculateFares,
28
- getLegModeLabel,
29
- getModeForPlace,
30
- getPlaceName,
31
- getStepDirection,
32
- getStepInstructions,
33
- getStepStreetName,
34
- getTimeZoneOffset,
35
- getTransitFare
30
+ // calculateFares,
31
+ // getLegModeLabel,
32
+ // getModeForPlace,
33
+ // getPlaceName,
34
+ // getStepDirection,
35
+ // getStepInstructions,
36
+ // getStepStreetName,
37
+ // getTimeZoneOffset,
38
+ // getTransitFare
36
39
  };
40
+ */
37
41
 
38
42
  // All OTP transit modes
39
43
  export const transitModes = [
@@ -445,12 +449,53 @@ export function calculatePhysicalActivity(
445
449
  };
446
450
  }
447
451
 
448
- export function calculateTncFares(itinerary) {
449
- // TODO: don't rely on deprecated methods!
450
- // At the moment this is safe as none of these exported variables contain strings
451
- const { maxTNCFare, minTNCFare, tncCurrencyCode } = calculateFares(
452
- itinerary,
453
- true
454
- );
455
- return { maxTNCFare, minTNCFare, tncCurrencyCode };
452
+ /**
453
+ * For an itinerary, calculates the TNC fares and returns an object with
454
+ * these values and currency info.
455
+ * It is assumed that the same currency is used for all TNC legs.
456
+ */
457
+ export function calculateTncFares(itinerary: Itinerary): TncFare {
458
+ let minTNCFare = 0;
459
+ let maxTNCFare = 0;
460
+ let currencyCode;
461
+ itinerary.legs.forEach(({ hailedCar, mode, tncData }) => {
462
+ if (mode === "CAR" && hailedCar && tncData) {
463
+ const { currency, maxCost, minCost } = tncData;
464
+ minTNCFare += minCost;
465
+ maxTNCFare += maxCost;
466
+ // Assumes a single currency for entire itinerary.
467
+ currencyCode = currency;
468
+ }
469
+ });
470
+
471
+ return {
472
+ currencyCode,
473
+ maxTNCFare,
474
+ minTNCFare
475
+ };
476
+ }
477
+
478
+ /**
479
+ * For a given fare component (either total fare or component parts), returns
480
+ * an object with the fare value (in cents).
481
+ */
482
+ export function getTransitFare(
483
+ fareComponent: Money
484
+ ): {
485
+ currencyCode: string;
486
+ transitFare: number;
487
+ } {
488
+ // Default values (if fare component is not valid).
489
+ let transitFare = 0;
490
+ let currencyCode = "USD";
491
+ if (fareComponent) {
492
+ // Assign values without declaration.
493
+ // See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#assignment_without_declaration
494
+ ({ currencyCode } = fareComponent.currency);
495
+ transitFare = fareComponent.cents;
496
+ }
497
+ return {
498
+ currencyCode,
499
+ transitFare
500
+ };
456
501
  }
package/src/map.ts CHANGED
@@ -1,14 +1,16 @@
1
1
  import { LatLngArray, Location, UserPosition } from "@opentripplanner/types";
2
2
  import { toSentenceCase } from "./itinerary";
3
3
 
4
+ /*
4
5
  import {
5
- coordsToString,
6
- getDetailText,
7
- latlngToString,
8
- logDeprecationWarning
6
+ // coordsToString,
7
+ // getDetailText //,
8
+ // latlngToString,
9
+ // logDeprecationWarning
9
10
  } from "./deprecated";
10
11
 
11
- export { coordsToString, getDetailText, latlngToString };
12
+ export { coordsToString, getDetailText , latlngToString };
13
+ */
12
14
 
13
15
  export function currentPositionToLocation(
14
16
  currentPosition: UserPosition
@@ -26,6 +28,12 @@ export function currentPositionToLocation(
26
28
  };
27
29
  }
28
30
 
31
+ // TRICKY: It is used in query.js and in the context of
32
+ // otp-rr actions where the intl context is not available.
33
+ export function coordsToString(coords: number[]): string {
34
+ return coords.length && coords.map(c => (+c).toFixed(5)).join(", ");
35
+ }
36
+
29
37
  export function stringToCoords(str: string): number[] {
30
38
  return (str && str.split(",").map(c => +c)) || [];
31
39
  }
@@ -40,23 +48,10 @@ export function constructLocation(latlng: {
40
48
  };
41
49
  }
42
50
 
43
- export function formatStoredPlaceName(
44
- location: Location,
45
- withDetails = true
46
- ): string {
47
- if (withDetails) {
48
- logDeprecationWarning("the formatStoredPlaceName withDetails parameter");
49
- }
50
-
51
- let displayName =
52
- location.type === "home" || location.type === "work"
53
- ? toSentenceCase(location.type)
54
- : location.name;
55
- if (withDetails) {
56
- const detailText = getDetailText(location);
57
- if (detailText) displayName += ` (${detailText})`;
58
- }
59
- return displayName;
51
+ export function formatStoredPlaceName(location: Location): string {
52
+ return location.type === "home" || location.type === "work"
53
+ ? toSentenceCase(location.type)
54
+ : location.name;
60
55
  }
61
56
 
62
57
  export function matchLatLon(location1: Location, location2: Location): boolean {
package/src/query.js CHANGED
@@ -1,20 +1,20 @@
1
- import moment from "moment";
1
+ import { format, isMatch, parse } from "date-fns";
2
2
  import getGeocoder from "@opentripplanner/geocoder/lib";
3
3
  import qs from "qs";
4
4
 
5
5
  import { getTransitModes, hasCar, isAccessMode } from "./itinerary";
6
- import { stringToCoords } from "./map";
6
+ import { coordsToString, stringToCoords } from "./map";
7
7
  import queryParams from "./query-params";
8
8
  import {
9
9
  getCurrentTime,
10
10
  getCurrentDate,
11
- OTP_API_DATE_FORMAT,
12
- OTP_API_TIME_FORMAT
11
+ OTP_API_TIME_FORMAT,
12
+ OTP_API_DATE_FORMAT_DATE_FNS
13
13
  } from "./time";
14
14
 
15
- import { coordsToString, summarizeQuery } from "./deprecated";
15
+ // import { coordsToString, summarizeQuery } from "./deprecated";
16
16
 
17
- export { summarizeQuery };
17
+ // export { summarizeQuery };
18
18
 
19
19
  /* The list of default parameters considered in the settings panel */
20
20
 
@@ -315,9 +315,15 @@ export function planParamsToQuery(params) {
315
315
  break;
316
316
  case "time":
317
317
  {
318
- const parsedTime = moment(params.time, TIME_FORMATS);
319
- query.time = parsedTime.isValid()
320
- ? parsedTime.format(OTP_API_TIME_FORMAT)
318
+ // Match one of the supported time formats
319
+ const matchedTimeFormat = TIME_FORMATS.find(timeFormat =>
320
+ isMatch(params.time, timeFormat)
321
+ );
322
+ query.time = matchedTimeFormat
323
+ ? format(
324
+ parse(params.time, matchedTimeFormat, new Date()),
325
+ OTP_API_TIME_FORMAT
326
+ )
321
327
  : getCurrentTime();
322
328
  }
323
329
  break;
@@ -421,8 +427,8 @@ export function getRoutingParams(config, currentQuery, ignoreRealtimeUpdates) {
421
427
  }
422
428
 
423
429
  // check date/time validity; ignore both if either is invalid
424
- const dateValid = moment(params.date, OTP_API_DATE_FORMAT).isValid();
425
- const timeValid = moment(params.time, OTP_API_TIME_FORMAT).isValid();
430
+ const dateValid = isMatch(params.date, OTP_API_DATE_FORMAT_DATE_FNS);
431
+ const timeValid = isMatch(params.time, OTP_API_TIME_FORMAT);
426
432
 
427
433
  if (!dateValid || !timeValid) {
428
434
  delete params.time;
@@ -447,11 +453,8 @@ export function getRoutingParams(config, currentQuery, ignoreRealtimeUpdates) {
447
453
  // Additional processing specific to PROFILE mode
448
454
  } else {
449
455
  // check start and end time validity; ignore both if either is invalid
450
- const startTimeValid = moment(
451
- params.startTime,
452
- OTP_API_TIME_FORMAT
453
- ).isValid();
454
- const endTimeValid = moment(params.endTime, OTP_API_TIME_FORMAT).isValid();
456
+ const startTimeValid = isMatch(params.startTime, OTP_API_TIME_FORMAT);
457
+ const endTimeValid = isMatch(params.endTime, OTP_API_TIME_FORMAT);
455
458
 
456
459
  if (!startTimeValid || !endTimeValid) {
457
460
  delete params.startTimeValid;
package/src/time.ts CHANGED
@@ -1,21 +1,7 @@
1
1
  import { Config } from "@opentripplanner/types";
2
- import {
3
- startOfDay,
4
- add,
5
- format,
6
- formatDuration as dateFnsFormatDuration
7
- } from "date-fns";
2
+ import { startOfDay, add, format } from "date-fns";
8
3
  import { utcToZonedTime } from "date-fns-tz";
9
4
 
10
- /* eslint-disable import/no-cycle */
11
- import {
12
- formatTime,
13
- formatDurationWithSeconds,
14
- formatDuration
15
- } from "./deprecated-with-types";
16
-
17
- export { formatTime, formatDuration, formatDurationWithSeconds };
18
-
19
5
  // special constants for making sure the following date format is always sent to
20
6
  // OTP regardless of whatever the user has configured as the display format
21
7
  export const OTP_API_DATE_FORMAT = "YYYY-MM-DD";
@@ -24,73 +10,22 @@ export const OTP_API_DATE_FORMAT = "YYYY-MM-DD";
24
10
  export const OTP_API_DATE_FORMAT_DATE_FNS = "yyyy-MM-dd";
25
11
  export const OTP_API_TIME_FORMAT = "HH:mm";
26
12
 
27
- /**
28
- * To ease the transition away from moment.js, this method uses date-fns to format durations
29
- * the way moment.js did.
30
- * @param {number} seconds The number of seconds to format
31
- * @param {boolean} showSeconds Whether to render seconds or not
32
- * @param {boolean} localize If true, will create output like moment.js using date-fns locale.
33
- * Otherwise, uses date-fns default
34
- * @returns Formatted duration
35
- */
36
- export function formatDurationLikeMoment(
37
- seconds: number,
38
- showSeconds: boolean,
39
- localize: { enabled: boolean; code: string } = {
40
- enabled: true,
41
- code: "en-US"
42
- }
43
- ): string {
44
- // date-fns doesn't do this automatically
45
- if ((!showSeconds && seconds < 60) || seconds === 0) {
46
- return "0 min";
47
- }
48
-
49
- const hours = Math.floor(seconds / 3600);
50
- const minutes = Math.floor((seconds - hours * 3600) / 60);
51
- const secondsLeftOver = showSeconds
52
- ? seconds - hours * 3600 - minutes * 60
53
- : 0;
54
- const specLookup = {
55
- xHours: "hr",
56
- xMinutes: "min",
57
- xSeconds: "sec"
58
- };
59
- const locale = localize
60
- ? {
61
- // Maintain backwards compatibility when called with localize=true
62
- code: localize?.code || "en-US",
63
- formatDistance: (spec, val) => {
64
- return `${val} ${specLookup[spec]}`;
65
- }
66
- }
67
- : undefined;
68
-
69
- return dateFnsFormatDuration(
70
- {
71
- hours,
72
- minutes,
73
- seconds: secondsLeftOver
74
- },
75
- {
76
- format: ["hours", "minutes", "seconds"],
77
- locale
78
- }
79
- );
80
- }
81
-
82
13
  /**
83
14
  * Breaks up a duration in seconds into hours, minutes, and seconds.
84
15
  * @param {number} seconds The number of seconds to break up
85
16
  * @returns an object with fields with the corresponding, hours, minutes, seconds.
86
17
  */
87
- export function toHoursMinutesSeconds(seconds) {
88
- const hours = Math.floor(seconds / 3600);
89
- const minutes = Math.floor((seconds - hours * 3600) / 60);
18
+ export function toHoursMinutesSeconds(
19
+ seconds: number
20
+ ): {
21
+ hours: number;
22
+ minutes: number;
23
+ seconds: number;
24
+ } {
90
25
  return {
91
- hours,
92
- minutes,
93
- seconds: seconds - hours * 3600 - minutes * 60
26
+ hours: Math.floor(seconds / 3600),
27
+ minutes: Math.floor(seconds / 60) % 60,
28
+ seconds: seconds % 60
94
29
  };
95
30
  }
96
31