Files
the-biergarten-app/web/frontend/app/features/breweries/utils/distance.ts

10 lines
354 B
TypeScript

export type DistanceUnit = 'km' | 'mi';
const METRES_PER_MILE = 1609.344;
export const formatDistance = (metres: number, unit: DistanceUnit = 'km'): string => {
const value = unit === 'mi' ? metres / METRES_PER_MILE : metres / 1000;
const rounded = value < 10 ? value.toFixed(1) : String(Math.round(value));
return `${rounded} ${unit}`;
};