@opentripplanner/core-utils 14.3.0 → 14.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/route.js CHANGED
@@ -1,10 +1,22 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getMostReadableTextColor = exports.makeRouteComparator = exports.makeMultiCriteriaSort = exports.getRouteSortOrderValue = exports.makeStringValueComparator = exports.makeNumericValueComparator = exports.alphabeticShortNameComparator = exports.routeTypeComparator = exports.makeTransitOperatorComparator = exports.getTransitOperatorFromOtpRoute = exports.getTransitOperatorFromLeg = exports.getTransitOperatorFromFeedIdAndAgencyId = void 0;
7
- const chroma_js_1 = __importDefault(require("chroma-js"));
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.alphabeticShortNameComparator = alphabeticShortNameComparator;
8
+ exports.getMostReadableTextColor = getMostReadableTextColor;
9
+ exports.getRouteSortOrderValue = getRouteSortOrderValue;
10
+ exports.getTransitOperatorFromFeedIdAndAgencyId = getTransitOperatorFromFeedIdAndAgencyId;
11
+ exports.getTransitOperatorFromLeg = getTransitOperatorFromLeg;
12
+ exports.getTransitOperatorFromOtpRoute = getTransitOperatorFromOtpRoute;
13
+ exports.makeMultiCriteriaSort = makeMultiCriteriaSort;
14
+ exports.makeNumericValueComparator = makeNumericValueComparator;
15
+ exports.makeRouteComparator = makeRouteComparator;
16
+ exports.makeStringValueComparator = makeStringValueComparator;
17
+ exports.makeTransitOperatorComparator = makeTransitOperatorComparator;
18
+ exports.routeTypeComparator = routeTypeComparator;
19
+ var _chromaJs = _interopRequireDefault(require("chroma-js"));
8
20
  /**
9
21
  * Returns the transit operator (if an exact match is found) from the transit
10
22
  * operators config value. It is critical to use both the feedId and agencyId in
@@ -18,10 +30,9 @@ const chroma_js_1 = __importDefault(require("chroma-js"));
18
30
  * was found
19
31
  */
20
32
  function getTransitOperatorFromFeedIdAndAgencyId(feedId, agencyId, transitOperators) {
21
- return (transitOperators.find(transitOperator => transitOperator.feedId === feedId &&
22
- transitOperator.agencyId === agencyId) || null);
33
+ return transitOperators.find(transitOperator => transitOperator.feedId === feedId && transitOperator.agencyId === agencyId) || null;
23
34
  }
24
- exports.getTransitOperatorFromFeedIdAndAgencyId = getTransitOperatorFromFeedIdAndAgencyId;
35
+
25
36
  /**
26
37
  * Looks up an operator from the provided leg.
27
38
  *
@@ -31,12 +42,11 @@ exports.getTransitOperatorFromFeedIdAndAgencyId = getTransitOperatorFromFeedIdAn
31
42
  * @return {object} the operator if one was found or null if no match was found
32
43
  */
33
44
  function getTransitOperatorFromLeg(leg, transitOperators) {
34
- if (!leg.routeId || !leg.agencyId)
35
- return null;
36
- const feedId = leg.routeId.split(":")[0];
37
- return getTransitOperatorFromFeedIdAndAgencyId(feedId, leg.agencyId, transitOperators);
45
+ if (!leg.routeId || !leg.agencyId) return null;
46
+ const feedId = leg.routeId.split(":")[0];
47
+ return getTransitOperatorFromFeedIdAndAgencyId(feedId, leg.agencyId, transitOperators);
38
48
  }
39
- exports.getTransitOperatorFromLeg = getTransitOperatorFromLeg;
49
+
40
50
  /**
41
51
  * Looks up an operator from the provided configuration given an OTP route.
42
52
  * NOTE: this assumes the use of the OTP Route model or a modified OTP
@@ -48,34 +58,33 @@ exports.getTransitOperatorFromLeg = getTransitOperatorFromLeg;
48
58
  * @return {object} the operator if one was found or null if no match was found
49
59
  */
50
60
  function getTransitOperatorFromOtpRoute(route, transitOperators) {
51
- if (!route.id)
52
- return null;
53
- const feedId = route.id.split(":")[0];
54
- let agencyId;
55
- if (route.agency) {
56
- // This is returned in OTP2
57
- agencyId = route.agency.id;
58
- }
59
- else if (route.agencyId) {
60
- // This is returned in OTP1
61
- agencyId = route.agencyId;
62
- }
63
- else {
64
- return null;
65
- }
66
- return getTransitOperatorFromFeedIdAndAgencyId(feedId, agencyId, transitOperators);
61
+ if (!route.id) return null;
62
+ const feedId = route.id.split(":")[0];
63
+ let agencyId;
64
+ if (route.agency) {
65
+ // This is returned in OTP2
66
+ agencyId = route.agency.id;
67
+ } else if (route.agencyId) {
68
+ // This is returned in OTP1
69
+ agencyId = route.agencyId;
70
+ } else {
71
+ return null;
72
+ }
73
+ return getTransitOperatorFromFeedIdAndAgencyId(feedId, agencyId, transitOperators);
67
74
  }
68
- exports.getTransitOperatorFromOtpRoute = getTransitOperatorFromOtpRoute;
75
+
69
76
  // The functions below are for enhanced route sorting functions for the route
70
77
  // viewer on OTP-react-redux.
71
78
  // They address route ordering issues discussed in
72
79
  // https://github.com/opentripplanner/otp-react-redux/pull/123 and
73
80
  // https://github.com/opentripplanner/otp-react-redux/pull/124.
81
+
74
82
  /**
75
83
  * A large comparator value that can safely be used in mathematical sort
76
84
  * comparisons to place things at the end of lists
77
85
  */
78
86
  const END_OF_LIST_COMPARATOR_VALUE = 999999999999;
87
+
79
88
  /**
80
89
  * Returns a transit operator comparator value given a route and an optional
81
90
  * transitOperators config value. This function will do its best to handle all
@@ -90,172 +99,180 @@ const END_OF_LIST_COMPARATOR_VALUE = 999999999999;
90
99
  * returned.
91
100
  */
92
101
  function getTransitOperatorComparatorValue(route, transitOperators) {
93
- // if the transitOperators is undefined or has zero length, use the route's
94
- // agency name as the comparator value
95
- if (!transitOperators || transitOperators.length === 0) {
96
- // OTP2 Route
97
- if (route.agency)
98
- return route.agency.name;
99
- // OTP1 Route
100
- if (route.agencyName)
101
- return route.agencyName;
102
- // shouldn't happen as agency names will be defined
103
- return "zzz";
104
- }
105
- // find operator associated with route
106
- const transitOperator = getTransitOperatorFromOtpRoute(route, transitOperators);
107
- // if transit operator not found, return infinity
108
- if (!transitOperator)
109
- return END_OF_LIST_COMPARATOR_VALUE;
110
- // return the transit operator's sort value or END_OF_LIST_COMPARATOR_VALUE if
111
- // the sort value is not a number
112
- return typeof transitOperator.order === "number"
113
- ? transitOperator.order
114
- : END_OF_LIST_COMPARATOR_VALUE;
102
+ // if the transitOperators is undefined or has zero length, use the route's
103
+ // agency name as the comparator value
104
+ if (!transitOperators || transitOperators.length === 0) {
105
+ // OTP2 Route
106
+ if (route.agency) return route.agency.name;
107
+ // OTP1 Route
108
+ if (route.agencyName) return route.agencyName;
109
+ // shouldn't happen as agency names will be defined
110
+ return "zzz";
111
+ }
112
+
113
+ // find operator associated with route
114
+ const transitOperator = getTransitOperatorFromOtpRoute(route, transitOperators);
115
+
116
+ // if transit operator not found, return infinity
117
+ if (!transitOperator) return END_OF_LIST_COMPARATOR_VALUE;
118
+
119
+ // return the transit operator's sort value or END_OF_LIST_COMPARATOR_VALUE if
120
+ // the sort value is not a number
121
+ return typeof transitOperator.order === "number" ? transitOperator.order : END_OF_LIST_COMPARATOR_VALUE;
115
122
  }
123
+
116
124
  /**
117
125
  * Calculates the sort comparator value given two routes based off of the
118
126
  * route's agency and provided transitOperators config data.
119
127
  */
120
128
  function makeTransitOperatorComparator(transitOperators) {
121
- return (a, b) => {
122
- const aVal = getTransitOperatorComparatorValue(a, transitOperators);
123
- const bVal = getTransitOperatorComparatorValue(b, transitOperators);
124
- if (typeof aVal === "string") {
125
- // happens when transitOperators is undefined. Both aVal are guaranteed to
126
- // be strings. Make a string comparison.
127
- if (aVal < bVal)
128
- return -1;
129
- if (aVal > bVal)
130
- return 1;
131
- return 0;
132
- }
133
- // @ts-expect-error transitOperators are defined and therefore a numeric value is guaranteed
134
- // to be returned
135
- return aVal - bVal;
136
- };
129
+ return (a, b) => {
130
+ const aVal = getTransitOperatorComparatorValue(a, transitOperators);
131
+ const bVal = getTransitOperatorComparatorValue(b, transitOperators);
132
+ if (typeof aVal === "string") {
133
+ // happens when transitOperators is undefined. Both aVal are guaranteed to
134
+ // be strings. Make a string comparison.
135
+ if (aVal < bVal) return -1;
136
+ if (aVal > bVal) return 1;
137
+ return 0;
138
+ }
139
+ // @ts-expect-error transitOperators are defined and therefore a numeric value is guaranteed
140
+ // to be returned
141
+ return aVal - bVal;
142
+ };
137
143
  }
138
- exports.makeTransitOperatorComparator = makeTransitOperatorComparator;
144
+
139
145
  /**
140
146
  * Gets the desired sort values according to an optional getter function. If the
141
147
  * getter function is not defined, the original sort values are returned.
142
148
  */
143
149
  function getSortValues(getterFn, a, b) {
144
- let aVal;
145
- let bVal;
146
- if (typeof getterFn === "function") {
147
- aVal = getterFn(a);
148
- bVal = getterFn(b);
149
- }
150
- else {
151
- aVal = a;
152
- bVal = b;
153
- }
154
- return { aVal, bVal };
150
+ let aVal;
151
+ let bVal;
152
+ if (typeof getterFn === "function") {
153
+ aVal = getterFn(a);
154
+ bVal = getterFn(b);
155
+ } else {
156
+ aVal = a;
157
+ bVal = b;
158
+ }
159
+ return {
160
+ aVal,
161
+ bVal
162
+ };
155
163
  }
164
+
156
165
  // Lookup for the sort values associated with various OTP modes.
157
166
  // Note: JSDoc format not used to avoid bug in documentationjs.
158
167
  // https://github.com/documentationjs/documentation/issues/372
159
168
  const modeComparatorValue = {
160
- SUBWAY: 1,
161
- TRAM: 2,
162
- TROLLEYBUS: 9,
163
- RAIL: 3,
164
- GONDOLA: 4,
165
- FERRY: 5,
166
- CABLE_CAR: 6,
167
- FUNICULAR: 7,
168
- BUS: 8
169
+ SUBWAY: 1,
170
+ TRAM: 2,
171
+ TROLLEYBUS: 9,
172
+ RAIL: 3,
173
+ GONDOLA: 4,
174
+ FERRY: 5,
175
+ CABLE_CAR: 6,
176
+ FUNICULAR: 7,
177
+ BUS: 8
169
178
  };
179
+
170
180
  // Lookup that maps route types to the OTP mode sort values.
171
181
  // Note: JSDoc format not used to avoid bug in documentationjs.
172
182
  // https://github.com/documentationjs/documentation/issues/372
173
183
  const routeTypeComparatorValue = {
174
- 0: modeComparatorValue.TRAM,
175
- 1: modeComparatorValue.SUBWAY,
176
- 2: modeComparatorValue.RAIL,
177
- 3: modeComparatorValue.BUS,
178
- 4: modeComparatorValue.FERRY,
179
- 5: modeComparatorValue.CABLE_CAR,
180
- 6: modeComparatorValue.GONDOLA,
181
- 7: modeComparatorValue.FUNICULAR,
182
- // TODO: 11 and 12 are not a part of OTP as of 2019-02-14, but for now just
183
- // associate them with bus/rail.
184
- 11: modeComparatorValue.BUS,
185
- 12: modeComparatorValue.RAIL,
186
- 13: modeComparatorValue.TROLLEYBUS
184
+ 0: modeComparatorValue.TRAM,
185
+ // - Tram, Streetcar, Light rail.
186
+ 1: modeComparatorValue.SUBWAY,
187
+ // - Subway, Metro.
188
+ 2: modeComparatorValue.RAIL,
189
+ // - Rail. Used for intercity or long-distance travel.
190
+ 3: modeComparatorValue.BUS,
191
+ // - Bus.
192
+ 4: modeComparatorValue.FERRY,
193
+ // - Ferry.
194
+ 5: modeComparatorValue.CABLE_CAR,
195
+ // - Cable tram.
196
+ 6: modeComparatorValue.GONDOLA,
197
+ // - Gondola, etc.
198
+ 7: modeComparatorValue.FUNICULAR,
199
+ // - Funicular.
200
+ // TODO: 11 and 12 are not a part of OTP as of 2019-02-14, but for now just
201
+ // associate them with bus/rail.
202
+ 11: modeComparatorValue.BUS,
203
+ // - Trolleybus.
204
+ 12: modeComparatorValue.RAIL,
205
+ // - Monorail.
206
+ 13: modeComparatorValue.TROLLEYBUS
187
207
  };
208
+
188
209
  // Gets a comparator value for a given route's type (OTP mode).
189
210
  // Note: JSDoc format not used to avoid bug in documentationjs.
190
211
  // ttps://github.com/documentationjs/documentation/issues/372
191
212
  function getRouteTypeComparatorValue(route) {
192
- // For some strange reason, the short route response in OTP returns the
193
- // string-based modes, but the long route response returns the
194
- // integer route type. This attempts to account for both of those cases.
195
- if (!route)
196
- throw new Error(`Route is undefined. ${route}`);
197
- if (typeof modeComparatorValue[route.mode] !== "undefined") {
198
- return modeComparatorValue[route.mode];
199
- }
200
- if (typeof routeTypeComparatorValue[route.type] !== "undefined") {
201
- return routeTypeComparatorValue[route.type];
202
- }
203
- // Default the comparator value to a large number (placing the route at the
204
- // end of the list).
205
- // eslint-disable-next-line no-console
206
- console.warn("no mode/route type found for route", route);
207
- return END_OF_LIST_COMPARATOR_VALUE;
213
+ // For some strange reason, the short route response in OTP returns the
214
+ // string-based modes, but the long route response returns the
215
+ // integer route type. This attempts to account for both of those cases.
216
+ if (!route) throw new Error(`Route is undefined. ${route}`);
217
+ if (typeof modeComparatorValue[route.mode] !== "undefined") {
218
+ return modeComparatorValue[route.mode];
219
+ }
220
+ if (typeof routeTypeComparatorValue[route.type] !== "undefined") {
221
+ return routeTypeComparatorValue[route.type];
222
+ }
223
+ // Default the comparator value to a large number (placing the route at the
224
+ // end of the list).
225
+ // eslint-disable-next-line no-console
226
+ console.warn("no mode/route type found for route", route);
227
+ return END_OF_LIST_COMPARATOR_VALUE;
208
228
  }
229
+
209
230
  /**
210
231
  * Calculates the sort comparator value given two routes based off of route type
211
232
  * (OTP mode).
212
233
  */
213
234
  function routeTypeComparator(a, b) {
214
- return getRouteTypeComparatorValue(a) - getRouteTypeComparatorValue(b);
235
+ return getRouteTypeComparatorValue(a) - getRouteTypeComparatorValue(b);
215
236
  }
216
- exports.routeTypeComparator = routeTypeComparator;
237
+
217
238
  /**
218
239
  * Determines whether a value is a string that starts with an alphabetic
219
240
  * ascii character.
220
241
  */
221
242
  function startsWithAlphabeticCharacter(val) {
222
- if (typeof val === "string" && val.length > 0) {
223
- const firstCharCode = val.charCodeAt(0);
224
- return ((firstCharCode >= 65 && firstCharCode <= 90) ||
225
- (firstCharCode >= 97 && firstCharCode <= 122));
226
- }
227
- return false;
243
+ if (typeof val === "string" && val.length > 0) {
244
+ const firstCharCode = val.charCodeAt(0);
245
+ return firstCharCode >= 65 && firstCharCode <= 90 || firstCharCode >= 97 && firstCharCode <= 122;
246
+ }
247
+ return false;
228
248
  }
249
+
229
250
  /**
230
251
  * Sorts routes based off of whether the shortName begins with an alphabetic
231
252
  * character. Routes with shortn that do start with an alphabetic character will
232
253
  * be prioritized over those that don't.
233
254
  */
234
255
  function alphabeticShortNameComparator(a, b) {
235
- const aStartsWithAlphabeticCharacter = startsWithAlphabeticCharacter(a.shortName);
236
- const bStartsWithAlphabeticCharacter = startsWithAlphabeticCharacter(b.shortName);
237
- if (aStartsWithAlphabeticCharacter && bStartsWithAlphabeticCharacter) {
238
- // both start with an alphabetic character, return equivalence
239
- return 0;
240
- }
241
- // a does start with an alphabetic character, but b does not. Prioritize a
242
- if (aStartsWithAlphabeticCharacter)
243
- return -1;
244
- // b does start with an alphabetic character, but a does not. Prioritize b
245
- if (bStartsWithAlphabeticCharacter)
246
- return 1;
247
- // neither route has a shortName that starts with an alphabetic character.
248
- // Return equivalence
256
+ const aStartsWithAlphabeticCharacter = startsWithAlphabeticCharacter(a.shortName);
257
+ const bStartsWithAlphabeticCharacter = startsWithAlphabeticCharacter(b.shortName);
258
+ if (aStartsWithAlphabeticCharacter && bStartsWithAlphabeticCharacter) {
259
+ // both start with an alphabetic character, return equivalence
249
260
  return 0;
261
+ }
262
+ // a does start with an alphabetic character, but b does not. Prioritize a
263
+ if (aStartsWithAlphabeticCharacter) return -1;
264
+ // b does start with an alphabetic character, but a does not. Prioritize b
265
+ if (bStartsWithAlphabeticCharacter) return 1;
266
+ // neither route has a shortName that starts with an alphabetic character.
267
+ // Return equivalence
268
+ return 0;
250
269
  }
251
- exports.alphabeticShortNameComparator = alphabeticShortNameComparator;
252
- const isNullOrNaN = (val) => {
253
- // isNaN(null) returns false so we have to check for null explicitly.
254
- // Note: Using the global version of isNaN (the Number version behaves differently.
255
- // eslint-disable-next-line no-restricted-globals
256
- if (typeof val === null || isNaN(val))
257
- return true;
258
- return typeof val !== "number";
270
+ const isNullOrNaN = val => {
271
+ // isNaN(null) returns false so we have to check for null explicitly.
272
+ // Note: Using the global version of isNaN (the Number version behaves differently.
273
+ // eslint-disable-next-line no-restricted-globals
274
+ if (typeof val === null || isNaN(val)) return true;
275
+ return typeof val !== "number";
259
276
  };
260
277
  /**
261
278
  * Checks whether an appropriate comparison of numeric values can be made for
@@ -277,24 +294,28 @@ const isNullOrNaN = (val) => {
277
294
  * comparison value from the comparator function arguments
278
295
  */
279
296
  function makeNumericValueComparator(objGetterFn) {
280
- return (a, b) => {
281
- const { aVal, bVal } = getSortValues(objGetterFn, a, b);
282
- // if both values aren't valid numbers, use the next sort criteria
283
- if (isNullOrNaN(aVal) && isNullOrNaN(bVal)) {
284
- return 0;
285
- }
286
- // b is a valid number, b gets priority
287
- if (isNullOrNaN(aVal))
288
- return 1;
289
- // a is a valid number, a gets priority
290
- if (isNullOrNaN(bVal))
291
- return -1;
292
- // a and b are valid numbers, return the sort value
293
- // @ts-expect-error We know from the checks above that both aVal and bVal are valid numbers.
294
- return aVal - bVal;
295
- };
297
+ return (a, b) => {
298
+ const {
299
+ aVal,
300
+ bVal
301
+ } = getSortValues(objGetterFn, a, b);
302
+
303
+ // if both values aren't valid numbers, use the next sort criteria
304
+ if (isNullOrNaN(aVal) && isNullOrNaN(bVal)) {
305
+ return 0;
306
+ }
307
+ // b is a valid number, b gets priority
308
+ if (isNullOrNaN(aVal)) return 1;
309
+
310
+ // a is a valid number, a gets priority
311
+ if (isNullOrNaN(bVal)) return -1;
312
+
313
+ // a and b are valid numbers, return the sort value
314
+ // @ts-expect-error We know from the checks above that both aVal and bVal are valid numbers.
315
+ return aVal - bVal;
316
+ };
296
317
  }
297
- exports.makeNumericValueComparator = makeNumericValueComparator;
318
+
298
319
  /**
299
320
  * Create a comparator function that compares string values. The comparison
300
321
  * values feed to the sort comparator function are assumed to be objects that
@@ -306,26 +327,24 @@ exports.makeNumericValueComparator = makeNumericValueComparator;
306
327
  * comparison value from the comparator function arguments
307
328
  */
308
329
  function makeStringValueComparator(objGetterFn) {
309
- return (a, b) => {
310
- const { aVal, bVal } = getSortValues(objGetterFn, a, b);
311
- // both a and b are uncomparable strings, return equivalent value
312
- if (!aVal && !bVal)
313
- return 0;
314
- // a is not a comparable string, b gets priority
315
- if (!aVal)
316
- return 1;
317
- // b is not a comparable string, a gets priority
318
- if (!bVal)
319
- return -1;
320
- // a and b are comparable strings, return the sort value
321
- if (aVal < bVal)
322
- return -1;
323
- if (aVal > bVal)
324
- return 1;
325
- return 0;
326
- };
330
+ return (a, b) => {
331
+ const {
332
+ aVal,
333
+ bVal
334
+ } = getSortValues(objGetterFn, a, b);
335
+ // both a and b are uncomparable strings, return equivalent value
336
+ if (!aVal && !bVal) return 0;
337
+ // a is not a comparable string, b gets priority
338
+ if (!aVal) return 1;
339
+ // b is not a comparable string, a gets priority
340
+ if (!bVal) return -1;
341
+ // a and b are comparable strings, return the sort value
342
+ if (aVal < bVal) return -1;
343
+ if (aVal > bVal) return 1;
344
+ return 0;
345
+ };
327
346
  }
328
- exports.makeStringValueComparator = makeStringValueComparator;
347
+
329
348
  /**
330
349
  * OTP1 sets the routeSortOrder to -999 by default. If we're encountering that value in OTP1,
331
350
  * assume that it actually means that the route sortOrder is not set in the GTFS. If we encounter
@@ -336,14 +355,16 @@ exports.makeStringValueComparator = makeStringValueComparator;
336
355
  * This was updated in OTP2 TO be empty by default. https://docs.opentripplanner.org/en/v2.3.0/OTP2-MigrationGuide/#:~:text=the%20Alerts-,Changes%20to%20the%20Index%20API,-Error%20handling%20is
337
356
  */
338
357
  function getRouteSortOrderValue(route) {
339
- const isOTP1 = !!route.agencyId;
340
- const { sortOrder } = route;
341
- if ((isOTP1 && sortOrder === -999) || sortOrder === undefined) {
342
- return null;
343
- }
344
- return sortOrder;
358
+ const isOTP1 = !!route.agencyId;
359
+ const {
360
+ sortOrder
361
+ } = route;
362
+ if (isOTP1 && sortOrder === -999 || sortOrder === undefined) {
363
+ return null;
364
+ }
365
+ return sortOrder;
345
366
  }
346
- exports.getRouteSortOrderValue = getRouteSortOrderValue;
367
+
347
368
  /**
348
369
  * Create a multi-criteria sort comparator function composed of other sort
349
370
  * comparator functions. Each comparator function will be ran in the order given
@@ -352,19 +373,19 @@ exports.getRouteSortOrderValue = getRouteSortOrderValue;
352
373
  * are assumed to be equivalent.
353
374
  */
354
375
  function makeMultiCriteriaSort(...criteria) {
355
- return (a, b) => {
356
- for (let i = 0; i < criteria.length; i++) {
357
- const curCriteriaComparatorValue = criteria[i](a, b);
358
- // if the comparison objects are not equivalent, return the value obtained
359
- // in this current criteria comparison
360
- if (curCriteriaComparatorValue !== 0) {
361
- return curCriteriaComparatorValue;
362
- }
363
- }
364
- return 0;
365
- };
376
+ return (a, b) => {
377
+ for (let i = 0; i < criteria.length; i++) {
378
+ const curCriteriaComparatorValue = criteria[i](a, b);
379
+ // if the comparison objects are not equivalent, return the value obtained
380
+ // in this current criteria comparison
381
+ if (curCriteriaComparatorValue !== 0) {
382
+ return curCriteriaComparatorValue;
383
+ }
384
+ }
385
+ return 0;
386
+ };
366
387
  }
367
- exports.makeMultiCriteriaSort = makeMultiCriteriaSort;
388
+
368
389
  /**
369
390
  * Creates a sort comparator function to compares routes for the purposes of
370
391
  * sorting and displaying in a user interface. This takes in a single optional
@@ -397,9 +418,9 @@ exports.makeMultiCriteriaSort = makeMultiCriteriaSort;
397
418
  * 7. longName as string.
398
419
  */
399
420
  function makeRouteComparator(transitOperators) {
400
- return makeMultiCriteriaSort(makeTransitOperatorComparator(transitOperators), makeNumericValueComparator(obj => getRouteSortOrderValue(obj)), routeTypeComparator, alphabeticShortNameComparator, makeNumericValueComparator(obj => parseInt(obj.shortName, 10)), makeStringValueComparator(obj => obj.shortName), makeStringValueComparator(obj => obj.longName));
421
+ return makeMultiCriteriaSort(makeTransitOperatorComparator(transitOperators), makeNumericValueComparator(obj => getRouteSortOrderValue(obj)), routeTypeComparator, alphabeticShortNameComparator, makeNumericValueComparator(obj => parseInt(obj.shortName, 10)), makeStringValueComparator(obj => obj.shortName), makeStringValueComparator(obj => obj.longName));
401
422
  }
402
- exports.makeRouteComparator = makeRouteComparator;
423
+
403
424
  /**
404
425
  * Tests if a pair of colors is readable. If it is, that readable color is returned.
405
426
  * If it is not, a more appropriate alternative is returned.
@@ -414,29 +435,26 @@ exports.makeRouteComparator = makeRouteComparator;
414
435
  * @param proposedTextColor A hex string, usually the "routeTextColor"
415
436
  */
416
437
  function getMostReadableTextColor(backgroundColor, proposedTextColor) {
417
- // Sometimes input will defy the method signature. Therefore we need extra fallbacks
418
- if (!backgroundColor)
419
- backgroundColor = "#333333";
420
- if (!proposedTextColor)
421
- proposedTextColor = "#ffffff";
422
- if (!backgroundColor.startsWith("#")) {
423
- backgroundColor = `#${backgroundColor}`;
424
- }
425
- if (!proposedTextColor.startsWith("#")) {
426
- proposedTextColor = `#${proposedTextColor}`;
427
- }
428
- // Check if proposed color is readable
429
- // Luminance thresholds have been selected based on actual transit agency colors
430
- const fgLuminance = (0, chroma_js_1.default)(proposedTextColor).luminance();
431
- const bgLuminance = (0, chroma_js_1.default)(backgroundColor).luminance();
432
- if (bgLuminance + fgLuminance < 1.41 &&
433
- bgLuminance + fgLuminance > 0.25 &&
434
- Math.abs(bgLuminance - fgLuminance) > 0.2) {
435
- return proposedTextColor;
436
- }
437
- // Return black or white based on luminance of background color
438
- // When generating colors, white is preferred.
439
- return (0, chroma_js_1.default)(backgroundColor).luminance() < 0.4 ? "#ffffff" : "#000000";
438
+ // Sometimes input will defy the method signature. Therefore we need extra fallbacks
439
+ if (!backgroundColor) backgroundColor = "#333333";
440
+ if (!proposedTextColor) proposedTextColor = "#ffffff";
441
+ if (!backgroundColor.startsWith("#")) {
442
+ backgroundColor = `#${backgroundColor}`;
443
+ }
444
+ if (!proposedTextColor.startsWith("#")) {
445
+ proposedTextColor = `#${proposedTextColor}`;
446
+ }
447
+
448
+ // Check if proposed color is readable
449
+ // Luminance thresholds have been selected based on actual transit agency colors
450
+ const fgLuminance = (0, _chromaJs.default)(proposedTextColor).luminance();
451
+ const bgLuminance = (0, _chromaJs.default)(backgroundColor).luminance();
452
+ if (bgLuminance + fgLuminance < 1.41 && bgLuminance + fgLuminance > 0.25 && Math.abs(bgLuminance - fgLuminance) > 0.2) {
453
+ return proposedTextColor;
454
+ }
455
+
456
+ // Return black or white based on luminance of background color
457
+ // When generating colors, white is preferred.
458
+ return (0, _chromaJs.default)(backgroundColor).luminance() < 0.4 ? "#ffffff" : "#000000";
440
459
  }
441
- exports.getMostReadableTextColor = getMostReadableTextColor;
442
460
  //# sourceMappingURL=route.js.map