Skip to content

feat(itinerary): add ability to sort itineraries by fare #1411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 23, 2025
Merged
1 change: 1 addition & 0 deletions i18n/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ components:
selectCost: Cost
selectDepartureTime: Departure time
selectDuration: Duration
selectFare: Fare
selectWalkTime: Walk time
sortResults: Sort results
viewAll: View all options
Expand Down
9 changes: 8 additions & 1 deletion lib/components/util/sortOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const sortOptions = (
'ARRIVALTIME',
'WALKTIME',
'COST',
'DEPARTURETIME'
'DEPARTURETIME',
'FARE'
]
): SortOptionEntry[] => {
const sortOptionsArray: SortOptionEntry[] = [
Expand Down Expand Up @@ -51,6 +52,12 @@ export const sortOptions = (
id: 'components.NarrativeItinerariesHeader.selectCost'
}),
value: 'COST'
},
{
text: intl.formatMessage({
id: 'components.NarrativeItinerariesHeader.selectFare'
}),
value: 'FARE'
}
]

Expand Down
1 change: 1 addition & 0 deletions lib/util/config-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export type ItinerarySortOption =
| 'WALKTIME'
| 'COST'
| 'DEPARTURETIME'
| 'FARE'

export interface ItineraryCostWeights {
driveReluctance: number
Expand Down
5 changes: 4 additions & 1 deletion lib/util/itinerary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface ItineraryWithCO2Info extends Itinerary {
export interface ItineraryWithSortingCosts extends Itinerary {
rank: number
totalFare: number
transitFare?: number
}

export interface ItineraryFareSummary {
Expand Down Expand Up @@ -453,10 +454,12 @@ export function addSortingCosts<T extends Itinerary>(
totalFareResult === null ? Number.MAX_VALUE : totalFareResult

const rank = calculateItineraryCost(itinerary, config)
const transitFare = getFare(itinerary).transitFare || 0
return {
...itinerary,
rank,
totalFare
totalFare,
transitFare
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/util/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export function sortItineraries(type, direction, a, b, config) {
return dirFactor * (a.duration - b.duration)
case 'COST':
return dirFactor * (a.totalFare - b.totalFare)
case 'FARE':
return dirFactor * (a.transitFare - b.transitFare)
default:
if (type !== 'BEST')
console.warn(`Sort (${type}) not supported. Defaulting to BEST.`)
Expand Down
Loading