mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 08:51:16 -05:00
Add unit preferences to user settings
This commit is contained in:
56
src/lib/units.ts
Normal file
56
src/lib/units.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
export type MeasurementSystem = 'metric' | 'imperial';
|
||||
|
||||
export interface UnitPreferences {
|
||||
measurement_system: MeasurementSystem;
|
||||
temperature: 'celsius' | 'fahrenheit';
|
||||
auto_detect: boolean;
|
||||
}
|
||||
|
||||
// Speed conversions
|
||||
export function convertSpeed(kmh: number, system: MeasurementSystem): number {
|
||||
if (system === 'imperial') {
|
||||
return Math.round(kmh * 0.621371);
|
||||
}
|
||||
return Math.round(kmh);
|
||||
}
|
||||
|
||||
// Distance conversions (meters to feet)
|
||||
export function convertDistance(meters: number, system: MeasurementSystem): number {
|
||||
if (system === 'imperial') {
|
||||
return Math.round(meters * 3.28084);
|
||||
}
|
||||
return Math.round(meters);
|
||||
}
|
||||
|
||||
// Height conversions (cm to inches)
|
||||
export function convertHeight(cm: number, system: MeasurementSystem): number {
|
||||
if (system === 'imperial') {
|
||||
return Math.round(cm * 0.393701);
|
||||
}
|
||||
return Math.round(cm);
|
||||
}
|
||||
|
||||
// Get unit labels
|
||||
export function getSpeedUnit(system: MeasurementSystem): string {
|
||||
return system === 'imperial' ? 'mph' : 'km/h';
|
||||
}
|
||||
|
||||
export function getDistanceUnit(system: MeasurementSystem): string {
|
||||
return system === 'imperial' ? 'ft' : 'm';
|
||||
}
|
||||
|
||||
export function getHeightUnit(system: MeasurementSystem): string {
|
||||
return system === 'imperial' ? 'in' : 'cm';
|
||||
}
|
||||
|
||||
export function getShortDistanceUnit(system: MeasurementSystem): string {
|
||||
return system === 'imperial' ? 'ft' : 'm';
|
||||
}
|
||||
|
||||
// Countries that primarily use imperial system
|
||||
export const IMPERIAL_COUNTRIES = ['US', 'LR', 'MM'];
|
||||
|
||||
// Detect measurement system from country code
|
||||
export function getMeasurementSystemFromCountry(countryCode: string): MeasurementSystem {
|
||||
return IMPERIAL_COUNTRIES.includes(countryCode.toUpperCase()) ? 'imperial' : 'metric';
|
||||
}
|
||||
Reference in New Issue
Block a user