Many more changes for btter experience.
This commit is contained in:
parent
87059a62e2
commit
e2a95252bb
16 changed files with 385 additions and 72 deletions
57
frontend/src/utils/units.ts
Normal file
57
frontend/src/utils/units.ts
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
export type UnitPreference = 'METRIC' | 'IMPERIAL'
|
||||
|
||||
const KM_TO_MILES = 0.621371
|
||||
const METERS_TO_FEET = 3.28084
|
||||
|
||||
export function formatDistance(km: number, unit: UnitPreference): string {
|
||||
if (unit === 'IMPERIAL') {
|
||||
if (km < 0.1) {
|
||||
const feet = km * 1000 * METERS_TO_FEET
|
||||
return `${Math.round(feet)} ft`
|
||||
}
|
||||
const miles = km * KM_TO_MILES
|
||||
return `${miles.toFixed(2)} mi`
|
||||
}
|
||||
if (km < 1) {
|
||||
const meters = km * 1000
|
||||
return `${Math.round(meters)} m`
|
||||
}
|
||||
return `${km.toFixed(2)} km`
|
||||
}
|
||||
|
||||
export function formatDistanceShort(km: number, unit: UnitPreference): string {
|
||||
if (unit === 'IMPERIAL') {
|
||||
const miles = km * KM_TO_MILES
|
||||
if (miles < 0.1) {
|
||||
return `${Math.round(miles * 5280)} ft`
|
||||
}
|
||||
return `${miles.toFixed(1)} mi`
|
||||
}
|
||||
if (km < 1) {
|
||||
return `${Math.round(km * 1000)} m`
|
||||
}
|
||||
return `${km.toFixed(1)} km`
|
||||
}
|
||||
|
||||
export function formatRadius(meters: number, unit: UnitPreference): string {
|
||||
if (unit === 'IMPERIAL') {
|
||||
const feet = meters * METERS_TO_FEET
|
||||
if (feet < 5280) {
|
||||
return `${Math.round(feet)} ft`
|
||||
}
|
||||
const miles = feet / 5280
|
||||
return `${miles.toFixed(1)} mi`
|
||||
}
|
||||
if (meters < 1000) {
|
||||
return `${Math.round(meters)} m`
|
||||
}
|
||||
return `${(meters / 1000).toFixed(1)} km`
|
||||
}
|
||||
|
||||
export function kmToMiles(km: number): number {
|
||||
return km * KM_TO_MILES
|
||||
}
|
||||
|
||||
export function metersToFeet(meters: number): number {
|
||||
return meters * METERS_TO_FEET
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue