@opentripplanner/core-utils 12.0.2 → 13.0.0-alpha.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.
Files changed (73) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/esm/graphql.d.js.map +1 -1
  4. package/esm/index.js.map +1 -1
  5. package/esm/itinerary.js +78 -114
  6. package/esm/itinerary.js.map +1 -1
  7. package/esm/map.js +3 -3
  8. package/esm/map.js.map +1 -1
  9. package/esm/otpSchema.json +12 -0
  10. package/esm/profile.js +18 -19
  11. package/esm/profile.js.map +1 -1
  12. package/esm/query-gen.js +35 -45
  13. package/esm/query-gen.js.map +1 -1
  14. package/esm/query.js +139 -191
  15. package/esm/query.js.map +1 -1
  16. package/esm/route.js +74 -97
  17. package/esm/route.js.map +1 -1
  18. package/esm/storage.js +10 -7
  19. package/esm/storage.js.map +1 -1
  20. package/esm/suspense.js +1 -3
  21. package/esm/suspense.js.map +1 -1
  22. package/esm/time.js +13 -17
  23. package/esm/time.js.map +1 -1
  24. package/esm/ui.js +0 -5
  25. package/esm/ui.js.map +1 -1
  26. package/lib/graphql.d.js.map +1 -1
  27. package/lib/index.js +3 -19
  28. package/lib/index.js.map +1 -1
  29. package/lib/itinerary.d.ts.map +1 -1
  30. package/lib/itinerary.js +151 -247
  31. package/lib/itinerary.js.map +1 -1
  32. package/lib/map.d.ts +1 -1
  33. package/lib/map.d.ts.map +1 -1
  34. package/lib/map.js +9 -20
  35. package/lib/map.js.map +1 -1
  36. package/lib/otpSchema.json +12 -0
  37. package/lib/profile.js +19 -21
  38. package/lib/profile.js.map +1 -1
  39. package/lib/query-gen.d.ts +4 -4
  40. package/lib/query-gen.d.ts.map +1 -1
  41. package/lib/query-gen.js +35 -47
  42. package/lib/query-gen.js.map +1 -1
  43. package/lib/query-params.jsx +893 -0
  44. package/lib/query.js +72 -122
  45. package/lib/query.js.map +1 -1
  46. package/lib/route.js +73 -101
  47. package/lib/route.js.map +1 -1
  48. package/lib/storage.d.ts.map +1 -1
  49. package/lib/storage.js +12 -13
  50. package/lib/storage.js.map +1 -1
  51. package/lib/suspense.d.ts +1 -1
  52. package/lib/suspense.d.ts.map +1 -1
  53. package/lib/suspense.js +3 -9
  54. package/lib/suspense.js.map +1 -1
  55. package/lib/time.js +21 -44
  56. package/lib/time.js.map +1 -1
  57. package/lib/ui.js +1 -8
  58. package/lib/ui.js.map +1 -1
  59. package/package.json +7 -8
  60. package/src/core-utils.story.tsx +1 -1
  61. package/src/itinerary.ts +2 -0
  62. package/src/otpSchema.json +12 -0
  63. package/src/profile.js +1 -1
  64. package/src/query-gen.ts +1 -1
  65. package/src/query-params.jsx +893 -0
  66. package/src/query.js +4 -1
  67. package/src/storage.ts +9 -5
  68. package/tsconfig.tsbuildinfo +1 -1
  69. package/esm/query-params.js +0 -786
  70. package/esm/query-params.js.map +0 -1
  71. package/lib/query-params.js +0 -756
  72. package/lib/query-params.js.map +0 -1
  73. /package/{src/query-params.js → esm/query-params.jsx} +0 -0
package/lib/route.js CHANGED
@@ -1,20 +1,17 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
7
+ exports.getMostReadableTextColor = getMostReadableTextColor;
8
8
  exports.getTransitOperatorFromFeedIdAndAgencyId = getTransitOperatorFromFeedIdAndAgencyId;
9
9
  exports.getTransitOperatorFromLeg = getTransitOperatorFromLeg;
10
10
  exports.getTransitOperatorFromOtpRoute = getTransitOperatorFromOtpRoute;
11
11
  exports.makeNumericValueComparator = makeNumericValueComparator;
12
- exports.makeStringValueComparator = makeStringValueComparator;
13
12
  exports.makeRouteComparator = makeRouteComparator;
14
- exports.getMostReadableTextColor = getMostReadableTextColor;
15
-
13
+ exports.makeStringValueComparator = makeStringValueComparator;
16
14
  var _chromaJs = _interopRequireDefault(require("chroma-js"));
17
-
18
15
  /**
19
16
  * Returns the transit operator (if an exact match is found) from the transit
20
17
  * operators config value. It is critical to use both the feedId and agencyId in
@@ -30,6 +27,7 @@ var _chromaJs = _interopRequireDefault(require("chroma-js"));
30
27
  function getTransitOperatorFromFeedIdAndAgencyId(feedId, agencyId, transitOperators) {
31
28
  return transitOperators.find(transitOperator => transitOperator.feedId === feedId && transitOperator.agencyId === agencyId) || null;
32
29
  }
30
+
33
31
  /**
34
32
  * Looks up an operator from the provided leg.
35
33
  *
@@ -38,13 +36,12 @@ function getTransitOperatorFromFeedIdAndAgencyId(feedId, agencyId, transitOperat
38
36
  * @param {object} transitOperators transitOperators from config.
39
37
  * @return {object} the operator if one was found or null if no match was found
40
38
  */
41
-
42
-
43
39
  function getTransitOperatorFromLeg(leg, transitOperators) {
44
40
  if (!leg.routeId || !leg.agencyId) return null;
45
41
  const feedId = leg.routeId.split(":")[0];
46
42
  return getTransitOperatorFromFeedIdAndAgencyId(feedId, leg.agencyId, transitOperators);
47
43
  }
44
+
48
45
  /**
49
46
  * Looks up an operator from the provided configuration given an OTP route.
50
47
  * NOTE: this assumes the use of the OTP Route model or a modified OTP
@@ -55,13 +52,10 @@ function getTransitOperatorFromLeg(leg, transitOperators) {
55
52
  * @param {array} transitOperators transitOperators from config
56
53
  * @return {object} the operator if one was found or null if no match was found
57
54
  */
58
-
59
-
60
55
  function getTransitOperatorFromOtpRoute(route, transitOperators) {
61
56
  if (!route.id) return null;
62
57
  const feedId = route.id.split(":")[0];
63
58
  let agencyId;
64
-
65
59
  if (route.agency) {
66
60
  // This is returned in the OTP Route model
67
61
  agencyId = route.agency.id;
@@ -71,9 +65,10 @@ function getTransitOperatorFromOtpRoute(route, transitOperators) {
71
65
  } else {
72
66
  return null;
73
67
  }
74
-
75
68
  return getTransitOperatorFromFeedIdAndAgencyId(feedId, agencyId, transitOperators);
76
- } // The functions below are for enhanced route sorting functions for the route
69
+ }
70
+
71
+ // The functions below are for enhanced route sorting functions for the route
77
72
  // viewer on OTP-react-redux.
78
73
  // They address route ordering issues discussed in
79
74
  // https://github.com/opentripplanner/otp-react-redux/pull/123 and
@@ -83,9 +78,8 @@ function getTransitOperatorFromOtpRoute(route, transitOperators) {
83
78
  * A large comparator value that can safely be used in mathematical sort
84
79
  * comparisons to place things at the end of lists
85
80
  */
86
-
87
-
88
81
  const END_OF_LIST_COMPARATOR_VALUE = 999999999999;
82
+
89
83
  /**
90
84
  * Returns a transit operator comparator value given a route and an optional
91
85
  * transitOperators config value. This function will do its best to handle all
@@ -99,61 +93,57 @@ const END_OF_LIST_COMPARATOR_VALUE = 999999999999;
99
93
  * the transitOperators value is not defined. Otherwise an integer will be
100
94
  * returned.
101
95
  */
102
-
103
96
  function getTransitOperatorComparatorValue(route, transitOperators) {
104
97
  // if the transitOperators is undefined or has zero length, use the route's
105
98
  // agency name as the comparator value
106
99
  if (!transitOperators || transitOperators.length === 0) {
107
100
  // OTP Route
108
- if (route.agency) return route.agency.name; // OTP RouteShort (base OTP repo or IBI fork)
109
-
110
- if (route.agencyName) return route.agencyName; // shouldn't happen as agency names will be defined
111
-
101
+ if (route.agency) return route.agency.name;
102
+ // OTP RouteShort (base OTP repo or IBI fork)
103
+ if (route.agencyName) return route.agencyName;
104
+ // shouldn't happen as agency names will be defined
112
105
  return "zzz";
113
- } // find operator associated with route
106
+ }
114
107
 
108
+ // find operator associated with route
109
+ const transitOperator = getTransitOperatorFromOtpRoute(route, transitOperators);
115
110
 
116
- const transitOperator = getTransitOperatorFromOtpRoute(route, transitOperators); // if transit operator not found, return infinity
111
+ // if transit operator not found, return infinity
112
+ if (!transitOperator) return END_OF_LIST_COMPARATOR_VALUE;
117
113
 
118
- if (!transitOperator) return END_OF_LIST_COMPARATOR_VALUE; // return the transit operator's sort value or END_OF_LIST_COMPARATOR_VALUE if
114
+ // return the transit operator's sort value or END_OF_LIST_COMPARATOR_VALUE if
119
115
  // the sort value is not a number
120
-
121
116
  return typeof transitOperator.order === "number" ? transitOperator.order : END_OF_LIST_COMPARATOR_VALUE;
122
117
  }
118
+
123
119
  /**
124
120
  * Calculates the sort comparator value given two routes based off of the
125
121
  * route's agency and provided transitOperators config data.
126
122
  */
127
-
128
-
129
123
  function makeTransitOperatorComparator(transitOperators) {
130
124
  return (a, b) => {
131
125
  const aVal = getTransitOperatorComparatorValue(a, transitOperators);
132
126
  const bVal = getTransitOperatorComparatorValue(b, transitOperators);
133
-
134
127
  if (typeof aVal === "string") {
135
128
  // happens when transitOperators is undefined. Both aVal are guaranteed to
136
129
  // be strings. Make a string comparison.
137
130
  if (aVal < bVal) return -1;
138
131
  if (aVal > bVal) return 1;
139
132
  return 0;
140
- } // @ts-expect-error transitOperators are defined and therefore a numeric value is guaranteed
133
+ }
134
+ // @ts-expect-error transitOperators are defined and therefore a numeric value is guaranteed
141
135
  // to be returned
142
-
143
-
144
136
  return aVal - bVal;
145
137
  };
146
138
  }
139
+
147
140
  /**
148
141
  * Gets the desired sort values according to an optional getter function. If the
149
142
  * getter function is not defined, the original sort values are returned.
150
143
  */
151
-
152
-
153
144
  function getSortValues(getterFn, a, b) {
154
145
  let aVal;
155
146
  let bVal;
156
-
157
147
  if (typeof getterFn === "function") {
158
148
  aVal = getterFn(a);
159
149
  bVal = getterFn(b);
@@ -161,16 +151,15 @@ function getSortValues(getterFn, a, b) {
161
151
  aVal = a;
162
152
  bVal = b;
163
153
  }
164
-
165
154
  return {
166
155
  aVal,
167
156
  bVal
168
157
  };
169
- } // Lookup for the sort values associated with various OTP modes.
158
+ }
159
+
160
+ // Lookup for the sort values associated with various OTP modes.
170
161
  // Note: JSDoc format not used to avoid bug in documentationjs.
171
162
  // https://github.com/documentationjs/documentation/issues/372
172
-
173
-
174
163
  const modeComparatorValue = {
175
164
  SUBWAY: 1,
176
165
  TRAM: 2,
@@ -181,10 +170,11 @@ const modeComparatorValue = {
181
170
  CABLE_CAR: 6,
182
171
  FUNICULAR: 7,
183
172
  BUS: 8
184
- }; // Lookup that maps route types to the OTP mode sort values.
173
+ };
174
+
175
+ // Lookup that maps route types to the OTP mode sort values.
185
176
  // Note: JSDoc format not used to avoid bug in documentationjs.
186
177
  // https://github.com/documentationjs/documentation/issues/372
187
-
188
178
  const routeTypeComparatorValue = {
189
179
  0: modeComparatorValue.TRAM,
190
180
  // - Tram, Streetcar, Light rail.
@@ -209,77 +199,70 @@ const routeTypeComparatorValue = {
209
199
  12: modeComparatorValue.RAIL,
210
200
  // - Monorail.
211
201
  13: modeComparatorValue.TROLLEYBUS
212
- }; // Gets a comparator value for a given route's type (OTP mode).
202
+ };
203
+
204
+ // Gets a comparator value for a given route's type (OTP mode).
213
205
  // Note: JSDoc format not used to avoid bug in documentationjs.
214
206
  // ttps://github.com/documentationjs/documentation/issues/372
215
-
216
207
  function getRouteTypeComparatorValue(route) {
217
208
  // For some strange reason, the short route response in OTP returns the
218
209
  // string-based modes, but the long route response returns the
219
210
  // integer route type. This attempts to account for both of those cases.
220
211
  if (!route) throw new Error(`Route is undefined. ${route}`);
221
-
222
212
  if (typeof modeComparatorValue[route.mode] !== "undefined") {
223
213
  return modeComparatorValue[route.mode];
224
214
  }
225
-
226
215
  if (typeof routeTypeComparatorValue[route.type] !== "undefined") {
227
216
  return routeTypeComparatorValue[route.type];
228
- } // Default the comparator value to a large number (placing the route at the
217
+ }
218
+ // Default the comparator value to a large number (placing the route at the
229
219
  // end of the list).
230
220
  // eslint-disable-next-line no-console
231
-
232
-
233
221
  console.warn("no mode/route type found for route", route);
234
222
  return END_OF_LIST_COMPARATOR_VALUE;
235
223
  }
224
+
236
225
  /**
237
226
  * Calculates the sort comparator value given two routes based off of route type
238
227
  * (OTP mode).
239
228
  */
240
-
241
-
242
229
  function routeTypeComparator(a, b) {
243
230
  return getRouteTypeComparatorValue(a) - getRouteTypeComparatorValue(b);
244
231
  }
232
+
245
233
  /**
246
234
  * Determines whether a value is a string that starts with an alphabetic
247
235
  * ascii character.
248
236
  */
249
-
250
-
251
237
  function startsWithAlphabeticCharacter(val) {
252
238
  if (typeof val === "string" && val.length > 0) {
253
239
  const firstCharCode = val.charCodeAt(0);
254
240
  return firstCharCode >= 65 && firstCharCode <= 90 || firstCharCode >= 97 && firstCharCode <= 122;
255
241
  }
256
-
257
242
  return false;
258
243
  }
244
+
259
245
  /**
260
246
  * Sorts routes based off of whether the shortName begins with an alphabetic
261
247
  * character. Routes with shortn that do start with an alphabetic character will
262
248
  * be prioritized over those that don't.
263
249
  */
264
-
265
-
266
250
  function alphabeticShortNameComparator(a, b) {
267
251
  const aStartsWithAlphabeticCharacter = startsWithAlphabeticCharacter(a.shortName);
268
252
  const bStartsWithAlphabeticCharacter = startsWithAlphabeticCharacter(b.shortName);
269
-
270
253
  if (aStartsWithAlphabeticCharacter && bStartsWithAlphabeticCharacter) {
271
254
  // both start with an alphabetic character, return equivalence
272
255
  return 0;
273
- } // a does start with an alphabetic character, but b does not. Prioritize a
274
-
275
-
276
- if (aStartsWithAlphabeticCharacter) return -1; // b does start with an alphabetic character, but a does not. Prioritize b
277
-
278
- if (bStartsWithAlphabeticCharacter) return 1; // neither route has a shortName that starts with an alphabetic character.
256
+ }
257
+ // a does start with an alphabetic character, but b does not. Prioritize a
258
+ if (aStartsWithAlphabeticCharacter) return -1;
259
+ // b does start with an alphabetic character, but a does not. Prioritize b
260
+ if (bStartsWithAlphabeticCharacter) return 1;
261
+ // neither route has a shortName that starts with an alphabetic character.
279
262
  // Return equivalence
280
-
281
263
  return 0;
282
264
  }
265
+
283
266
  /**
284
267
  * Checks whether an appropriate comparison of numeric values can be made for
285
268
  * sorting purposes. If both values are not valid numbers according to the
@@ -299,28 +282,27 @@ function alphabeticShortNameComparator(a, b) {
299
282
  * @param {function} [objGetterFn] An optional function to obtain the
300
283
  * comparison value from the comparator function arguments
301
284
  */
302
-
303
-
304
285
  function makeNumericValueComparator(objGetterFn) {
305
286
  /* Note: Using the global version of isNaN (the Number version behaves differently. */
306
-
307
287
  /* eslint-disable no-restricted-globals */
308
288
  return (a, b) => {
309
289
  const {
310
290
  aVal,
311
291
  bVal
312
292
  } = getSortValues(objGetterFn, a, b);
313
- if (typeof aVal !== "number" || typeof bVal !== "number") return 0; // if both values aren't valid numbers, use the next sort criteria
314
-
315
- if (isNaN(aVal) && isNaN(bVal)) return 0; // b is a valid number, b gets priority
316
-
317
- if (isNaN(aVal)) return 1; // a is a valid number, a gets priority
318
-
319
- if (isNaN(bVal)) return -1; // a and b are valid numbers, return the sort value
320
-
293
+ if (typeof aVal !== "number" || typeof bVal !== "number") return 0;
294
+
295
+ // if both values aren't valid numbers, use the next sort criteria
296
+ if (isNaN(aVal) && isNaN(bVal)) return 0;
297
+ // b is a valid number, b gets priority
298
+ if (isNaN(aVal)) return 1;
299
+ // a is a valid number, a gets priority
300
+ if (isNaN(bVal)) return -1;
301
+ // a and b are valid numbers, return the sort value
321
302
  return aVal - bVal;
322
303
  };
323
304
  }
305
+
324
306
  /**
325
307
  * Create a comparator function that compares string values. The comparison
326
308
  * values feed to the sort comparator function are assumed to be objects that
@@ -331,26 +313,25 @@ function makeNumericValueComparator(objGetterFn) {
331
313
  * @param {function} [objGetterFn] An optional function to obtain the
332
314
  * comparison value from the comparator function arguments
333
315
  */
334
-
335
-
336
316
  function makeStringValueComparator(objGetterFn) {
337
317
  return (a, b) => {
338
318
  const {
339
319
  aVal,
340
320
  bVal
341
- } = getSortValues(objGetterFn, a, b); // both a and b are uncomparable strings, return equivalent value
342
-
343
- if (!aVal && !bVal) return 0; // a is not a comparable string, b gets priority
344
-
345
- if (!aVal) return 1; // b is not a comparable string, a gets priority
346
-
347
- if (!bVal) return -1; // a and b are comparable strings, return the sort value
348
-
321
+ } = getSortValues(objGetterFn, a, b);
322
+ // both a and b are uncomparable strings, return equivalent value
323
+ if (!aVal && !bVal) return 0;
324
+ // a is not a comparable string, b gets priority
325
+ if (!aVal) return 1;
326
+ // b is not a comparable string, a gets priority
327
+ if (!bVal) return -1;
328
+ // a and b are comparable strings, return the sort value
349
329
  if (aVal < bVal) return -1;
350
330
  if (aVal > bVal) return 1;
351
331
  return 0;
352
332
  };
353
333
  }
334
+
354
335
  /**
355
336
  * OpenTripPlanner sets the routeSortOrder to -999 by default. So, if that value
356
337
  * is encountered, assume that it actually means that the routeSortOrder is not
@@ -359,11 +340,10 @@ function makeStringValueComparator(objGetterFn) {
359
340
  * See https://github.com/opentripplanner/OpenTripPlanner/issues/2938
360
341
  * Also see https://github.com/opentripplanner/otp-react-redux/issues/122
361
342
  */
362
-
363
-
364
343
  function getRouteSortOrderValue(val) {
365
344
  return val === -999 ? undefined : val;
366
345
  }
346
+
367
347
  /**
368
348
  * Create a multi-criteria sort comparator function composed of other sort
369
349
  * comparator functions. Each comparator function will be ran in the order given
@@ -371,22 +351,20 @@ function getRouteSortOrderValue(val) {
371
351
  * returned. If all comparison functions return equivalence, then the values
372
352
  * are assumed to be equivalent.
373
353
  */
374
-
375
-
376
354
  function makeMultiCriteriaSort(...criteria) {
377
355
  return (a, b) => {
378
356
  for (let i = 0; i < criteria.length; i++) {
379
- const curCriteriaComparatorValue = criteria[i](a, b); // if the comparison objects are not equivalent, return the value obtained
357
+ const curCriteriaComparatorValue = criteria[i](a, b);
358
+ // if the comparison objects are not equivalent, return the value obtained
380
359
  // in this current criteria comparison
381
-
382
360
  if (curCriteriaComparatorValue !== 0) {
383
361
  return curCriteriaComparatorValue;
384
362
  }
385
363
  }
386
-
387
364
  return 0;
388
365
  };
389
366
  }
367
+
390
368
  /**
391
369
  * Creates a sort comparator function to compares routes for the purposes of
392
370
  * sorting and displaying in a user interface. This takes in a single optional
@@ -418,11 +396,10 @@ function makeMultiCriteriaSort(...criteria) {
418
396
  * those with shortNames.
419
397
  * 7. longName as string.
420
398
  */
421
-
422
-
423
399
  function makeRouteComparator(transitOperators) {
424
400
  return makeMultiCriteriaSort(makeTransitOperatorComparator(transitOperators), makeNumericValueComparator(obj => getRouteSortOrderValue(obj.sortOrder)), routeTypeComparator, alphabeticShortNameComparator, makeNumericValueComparator(obj => parseInt(obj.shortName, 10)), makeStringValueComparator(obj => obj.shortName), makeStringValueComparator(obj => obj.longName));
425
401
  }
402
+
426
403
  /**
427
404
  * Tests if a pair of colors is readable. If it is, that readable color is returned.
428
405
  * If it is not, a more appropriate alternative is returned.
@@ -436,32 +413,27 @@ function makeRouteComparator(transitOperators) {
436
413
  * @param backgroundColor A hex string, usually the "routeColor"
437
414
  * @param proposedTextColor A hex string, usually the "routeTextColor"
438
415
  */
439
-
440
-
441
416
  function getMostReadableTextColor(backgroundColor, proposedTextColor) {
442
417
  // Sometimes input will defy the method signature. Therefore we need extra fallbacks
443
418
  if (!backgroundColor) backgroundColor = "#333333";
444
419
  if (!proposedTextColor) proposedTextColor = "#ffffff";
445
-
446
420
  if (!backgroundColor.startsWith("#")) {
447
421
  backgroundColor = `#${backgroundColor}`;
448
422
  }
449
-
450
423
  if (!proposedTextColor.startsWith("#")) {
451
424
  proposedTextColor = `#${proposedTextColor}`;
452
- } // Check if proposed color is readable
453
- // Luminance thresholds have been selected based on actual transit agency colors
454
-
425
+ }
455
426
 
427
+ // Check if proposed color is readable
428
+ // Luminance thresholds have been selected based on actual transit agency colors
456
429
  const fgLuminance = (0, _chromaJs.default)(proposedTextColor).luminance();
457
430
  const bgLuminance = (0, _chromaJs.default)(backgroundColor).luminance();
458
-
459
431
  if (bgLuminance + fgLuminance < 1.41 && bgLuminance + fgLuminance > 0.25 && Math.abs(bgLuminance - fgLuminance) > 0.2) {
460
432
  return proposedTextColor;
461
- } // Return black or white based on luminance of background color
462
- // When generating colors, white is preferred.
463
-
433
+ }
464
434
 
435
+ // Return black or white based on luminance of background color
436
+ // When generating colors, white is preferred.
465
437
  return (0, _chromaJs.default)(backgroundColor).luminance() < 0.4 ? "#ffffff" : "#000000";
466
438
  }
467
439
  //# sourceMappingURL=route.js.map
package/lib/route.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/route.ts"],"names":["getTransitOperatorFromFeedIdAndAgencyId","feedId","agencyId","transitOperators","find","transitOperator","getTransitOperatorFromLeg","leg","routeId","split","getTransitOperatorFromOtpRoute","route","id","agency","END_OF_LIST_COMPARATOR_VALUE","getTransitOperatorComparatorValue","length","name","agencyName","order","makeTransitOperatorComparator","a","b","aVal","bVal","getSortValues","getterFn","modeComparatorValue","SUBWAY","TRAM","TROLLEYBUS","RAIL","GONDOLA","FERRY","CABLE_CAR","FUNICULAR","BUS","routeTypeComparatorValue","getRouteTypeComparatorValue","Error","mode","type","console","warn","routeTypeComparator","startsWithAlphabeticCharacter","val","firstCharCode","charCodeAt","alphabeticShortNameComparator","aStartsWithAlphabeticCharacter","shortName","bStartsWithAlphabeticCharacter","makeNumericValueComparator","objGetterFn","isNaN","makeStringValueComparator","getRouteSortOrderValue","undefined","makeMultiCriteriaSort","criteria","i","curCriteriaComparatorValue","makeRouteComparator","obj","sortOrder","parseInt","longName","getMostReadableTextColor","backgroundColor","proposedTextColor","startsWith","fgLuminance","luminance","bgLuminance","Math","abs"],"mappings":";;;;;;;;;;;;;;;AACA;;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,uCAAT,CACLC,MADK,EAELC,QAFK,EAGLC,gBAHK,EAIY;AACjB,SACEA,gBAAgB,CAACC,IAAjB,CACEC,eAAe,IACbA,eAAe,CAACJ,MAAhB,KAA2BA,MAA3B,IACAI,eAAe,CAACH,QAAhB,KAA6BA,QAHjC,KAIK,IALP;AAOD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASI,yBAAT,CACLC,GADK,EAELJ,gBAFK,EAGY;AACjB,MAAI,CAACI,GAAG,CAACC,OAAL,IAAgB,CAACD,GAAG,CAACL,QAAzB,EAAmC,OAAO,IAAP;AACnC,QAAMD,MAAM,GAAGM,GAAG,CAACC,OAAJ,CAAYC,KAAZ,CAAkB,GAAlB,EAAuB,CAAvB,CAAf;AACA,SAAOT,uCAAuC,CAC5CC,MAD4C,EAE5CM,GAAG,CAACL,QAFwC,EAG5CC,gBAH4C,CAA9C;AAKD;AAED;AACA;AACA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AI,CAACC,iBAAL,EAAwBA,iBAAiB,GAAG,SAApB;;AAExB,MAAI,CAACD,eAAe,CAACE,UAAhB,CAA2B,GAA3B,CAAL,EAAsC;AACpCF,IAAAA,eAAe,GAAI,IAAGA,eAAgB,EAAtC;AACD;;AACD,MAAI,CAACC,iBAAiB,CAACC,UAAlB,CAA6B,GAA7B,CAAL,EAAwC;AACtCD,IAAAA,iBAAiB,GAAI,IAAGA,iBAAkB,EAA1C;AACD,GAVO,CAYR;AACA;;;AACA,QAAME,WAAW,GAAG,uBAAOF,iBAAP,EAA0BG,SAA1B,EAApB;AACA,QAAMC,WAAW,GAAG,uBAAOL,eAAP,EAAwBI,SAAxB,EAApB;;AACA,MACEC,WAAW,GAAGF,WAAd,GAA4B,IAA5B,IACAE,WAAW,GAAGF,WAAd,GAA4B,IAD5B,IAEAG,IAAI,CAACC,GAAL,CAASF,WAAW,GAAGF,WAAvB,IAAsC,GAHxC,EAIE;AACA,WAAOF,iBAAP;AACD,GAtBO,CAwBR;AACA;;;AACA,SAAO,uBAAOD,eAAP,EAAwBI,SAAxB,KAAsC,GAAtC,GAA4C,SAA5C,GAAwD,SAA/D;AACD","sourcesContent":["import { Leg, Route, TransitOperator } from \"@opentripplanner/types\";\nimport chroma from \"chroma-js\";\n/**\n * Returns the transit operator (if an exact match is found) from the transit\n * operators config value. It is critical to use both the feedId and agencyId in\n * this method because it is possible in OTP for there to be a duplicate\n * agencyId in separate feeds.\n *\n * @param {string} feedId The feedId that this transit agency belongs to\n * @param {string} agencyId The agencyId of the transit agency\n * @param {array} transitOperators The transitOperators list from the config\n * @return {object} The transitOperator if a match was found or null if no match\n * was found\n */\nexport function getTransitOperatorFromFeedIdAndAgencyId(\n feedId: string,\n agencyId: string | number,\n transitOperators: TransitOperator[]\n): TransitOperator {\n return (\n transitOperators.find(\n transitOperator =>\n transitOperator.feedId === feedId &&\n transitOperator.agencyId === agencyId\n ) || null\n );\n}\n\n/**\n * Looks up an operator from the provided leg.\n *\n * @param {object} leg The Itinerary Leg from which to find the transit\n * operator\n * @param {object} transitOperators transitOperators from config.\n * @return {object} the operator if one was found or null if no match was found\n */\nexport function getTransitOperatorFromLeg(\n leg: Leg,\n transitOperators: TransitOperator[]\n): TransitOperator {\n if (!leg.routeId || !leg.agencyId) return null;\n const feedId = leg.routeId.split(\":\")[0];\n return getTransitOperatorFromFeedIdAndAgencyId(\n feedId,\n leg.agencyId,\n transitOperators\n );\n}\n\n/**\n * Looks up an operator from the provided configuration given an OTP route.\n * NOTE: this assumes the use of the OTP Route model or a modified OTP\n * RouteShort model (such as the one found in the IBI fork of OTP) that also\n * returns the agencyId.\n *\n * @param {object} route Either an OTP Route or RouteShort model\n * @param {array} transitOperators transitOperators from config\n * @return {object} the operator if one was found or null if no match was found\n */\nexport function getTransitOperatorFromOtpRoute(\n route: Route,\n transitOperators: TransitOperator[]\n): TransitOperator {\n if (!route.id) return null;\n const feedId = route.id.split(\":\")[0];\n let agencyId: string | number;\n if (route.agency) {\n // This is returned in the OTP Route model\n agencyId = route.agency.id;\n } else if (route.agencyId) {\n // This is returned in the OTP RouteShort model (such as in the IBI fork)\n agencyId = route.agencyId;\n } else {\n return null;\n }\n return getTransitOperatorFromFeedIdAndAgencyId(\n feedId,\n agencyId,\n transitOperators\n );\n}\n\n// The functions below are for enhanced route sorting functions for the route\n// viewer on OTP-react-redux.\n// They address route ordering issues discussed in\n// https://github.com/opentripplanner/otp-react-redux/pull/123 and\n// https://github.com/opentripplanner/otp-react-redux/pull/124.\n\n/**\n * A large comparator value that can safely be used in mathematical sort\n * comparisons to place things at the end of lists\n */\nconst END_OF_LIST_COMPARATOR_VALUE = 999999999999;\n\n/**\n * Returns a transit operator comparator value given a route and an optional\n * transitOperators config value. This function will do its best to handle all\n * kinds of input data as certain deployments of an implementing webapp may have\n * incomplete data and certain versions of OTP might not have a modified\n * implementation of the RouteShort model.\n *\n * @param {object} route Either an OTP Route or RouteShort model\n * @param {array} transitOperators transitOperators from config\n * @return {mixed} this could return a string value (the route's agency name) if\n * the transitOperators value is not defined. Otherwise an integer will be\n * returned.\n */\nfunction getTransitOperatorComparatorValue(\n route: Route,\n transitOperators: TransitOperator[]\n): number | string {\n // if the transitOperators is undefined or has zero length, use the route's\n // agency name as the comparator value\n if (!transitOperators || transitOperators.length === 0) {\n // OTP Route\n if (route.agency) return route.agency.name;\n // OTP RouteShort (base OTP repo or IBI fork)\n if (route.agencyName) return route.agencyName;\n // shouldn't happen as agency names will be defined\n return \"zzz\";\n }\n\n // find operator associated with route\n const transitOperator = getTransitOperatorFromOtpRoute(\n route,\n transitOperators\n );\n\n // if transit operator not found, return infinity\n if (!transitOperator) return END_OF_LIST_COMPARATOR_VALUE;\n\n // return the transit operator's sort value or END_OF_LIST_COMPARATOR_VALUE if\n // the sort value is not a number\n return typeof transitOperator.order === \"number\"\n ? transitOperator.order\n : END_OF_LIST_COMPARATOR_VALUE;\n}\n\n/**\n * Calculates the sort comparator value given two routes based off of the\n * route's agency and provided transitOperators config data.\n */\nfunction makeTransitOperatorComparator(transitOperators: TransitOperator[]) {\n return (a: Route, b: Route) => {\n const aVal = getTransitOperatorComparatorValue(a, transitOperators);\n const bVal = getTransitOperatorComparatorValue(b, transitOperators);\n if (typeof aVal === \"string\") {\n // happens when transitOperators is undefined. Both aVal are guaranteed to\n // be strings. Make a string comparison.\n if (aVal < bVal) return -1;\n if (aVal > bVal) return 1;\n return 0;\n }\n // @ts-expect-error transitOperators are defined and therefore a numeric value is guaranteed\n // to be returned\n return aVal - bVal;\n };\n}\n\n/**\n * Gets the desired sort values according to an optional getter function. If the\n * getter function is not defined, the original sort values are returned.\n */\nfunction getSortValues(\n getterFn: (item: unknown) => unknown,\n a: unknown,\n b: unknown\n) {\n let aVal: unknown;\n let bVal: unknown;\n if (typeof getterFn === \"function\") {\n aVal = getterFn(a);\n bVal = getterFn(b);\n } else {\n aVal = a;\n bVal = b;\n }\n return { aVal, bVal };\n}\n\n// Lookup for the sort values associated with various OTP modes.\n// Note: JSDoc format not used to avoid bug in documentationjs.\n// https://github.com/documentationjs/documentation/issues/372\nconst modeComparatorValue = {\n SUBWAY: 1,\n TRAM: 2,\n TROLLEYBUS: 9,\n RAIL: 3,\n GONDOLA: 4,\n FERRY: 5,\n CABLE_CAR: 6,\n FUNICULAR: 7,\n BUS: 8\n};\n\n// Lookup that maps route types to the OTP mode sort values.\n// Note: JSDoc format not used to avoid bug in documentationjs.\n// https://github.com/documentationjs/documentation/issues/372\nconst routeTypeComparatorValue = {\n 0: modeComparatorValue.TRAM, // - Tram, Streetcar, Light rail.\n 1: modeComparatorValue.SUBWAY, // - Subway, Metro.\n 2: modeComparatorValue.RAIL, // - Rail. Used for intercity or long-distance travel.\n 3: modeComparatorValue.BUS, // - Bus.\n 4: modeComparatorValue.FERRY, // - Ferry.\n 5: modeComparatorValue.CABLE_CAR, // - Cable tram.\n 6: modeComparatorValue.GONDOLA, // - Gondola, etc.\n 7: modeComparatorValue.FUNICULAR, // - Funicular.\n // TODO: 11 and 12 are not a part of OTP as of 2019-02-14, but for now just\n // associate them with bus/rail.\n 11: modeComparatorValue.BUS, // - Trolleybus.\n 12: modeComparatorValue.RAIL, // - Monorail.\n 13: modeComparatorValue.TROLLEYBUS\n};\n\n// Gets a comparator value for a given route's type (OTP mode).\n// Note: JSDoc format not used to avoid bug in documentationjs.\n// ttps://github.com/documentationjs/documentation/issues/372\nfunction getRouteTypeComparatorValue(route: Route): number {\n // For some strange reason, the short route response in OTP returns the\n // string-based modes, but the long route response returns the\n // integer route type. This attempts to account for both of those cases.\n if (!route) throw new Error(`Route is undefined. ${route}`);\n if (typeof modeComparatorValue[route.mode] !== \"undefined\") {\n return modeComparatorValue[route.mode];\n }\n if (typeof routeTypeComparatorValue[route.type] !== \"undefined\") {\n return routeTypeComparatorValue[route.type];\n }\n // Default the comparator value to a large number (placing the route at the\n // end of the list).\n // eslint-disable-next-line no-console\n console.warn(\"no mode/route type found for route\", route);\n return END_OF_LIST_COMPARATOR_VALUE;\n}\n\n/**\n * Calculates the sort comparator value given two routes based off of route type\n * (OTP mode).\n */\nfunction routeTypeComparator(a: Route, b: Route): number {\n return getRouteTypeComparatorValue(a) - getRouteTypeComparatorValue(b);\n}\n\n/**\n * Determines whether a value is a string that starts with an alphabetic\n * ascii character.\n */\nfunction startsWithAlphabeticCharacter(val: unknown): boolean {\n if (typeof val === \"string\" && val.length > 0) {\n const firstCharCode = val.charCodeAt(0);\n return (\n (firstCharCode >= 65 && firstCharCode <= 90) ||\n (firstCharCode >= 97 && firstCharCode <= 122)\n );\n }\n return false;\n}\n\n/**\n * Sorts routes based off of whether the shortName begins with an alphabetic\n * character. Routes with shortn that do start with an alphabetic character will\n * be prioritized over those that don't.\n */\nfunction alphabeticShortNameComparator(a: Route, b: Route): number {\n const aStartsWithAlphabeticCharacter = startsWithAlphabeticCharacter(\n a.shortName\n );\n const bStartsWithAlphabeticCharacter = startsWithAlphabeticCharacter(\n b.shortName\n );\n\n if (aStartsWithAlphabeticCharacter && bStartsWithAlphabeticCharacter) {\n // both start with an alphabetic character, return equivalence\n return 0;\n }\n // a does start with an alphabetic character, but b does not. Prioritize a\n if (aStartsWithAlphabeticCharacter) return -1;\n // b does start with an alphabetic character, but a does not. Prioritize b\n if (bStartsWithAlphabeticCharacter) return 1;\n // neither route has a shortName that starts with an alphabetic character.\n // Return equivalence\n return 0;\n}\n\n/**\n * Checks whether an appropriate comparison of numeric values can be made for\n * sorting purposes. If both values are not valid numbers according to the\n * isNaN check, then this function returns undefined which indicates that a\n * secondary sorting criteria should be used instead. If one value is valid and\n * the other is not, then the valid value will be given sorting priority. If\n * both values are valid numbers, the difference is obtained as the sort value.\n *\n * An optional argument can be provided which will be used to obtain the\n * comparison value from the comparison function arguments.\n *\n * IMPORTANT: the comparison values must be numeric values or at least be\n * attempted to be converted to numeric values! If one of the arguments is\n * something crazy like an empty string, unexpected behavior will occur because\n * JavaScript.\n *\n * @param {function} [objGetterFn] An optional function to obtain the\n * comparison value from the comparator function arguments\n */\nexport function makeNumericValueComparator(\n objGetterFn?: (item: Route) => number\n) {\n /* Note: Using the global version of isNaN (the Number version behaves differently. */\n /* eslint-disable no-restricted-globals */\n return (a: number, b: number): number => {\n const { aVal, bVal } = getSortValues(objGetterFn, a, b);\n if (typeof aVal !== \"number\" || typeof bVal !== \"number\") return 0;\n\n // if both values aren't valid numbers, use the next sort criteria\n if (isNaN(aVal) && isNaN(bVal)) return 0;\n // b is a valid number, b gets priority\n if (isNaN(aVal)) return 1;\n // a is a valid number, a gets priority\n if (isNaN(bVal)) return -1;\n // a and b are valid numbers, return the sort value\n return aVal - bVal;\n };\n}\n\n/**\n * Create a comparator function that compares string values. The comparison\n * values feed to the sort comparator function are assumed to be objects that\n * will have either undefined, null or string values at the given key. If one\n * object has undefined, null or an empty string, but the other does have a\n * string with length > 0, then that string will get priority.\n *\n * @param {function} [objGetterFn] An optional function to obtain the\n * comparison value from the comparator function arguments\n */\nexport function makeStringValueComparator(\n objGetterFn?: (item: Route) => string\n) {\n return (a: string, b: string): number => {\n const { aVal, bVal } = getSortValues(objGetterFn, a, b);\n // both a and b are uncomparable strings, return equivalent value\n if (!aVal && !bVal) return 0;\n // a is not a comparable string, b gets priority\n if (!aVal) return 1;\n // b is not a comparable string, a gets priority\n if (!bVal) return -1;\n // a and b are comparable strings, return the sort value\n if (aVal < bVal) return -1;\n if (aVal > bVal) return 1;\n return 0;\n };\n}\n\n/**\n * OpenTripPlanner sets the routeSortOrder to -999 by default. So, if that value\n * is encountered, assume that it actually means that the routeSortOrder is not\n * set in the GTFS.\n *\n * See https://github.com/opentripplanner/OpenTripPlanner/issues/2938\n * Also see https://github.com/opentripplanner/otp-react-redux/issues/122\n */\nfunction getRouteSortOrderValue(val: number): number {\n return val === -999 ? undefined : val;\n}\n\n/**\n * Create a multi-criteria sort comparator function composed of other sort\n * comparator functions. Each comparator function will be ran in the order given\n * until a non-zero comparison value is obtained which is then immediately\n * returned. If all comparison functions return equivalence, then the values\n * are assumed to be equivalent.\n */\nfunction makeMultiCriteriaSort(\n ...criteria: ((a: unknown, b: unknown) => number)[]\n) {\n return (a: number, b: number): number => {\n for (let i = 0; i < criteria.length; i++) {\n const curCriteriaComparatorValue = criteria[i](a, b);\n // if the comparison objects are not equivalent, return the value obtained\n // in this current criteria comparison\n if (curCriteriaComparatorValue !== 0) {\n return curCriteriaComparatorValue;\n }\n }\n return 0;\n };\n}\n\n/**\n * Creates a sort comparator function to compares routes for the purposes of\n * sorting and displaying in a user interface. This takes in a single optional\n * argument which should be a list of transitOperators as defined in the config\n * file. Due to GTFS feeds having varying levels of data quality, a multi-\n * criteria sort is needed to account for various differences. The criteria\n * included here are each applied to the routes in the order listed. If a given\n * sort criterion yields equivalence (e.g., two routes have the short name\n * \"20\"), the comparator falls back onto the next sort criterion (e.g., long\n * name). The sort operates on the following values (in order):\n *\n * 1. Transit Operator. The transit operator will be attempted to be obtained\n * for each route. If no argument is provided when creating this comparator\n * function, then routes will be sorted by their agency's name. If an\n * argument is provided and a match is found based off of the route's feed_id\n * and agency_id and a transitOperator's feed_id and agency_id, then the\n * field transitOperator.order will be used as the comparator value as long\n * as it is numeric. If it is not numeric, a value is returned indicating\n * that this transit operator should be placed at the end of the list.\n * 2. sortOrder. Routes that do not have a valid sortOrder will be placed\n * beneath those that do.\n * 3. route type (OTP mode). See routeTypeComparator code for prioritization of\n * route types.\n * 4. shortNames that begin with alphabetic characters. shortNames that do not\n * start with alphabetic characters will be place beneath those that do.\n * 5. shortName as integer. shortNames that cannot be parsed as integers will\n * be placed beneath those that are valid.\n * 6. shortName as string. Routes without shortNames will be placed beneath\n * those with shortNames.\n * 7. longName as string.\n */\nexport function makeRouteComparator(\n transitOperators: TransitOperator[]\n): (a: number, b: number) => number {\n return makeMultiCriteriaSort(\n makeTransitOperatorComparator(transitOperators),\n makeNumericValueComparator(obj => getRouteSortOrderValue(obj.sortOrder)),\n routeTypeComparator,\n alphabeticShortNameComparator,\n makeNumericValueComparator(obj => parseInt(obj.shortName, 10)),\n makeStringValueComparator(obj => obj.shortName),\n makeStringValueComparator(obj => obj.longName)\n );\n}\n\n/**\n * Tests if a pair of colors is readable. If it is, that readable color is returned.\n * If it is not, a more appropriate alternative is returned.\n *\n * Uses algorithm based on combined luminance. Values have been derived from\n * looking at real agency color pairings. These pairings are difficult to\n * generate for, as some colors see both white and black used by different agencies.\n *\n * This method therefore can accept multiple colors (including black and white) for the same background color.\n *\n * @param backgroundColor A hex string, usually the \"routeColor\"\n * @param proposedTextColor A hex string, usually the \"routeTextColor\"\n */\nexport function getMostReadableTextColor(\n backgroundColor: string,\n proposedTextColor?: string\n): string {\n // Sometimes input will defy the method signature. Therefore we need extra fallbacks\n if (!backgroundColor) backgroundColor = \"#333333\";\n if (!proposedTextColor) proposedTextColor = \"#ffffff\";\n\n if (!backgroundColor.startsWith(\"#\")) {\n backgroundColor = `#${backgroundColor}`;\n }\n if (!proposedTextColor.startsWith(\"#\")) {\n proposedTextColor = `#${proposedTextColor}`;\n }\n\n // Check if proposed color is readable\n // Luminance thresholds have been selected based on actual transit agency colors\n const fgLuminance = chroma(proposedTextColor).luminance();\n const bgLuminance = chroma(backgroundColor).luminance();\n if (\n bgLuminance + fgLuminance < 1.41 &&\n bgLuminance + fgLuminance > 0.25 &&\n Math.abs(bgLuminance - fgLuminance) > 0.2\n ) {\n return proposedTextColor;\n }\n\n // Return black or white based on luminance of background color\n // When generating colors, white is preferred.\n return chroma(backgroundColor).luminance() < 0.4 ? \"#ffffff\" : \"#000000\";\n}\n"],"file":"route.js"}
1
+ {"version":3,"file":"route.js","names":["_chromaJs","_interopRequireDefault","require","getTransitOperatorFromFeedIdAndAgencyId","feedId","agencyId","transitOperators","find","transitOperator","getTransitOperatorFromLeg","leg","routeId","split","getTransitOperatorFromOtpRoute","route","id","agency","END_OF_LIST_COMPARATOR_VALUE","getTransitOperatorComparatorValue","length","name","agencyName","order","makeTransitOperatorComparator","a","b","aVal","bVal","getSortValues","getterFn","modeComparatorValue","SUBWAY","TRAM","TROLLEYBUS","RAIL","GONDOLA","FERRY","CABLE_CAR","FUNICULAR","BUS","routeTypeComparatorValue","getRouteTypeComparatorValue","Error","mode","type","console","warn","routeTypeComparator","startsWithAlphabeticCharacter","val","firstCharCode","charCodeAt","alphabeticShortNameComparator","aStartsWithAlphabeticCharacter","shortName","bStartsWithAlphabeticCharacter","makeNumericValueComparator","objGetterFn","isNaN","makeStringValueComparator","getRouteSortOrderValue","undefined","makeMultiCriteriaSort","criteria","i","curCriteriaComparatorValue","makeRouteComparator","obj","sortOrder","parseInt","longName","getMostReadableTextColor","backgroundColor","proposedTextColor","startsWith","fgLuminance","chroma","luminance","bgLuminance","Math","abs"],"sources":["../src/route.ts"],"sourcesContent":["import { Leg, Route, TransitOperator } from \"@opentripplanner/types\";\nimport chroma from \"chroma-js\";\n/**\n * Returns the transit operator (if an exact match is found) from the transit\n * operators config value. It is critical to use both the feedId and agencyId in\n * this method because it is possible in OTP for there to be a duplicate\n * agencyId in separate feeds.\n *\n * @param {string} feedId The feedId that this transit agency belongs to\n * @param {string} agencyId The agencyId of the transit agency\n * @param {array} transitOperators The transitOperators list from the config\n * @return {object} The transitOperator if a match was found or null if no match\n * was found\n */\nexport function getTransitOperatorFromFeedIdAndAgencyId(\n feedId: string,\n agencyId: string | number,\n transitOperators: TransitOperator[]\n): TransitOperator {\n return (\n transitOperators.find(\n transitOperator =>\n transitOperator.feedId === feedId &&\n transitOperator.agencyId === agencyId\n ) || null\n );\n}\n\n/**\n * Looks up an operator from the provided leg.\n *\n * @param {object} leg The Itinerary Leg from which to find the transit\n * operator\n * @param {object} transitOperators transitOperators from config.\n * @return {object} the operator if one was found or null if no match was found\n */\nexport function getTransitOperatorFromLeg(\n leg: Leg,\n transitOperators: TransitOperator[]\n): TransitOperator {\n if (!leg.routeId || !leg.agencyId) return null;\n const feedId = leg.routeId.split(\":\")[0];\n return getTransitOperatorFromFeedIdAndAgencyId(\n feedId,\n leg.agencyId,\n transitOperators\n );\n}\n\n/**\n * Looks up an operator from the provided configuration given an OTP route.\n * NOTE: this assumes the use of the OTP Route model or a modified OTP\n * RouteShort model (such as the one found in the IBI fork of OTP) that also\n * returns the agencyId.\n *\n * @param {object} route Either an OTP Route or RouteShort model\n * @param {array} transitOperators transitOperators from config\n * @return {object} the operator if one was found or null if no match was found\n */\nexport function getTransitOperatorFromOtpRoute(\n route: Route,\n transitOperators: TransitOperator[]\n): TransitOperator {\n if (!route.id) return null;\n const feedId = route.id.split(\":\")[0];\n let agencyId: string | number;\n if (route.agency) {\n // This is returned in the OTP Route model\n agencyId = route.agency.id;\n } else if (route.agencyId) {\n // This is returned in the OTP RouteShort model (such as in the IBI fork)\n agencyId = route.agencyId;\n } else {\n return null;\n }\n return getTransitOperatorFromFeedIdAndAgencyId(\n feedId,\n agencyId,\n transitOperators\n );\n}\n\n// The functions below are for enhanced route sorting functions for the route\n// viewer on OTP-react-redux.\n// They address route ordering issues discussed in\n// https://github.com/opentripplanner/otp-react-redux/pull/123 and\n// https://github.com/opentripplanner/otp-react-redux/pull/124.\n\n/**\n * A large comparator value that can safely be used in mathematical sort\n * comparisons to place things at the end of lists\n */\nconst END_OF_LIST_COMPARATOR_VALUE = 999999999999;\n\n/**\n * Returns a transit operator comparator value given a route and an optional\n * transitOperators config value. This function will do its best to handle all\n * kinds of input data as certain deployments of an implementing webapp may have\n * incomplete data and certain versions of OTP might not have a modified\n * implementation of the RouteShort model.\n *\n * @param {object} route Either an OTP Route or RouteShort model\n * @param {array} transitOperators transitOperators from config\n * @return {mixed} this could return a string value (the route's agency name) if\n * the transitOperators value is not defined. Otherwise an integer will be\n * returned.\n */\nfunction getTransitOperatorComparatorValue(\n route: Route,\n transitOperators: TransitOperator[]\n): number | string {\n // if the transitOperators is undefined or has zero length, use the route's\n // agency name as the comparator value\n if (!transitOperators || transitOperators.length === 0) {\n // OTP Route\n if (route.agency) return route.agency.name;\n // OTP RouteShort (base OTP repo or IBI fork)\n if (route.agencyName) return route.agencyName;\n // shouldn't happen as agency names will be defined\n return \"zzz\";\n }\n\n // find operator associated with route\n const transitOperator = getTransitOperatorFromOtpRoute(\n route,\n transitOperators\n );\n\n // if transit operator not found, return infinity\n if (!transitOperator) return END_OF_LIST_COMPARATOR_VALUE;\n\n // return the transit operator's sort value or END_OF_LIST_COMPARATOR_VALUE if\n // the sort value is not a number\n return typeof transitOperator.order === \"number\"\n ? transitOperator.order\n : END_OF_LIST_COMPARATOR_VALUE;\n}\n\n/**\n * Calculates the sort comparator value given two routes based off of the\n * route's agency and provided transitOperators config data.\n */\nfunction makeTransitOperatorComparator(transitOperators: TransitOperator[]) {\n return (a: Route, b: Route) => {\n const aVal = getTransitOperatorComparatorValue(a, transitOperators);\n const bVal = getTransitOperatorComparatorValue(b, transitOperators);\n if (typeof aVal === \"string\") {\n // happens when transitOperators is undefined. Both aVal are guaranteed to\n // be strings. Make a string comparison.\n if (aVal < bVal) return -1;\n if (aVal > bVal) return 1;\n return 0;\n }\n // @ts-expect-error transitOperators are defined and therefore a numeric value is guaranteed\n // to be returned\n return aVal - bVal;\n };\n}\n\n/**\n * Gets the desired sort values according to an optional getter function. If the\n * getter function is not defined, the original sort values are returned.\n */\nfunction getSortValues(\n getterFn: (item: unknown) => unknown,\n a: unknown,\n b: unknown\n) {\n let aVal: unknown;\n let bVal: unknown;\n if (typeof getterFn === \"function\") {\n aVal = getterFn(a);\n bVal = getterFn(b);\n } else {\n aVal = a;\n bVal = b;\n }\n return { aVal, bVal };\n}\n\n// Lookup for the sort values associated with various OTP modes.\n// Note: JSDoc format not used to avoid bug in documentationjs.\n// https://github.com/documentationjs/documentation/issues/372\nconst modeComparatorValue = {\n SUBWAY: 1,\n TRAM: 2,\n TROLLEYBUS: 9,\n RAIL: 3,\n GONDOLA: 4,\n FERRY: 5,\n CABLE_CAR: 6,\n FUNICULAR: 7,\n BUS: 8\n};\n\n// Lookup that maps route types to the OTP mode sort values.\n// Note: JSDoc format not used to avoid bug in documentationjs.\n// https://github.com/documentationjs/documentation/issues/372\nconst routeTypeComparatorValue = {\n 0: modeComparatorValue.TRAM, // - Tram, Streetcar, Light rail.\n 1: modeComparatorValue.SUBWAY, // - Subway, Metro.\n 2: modeComparatorValue.RAIL, // - Rail. Used for intercity or long-distance travel.\n 3: modeComparatorValue.BUS, // - Bus.\n 4: modeComparatorValue.FERRY, // - Ferry.\n 5: modeComparatorValue.CABLE_CAR, // - Cable tram.\n 6: modeComparatorValue.GONDOLA, // - Gondola, etc.\n 7: modeComparatorValue.FUNICULAR, // - Funicular.\n // TODO: 11 and 12 are not a part of OTP as of 2019-02-14, but for now just\n // associate them with bus/rail.\n 11: modeComparatorValue.BUS, // - Trolleybus.\n 12: modeComparatorValue.RAIL, // - Monorail.\n 13: modeComparatorValue.TROLLEYBUS\n};\n\n// Gets a comparator value for a given route's type (OTP mode).\n// Note: JSDoc format not used to avoid bug in documentationjs.\n// ttps://github.com/documentationjs/documentation/issues/372\nfunction getRouteTypeComparatorValue(route: Route): number {\n // For some strange reason, the short route response in OTP returns the\n // string-based modes, but the long route response returns the\n // integer route type. This attempts to account for both of those cases.\n if (!route) throw new Error(`Route is undefined. ${route}`);\n if (typeof modeComparatorValue[route.mode] !== \"undefined\") {\n return modeComparatorValue[route.mode];\n }\n if (typeof routeTypeComparatorValue[route.type] !== \"undefined\") {\n return routeTypeComparatorValue[route.type];\n }\n // Default the comparator value to a large number (placing the route at the\n // end of the list).\n // eslint-disable-next-line no-console\n console.warn(\"no mode/route type found for route\", route);\n return END_OF_LIST_COMPARATOR_VALUE;\n}\n\n/**\n * Calculates the sort comparator value given two routes based off of route type\n * (OTP mode).\n */\nfunction routeTypeComparator(a: Route, b: Route): number {\n return getRouteTypeComparatorValue(a) - getRouteTypeComparatorValue(b);\n}\n\n/**\n * Determines whether a value is a string that starts with an alphabetic\n * ascii character.\n */\nfunction startsWithAlphabeticCharacter(val: unknown): boolean {\n if (typeof val === \"string\" && val.length > 0) {\n const firstCharCode = val.charCodeAt(0);\n return (\n (firstCharCode >= 65 && firstCharCode <= 90) ||\n (firstCharCode >= 97 && firstCharCode <= 122)\n );\n }\n return false;\n}\n\n/**\n * Sorts routes based off of whether the shortName begins with an alphabetic\n * character. Routes with shortn that do start with an alphabetic character will\n * be prioritized over those that don't.\n */\nfunction alphabeticShortNameComparator(a: Route, b: Route): number {\n const aStartsWithAlphabeticCharacter = startsWithAlphabeticCharacter(\n a.shortName\n );\n const bStartsWithAlphabeticCharacter = startsWithAlphabeticCharacter(\n b.shortName\n );\n\n if (aStartsWithAlphabeticCharacter && bStartsWithAlphabeticCharacter) {\n // both start with an alphabetic character, return equivalence\n return 0;\n }\n // a does start with an alphabetic character, but b does not. Prioritize a\n if (aStartsWithAlphabeticCharacter) return -1;\n // b does start with an alphabetic character, but a does not. Prioritize b\n if (bStartsWithAlphabeticCharacter) return 1;\n // neither route has a shortName that starts with an alphabetic character.\n // Return equivalence\n return 0;\n}\n\n/**\n * Checks whether an appropriate comparison of numeric values can be made for\n * sorting purposes. If both values are not valid numbers according to the\n * isNaN check, then this function returns undefined which indicates that a\n * secondary sorting criteria should be used instead. If one value is valid and\n * the other is not, then the valid value will be given sorting priority. If\n * both values are valid numbers, the difference is obtained as the sort value.\n *\n * An optional argument can be provided which will be used to obtain the\n * comparison value from the comparison function arguments.\n *\n * IMPORTANT: the comparison values must be numeric values or at least be\n * attempted to be converted to numeric values! If one of the arguments is\n * something crazy like an empty string, unexpected behavior will occur because\n * JavaScript.\n *\n * @param {function} [objGetterFn] An optional function to obtain the\n * comparison value from the comparator function arguments\n */\nexport function makeNumericValueComparator(\n objGetterFn?: (item: Route) => number\n) {\n /* Note: Using the global version of isNaN (the Number version behaves differently. */\n /* eslint-disable no-restricted-globals */\n return (a: number, b: number): number => {\n const { aVal, bVal } = getSortValues(objGetterFn, a, b);\n if (typeof aVal !== \"number\" || typeof bVal !== \"number\") return 0;\n\n // if both values aren't valid numbers, use the next sort criteria\n if (isNaN(aVal) && isNaN(bVal)) return 0;\n // b is a valid number, b gets priority\n if (isNaN(aVal)) return 1;\n // a is a valid number, a gets priority\n if (isNaN(bVal)) return -1;\n // a and b are valid numbers, return the sort value\n return aVal - bVal;\n };\n}\n\n/**\n * Create a comparator function that compares string values. The comparison\n * values feed to the sort comparator function are assumed to be objects that\n * will have either undefined, null or string values at the given key. If one\n * object has undefined, null or an empty string, but the other does have a\n * string with length > 0, then that string will get priority.\n *\n * @param {function} [objGetterFn] An optional function to obtain the\n * comparison value from the comparator function arguments\n */\nexport function makeStringValueComparator(\n objGetterFn?: (item: Route) => string\n) {\n return (a: string, b: string): number => {\n const { aVal, bVal } = getSortValues(objGetterFn, a, b);\n // both a and b are uncomparable strings, return equivalent value\n if (!aVal && !bVal) return 0;\n // a is not a comparable string, b gets priority\n if (!aVal) return 1;\n // b is not a comparable string, a gets priority\n if (!bVal) return -1;\n // a and b are comparable strings, return the sort value\n if (aVal < bVal) return -1;\n if (aVal > bVal) return 1;\n return 0;\n };\n}\n\n/**\n * OpenTripPlanner sets the routeSortOrder to -999 by default. So, if that value\n * is encountered, assume that it actually means that the routeSortOrder is not\n * set in the GTFS.\n *\n * See https://github.com/opentripplanner/OpenTripPlanner/issues/2938\n * Also see https://github.com/opentripplanner/otp-react-redux/issues/122\n */\nfunction getRouteSortOrderValue(val: number): number {\n return val === -999 ? undefined : val;\n}\n\n/**\n * Create a multi-criteria sort comparator function composed of other sort\n * comparator functions. Each comparator function will be ran in the order given\n * until a non-zero comparison value is obtained which is then immediately\n * returned. If all comparison functions return equivalence, then the values\n * are assumed to be equivalent.\n */\nfunction makeMultiCriteriaSort(\n ...criteria: ((a: unknown, b: unknown) => number)[]\n) {\n return (a: number, b: number): number => {\n for (let i = 0; i < criteria.length; i++) {\n const curCriteriaComparatorValue = criteria[i](a, b);\n // if the comparison objects are not equivalent, return the value obtained\n // in this current criteria comparison\n if (curCriteriaComparatorValue !== 0) {\n return curCriteriaComparatorValue;\n }\n }\n return 0;\n };\n}\n\n/**\n * Creates a sort comparator function to compares routes for the purposes of\n * sorting and displaying in a user interface. This takes in a single optional\n * argument which should be a list of transitOperators as defined in the config\n * file. Due to GTFS feeds having varying levels of data quality, a multi-\n * criteria sort is needed to account for various differences. The criteria\n * included here are each applied to the routes in the order listed. If a given\n * sort criterion yields equivalence (e.g., two routes have the short name\n * \"20\"), the comparator falls back onto the next sort criterion (e.g., long\n * name). The sort operates on the following values (in order):\n *\n * 1. Transit Operator. The transit operator will be attempted to be obtained\n * for each route. If no argument is provided when creating this comparator\n * function, then routes will be sorted by their agency's name. If an\n * argument is provided and a match is found based off of the route's feed_id\n * and agency_id and a transitOperator's feed_id and agency_id, then the\n * field transitOperator.order will be used as the comparator value as long\n * as it is numeric. If it is not numeric, a value is returned indicating\n * that this transit operator should be placed at the end of the list.\n * 2. sortOrder. Routes that do not have a valid sortOrder will be placed\n * beneath those that do.\n * 3. route type (OTP mode). See routeTypeComparator code for prioritization of\n * route types.\n * 4. shortNames that begin with alphabetic characters. shortNames that do not\n * start with alphabetic characters will be place beneath those that do.\n * 5. shortName as integer. shortNames that cannot be parsed as integers will\n * be placed beneath those that are valid.\n * 6. shortName as string. Routes without shortNames will be placed beneath\n * those with shortNames.\n * 7. longName as string.\n */\nexport function makeRouteComparator(\n transitOperators: TransitOperator[]\n): (a: number, b: number) => number {\n return makeMultiCriteriaSort(\n makeTransitOperatorComparator(transitOperators),\n makeNumericValueComparator(obj => getRouteSortOrderValue(obj.sortOrder)),\n routeTypeComparator,\n alphabeticShortNameComparator,\n makeNumericValueComparator(obj => parseInt(obj.shortName, 10)),\n makeStringValueComparator(obj => obj.shortName),\n makeStringValueComparator(obj => obj.longName)\n );\n}\n\n/**\n * Tests if a pair of colors is readable. If it is, that readable color is returned.\n * If it is not, a more appropriate alternative is returned.\n *\n * Uses algorithm based on combined luminance. Values have been derived from\n * looking at real agency color pairings. These pairings are difficult to\n * generate for, as some colors see both white and black used by different agencies.\n *\n * This method therefore can accept multiple colors (including black and white) for the same background color.\n *\n * @param backgroundColor A hex string, usually the \"routeColor\"\n * @param proposedTextColor A hex string, usually the \"routeTextColor\"\n */\nexport function getMostReadableTextColor(\n backgroundColor: string,\n proposedTextColor?: string\n): string {\n // Sometimes input will defy the method signature. Therefore we need extra fallbacks\n if (!backgroundColor) backgroundColor = \"#333333\";\n if (!proposedTextColor) proposedTextColor = \"#ffffff\";\n\n if (!backgroundColor.startsWith(\"#\")) {\n backgroundColor = `#${backgroundColor}`;\n }\n if (!proposedTextColor.startsWith(\"#\")) {\n proposedTextColor = `#${proposedTextColor}`;\n }\n\n // Check if proposed color is readable\n // Luminance thresholds have been selected based on actual transit agency colors\n const fgLuminance = chroma(proposedTextColor).luminance();\n const bgLuminance = chroma(backgroundColor).luminance();\n if (\n bgLuminance + fgLuminance < 1.41 &&\n bgLuminance + fgLuminance > 0.25 &&\n Math.abs(bgLuminance - fgLuminance) > 0.2\n ) {\n return proposedTextColor;\n }\n\n // Return black or white based on luminance of background color\n // When generating colors, white is preferred.\n return chroma(backgroundColor).luminance() < 0.4 ? \"#ffffff\" : \"#000000\";\n}\n"],"mappings":";;;;;;;;;;;;;AACA,IAAAA,SAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,uCAAuCA,CACrDC,MAAc,EACdC,QAAyB,EACzBC,gBAAmC,EAClB;EACjB,OACEA,gBAAgB,CAACC,IAAI,CACnBC,eAAe,IACbA,eAAe,CAACJ,MAAM,KAAKA,MAAM,IACjCI,eAAe,CAACH,QAAQ,KAAKA,QACjC,CAAC,IAAI,IAAI;AAEb;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASI,yBAAyBA,CACvCC,GAAQ,EACRJ,gBAAmC,EAClB;EACjB,IAAI,CAACI,GAAG,CAACC,OAAO,IAAI,CAACD,GAAG,CAACL,QAAQ,EAAE,OAAO,IAAI;EAC9C,MAAMD,MAAM,GAAGM,GAAG,CAACC,OAAO,CAACC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CA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,EAAE;EAC7C;;EAEA;EACA;EACA,MAAME,WAAW,GAAG,IAAAC,iBAAM,EAACH,iBAAiB,CAAC,CAACI,SAAS,CAAC,CAAC;EACzD,MAAMC,WAAW,GAAG,IAAAF,iBAAM,EAACJ,eAAe,CAAC,CAACK,SAAS,CAAC,CAAC;EACvD,IACEC,WAAW,GAAGH,WAAW,GAAG,IAAI,IAChCG,WAAW,GAAGH,WAAW,GAAG,IAAI,IAChCI,IAAI,CAACC,GAAG,CAACF,WAAW,GAAGH,WAAW,CAAC,GAAG,GAAG,EACzC;IACA,OAAOF,iBAAiB;EAC1B;;EAEA;EACA;EACA,OAAO,IAAAG,iBAAM,EAACJ,eAAe,CAAC,CAACK,SAAS,CAAC,CAAC,GAAG,GAAG,GAAG,SAAS,GAAG,SAAS;AAC1E","ignoreList":[]}
@@ -1 +1 @@
1
- {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI,CAK5D;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,aAAa,GAAE,OAAc,GAAG,OAAO,CAY3E;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAE5C;AAED;;;GAGG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAI/B"}
1
+ {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI,CAO5D;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,aAAa,GAAE,OAAc,GAAG,OAAO,CAY3E;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAI5C;AAED;;;GAGG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAI/B"}
package/lib/storage.js CHANGED
@@ -3,30 +3,29 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.storeItem = storeItem;
7
6
  exports.getItem = getItem;
8
- exports.removeItem = removeItem;
9
7
  exports.randId = randId;
10
-
8
+ exports.removeItem = removeItem;
9
+ exports.storeItem = storeItem;
11
10
  /* eslint-disable no-console */
12
11
  // Prefix to use with local storage keys.
13
12
  const STORAGE_PREFIX = "otp";
13
+
14
14
  /**
15
15
  * Store a javascript object at the specified key.
16
16
  */
17
-
18
17
  function storeItem(key, object) {
19
- window.localStorage.setItem(`${STORAGE_PREFIX}.${key}`, JSON.stringify(object));
18
+ if (window) {
19
+ window.localStorage.setItem(`${STORAGE_PREFIX}.${key}`, JSON.stringify(object));
20
+ }
20
21
  }
22
+
21
23
  /**
22
24
  * Retrieve a javascript object at the specified key. If not found, defaults to
23
25
  * null or, the optionally provided notFoundValue.
24
26
  */
25
-
26
-
27
27
  function getItem(key, notFoundValue = null) {
28
28
  let itemAsString;
29
-
30
29
  try {
31
30
  itemAsString = window.localStorage.getItem(`${STORAGE_PREFIX}.${key}`);
32
31
  const json = JSON.parse(itemAsString);
@@ -38,20 +37,20 @@ function getItem(key, notFoundValue = null) {
38
37
  return notFoundValue;
39
38
  }
40
39
  }
40
+
41
41
  /**
42
42
  * Remove item at specified key.
43
43
  */
44
-
45
-
46
44
  function removeItem(key) {
47
- window.localStorage.removeItem(`${STORAGE_PREFIX}.${key}`);
45
+ if (window) {
46
+ window.localStorage.removeItem(`${STORAGE_PREFIX}.${key}`);
47
+ }
48
48
  }
49
+
49
50
  /**
50
51
  * Generate a random ID. This might not quite be a UUID, but it serves our
51
52
  * purposes for now.
52
53
  */
53
-
54
-
55
54
  function randId() {
56
55
  return Math.random().toString(36).substr(2, 9);
57
56
  }