@opentripplanner/core-utils 7.0.0-alpha.4 → 7.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/time.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/time.ts"],"names":["OTP_API_DATE_FORMAT","OTP_API_DATE_FORMAT_DATE_FNS","OTP_API_TIME_FORMAT","toHoursMinutesSeconds","seconds","hours","Math","floor","minutes","getTimeFormat","config","dateTime","timeFormat","getDateFormat","dateFormat","getLongDateFormat","longDateFormat","offsetTime","ms","options","offset","formatSecondsAfterMidnight","time","Date","getUserTimezone","fallbackTimezone","process","env","NODE_ENV","TZ","Intl","DateTimeFormat","resolvedOptions","timeZone","getCurrentTime","timezone","now","getCurrentDate"],"mappings":";;;;;;;;;;;;;;;;AACA;;AACA;;AAEA;AACA;AACO,MAAMA,mBAAmB,GAAG,YAA5B,C,CACP;AACA;;;AACO,MAAMC,4BAA4B,GAAG,YAArC;;AACA,MAAMC,mBAAmB,GAAG,OAA5B;AAEP;AACA;AACA;AACA;AACA;;;;AACO,SAASC,qBAAT,CACLC,OADK,EAML;AACA,SAAO;AACLC,IAAAA,KAAK,EAAEC,IAAI,CAACC,KAAL,CAAWH,OAAO,GAAG,IAArB,CADF;AAELI,IAAAA,OAAO,EAAEF,IAAI,CAACC,KAAL,CAAWH,OAAO,GAAG,EAArB,IAA2B,EAF/B;AAGLA,IAAAA,OAAO,EAAEA,OAAO,GAAG;AAHd,GAAP;AAKD;AAED;AACA;AACA;AACA;;;AACO,SAASK,aAAT,CAAuBC,MAAvB,EAA+C;AAAA;;AACpD,SAAO,CAAAA,MAAM,SAAN,IAAAA,MAAM,WAAN,gCAAAA,MAAM,CAAEC,QAAR,sEAAkBC,UAAlB,KAAgCV,mBAAvC;AACD;;AAEM,SAASW,aAAT,CAAuBH,MAAvB,EAA+C;AAAA;;AACpD,SAAO,CAAAA,MAAM,SAAN,IAAAA,MAAM,WAAN,iC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O,qBACL,+BAAeD,IAAI,CAACa,GAAL,EAAf,EAA2BD,QAA3B,CADK,EAELlC,4BAFK,CAAP;AAID","sourcesContent":["import { Config } from \"@opentripplanner/types\";\nimport { startOfDay, add, format } from \"date-fns\";\nimport { utcToZonedTime } from \"date-fns-tz\";\n\n// special constants for making sure the following date format is always sent to\n// OTP regardless of whatever the user has configured as the display format\nexport const OTP_API_DATE_FORMAT = \"YYYY-MM-DD\";\n// Date-Fns uses a different string format than moment.js\n// see https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md\nexport const OTP_API_DATE_FORMAT_DATE_FNS = \"yyyy-MM-dd\";\nexport const OTP_API_TIME_FORMAT = \"HH:mm\";\n\n/**\n * Breaks up a duration in seconds into hours, minutes, and seconds.\n * @param {number} seconds The number of seconds to break up\n * @returns an object with fields with the corresponding, hours, minutes, seconds.\n */\nexport function toHoursMinutesSeconds(\n seconds: number\n): {\n hours: number;\n minutes: number;\n seconds: number;\n} {\n return {\n hours: Math.floor(seconds / 3600),\n minutes: Math.floor(seconds / 60) % 60,\n seconds: seconds % 60\n };\n}\n\n/**\n * @param {[type]} config the OTP config object found in store\n * @return {string} the config-defined time formatter or HH:mm (24-hr time)\n */\nexport function getTimeFormat(config: Config): string {\n return config?.dateTime?.timeFormat || OTP_API_TIME_FORMAT;\n}\n\nexport function getDateFormat(config: Config): string {\n return config?.dateTime?.dateFormat || OTP_API_DATE_FORMAT;\n}\n\nexport function getLongDateFormat(config: Config): string {\n return config?.dateTime?.longDateFormat || \"D MMMM YYYY\";\n}\n/**\n * Offsets a time according to the provided time options\n * and returns the result.\n */\nexport function offsetTime(ms, options) {\n return ms + (options?.offset || 0);\n}\n\n/**\n * Formats a seconds after midnight value for display in narrative\n * @param {number} seconds time since midnight in seconds\n * @param {string} timeFormat A valid date-fns time format\n * @return {string} formatted text representation\n */\nexport function formatSecondsAfterMidnight(\n seconds: number,\n timeFormat: string\n): string {\n const time = add(startOfDay(new Date()), { seconds });\n return format(time, timeFormat);\n}\n\n/**\n * Uses Intl.DateTimeFormat() api to get the user's time zone. In a test\n * environment, pulls timezone information from an env variable. Default to\n * GMT+0 if the Intl API is unavailable.\n */\nexport function getUserTimezone(fallbackTimezone = \"Etc/Greenwich\"): string {\n if (process.env.NODE_ENV === \"test\") return process.env.TZ;\n return Intl?.DateTimeFormat().resolvedOptions().timeZone || fallbackTimezone;\n}\n\n/**\n * Formats current time for use in OTP query\n * The conversion to the user's timezone is needed for testing purposes.\n */\nexport function getCurrentTime(timezone = getUserTimezone()): string {\n return format(utcToZonedTime(Date.now(), timezone), OTP_API_TIME_FORMAT);\n}\n\n/**\n * Formats current date for use in OTP query\n * The conversion to the user's timezone is needed for testing purposes.\n */\nexport function getCurrentDate(timezone = getUserTimezone()): string {\n return format(\n utcToZonedTime(Date.now(), timezone),\n OTP_API_DATE_FORMAT_DATE_FNS\n );\n}\n"],"file":"time.js"}
1
+ {"version":3,"sources":["../src/time.ts"],"names":["OTP_API_DATE_FORMAT","OTP_API_TIME_FORMAT","toHoursMinutesSeconds","seconds","hours","Math","floor","minutes","getTimeFormat","config","dateTime","timeFormat","getDateFormat","dateFormat","getLongDateFormat","longDateFormat","offsetTime","ms","options","offset","formatSecondsAfterMidnight","time","Date","getUserTimezone","fallbackTimezone","process","env","NODE_ENV","TZ","Intl","DateTimeFormat","resolvedOptions","timeZone","getCurrentTime","timezone","now","getCurrentDate"],"mappings":";;;;;;;;;;;;;;;;AACA;;AACA;;AAEA;AACA;AACO,MAAMA,mBAAmB,GAAG,YAA5B;;AACA,MAAMC,mBAAmB,GAAG,OAA5B;AAEP;AACA;AACA;AACA;AACA;;;;AACO,SAASC,qBAAT,CACLC,OADK,EAML;AACA,SAAO;AACLC,IAAAA,KAAK,EAAEC,IAAI,CAACC,KAAL,CAAWH,OAAO,GAAG,IAArB,CADF;AAELI,IAAAA,OAAO,EAAEF,IAAI,CAACC,KAAL,CAAWH,OAAO,GAAG,EAArB,IAA2B,EAF/B;AAGLA,IAAAA,OA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AAuBC,eAAvB,GAAyCC,QAAzC,KAAqDR,gBAA5D;AACD;AAED;AACA;AACA;AACA;;;AACO,SAASS,cAAT,CAAwBC,QAAQ,GAAGX,eAAe,EAAlD,EAA8D;AACnE,SAAO,qBAAO,+BAAeD,IAAI,CAACa,GAAL,EAAf,EAA2BD,QAA3B,CAAP,EAA6CjC,mBAA7C,CAAP;AACD;AAED;AACA;AACA;AACA;;;AACO,SAASmC,cAAT,CAAwBF,QAAQ,GAAGX,eAAe,EAAlD,EAA8D;AACnE,SAAO,qBAAO,+BAAeD,IAAI,CAACa,GAAL,EAAf,EAA2BD,QAA3B,CAAP,EAA6ClC,mBAA7C,CAAP;AACD","sourcesContent":["import { Config } from \"@opentripplanner/types\";\nimport { startOfDay, add, format } from \"date-fns\";\nimport { utcToZonedTime } from \"date-fns-tz\";\n\n// Date/time formats (per date-fns) when sending/receiving date from OTP\n// regardless of whatever the user has configured as the display format.\nexport const OTP_API_DATE_FORMAT = \"yyyy-MM-dd\";\nexport const OTP_API_TIME_FORMAT = \"HH:mm\";\n\n/**\n * Breaks up a duration in seconds into hours, minutes, and seconds.\n * @param {number} seconds The number of seconds to break up\n * @returns an object with fields with the corresponding, hours, minutes, seconds.\n */\nexport function toHoursMinutesSeconds(\n seconds: number\n): {\n hours: number;\n minutes: number;\n seconds: number;\n} {\n return {\n hours: Math.floor(seconds / 3600),\n minutes: Math.floor(seconds / 60) % 60,\n seconds: seconds % 60\n };\n}\n\n/**\n * @param {[type]} config the OTP config object found in store\n * @return {string} the config-defined time formatter or HH:mm (24-hr time)\n */\nexport function getTimeFormat(config: Config): string {\n return config?.dateTime?.timeFormat || OTP_API_TIME_FORMAT;\n}\n\nexport function getDateFormat(config: Config): string {\n return config?.dateTime?.dateFormat || OTP_API_DATE_FORMAT;\n}\n\nexport function getLongDateFormat(config: Config): string {\n return config?.dateTime?.longDateFormat || \"D MMMM YYYY\";\n}\n/**\n * Offsets a time according to the provided time options\n * and returns the result.\n */\nexport function offsetTime(ms, options) {\n return ms + (options?.offset || 0);\n}\n\n/**\n * Formats a seconds after midnight value for display in narrative\n * @param {number} seconds time since midnight in seconds\n * @param {string} timeFormat A valid date-fns time format\n * @return {string} formatted text representation\n */\nexport function formatSecondsAfterMidnight(\n seconds: number,\n timeFormat: string\n): string {\n const time = add(startOfDay(new Date()), { seconds });\n return format(time, timeFormat);\n}\n\n/**\n * Uses Intl.DateTimeFormat() api to get the user's time zone. In a test\n * environment, pulls timezone information from an env variable. Default to\n * GMT+0 if the Intl API is unavailable.\n */\nexport function getUserTimezone(fallbackTimezone = \"Etc/Greenwich\"): string {\n if (process.env.NODE_ENV === \"test\") return process.env.TZ;\n return Intl?.DateTimeFormat().resolvedOptions().timeZone || fallbackTimezone;\n}\n\n/**\n * Formats current time for use in OTP query\n * The conversion to the user's timezone is needed for testing purposes.\n */\nexport function getCurrentTime(timezone = getUserTimezone()): string {\n return format(utcToZonedTime(Date.now(), timezone), OTP_API_TIME_FORMAT);\n}\n\n/**\n * Formats current date for use in OTP query\n * The conversion to the user's timezone is needed for testing purposes.\n */\nexport function getCurrentDate(timezone = getUserTimezone()): string {\n return format(utcToZonedTime(Date.now(), timezone), OTP_API_DATE_FORMAT);\n}\n"],"file":"time.js"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentripplanner/core-utils",
3
- "version": "7.0.0-alpha.4",
3
+ "version": "7.0.2",
4
4
  "description": "Core functionality that is shared among numerous UI components",
5
5
  "engines": {
6
6
  "node": ">=13"
@@ -13,7 +13,7 @@
13
13
  "private": false,
14
14
  "dependencies": {
15
15
  "@mapbox/polyline": "^1.1.0",
16
- "@opentripplanner/geocoder": "^1.2.2",
16
+ "@opentripplanner/geocoder": "^1.3.0",
17
17
  "@styled-icons/foundation": "^10.34.0",
18
18
  "@turf/along": "^6.0.1",
19
19
  "bowser": "^2.7.0",
@@ -28,6 +28,6 @@
28
28
  "tsc": "tsc"
29
29
  },
30
30
  "devDependencies": {
31
- "@opentripplanner/types": "3.0.0-alpha.4"
31
+ "@opentripplanner/types": "^4.0.0"
32
32
  }
33
33
  }
@@ -7,47 +7,57 @@ import {
7
7
 
8
8
  const bikeRentalItinerary = require("./__mocks__/bike-rental-itinerary.json");
9
9
  const tncItinerary = require("./__mocks__/tnc-itinerary.json");
10
- // const multiCurrencyItinerary = require("./__mocks__/multi-currency-itinerary.json");
11
10
 
12
11
  describe("util > itinerary", () => {
13
- it("isTransit should work", () => {
14
- expect(isTransit("CAR")).toBeFalsy();
12
+ describe("isTransit", () => {
13
+ it("should work", () => {
14
+ expect(isTransit("CAR")).toBeFalsy();
15
+ expect(isTransit("BUS")).toBeTruthy();
16
+ });
15
17
  });
16
18
 
17
- it("getCompanyFromLeg should return company for bike rental leg", () => {
18
- const company = getCompanyFromLeg(bikeRentalItinerary.legs[1]);
19
- expect(company).toEqual("GBFS");
20
- });
19
+ describe("getCompanyFromLeg", () => {
20
+ it("should return company for bike rental leg", () => {
21
+ const company = getCompanyFromLeg(bikeRentalItinerary.legs[1]);
22
+ expect(company).toEqual("GBFS");
23
+ });
21
24
 
22
- it("getCompanyFromLeg should return company for TNC leg", () => {
23
- const company = getCompanyFromLeg(tncItinerary.legs[0]);
24
- expect(company).toEqual("UBER");
25
+ it("should return company for TNC leg", () => {
26
+ const company = getCompanyFromLeg(tncItinerary.legs[0]);
27
+ expect(company).toEqual("UBER");
28
+ });
25
29
  });
26
30
 
27
- it("getTransitFare should return defaults with missing fare", () => {
28
- const { transitFare } = getTransitFare(null);
29
- // transit fare value should be zero
30
- expect(transitFare).toMatchSnapshot();
31
- });
31
+ describe("getTransitFare", () => {
32
+ it("should return defaults with missing fare", () => {
33
+ const { transitFare } = getTransitFare(null);
34
+ // transit fare value should be zero
35
+ expect(transitFare).toMatchSnapshot();
36
+ });
32
37
 
33
- it("getTransitFare should work with valid fare component", () => {
34
- const fareComponent = {
35
- currency: {
36
- currency: "USD",
37
- defaultFractionDigits: 2,
38
- currencyCode: "USD",
39
- symbol: "$"
40
- },
41
- cents: 575
42
- };
43
- const { currencyCode, transitFare } = getTransitFare(fareComponent);
44
- expect(currencyCode).toEqual(fareComponent.currency.currencyCode);
45
- // Snapshot tests
46
- expect(transitFare).toMatchSnapshot();
38
+ it("should work with valid fare component", () => {
39
+ const fareComponent = {
40
+ currency: {
41
+ currency: "USD",
42
+ defaultFractionDigits: 2,
43
+ currencyCode: "USD",
44
+ symbol: "$"
45
+ },
46
+ cents: 575
47
+ };
48
+ const { currencyCode, transitFare } = getTransitFare(fareComponent);
49
+ expect(currencyCode).toEqual(fareComponent.currency.currencyCode);
50
+ // Snapshot tests
51
+ expect(transitFare).toMatchSnapshot();
52
+ });
47
53
  });
48
54
 
49
- it("calculateFare should return the correct currency code for TNC leg", () => {
50
- const fareResult = calculateTncFares(tncItinerary, true);
51
- expect(fareResult.currencyCode).toEqual("USD");
55
+ describe("calculateTncFares", () => {
56
+ it("should return the correct amounts and currency for an itinerary with TNC", () => {
57
+ const fareResult = calculateTncFares(tncItinerary, true);
58
+ expect(fareResult.currencyCode).toEqual("USD");
59
+ expect(fareResult.maxTNCFare).toEqual(19);
60
+ expect(fareResult.minTNCFare).toEqual(17);
61
+ });
52
62
  });
53
63
  });
package/src/itinerary.ts CHANGED
@@ -13,32 +13,6 @@ import {
13
13
  } from "@opentripplanner/types";
14
14
  import turfAlong from "@turf/along";
15
15
 
16
- /*
17
- import {
18
- // calculateFares,
19
- // getLegModeLabel,
20
- // getModeForPlace,
21
- // getPlaceName,
22
- // getStepDirection,
23
- // getStepInstructions,
24
- // getStepStreetName,
25
- // getTimeZoneOffset,
26
- // getTransitFare
27
- } from "./deprecated";
28
-
29
- export {
30
- // calculateFares,
31
- // getLegModeLabel,
32
- // getModeForPlace,
33
- // getPlaceName,
34
- // getStepDirection,
35
- // getStepInstructions,
36
- // getStepStreetName,
37
- // getTimeZoneOffset,
38
- // getTransitFare
39
- };
40
- */
41
-
42
16
  // All OTP transit modes
43
17
  export const transitModes = [
44
18
  "TRAM",
@@ -455,24 +429,24 @@ export function calculatePhysicalActivity(
455
429
  * It is assumed that the same currency is used for all TNC legs.
456
430
  */
457
431
  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
- };
432
+ return itinerary.legs
433
+ .filter(leg => leg.mode === "CAR" && leg.hailedCar && leg.tncData)
434
+ .reduce(
435
+ ({ maxTNCFare, minTNCFare }, { tncData }) => {
436
+ const { currency, maxCost, minCost } = tncData;
437
+ return {
438
+ // Assumes a single currency for entire itinerary.
439
+ currencyCode: currency,
440
+ maxTNCFare: maxTNCFare + maxCost,
441
+ minTNCFare: minTNCFare + minCost
442
+ };
443
+ },
444
+ {
445
+ currencyCode: null,
446
+ maxTNCFare: 0,
447
+ minTNCFare: 0
448
+ }
449
+ );
476
450
  }
477
451
 
478
452
  /**
@@ -485,17 +459,77 @@ export function getTransitFare(
485
459
  currencyCode: string;
486
460
  transitFare: number;
487
461
  } {
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
462
+ return fareComponent
463
+ ? {
464
+ currencyCode: fareComponent.currency.currencyCode,
465
+ transitFare: fareComponent.cents
466
+ }
467
+ : {
468
+ currencyCode: "USD",
469
+ transitFare: 0
470
+ };
471
+ }
472
+
473
+ /**
474
+ * Sources:
475
+ * - https://www.itf-oecd.org/sites/default/files/docs/environmental-performance-new-mobility.pdf
476
+ * - https://www.thrustcarbon.com/insights/how-to-calculate-emissions-from-a-ferry-journey
477
+ * - https://www.itf-oecd.org/sites/default/files/life-cycle-assessment-calculations-2020.xlsx
478
+ * Other values extrapolated.
479
+ */
480
+ const CARBON_INTENSITY_DEFAULTS = {
481
+ walk: 0.026,
482
+ bicycle: 0.017,
483
+ car: 0.162,
484
+ tram: 0.066,
485
+ subway: 0.066,
486
+ rail: 0.066,
487
+ bus: 0.09,
488
+ ferry: 0.082,
489
+ cable_car: 0.021,
490
+ gondola: 0.021,
491
+ funicular: 0.066,
492
+ transit: 0.066,
493
+ leg_switch: 0,
494
+ airplane: 0.382,
495
+ micromobility: 0.095
496
+ };
497
+
498
+ /**
499
+ * @param {itinerary} itinerary OTP trip itinierary
500
+ * @param {carbonIntensity} carbonIntensity carbon intensity by mode in grams/meter
501
+ * @param {units} units units to be used in return value
502
+ * @return Amount of carbon in chosen unit
503
+ */
504
+ export function calculateEmissions(
505
+ itinerary: Itinerary,
506
+ carbonIntensity: Record<string, number> = {},
507
+ units?: string
508
+ ): number {
509
+ // Apply defaults for any values that we don't have.
510
+ const carbonIntensityWithDefaults = {
511
+ ...CARBON_INTENSITY_DEFAULTS,
512
+ ...carbonIntensity
500
513
  };
514
+
515
+ // Distance is in meters, totalCarbon is in grams
516
+ const totalCarbon =
517
+ itinerary?.legs?.reduce((total, leg) => {
518
+ return (
519
+ (leg.distance * carbonIntensityWithDefaults[leg.mode.toLowerCase()] ||
520
+ 0) + total
521
+ );
522
+ }, 0) || 0;
523
+
524
+ switch (units) {
525
+ case "ounce":
526
+ return totalCarbon / 28.35;
527
+ case "kilogram":
528
+ return totalCarbon / 1000;
529
+ case "pound":
530
+ return totalCarbon / 454;
531
+ case "gram":
532
+ default:
533
+ return totalCarbon;
534
+ }
501
535
  }
package/src/query.js CHANGED
@@ -8,14 +8,10 @@ import queryParams from "./query-params";
8
8
  import {
9
9
  getCurrentTime,
10
10
  getCurrentDate,
11
- OTP_API_TIME_FORMAT,
12
- OTP_API_DATE_FORMAT_DATE_FNS
11
+ OTP_API_DATE_FORMAT,
12
+ OTP_API_TIME_FORMAT
13
13
  } from "./time";
14
14
 
15
- // import { coordsToString, summarizeQuery } from "./deprecated";
16
-
17
- // export { summarizeQuery };
18
-
19
15
  /* The list of default parameters considered in the settings panel */
20
16
 
21
17
  export const defaultParams = [
@@ -148,7 +144,9 @@ function isParamApplicable(paramInfo, query, config) {
148
144
  * Helper method which replaces OTP flex modes with single FLEX mode that's
149
145
  * more useful and easier to work with.
150
146
  */
151
- export function reduceOtpFlexModes(modes) {
147
+ export function reduceOtpFlexModes(modes, enabled = true) {
148
+ if (!enabled) return modes;
149
+
152
150
  return modes.reduce((prev, cur) => {
153
151
  const newModes = prev;
154
152
  // Add the current mode if it is not a flex mode
@@ -191,7 +189,10 @@ export function expandOtpFlexMode(mode) {
191
189
  * default values.
192
190
  */
193
191
  export function isNotDefaultQuery(query, config) {
194
- const activeModes = reduceOtpFlexModes(query.mode.split(",").sort());
192
+ const activeModes = reduceOtpFlexModes(
193
+ query.mode.split(",").sort(),
194
+ config.modes?.mergeFlex
195
+ );
195
196
  if (
196
197
  activeModes.length !== 2 ||
197
198
  activeModes[0] !== "TRANSIT" ||
@@ -427,7 +428,7 @@ export function getRoutingParams(config, currentQuery, ignoreRealtimeUpdates) {
427
428
  }
428
429
 
429
430
  // check date/time validity; ignore both if either is invalid
430
- const dateValid = isMatch(params.date, OTP_API_DATE_FORMAT_DATE_FNS);
431
+ const dateValid = isMatch(params.date, OTP_API_DATE_FORMAT);
431
432
  const timeValid = isMatch(params.time, OTP_API_TIME_FORMAT);
432
433
 
433
434
  if (!dateValid || !timeValid) {
@@ -470,7 +471,8 @@ export function getRoutingParams(config, currentQuery, ignoreRealtimeUpdates) {
470
471
  }
471
472
 
472
473
  // Replace FLEX placeholder with OTP flex modes
473
- if (params.mode) {
474
+ // Explicit false check allows avoiding a breaking change -- undefined is true
475
+ if (params.mode && config.modes?.mergeFlex !== false) {
474
476
  // Ensure query is in reduced format to avoid replacing twice
475
477
  const reducedMode = reduceOtpFlexModes(params.mode.split(",")).join(",");
476
478
  params.mode = expandOtpFlexMode(reducedMode);
package/src/time.ts CHANGED
@@ -2,12 +2,9 @@ import { Config } from "@opentripplanner/types";
2
2
  import { startOfDay, add, format } from "date-fns";
3
3
  import { utcToZonedTime } from "date-fns-tz";
4
4
 
5
- // special constants for making sure the following date format is always sent to
6
- // OTP regardless of whatever the user has configured as the display format
7
- export const OTP_API_DATE_FORMAT = "YYYY-MM-DD";
8
- // Date-Fns uses a different string format than moment.js
9
- // see https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
10
- export const OTP_API_DATE_FORMAT_DATE_FNS = "yyyy-MM-dd";
5
+ // Date/time formats (per date-fns) when sending/receiving date from OTP
6
+ // regardless of whatever the user has configured as the display format.
7
+ export const OTP_API_DATE_FORMAT = "yyyy-MM-dd";
11
8
  export const OTP_API_TIME_FORMAT = "HH:mm";
12
9
 
13
10
  /**
@@ -89,8 +86,5 @@ export function getCurrentTime(timezone = getUserTimezone()): string {
89
86
  * The conversion to the user's timezone is needed for testing purposes.
90
87
  */
91
88
  export function getCurrentDate(timezone = getUserTimezone()): string {
92
- return format(
93
- utcToZonedTime(Date.now(), timezone),
94
- OTP_API_DATE_FORMAT_DATE_FNS
95
- );
89
+ return format(utcToZonedTime(Date.now(), timezone), OTP_API_DATE_FORMAT);
96
90
  }
package/tsconfig.json CHANGED
@@ -6,10 +6,5 @@
6
6
  "rootDir": "./src",
7
7
  "skipLibCheck": true
8
8
  },
9
- "include": ["src/**/*"],
10
- "references": [
11
- {
12
- "path": "../types"
13
- }
14
- ]
9
+ "include": ["src/**/*"]
15
10
  }