@@ -3,9 +3,35 @@ import { getRouteMode } from '../../util/modeUtils';
33import { AlertEntityType , LocationTypes } from '../../constants' ;
44import { stopPagePath , routePagePath } from '../../util/path' ;
55
6+ const sortAlphaNumeric = ( a , b ) => {
7+ const first = typeof a === 'string' ? a . toLowerCase ( ) : a . toString ( ) ;
8+ const second = typeof b === 'string' ? b . toLowerCase ( ) : b . toString ( ) ;
9+
10+ return first . localeCompare ( second ) ;
11+ } ;
12+
13+ const getMode = ( stopOrRoute , config ) => {
14+ const routeMode = getRouteMode ( stopOrRoute , config ) ;
15+ if ( routeMode ) {
16+ return routeMode ;
17+ }
18+
19+ return stopOrRoute ?. vehicleMode ?. toLowerCase ( ) ;
20+ } ;
21+
622const addToModeGroup = (
723 acc ,
8- { mode, id, shortName, name, gtfsId, isStop = false , isStation = false } ,
24+ {
25+ mode,
26+ id,
27+ shortName,
28+ name,
29+ gtfsId,
30+ platformCode,
31+ locationType,
32+ isStop = false ,
33+ isStation = false ,
34+ } ,
935) => {
1036 const url =
1137 isStop || isStation
@@ -18,25 +44,31 @@ const addToModeGroup = (
1844 mode,
1945 isRoute : ! isStop && ! isStation ,
2046 entities : [ ] ,
47+ ids : new Set ( ) ,
48+ platformCode,
49+ locationType,
2150 } ;
2251 }
23- acc [ key ] . entities . push ( {
24- id,
25- name : shortName || name ,
26- url,
27- isStop,
28- isStation,
29- } ) ;
52+ if ( ! acc [ key ] . ids . has ( id ) ) {
53+ acc [ key ] . entities . push ( {
54+ id,
55+ name : shortName || name ,
56+ url,
57+ isStop,
58+ isStation,
59+ } ) ;
60+ acc [ key ] . ids . add ( id ) ;
61+ }
3062} ;
3163
3264const groupEntitiesByMode = ( entities , config ) => {
33- const group = entities
65+ const grouped = entities
3466 . filter ( e => e . __typename !== AlertEntityType . Unknown )
3567 . reduce ( ( acc , e ) => {
3668 if ( ! e . route && ! e . stop ) {
3769 addToModeGroup ( acc , {
3870 ...e ,
39- mode : getRouteMode ( e , config ) || e . vehicleMode ?. toLowerCase ( ) ,
71+ mode : getMode ( e , config ) ,
4072 isStop : ! ! e . locationType ,
4173 isStation : e . locationType === LocationTypes . STATION ,
4274 } ) ;
@@ -46,20 +78,25 @@ const groupEntitiesByMode = (entities, config) => {
4678 if ( e . route ) {
4779 addToModeGroup ( acc , {
4880 ...e . route ,
49- mode : getRouteMode ( e . route , config ) ,
81+ mode : getMode ( e . route , config ) ,
5082 } ) ;
5183 }
5284 if ( e . stop ) {
5385 addToModeGroup ( acc , {
5486 ...e . stop ,
55- mode : e . stop . vehicleMode ?. toLowerCase ( ) ,
87+ mode : getMode ( e . stop , config ) ,
5688 isStop : true ,
5789 isStation : e . locationType === LocationTypes . STATION ,
5890 } ) ;
5991 }
6092 return acc ;
6193 } , { } ) ;
62- return group ;
94+
95+ Object . values ( grouped ) . forEach ( group => {
96+ group . entities . sort ( ( a , b ) => sortAlphaNumeric ( a . name , b . name ) ) ;
97+ } ) ;
98+
99+ return grouped ;
63100} ;
64101
65102export { groupEntitiesByMode } ;
0 commit comments