@opentripplanner/core-utils 4.11.5 → 5.0.1-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.
- package/esm/deprecated-with-types.js +47 -0
- package/esm/deprecated-with-types.js.map +1 -0
- package/esm/deprecated.js +7 -0
- package/esm/deprecated.js.map +1 -1
- package/esm/index.js +0 -4
- package/esm/index.js.map +1 -1
- package/esm/itinerary.js +3 -9
- package/esm/itinerary.js.map +1 -1
- package/esm/map.js +9 -2
- package/esm/map.js.map +1 -1
- package/esm/route.js +5 -3
- package/esm/route.js.map +1 -1
- package/esm/storage.js +1 -0
- package/esm/storage.js.map +1 -1
- package/esm/time.js +6 -36
- package/esm/time.js.map +1 -1
- package/esm/ui.js +1 -1
- package/esm/ui.js.map +1 -1
- package/lib/deprecated-with-types.d.ts +23 -0
- package/lib/deprecated-with-types.d.ts.map +1 -0
- package/lib/deprecated-with-types.js +61 -0
- package/lib/deprecated-with-types.js.map +1 -0
- package/lib/deprecated.js +9 -0
- package/lib/deprecated.js.map +1 -1
- package/lib/index.d.ts +19 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +0 -6
- package/lib/index.js.map +1 -1
- package/lib/itinerary.d.ts +113 -0
- package/lib/itinerary.d.ts.map +1 -0
- package/lib/itinerary.js +8 -9
- package/lib/itinerary.js.map +1 -1
- package/lib/map.d.ts +30 -0
- package/lib/map.d.ts.map +1 -0
- package/lib/map.js +8 -1
- package/lib/map.js.map +1 -1
- package/lib/route.d.ts +98 -0
- package/lib/route.d.ts.map +1 -0
- package/lib/route.js +5 -3
- package/lib/route.js.map +1 -1
- package/lib/storage.d.ts +19 -0
- package/lib/storage.d.ts.map +1 -0
- package/lib/storage.js +2 -0
- package/lib/storage.js.map +1 -1
- package/lib/time.d.ts +65 -0
- package/lib/time.d.ts.map +1 -0
- package/lib/time.js +22 -39
- package/lib/time.js.map +1 -1
- package/lib/ui.d.ts +13 -0
- package/lib/ui.d.ts.map +1 -0
- package/lib/ui.js +1 -1
- package/lib/ui.js.map +1 -1
- package/package.json +4 -1
- package/src/__tests__/__snapshots__/route.js.snap +30 -30
- package/src/deprecated-with-types.ts +62 -0
- package/src/deprecated.js +16 -0
- package/src/{index.js → index.ts} +0 -4
- package/src/{itinerary.js → itinerary.ts} +72 -50
- package/src/{map.js → map.ts} +48 -21
- package/src/{route.js → route.ts} +52 -28
- package/src/{storage.js → storage.ts} +6 -5
- package/src/{time.js → time.ts} +28 -46
- package/src/{ui.js → ui.ts} +8 -8
- package/tsconfig.json +15 -0
- package/tsconfig.tsbuildinfo +4921 -0
- package/esm/messages.js +0 -25
- package/esm/messages.js.map +0 -1
- package/esm/types.js +0 -560
- package/esm/types.js.map +0 -1
- package/lib/messages.js +0 -29
- package/lib/messages.js.map +0 -1
- package/lib/types.js +0 -661
- package/lib/types.js.map +0 -1
- package/src/messages.js +0 -20
- package/src/types.js +0 -605
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Leg, Route, TransitOperator } from "@opentripplanner/types";
|
|
1
2
|
/**
|
|
2
3
|
* Returns the transit operator (if an exact match is found) from the transit
|
|
3
4
|
* operators config value. It is critical to use both the feedId and agencyId in
|
|
@@ -11,10 +12,10 @@
|
|
|
11
12
|
* was found
|
|
12
13
|
*/
|
|
13
14
|
export function getTransitOperatorFromFeedIdAndAgencyId(
|
|
14
|
-
feedId,
|
|
15
|
-
agencyId,
|
|
16
|
-
transitOperators
|
|
17
|
-
) {
|
|
15
|
+
feedId: string,
|
|
16
|
+
agencyId: string | number,
|
|
17
|
+
transitOperators: TransitOperator[]
|
|
18
|
+
): TransitOperator {
|
|
18
19
|
return (
|
|
19
20
|
transitOperators.find(
|
|
20
21
|
transitOperator =>
|
|
@@ -32,7 +33,10 @@ export function getTransitOperatorFromFeedIdAndAgencyId(
|
|
|
32
33
|
* @param {object} transitOperators transitOperators from config.
|
|
33
34
|
* @return {object} the operator if one was found or null if no match was found
|
|
34
35
|
*/
|
|
35
|
-
export function getTransitOperatorFromLeg(
|
|
36
|
+
export function getTransitOperatorFromLeg(
|
|
37
|
+
leg: Leg,
|
|
38
|
+
transitOperators: TransitOperator[]
|
|
39
|
+
): TransitOperator {
|
|
36
40
|
if (!leg.routeId || !leg.agencyId) return null;
|
|
37
41
|
const feedId = leg.routeId.split(":")[0];
|
|
38
42
|
return getTransitOperatorFromFeedIdAndAgencyId(
|
|
@@ -52,10 +56,13 @@ export function getTransitOperatorFromLeg(leg, transitOperators) {
|
|
|
52
56
|
* @param {array} transitOperators transitOperators from config
|
|
53
57
|
* @return {object} the operator if one was found or null if no match was found
|
|
54
58
|
*/
|
|
55
|
-
export function getTransitOperatorFromOtpRoute(
|
|
59
|
+
export function getTransitOperatorFromOtpRoute(
|
|
60
|
+
route: Route,
|
|
61
|
+
transitOperators: TransitOperator[]
|
|
62
|
+
): TransitOperator {
|
|
56
63
|
if (!route.id) return null;
|
|
57
64
|
const feedId = route.id.split(":")[0];
|
|
58
|
-
let agencyId;
|
|
65
|
+
let agencyId: string | number;
|
|
59
66
|
if (route.agency) {
|
|
60
67
|
// This is returned in the OTP Route model
|
|
61
68
|
agencyId = route.agency.id;
|
|
@@ -97,7 +104,10 @@ const END_OF_LIST_COMPARATOR_VALUE = 999999999999;
|
|
|
97
104
|
* the transitOperators value is not defined. Otherwise an integer will be
|
|
98
105
|
* returned.
|
|
99
106
|
*/
|
|
100
|
-
function getTransitOperatorComparatorValue(
|
|
107
|
+
function getTransitOperatorComparatorValue(
|
|
108
|
+
route: Route,
|
|
109
|
+
transitOperators: TransitOperator[]
|
|
110
|
+
): number | string {
|
|
101
111
|
// if the transitOperators is undefined or has zero length, use the route's
|
|
102
112
|
// agency name as the comparator value
|
|
103
113
|
if (!transitOperators || transitOperators.length === 0) {
|
|
@@ -129,8 +139,8 @@ function getTransitOperatorComparatorValue(route, transitOperators) {
|
|
|
129
139
|
* Calculates the sort comparator value given two routes based off of the
|
|
130
140
|
* route's agency and provided transitOperators config data.
|
|
131
141
|
*/
|
|
132
|
-
function makeTransitOperatorComparator(transitOperators) {
|
|
133
|
-
return (a, b) => {
|
|
142
|
+
function makeTransitOperatorComparator(transitOperators: TransitOperator[]) {
|
|
143
|
+
return (a: Route, b: Route) => {
|
|
134
144
|
const aVal = getTransitOperatorComparatorValue(a, transitOperators);
|
|
135
145
|
const bVal = getTransitOperatorComparatorValue(b, transitOperators);
|
|
136
146
|
if (typeof aVal === "string") {
|
|
@@ -140,8 +150,7 @@ function makeTransitOperatorComparator(transitOperators) {
|
|
|
140
150
|
if (aVal > bVal) return 1;
|
|
141
151
|
return 0;
|
|
142
152
|
}
|
|
143
|
-
|
|
144
|
-
// transitOperators are defined and therefore a numeric value is guaranteed
|
|
153
|
+
// @ts-expect-error transitOperators are defined and therefore a numeric value is guaranteed
|
|
145
154
|
// to be returned
|
|
146
155
|
return aVal - bVal;
|
|
147
156
|
};
|
|
@@ -151,9 +160,13 @@ function makeTransitOperatorComparator(transitOperators) {
|
|
|
151
160
|
* Gets the desired sort values according to an optional getter function. If the
|
|
152
161
|
* getter function is not defined, the original sort values are returned.
|
|
153
162
|
*/
|
|
154
|
-
function getSortValues(
|
|
155
|
-
|
|
156
|
-
|
|
163
|
+
function getSortValues(
|
|
164
|
+
getterFn: (item: unknown) => unknown,
|
|
165
|
+
a: unknown,
|
|
166
|
+
b: unknown
|
|
167
|
+
) {
|
|
168
|
+
let aVal: unknown;
|
|
169
|
+
let bVal: unknown;
|
|
157
170
|
if (typeof getterFn === "function") {
|
|
158
171
|
aVal = getterFn(a);
|
|
159
172
|
bVal = getterFn(b);
|
|
@@ -199,11 +212,11 @@ const routeTypeComparatorValue = {
|
|
|
199
212
|
// Gets a comparator value for a given route's type (OTP mode).
|
|
200
213
|
// Note: JSDoc format not used to avoid bug in documentationjs.
|
|
201
214
|
// ttps://github.com/documentationjs/documentation/issues/372
|
|
202
|
-
function getRouteTypeComparatorValue(route) {
|
|
215
|
+
function getRouteTypeComparatorValue(route: Route): number {
|
|
203
216
|
// For some strange reason, the short route response in OTP returns the
|
|
204
217
|
// string-based modes, but the long route response returns the
|
|
205
218
|
// integer route type. This attempts to account for both of those cases.
|
|
206
|
-
if (!route) throw new Error(
|
|
219
|
+
if (!route) throw new Error(`Route is undefined. ${route}`);
|
|
207
220
|
if (typeof modeComparatorValue[route.mode] !== "undefined") {
|
|
208
221
|
return modeComparatorValue[route.mode];
|
|
209
222
|
}
|
|
@@ -212,6 +225,7 @@ function getRouteTypeComparatorValue(route) {
|
|
|
212
225
|
}
|
|
213
226
|
// Default the comparator value to a large number (placing the route at the
|
|
214
227
|
// end of the list).
|
|
228
|
+
// eslint-disable-next-line no-console
|
|
215
229
|
console.warn("no mode/route type found for route", route);
|
|
216
230
|
return END_OF_LIST_COMPARATOR_VALUE;
|
|
217
231
|
}
|
|
@@ -220,7 +234,7 @@ function getRouteTypeComparatorValue(route) {
|
|
|
220
234
|
* Calculates the sort comparator value given two routes based off of route type
|
|
221
235
|
* (OTP mode).
|
|
222
236
|
*/
|
|
223
|
-
function routeTypeComparator(a, b) {
|
|
237
|
+
function routeTypeComparator(a: Route, b: Route): number {
|
|
224
238
|
return getRouteTypeComparatorValue(a) - getRouteTypeComparatorValue(b);
|
|
225
239
|
}
|
|
226
240
|
|
|
@@ -228,7 +242,7 @@ function routeTypeComparator(a, b) {
|
|
|
228
242
|
* Determines whether a value is a string that starts with an alphabetic
|
|
229
243
|
* ascii character.
|
|
230
244
|
*/
|
|
231
|
-
function startsWithAlphabeticCharacter(val) {
|
|
245
|
+
function startsWithAlphabeticCharacter(val: unknown): boolean {
|
|
232
246
|
if (typeof val === "string" && val.length > 0) {
|
|
233
247
|
const firstCharCode = val.charCodeAt(0);
|
|
234
248
|
return (
|
|
@@ -244,7 +258,7 @@ function startsWithAlphabeticCharacter(val) {
|
|
|
244
258
|
* character. Routes with shortn that do start with an alphabetic character will
|
|
245
259
|
* be prioritized over those that don't.
|
|
246
260
|
*/
|
|
247
|
-
function alphabeticShortNameComparator(a, b) {
|
|
261
|
+
function alphabeticShortNameComparator(a: Route, b: Route): number {
|
|
248
262
|
const aStartsWithAlphabeticCharacter = startsWithAlphabeticCharacter(
|
|
249
263
|
a.shortName
|
|
250
264
|
);
|
|
@@ -284,11 +298,15 @@ function alphabeticShortNameComparator(a, b) {
|
|
|
284
298
|
* @param {function} [objGetterFn] An optional function to obtain the
|
|
285
299
|
* comparison value from the comparator function arguments
|
|
286
300
|
*/
|
|
287
|
-
export function makeNumericValueComparator(
|
|
301
|
+
export function makeNumericValueComparator(
|
|
302
|
+
objGetterFn?: (item: Route) => number
|
|
303
|
+
) {
|
|
288
304
|
/* Note: Using the global version of isNaN (the Number version behaves differently. */
|
|
289
305
|
/* eslint-disable no-restricted-globals */
|
|
290
|
-
return (a, b) => {
|
|
306
|
+
return (a: number, b: number): number => {
|
|
291
307
|
const { aVal, bVal } = getSortValues(objGetterFn, a, b);
|
|
308
|
+
if (typeof aVal !== "number" || typeof bVal !== "number") return 0;
|
|
309
|
+
|
|
292
310
|
// if both values aren't valid numbers, use the next sort criteria
|
|
293
311
|
if (isNaN(aVal) && isNaN(bVal)) return 0;
|
|
294
312
|
// b is a valid number, b gets priority
|
|
@@ -310,8 +328,10 @@ export function makeNumericValueComparator(objGetterFn) {
|
|
|
310
328
|
* @param {function} [objGetterFn] An optional function to obtain the
|
|
311
329
|
* comparison value from the comparator function arguments
|
|
312
330
|
*/
|
|
313
|
-
export function makeStringValueComparator(
|
|
314
|
-
|
|
331
|
+
export function makeStringValueComparator(
|
|
332
|
+
objGetterFn?: (item: Route) => string
|
|
333
|
+
) {
|
|
334
|
+
return (a: string, b: string): number => {
|
|
315
335
|
const { aVal, bVal } = getSortValues(objGetterFn, a, b);
|
|
316
336
|
// both a and b are uncomparable strings, return equivalent value
|
|
317
337
|
if (!aVal && !bVal) return 0;
|
|
@@ -334,7 +354,7 @@ export function makeStringValueComparator(objGetterFn) {
|
|
|
334
354
|
* See https://github.com/opentripplanner/OpenTripPlanner/issues/2938
|
|
335
355
|
* Also see https://github.com/opentripplanner/otp-react-redux/issues/122
|
|
336
356
|
*/
|
|
337
|
-
function getRouteSortOrderValue(val) {
|
|
357
|
+
function getRouteSortOrderValue(val: number): number {
|
|
338
358
|
return val === -999 ? undefined : val;
|
|
339
359
|
}
|
|
340
360
|
|
|
@@ -345,8 +365,10 @@ function getRouteSortOrderValue(val) {
|
|
|
345
365
|
* returned. If all comparison functions return equivalence, then the values
|
|
346
366
|
* are assumed to be equivalent.
|
|
347
367
|
*/
|
|
348
|
-
function makeMultiCriteriaSort(
|
|
349
|
-
|
|
368
|
+
function makeMultiCriteriaSort(
|
|
369
|
+
...criteria: ((a: unknown, b: unknown) => number)[]
|
|
370
|
+
) {
|
|
371
|
+
return (a: number, b: number): number => {
|
|
350
372
|
for (let i = 0; i < criteria.length; i++) {
|
|
351
373
|
const curCriteriaComparatorValue = criteria[i](a, b);
|
|
352
374
|
// if the comparison objects are not equivalent, return the value obtained
|
|
@@ -390,7 +412,9 @@ function makeMultiCriteriaSort(...criteria) {
|
|
|
390
412
|
* those with shortNames.
|
|
391
413
|
* 7. longName as string.
|
|
392
414
|
*/
|
|
393
|
-
export function makeRouteComparator(
|
|
415
|
+
export function makeRouteComparator(
|
|
416
|
+
transitOperators: TransitOperator[]
|
|
417
|
+
): (a: number, b: number) => number {
|
|
394
418
|
return makeMultiCriteriaSort(
|
|
395
419
|
makeTransitOperatorComparator(transitOperators),
|
|
396
420
|
makeNumericValueComparator(obj => getRouteSortOrderValue(obj.sortOrder)),
|
|
@@ -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);
|
package/src/{time.js → time.ts}
RENAMED
|
@@ -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
|
|
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(
|
|
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
|
package/src/{ui.js → ui.ts}
RENAMED
|
@@ -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.
|
|
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