11import sortBy from 'lodash/sortBy' ;
22
3- /**
4- * @example
5- * const oldRouteCollection = {
6- * [
7- * {
8- * type: 'FutureRoute',
9- * properties: {
10- * layer: 'futureRoute',
11- * origin: {
12- * name: 'Pasila',
13- * localadmin: 'Helsinki',
14- * coordinates: {
15- * lat: 60.198828,
16- * lon: 24.933514,
17- * },
18- * },
19- * destination: {
20- * name: 'Ilmala',
21- * localadmin: 'Helsinki',
22- * coordinates: {
23- * lat: 60.208466,
24- * lon: 24.919756,
25- * },
26- * },
27- * arriveBy: 'true',
28- * time: 1600866900,
29- * url: '/reitti/Pasila%2C%20Helsinki%3A%3A60.198828%2C24.933514/Ilmala%2C%20Helsinki%3A%3A60.208466%2C24.919756?arriveBy=true&time=1600866900',
30- * },
31- * },
32- * {
33- * type: 'FutureRoute',
34- * properties: {
35- * layer: 'futureRoute',
36- * origin: {
37- * name: 'Ilmala',
38- * localadmin: 'Helsinki',
39- * coordinates: {
40- * lat: 60.208466,
41- * lon: 24.919756,
42- * },
43- * },
44- * destination: {
45- * name: 'Pasila',
46- * localadmin: 'Helsinki',
47- * coordinates: {
48- * lat: 60.198828,
49- * lon: 24.933514,
50- * },
51- * },
52- * time: 1600877700,
53- * url: '/reitti/Ilmala%2C%20Helsinki%3A%3A60.208466%2C24.919756/Pasila%2C%20Helsinki%3A%3A60.198828%2C24.933514?arriveBy=true&time=1600877700',
54- * },
55- * },
56- * ],
57- * }
58- *
59- * const newRoute = {
60- * origin: {
61- * address: 'Pasila, Helsinki',
62- * coordinates: { lat: 60.198828, lon: 24.933514 },
63- * },
64- * destination: {
65- * address: 'Myyrmäki, Vantaa',
66- * coordinates: { lat: 60.261238, lon: 24.854782 },
67- * },
68- * arriveBy: false,
69- * time: 1600888888,
70- * };
71- *
72- * //add newRoute to oldRouteCollection
73- * const newRouteCollection = addFutureRoute(newRoute, oldRouteCollection, { prefixItinerarySummary: 'reitti' });
74- *
75- * const url = createUrl(newRoute, { prefixItinerarySummary: 'reitti' });
76- * //'/reitti/Pasila%2C%20Helsinki%3A%3A60.198828%2C24.933514/Myyrmäki%2C%20Vantaa%3A%3A60.261238%2C24.854782?time=1600888888'
77- */
78-
79- function extractRoute ( routeIn ) {
80- let extractedRoute = routeIn ;
81- if ( routeIn . properties ) {
82- const route = routeIn . properties ;
83- const oLoc = route . origin . localadmin ? `, ${ route . origin . localadmin } ` : '' ;
84- const dLoc = route . destination . localadmin
85- ? `, ${ route . destination . localadmin } `
86- : '' ;
87-
88- extractedRoute = {
89- origin : {
90- address : `${ route . origin . name } ${ oLoc } ` ,
91- coordinates : route . origin . coordinates ,
92- } ,
93- destination : {
94- address : `${ route . destination . name } ${ dLoc } ` ,
95- coordinates : route . destination . coordinates ,
96- } ,
97- arriveBy : route . arriveBy ? route . arriveBy : false ,
98- time : route . time ,
99- } ;
100- }
101- return extractedRoute ;
102- }
103-
1043const DEFAULT_ITINERARY_PREFIX = 'reitti' ;
1054
106- export function createUrl ( routeIn , pathOpts ) {
107- const route = extractRoute ( routeIn ) ;
5+ export function createUrl ( item , pathOpts ) {
6+ const props = item . properties ;
1087 if (
109- route &&
110- route . origin &&
111- route . origin . address &&
112- route . origin . coordinates &&
113- route . destination &&
114- route . destination . address &&
115- route . destination . coordinates &&
116- route . time
8+ props . origin ?. coordinates &&
9+ props . destination ?. coordinates &&
10+ props . time
11711 ) {
12+ const oLoc = props . origin . localadmin ? `, ${ props . origin . localadmin } ` : '' ;
13+ const oAddr = `${ props . origin . name } ${ oLoc } ` ;
14+ const dLoc = props . destination . localadmin
15+ ? `, ${ props . destination . localadmin } `
16+ : '' ;
17+ const dAddr = `${ props . destination . name } ${ dLoc } ` ;
18+
11819 let prefix ;
119- if ( pathOpts && pathOpts . itinerarySummaryPrefix ) {
20+ if ( pathOpts ? .itinerarySummaryPrefix ) {
12021 prefix = pathOpts . itinerarySummaryPrefix ;
12122 } else {
12223 prefix = DEFAULT_ITINERARY_PREFIX ;
12324 }
124-
125- let url = `/${ prefix } /` ;
126- // set origin
127- url += `${ encodeURIComponent (
128- `${ route . origin . address } ::${ route . origin . coordinates . lat } ,${ route . origin . coordinates . lon } ` ,
129- ) } /`;
130- // set destination
131- url += encodeURIComponent (
132- `${ route . destination . address } ::${ route . destination . coordinates . lat } ,${ route . destination . coordinates . lon } ` ,
25+ const from = encodeURIComponent (
26+ `${ oAddr } ::${ props . origin . coordinates . lat } ,${ props . origin . coordinates . lon } ` ,
13327 ) ;
134- // set arrive by and time
135- if ( route . arriveBy ) {
136- url += `?arriveBy=true&time=${ route . time } ` ;
137- } else {
138- url += `?time=${ route . time } ` ;
28+ const to = encodeURIComponent (
29+ `${ dAddr } ::${ props . destination . coordinates . lat } ,${ props . destination . coordinates . lon } ` ,
30+ ) ;
31+ let url = `/${ prefix } /${ from } /${ to } ?time=${ props . time } ` ;
32+ if ( props . arriveBy ) {
33+ url += '&arriveBy=true' ;
13934 }
14035 return url ;
14136 }
14237 return '' ;
14338}
14439
145- export function addFutureRoute ( newRoute , routeCollection , pathOpts ) {
146- if ( newRoute && newRoute . time > new Date ( ) . getTime ( ) / 1000 ) {
147- const originAddress = newRoute . origin . address . split ( ', ' ) ;
40+ function searchId ( props ) {
41+ return `${ props . origin . name } , ${ props . origin . localadmin } - ${ props . destination . name } , ${ props . destination . localadmin } ` ;
42+ }
43+
44+ export function addFutureRoute ( item , collection ) {
45+ const now = new Date ( ) . getTime ( ) / 1000 ;
46+ if ( item && item . time > now ) {
47+ const originAddress = item . origin . address . split ( ', ' ) ;
14848 const originName = originAddress [ 0 ] ;
14949 originAddress . shift ( ) ;
15050 const originLocalAdmin =
15151 originAddress . length === 1 ? originAddress [ 0 ] : originAddress . join ( ', ' ) ;
15252
153- const destinationAddress = newRoute . destination . address . split ( ', ' ) ;
53+ const destinationAddress = item . destination . address . split ( ', ' ) ;
15454 const destinationName = destinationAddress [ 0 ] ;
15555 destinationAddress . shift ( ) ;
15656 const destinationLocalAdmin =
@@ -166,31 +66,28 @@ export function addFutureRoute(newRoute, routeCollection, pathOpts) {
16666 name : originName ,
16767 localadmin : originLocalAdmin ,
16868 coordinates : {
169- lat : newRoute . origin . coordinates . lat ,
170- lon : newRoute . origin . coordinates . lon ,
69+ lat : item . origin . coordinates . lat ,
70+ lon : item . origin . coordinates . lon ,
17171 } ,
17272 } ,
17373 destination : {
17474 name : destinationName ,
17575 localadmin : destinationLocalAdmin ,
17676 coordinates : {
177- lat : newRoute . destination . coordinates . lat ,
178- lon : newRoute . destination . coordinates . lon ,
77+ lat : item . destination . coordinates . lat ,
78+ lon : item . destination . coordinates . lon ,
17979 } ,
18080 } ,
181- arriveBy : newRoute . arriveBy ,
182- time : newRoute . time ,
183- url : createUrl ( newRoute , pathOpts ) ,
81+ arriveBy : item . arriveBy ,
82+ time : item . time ,
18483 } ,
18584 } ;
18685
187- const newRouteOriginAndDestination = `${ routeToAdd . properties . origin . name } , ${ routeToAdd . properties . origin . localadmin } - ${ routeToAdd . properties . destination . name } , ${ routeToAdd . properties . destination . localadmin } ` ;
188- const futureRoutes = routeCollection
189- ? routeCollection . filter (
190- r =>
191- r . properties . time >= new Date ( ) . getTime ( ) / 1000 &&
192- `${ r . properties . origin . name } , ${ r . properties . origin . localadmin } - ${ r . properties . destination . name } , ${ r . properties . destination . localadmin } ` !==
193- newRouteOriginAndDestination ,
86+ const newId = searchId ( routeToAdd . properties ) ;
87+
88+ const futureRoutes = collection
89+ ? collection . filter (
90+ r => r . properties . time >= now && searchId ( r . properties ) !== newId ,
19491 )
19592 : [ ] ;
19693 const sortedItems = sortBy (
@@ -203,5 +100,5 @@ export function addFutureRoute(newRoute, routeCollection, pathOpts) {
203100 ) ;
204101 return sortedItems ;
205102 }
206- return routeCollection || JSON . parse ( '[]' ) ;
103+ return collection || [ ] ;
207104}
0 commit comments