@opentripplanner/core-utils 4.11.6-alpha.1 → 5.0.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 (70) hide show
  1. package/esm/deprecated-with-types.js +47 -0
  2. package/esm/deprecated-with-types.js.map +1 -0
  3. package/esm/index.js +0 -4
  4. package/esm/index.js.map +1 -1
  5. package/esm/itinerary.js +1 -1
  6. package/esm/itinerary.js.map +1 -1
  7. package/esm/map.js +16 -8
  8. package/esm/map.js.map +1 -1
  9. package/esm/route.js +5 -3
  10. package/esm/route.js.map +1 -1
  11. package/esm/storage.js +1 -0
  12. package/esm/storage.js.map +1 -1
  13. package/esm/time.js +6 -36
  14. package/esm/time.js.map +1 -1
  15. package/esm/ui.js +1 -1
  16. package/esm/ui.js.map +1 -1
  17. package/lib/deprecated-with-types.d.ts +23 -0
  18. package/lib/deprecated-with-types.d.ts.map +1 -0
  19. package/lib/deprecated-with-types.js +61 -0
  20. package/lib/deprecated-with-types.js.map +1 -0
  21. package/lib/index.d.ts +19 -0
  22. package/lib/index.d.ts.map +1 -0
  23. package/lib/index.js +0 -6
  24. package/lib/index.js.map +1 -1
  25. package/lib/itinerary.d.ts +113 -0
  26. package/lib/itinerary.d.ts.map +1 -0
  27. package/lib/itinerary.js +2 -1
  28. package/lib/itinerary.js.map +1 -1
  29. package/lib/map.d.ts +30 -0
  30. package/lib/map.d.ts.map +1 -0
  31. package/lib/map.js +15 -7
  32. package/lib/map.js.map +1 -1
  33. package/lib/route.d.ts +98 -0
  34. package/lib/route.d.ts.map +1 -0
  35. package/lib/route.js +5 -3
  36. package/lib/route.js.map +1 -1
  37. package/lib/storage.d.ts +19 -0
  38. package/lib/storage.d.ts.map +1 -0
  39. package/lib/storage.js +2 -0
  40. package/lib/storage.js.map +1 -1
  41. package/lib/time.d.ts +65 -0
  42. package/lib/time.d.ts.map +1 -0
  43. package/lib/time.js +22 -39
  44. package/lib/time.js.map +1 -1
  45. package/lib/ui.d.ts +13 -0
  46. package/lib/ui.d.ts.map +1 -0
  47. package/lib/ui.js +1 -1
  48. package/lib/ui.js.map +1 -1
  49. package/package.json +4 -1
  50. package/src/__tests__/__snapshots__/route.js.snap +30 -30
  51. package/src/deprecated-with-types.ts +62 -0
  52. package/src/{index.js → index.ts} +0 -4
  53. package/src/{itinerary.js → itinerary.ts} +70 -36
  54. package/src/{map.js → map.ts} +51 -28
  55. package/src/{route.js → route.ts} +52 -28
  56. package/src/{storage.js → storage.ts} +6 -5
  57. package/src/{time.js → time.ts} +28 -46
  58. package/src/{ui.js → ui.ts} +8 -8
  59. package/tsconfig.json +15 -0
  60. package/tsconfig.tsbuildinfo +4921 -0
  61. package/esm/messages.js +0 -25
  62. package/esm/messages.js.map +0 -1
  63. package/esm/types.js +0 -560
  64. package/esm/types.js.map +0 -1
  65. package/lib/messages.js +0 -29
  66. package/lib/messages.js.map +0 -1
  67. package/lib/types.js +0 -661
  68. package/lib/types.js.map +0 -1
  69. package/src/messages.js +0 -20
  70. package/src/types.js +0 -605
@@ -1,10 +1,11 @@
1
+ /* eslint-disable no-console */
1
2
  // Prefix to use with local storage keys.
2
3
  const STORAGE_PREFIX = "otp";
3
4
 
4
5
  /**
5
6
  * Store a javascript object at the specified key.
6
7
  */
7
- export function storeItem(key, object) {
8
+ export function storeItem(key: string, object: unknown): void {
8
9
  window.localStorage.setItem(
9
10
  `${STORAGE_PREFIX}.${key}`,
10
11
  JSON.stringify(object)
@@ -15,8 +16,8 @@ export function storeItem(key, object) {
15
16
  * Retrieve a javascript object at the specified key. If not found, defaults to
16
17
  * null or, the optionally provided notFoundValue.
17
18
  */
18
- export function getItem(key, notFoundValue = null) {
19
- let itemAsString;
19
+ export function getItem(key: string, notFoundValue: unknown = null): unknown {
20
+ let itemAsString: string;
20
21
  try {
21
22
  itemAsString = window.localStorage.getItem(`${STORAGE_PREFIX}.${key}`);
22
23
  const json = JSON.parse(itemAsString);
@@ -32,7 +33,7 @@ export function getItem(key, notFoundValue = null) {
32
33
  /**
33
34
  * Remove item at specified key.
34
35
  */
35
- export function removeItem(key) {
36
+ export function removeItem(key: string): void {
36
37
  window.localStorage.removeItem(`${STORAGE_PREFIX}.${key}`);
37
38
  }
38
39
 
@@ -40,7 +41,7 @@ export function removeItem(key) {
40
41
  * Generate a random ID. This might not quite be a UUID, but it serves our
41
42
  * purposes for now.
42
43
  */
43
- export function randId() {
44
+ export function randId(): string {
44
45
  return Math.random()
45
46
  .toString(36)
46
47
  .substr(2, 9);
@@ -1,3 +1,4 @@
1
+ import { Config } from "@opentripplanner/types";
1
2
  import {
2
3
  startOfDay,
3
4
  add,
@@ -6,6 +7,15 @@ import {
6
7
  } from "date-fns";
7
8
  import { utcToZonedTime } from "date-fns-tz";
8
9
 
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
+
9
19
  // special constants for making sure the following date format is always sent to
10
20
  // OTP regardless of whatever the user has configured as the display format
11
21
  export const OTP_API_DATE_FORMAT = "YYYY-MM-DD";
@@ -23,11 +33,14 @@ export const OTP_API_TIME_FORMAT = "HH:mm";
23
33
  * Otherwise, uses date-fns default
24
34
  * @returns Formatted duration
25
35
  */
26
- function formatDurationLikeMoment(
27
- seconds,
28
- showSeconds,
29
- localize = { enabled: true, code: "en-US" }
30
- ) {
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 {
31
44
  // date-fns doesn't do this automatically
32
45
  if ((!showSeconds && seconds < 60) || seconds === 0) {
33
46
  return "0 min";
@@ -85,38 +98,17 @@ export function toHoursMinutesSeconds(seconds) {
85
98
  * @param {[type]} config the OTP config object found in store
86
99
  * @return {string} the config-defined time formatter or HH:mm (24-hr time)
87
100
  */
88
- export function getTimeFormat(config) {
101
+ export function getTimeFormat(config: Config): string {
89
102
  return config?.dateTime?.timeFormat || OTP_API_TIME_FORMAT;
90
103
  }
91
104
 
92
- export function getDateFormat(config) {
105
+ export function getDateFormat(config: Config): string {
93
106
  return config?.dateTime?.dateFormat || OTP_API_DATE_FORMAT;
94
107
  }
95
108
 
96
- export function getLongDateFormat(config) {
109
+ export function getLongDateFormat(config: Config): string {
97
110
  return config?.dateTime?.longDateFormat || "D MMMM YYYY";
98
111
  }
99
-
100
- /**
101
- * Formats an elapsed time duration for display in narrative.
102
- * TODO: internationalization
103
- * @param {number} seconds duration in seconds
104
- * @returns {string} formatted text representation
105
- */
106
- export function formatDuration(seconds) {
107
- return formatDurationLikeMoment(seconds, false);
108
- }
109
-
110
- /**
111
- * Formats an elapsed time in seconds, minutes, hours duration for display in narrative
112
- * @param {number} seconds duration in seconds
113
- * @param {object} region an object that allows internationalization of the time
114
- * @returns {string} formatted text representation
115
- */
116
- export function formatDurationWithSeconds(seconds, region) {
117
- return formatDurationLikeMoment(seconds, { enabled: true, code: region });
118
- }
119
-
120
112
  /**
121
113
  * Offsets a time according to the provided time options
122
114
  * and returns the result.
@@ -125,26 +117,16 @@ export function offsetTime(ms, options) {
125
117
  return ms + (options?.offset || 0);
126
118
  }
127
119
 
128
- /**
129
- * Formats a time value for display in narrative
130
- * TODO: internationalization/timezone
131
- * @param {number} ms epoch time value in milliseconds
132
- * @returns {string} formatted text representation
133
- */
134
- export function formatTime(ms, options) {
135
- return format(
136
- offsetTime(ms, options),
137
- options?.format || OTP_API_TIME_FORMAT
138
- );
139
- }
140
-
141
120
  /**
142
121
  * Formats a seconds after midnight value for display in narrative
143
122
  * @param {number} seconds time since midnight in seconds
144
123
  * @param {string} timeFormat A valid date-fns time format
145
124
  * @return {string} formatted text representation
146
125
  */
147
- export function formatSecondsAfterMidnight(seconds, timeFormat) {
126
+ export function formatSecondsAfterMidnight(
127
+ seconds: number,
128
+ timeFormat: string
129
+ ): string {
148
130
  const time = add(startOfDay(new Date()), { seconds });
149
131
  return format(time, timeFormat);
150
132
  }
@@ -154,7 +136,7 @@ export function formatSecondsAfterMidnight(seconds, timeFormat) {
154
136
  * environment, pulls timezone information from an env variable. Default to
155
137
  * GMT+0 if the Intl API is unavailable.
156
138
  */
157
- export function getUserTimezone(fallbackTimezone = "Etc/Greenwich") {
139
+ export function getUserTimezone(fallbackTimezone = "Etc/Greenwich"): string {
158
140
  if (process.env.NODE_ENV === "test") return process.env.TZ;
159
141
  return Intl?.DateTimeFormat().resolvedOptions().timeZone || fallbackTimezone;
160
142
  }
@@ -163,7 +145,7 @@ export function getUserTimezone(fallbackTimezone = "Etc/Greenwich") {
163
145
  * Formats current time for use in OTP query
164
146
  * The conversion to the user's timezone is needed for testing purposes.
165
147
  */
166
- export function getCurrentTime(timezone = getUserTimezone()) {
148
+ export function getCurrentTime(timezone = getUserTimezone()): string {
167
149
  return format(utcToZonedTime(Date.now(), timezone), OTP_API_TIME_FORMAT);
168
150
  }
169
151
 
@@ -171,7 +153,7 @@ export function getCurrentTime(timezone = getUserTimezone()) {
171
153
  * Formats current date for use in OTP query
172
154
  * The conversion to the user's timezone is needed for testing purposes.
173
155
  */
174
- export function getCurrentDate(timezone = getUserTimezone()) {
156
+ export function getCurrentDate(timezone = getUserTimezone()): string {
175
157
  return format(
176
158
  utcToZonedTime(Date.now(), timezone),
177
159
  OTP_API_DATE_FORMAT_DATE_FNS
@@ -1,6 +1,6 @@
1
1
  import bowser from "bowser";
2
2
 
3
- export function isMobile() {
3
+ export function isMobile(): boolean {
4
4
  // TODO: consider using 3rd-party library?
5
5
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
6
6
  navigator.userAgent
@@ -10,8 +10,8 @@ export function isMobile() {
10
10
  /**
11
11
  * Returns true if the user is using a [redacted] browser
12
12
  */
13
- export function isIE() {
14
- return bowser.name === "Internet Explorer";
13
+ export function isIE(): boolean {
14
+ return bowser.parse(navigator.userAgent).browser === "Internet Explorer";
15
15
  }
16
16
 
17
17
  /**
@@ -20,16 +20,16 @@ export function isIE() {
20
20
  * and intended to fix issues with iOS elastic scrolling, e.g.,
21
21
  * https://github.com/conveyal/trimet-mod-otp/issues/92.
22
22
  */
23
- export function enableScrollForSelector(selector) {
23
+ export function enableScrollForSelector(selector: string): void {
24
24
  const overlay = document.querySelector(selector);
25
25
  let clientY = null; // remember Y position on touch start
26
26
 
27
- function isOverlayTotallyScrolled() {
27
+ function isOverlayTotallyScrolled(): boolean {
28
28
  // https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#Problems_and_solutions
29
29
  return overlay.scrollHeight - overlay.scrollTop <= overlay.clientHeight;
30
30
  }
31
31
 
32
- function disableRubberBand(event) {
32
+ function disableRubberBand(event: TouchEvent) {
33
33
  const clientYDelta = event.targetTouches[0].clientY - clientY;
34
34
 
35
35
  if (overlay.scrollTop === 0 && clientYDelta > 0) {
@@ -45,7 +45,7 @@ export function enableScrollForSelector(selector) {
45
45
 
46
46
  overlay.addEventListener(
47
47
  "touchstart",
48
- function(event) {
48
+ function(event: TouchEvent) {
49
49
  if (event.targetTouches.length === 1) {
50
50
  // detect single touch
51
51
  clientY = event.targetTouches[0].clientY;
@@ -56,7 +56,7 @@ export function enableScrollForSelector(selector) {
56
56
 
57
57
  overlay.addEventListener(
58
58
  "touchmove",
59
- function(event) {
59
+ function(event: TouchEvent) {
60
60
  if (event.targetTouches.length === 1) {
61
61
  // detect single touch
62
62
  disableRubberBand(event);
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "composite": true,
5
+ "outDir": "./lib",
6
+ "rootDir": "./src",
7
+ "skipLibCheck": true
8
+ },
9
+ "include": ["src/**/*"],
10
+ "references": [
11
+ {
12
+ "path": "../types"
13
+ }
14
+ ]
15
+ }