mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 06:51:13 -05:00
Fix unit conversions in forms
This commit is contained in:
129
src/lib/units.ts
129
src/lib/units.ts
@@ -75,4 +75,133 @@ 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';
|
||||
}
|
||||
|
||||
// Unit type detection
|
||||
export type UnitType = 'speed' | 'distance' | 'height' | 'weight' | 'unknown';
|
||||
|
||||
export function detectUnitType(unit: string): UnitType {
|
||||
const normalized = unit.toLowerCase().trim();
|
||||
|
||||
// Speed units
|
||||
if (['km/h', 'kmh', 'kph', 'mph', 'm/s', 'ms'].includes(normalized)) {
|
||||
return 'speed';
|
||||
}
|
||||
|
||||
// Distance units (meters/feet)
|
||||
if (['m', 'meter', 'meters', 'metre', 'metres', 'ft', 'feet', 'foot'].includes(normalized)) {
|
||||
return 'distance';
|
||||
}
|
||||
|
||||
// Height units (cm/inches)
|
||||
if (['cm', 'centimeter', 'centimeters', 'in', 'inch', 'inches'].includes(normalized)) {
|
||||
return 'height';
|
||||
}
|
||||
|
||||
// Weight units
|
||||
if (['kg', 'kilogram', 'kilograms', 'lb', 'lbs', 'pound', 'pounds'].includes(normalized)) {
|
||||
return 'weight';
|
||||
}
|
||||
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
// Convert any value to metric based on its unit
|
||||
export function convertValueToMetric(value: number, unit: string): number {
|
||||
const normalized = unit.toLowerCase().trim();
|
||||
|
||||
// Speed conversions to km/h
|
||||
if (normalized === 'mph') {
|
||||
return Math.round(value / 0.621371);
|
||||
}
|
||||
if (['m/s', 'ms'].includes(normalized)) {
|
||||
return Math.round(value * 3.6);
|
||||
}
|
||||
if (['km/h', 'kmh', 'kph'].includes(normalized)) {
|
||||
return Math.round(value);
|
||||
}
|
||||
|
||||
// Distance conversions to meters
|
||||
if (['ft', 'feet', 'foot'].includes(normalized)) {
|
||||
return Math.round(value / 3.28084);
|
||||
}
|
||||
if (['m', 'meter', 'meters', 'metre', 'metres'].includes(normalized)) {
|
||||
return Math.round(value);
|
||||
}
|
||||
|
||||
// Height conversions to cm
|
||||
if (['in', 'inch', 'inches'].includes(normalized)) {
|
||||
return Math.round(value / 0.393701);
|
||||
}
|
||||
if (['cm', 'centimeter', 'centimeters'].includes(normalized)) {
|
||||
return Math.round(value);
|
||||
}
|
||||
|
||||
// Weight conversions to kg
|
||||
if (['lb', 'lbs', 'pound', 'pounds'].includes(normalized)) {
|
||||
return Math.round(value / 2.20462);
|
||||
}
|
||||
if (['kg', 'kilogram', 'kilograms'].includes(normalized)) {
|
||||
return Math.round(value);
|
||||
}
|
||||
|
||||
// Unknown unit, return as-is
|
||||
return value;
|
||||
}
|
||||
|
||||
// Convert metric value to target unit
|
||||
export function convertValueFromMetric(value: number, targetUnit: string, metricUnit: string): number {
|
||||
const normalized = targetUnit.toLowerCase().trim();
|
||||
const metricNormalized = metricUnit.toLowerCase().trim();
|
||||
|
||||
// Speed conversions from km/h
|
||||
if (metricNormalized === 'km/h' || metricNormalized === 'kmh' || metricNormalized === 'kph') {
|
||||
if (normalized === 'mph') {
|
||||
return Math.round(value * 0.621371);
|
||||
}
|
||||
if (normalized === 'm/s' || normalized === 'ms') {
|
||||
return Math.round(value / 3.6);
|
||||
}
|
||||
}
|
||||
|
||||
// Distance conversions from meters
|
||||
if (metricNormalized === 'm' || metricNormalized === 'meter' || metricNormalized === 'meters') {
|
||||
if (['ft', 'feet', 'foot'].includes(normalized)) {
|
||||
return Math.round(value * 3.28084);
|
||||
}
|
||||
}
|
||||
|
||||
// Height conversions from cm
|
||||
if (metricNormalized === 'cm' || metricNormalized === 'centimeter' || metricNormalized === 'centimeters') {
|
||||
if (['in', 'inch', 'inches'].includes(normalized)) {
|
||||
return Math.round(value * 0.393701);
|
||||
}
|
||||
}
|
||||
|
||||
// Weight conversions from kg
|
||||
if (metricNormalized === 'kg' || metricNormalized === 'kilogram' || metricNormalized === 'kilograms') {
|
||||
if (['lb', 'lbs', 'pound', 'pounds'].includes(normalized)) {
|
||||
return Math.round(value * 2.20462);
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
// Get metric unit for a given unit type
|
||||
export function getMetricUnit(unit: string): string {
|
||||
const unitType = detectUnitType(unit);
|
||||
|
||||
switch (unitType) {
|
||||
case 'speed':
|
||||
return 'km/h';
|
||||
case 'distance':
|
||||
return 'm';
|
||||
case 'height':
|
||||
return 'cm';
|
||||
case 'weight':
|
||||
return 'kg';
|
||||
default:
|
||||
return unit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user