Skip to content

Commit f0a231b

Browse files
Merge pull request #367 from opentripplanner/typescript-core-utils
Add Typescript to `core-utils`
2 parents 11d6ebc + 712b601 commit f0a231b

File tree

75 files changed

+4611
-4415
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+4611
-4415
lines changed

Diff for: __snapshots__/storybook.test.ts.snap

+3,848-3,338
Large diffs are not rendered by default.

Diff for: packages/base-map/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"module": "esm/index.js",
1010
"private": false,
1111
"dependencies": {
12-
"@opentripplanner/core-utils": "^4.5.0",
1312
"prop-types": "^15.7.2"
1413
},
1514
"peerDependencies": {

Diff for: packages/base-map/src/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { Component } from "react";
22
import PropTypes from "prop-types";
33
import { LayersControl, Map, Popup, TileLayer } from "react-leaflet";
4-
import utils from "@opentripplanner/core-utils";
54
import L from "leaflet";
65

76
import * as Styled from "./styled";
@@ -332,7 +331,10 @@ BaseMap.propTypes = {
332331
/**
333332
* The center of the map, as a [lat, lng] array.
334333
*/
335-
center: utils.types.latlngType.isRequired,
334+
// center: utils.types.latlngType.isRequired,
335+
// Typescript TODO: restore correct type
336+
// eslint-disable-next-line react/forbid-prop-types
337+
center: PropTypes.array.isRequired,
336338
/**
337339
* The maximum zoom level allowed on the map.
338340
*/
@@ -379,7 +381,10 @@ BaseMap.propTypes = {
379381
*/
380382
popup: PropTypes.shape({
381383
contents: PropTypes.node.isRequired,
382-
location: utils.types.latlngType.isRequired
384+
// Typescript TODO: restore correct type
385+
// eslint-disable-next-line react/forbid-prop-types
386+
location: PropTypes.object.isRequired
387+
// location: utils.types.latlngType.isRequired
383388
}),
384389
/**
385390
* The zoom level of the map.

Diff for: packages/core-utils/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@
2626
"qs": "^6.9.1"
2727
},
2828
"gitHead": "0af1b7cda60bd4252b219dcf893e01c2acb2ed5d",
29+
"scripts": {
30+
"tsc": "tsc"
31+
},
2932
"devDependencies": {}
3033
}

Diff for: packages/core-utils/src/__tests__/__snapshots__/route.js.snap

+30-30
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ Array [
3535

3636
exports[`util > route routeComparator should prioritize routes with valid sortOrder 1`] = `
3737
Array [
38-
Object {
39-
"longName": "Around town",
40-
"mode": "BUS",
41-
"shortName": "20",
42-
"sortOrder": 2,
43-
},
4438
Object {
4539
"longName": "Around another town",
4640
"shortName": "3",
4741
"sortOrder": -999,
4842
"type": 3,
4943
},
44+
Object {
45+
"longName": "Around town",
46+
"mode": "BUS",
47+
"shortName": "20",
48+
"sortOrder": 2,
49+
},
5050
]
5151
`;
5252

@@ -191,6 +191,30 @@ Array [
191191
"shortName": "IC",
192192
"sortOrder": 2,
193193
},
194+
Object {
195+
"longName": "A-line",
196+
"mode": "BUS",
197+
"shortName": "A",
198+
"sortOrder": -999,
199+
},
200+
Object {
201+
"longName": "B-line",
202+
"mode": "BUS",
203+
"shortName": "B",
204+
"sortOrder": -999,
205+
},
206+
Object {
207+
"longName": "Loop route",
208+
"mode": "BUS",
209+
"shortName": "2",
210+
"sortOrder": -999,
211+
},
212+
Object {
213+
"longName": "Around another town",
214+
"shortName": "3",
215+
"sortOrder": -999,
216+
"type": 3,
217+
},
194218
Object {
195219
"longName": "Local route",
196220
"mode": "BUS",
@@ -239,30 +263,6 @@ Array [
239263
"shortName": "10",
240264
"sortOrder": 10,
241265
},
242-
Object {
243-
"longName": "A-line",
244-
"mode": "BUS",
245-
"shortName": "A",
246-
"sortOrder": -999,
247-
},
248-
Object {
249-
"longName": "B-line",
250-
"mode": "BUS",
251-
"shortName": "B",
252-
"sortOrder": -999,
253-
},
254-
Object {
255-
"longName": "Loop route",
256-
"mode": "BUS",
257-
"shortName": "2",
258-
"sortOrder": -999,
259-
},
260-
Object {
261-
"longName": "Around another town",
262-
"shortName": "3",
263-
"sortOrder": -999,
264-
"type": 3,
265-
},
266266
Object {
267267
"longName": "A meandering route",
268268
"mode": "BUS",

Diff for: packages/core-utils/src/deprecated-with-types.ts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

Diff for: packages/core-utils/src/index.js renamed to packages/core-utils/src/index.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
import * as itinerary from "./itinerary";
22
import * as map from "./map";
3-
import * as messages from "./messages";
43
import * as profile from "./profile";
54
import * as query from "./query";
65
import * as queryParams from "./query-params";
76
import * as route from "./route";
87
import * as storage from "./storage";
98
import * as time from "./time";
10-
import * as types from "./types";
119
import * as ui from "./ui";
1210

1311
const core = {
1412
itinerary,
1513
map,
16-
messages,
1714
profile,
1815
query,
1916
queryParams,
2017
route,
2118
storage,
2219
time,
23-
types,
2420
ui
2521
};
2622

0 commit comments

Comments
 (0)