Files
web/apps/scandic-web/stores/hotel-listing-data/helper.ts
Anton Gunnarsson 08804e8675 Merged in chore/cleanup-scandic-web (pull request #2831)
chore: Cleanup scandic-web

* Remove unused files

* Remove unused and add missing packages

* Remove unused exports


Approved-by: Linus Flood
2025-09-18 15:33:00 +00:00

54 lines
1.4 KiB
TypeScript

import {
type HotelListingHotelData,
type HotelSortItem,
HotelSortOption,
} from "@scandic-hotels/trpc/types/hotel"
const HOTEL_SORTING_STRATEGIES: Partial<
Record<
HotelSortOption,
(a: HotelListingHotelData, b: HotelListingHotelData) => number
>
> = {
[HotelSortOption.Name]: function (a, b) {
return a.hotel.name.localeCompare(b.hotel.name)
},
[HotelSortOption.TripAdvisorRating]: function (a, b) {
return (b.hotel.tripadvisor ?? 0) - (a.hotel.tripadvisor ?? 0)
},
}
export function getFilteredHotels(
hotels: HotelListingHotelData[],
filters: string[]
) {
if (filters.length) {
return hotels.filter(({ hotel }) =>
filters.every((filter) => {
const matchesFacility = hotel.detailedFacilities.some(
(facility) => facility.slug === filter
)
const matchesCountry =
hotel.countryCode.toLowerCase() === filter.toLowerCase()
return matchesFacility || matchesCountry
})
)
}
return hotels
}
export function getSortedHotels(
hotels: HotelListingHotelData[],
sortOption: HotelSortOption
) {
const sortFn = HOTEL_SORTING_STRATEGIES[sortOption]
return sortFn ? [...hotels].sort(sortFn) : hotels
}
export function isValidSortOption(
value: string,
sortItems: HotelSortItem[]
): value is HotelSortOption {
return sortItems.map((item) => item.value).includes(value as HotelSortOption)
}