@@ -11,7 +11,12 @@ import {
1111import { PlannerMessageType , ExtendedRouteTypes } from '../../constants' ;
1212import { addAnalyticsEvent } from '../../util/analyticsUtils' ;
1313import { boundWithMinimumArea } from '../../util/geo-utils' ;
14- import { compressLegs , getTotalBikingDistance } from '../../util/legUtils' ;
14+ import {
15+ compressLegs ,
16+ getTotalBikingDistance ,
17+ getTotalTransitLegsInExternalFlexTransitItinerary ,
18+ getTotalTransitLegsInInternalFlexTransitItinerary ,
19+ } from '../../util/legUtils' ;
1520import { getMapLayerOptions } from '../../util/mapLayerUtils' ;
1621import { getDefaultSettings , getSettings } from '../../util/planParamUtil' ;
1722import { getStartTimeWithColon } from '../../util/timeUtils' ;
@@ -373,6 +378,7 @@ export function scooterEdges(edges, allowDirectScooterJourneys) {
373378
374379 return filteredEdges ;
375380}
381+
376382/** Filters away itineraries that are not flex */
377383export function flexEdges ( edges ) {
378384 if ( ! edges ) {
@@ -409,9 +415,6 @@ export function filterItineraries(edges, modes) {
409415 ) ;
410416}
411417
412- /**
413- * Filters itineraries that are not the right route type
414- */
415418export function filterItinerariesByRouteType (
416419 edges ,
417420 types ,
@@ -429,6 +432,28 @@ export function filterItinerariesByRouteType(
429432 ) ;
430433}
431434
435+ /**
436+ * This function filters away itineraries that have a greater count than the
437+ * itinerary with the lowest count. The count is determined by the countFunction.
438+ */
439+ export function filterItinerariesKeepOnlyLeastCount ( edges , countFunction ) {
440+ if ( ! edges || edges . length === 0 ) {
441+ return [ ] ;
442+ }
443+
444+ let leastAmountOfTransitLegs = countFunction ( edges [ 0 ] . node ) ;
445+ edges . forEach ( edge => {
446+ const amountOfTransitLegs = countFunction ( edge . node ) ;
447+ if ( leastAmountOfTransitLegs > amountOfTransitLegs ) {
448+ leastAmountOfTransitLegs = amountOfTransitLegs ;
449+ }
450+ } ) ;
451+
452+ return edges . filter (
453+ edge => countFunction ( edge . node ) === leastAmountOfTransitLegs ,
454+ ) ;
455+ }
456+
432457/**
433458 * Pick combination of itineraries for bike and transit
434459 */
@@ -564,10 +589,13 @@ export function mergeExternalTransitPlan(
564589 allowedExternalFlexRouteTypes ,
565590 includeTaxiSuggestions ,
566591) {
567- const externalTransitEdges = filterItinerariesByRouteType (
568- externalPlan . edges ,
569- allowedExternalFlexRouteTypes ,
570- includeTaxiSuggestions ,
592+ const externalTransitEdges = filterItinerariesKeepOnlyLeastCount (
593+ filterItinerariesByRouteType (
594+ externalPlan . edges ,
595+ allowedExternalFlexRouteTypes ,
596+ includeTaxiSuggestions ,
597+ ) ,
598+ getTotalTransitLegsInExternalFlexTransitItinerary ,
571599 ) ;
572600 return sortAndMergePlans ( externalTransitEdges , transitPlan , arriveBy ) ;
573601}
@@ -595,7 +623,10 @@ export function mergeFlexPlan(
595623 arriveBy ,
596624 maxAdditionalEdges = 1 ,
597625) {
598- const edges = flexEdges ( flexPlan . edges ) ;
626+ const edges = filterItinerariesKeepOnlyLeastCount (
627+ flexEdges ( flexPlan . edges ) ,
628+ getTotalTransitLegsInInternalFlexTransitItinerary ,
629+ ) ;
599630 return sortAndMergePlans ( edges , plan , arriveBy , maxAdditionalEdges ) ;
600631}
601632
0 commit comments