Files
web/packages/trpc/lib/utils/getSortedCities.ts
Erik Tiekstra fa7214cb58 Feat/SW-2271 hotel list filtering
* feat(SW-2271): Changes to hotel data types in preperation for filtering
* feat(SW-2271): Added filter and sort functionality

Approved-by: Matilda Landström
2025-07-04 09:27:20 +00:00

35 lines
903 B
TypeScript

import { HotelSortOption } from "../types/hotel"
import type { DestinationCityListItem } from "../types/destinationCityPage"
const CITY_SORTING_STRATEGIES: Partial<
Record<
HotelSortOption,
(a: DestinationCityListItem, b: DestinationCityListItem) => number
>
> = {
[HotelSortOption.Name]: function (a, b) {
return a.cityName.localeCompare(b.cityName)
},
[HotelSortOption.Recommended]: function (a, b) {
if (a.sort_order === null && b.sort_order === null) {
return a.cityName.localeCompare(b.cityName)
}
if (a.sort_order === null) {
return 1
}
if (b.sort_order === null) {
return -1
}
return b.sort_order - a.sort_order
},
}
export function getSortedCities(
cities: DestinationCityListItem[],
sortOption: HotelSortOption
) {
const sortFn = CITY_SORTING_STRATEGIES[sortOption]
return sortFn ? cities.sort(sortFn) : cities
}