mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
fix: address review feedback on the distance unit setting
- server: allow distance_unit as an admin default (+ value validation) so the Admin "Default User Settings" toggle persists instead of returning 400 - i18n: add settings.distance to all 20 locales and translate the labels through t() instead of hardcoding "Distance" - route legs: include the unit in the OSRM cache key and recompute on a unit switch, so map and sidebar distances refresh and never mix units - keep wind speed tied to the temperature unit — a distance setting must not silently flip existing Fahrenheit users from mph to km/h - restore the sub-1km metres reading for metric, convert GPX elevation to feet for imperial, and format distances with a '.' decimal in every locale - add units.test.ts
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { convertDistance, formatDistance, getDistanceUnitLabel } from './units'
|
||||
|
||||
describe('units', () => {
|
||||
describe('getDistanceUnitLabel', () => {
|
||||
it('returns km for metric and mi for imperial', () => {
|
||||
expect(getDistanceUnitLabel('metric')).toBe('km')
|
||||
expect(getDistanceUnitLabel('imperial')).toBe('mi')
|
||||
})
|
||||
})
|
||||
|
||||
describe('convertDistance', () => {
|
||||
it('keeps kilometres for metric', () => {
|
||||
expect(convertDistance(10, 'metric')).toBe(10)
|
||||
})
|
||||
it('converts kilometres to miles for imperial', () => {
|
||||
expect(convertDistance(10, 'imperial')).toBeCloseTo(6.21371, 4)
|
||||
})
|
||||
it('clamps negative and non-finite input to 0', () => {
|
||||
expect(convertDistance(-5, 'imperial')).toBe(0)
|
||||
expect(convertDistance(NaN, 'metric')).toBe(0)
|
||||
expect(convertDistance(Infinity, 'metric')).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('formatDistance', () => {
|
||||
it('shows metres below 1 km for metric', () => {
|
||||
expect(formatDistance(0.3, 'metric')).toBe('300 m')
|
||||
expect(formatDistance(0.05, 'metric')).toBe('50 m')
|
||||
})
|
||||
it('shows kilometres at or above 1 km for metric', () => {
|
||||
expect(formatDistance(1.5, 'metric')).toBe('1.5 km')
|
||||
expect(formatDistance(10, 'metric')).toBe('10 km')
|
||||
})
|
||||
it('shows miles for imperial', () => {
|
||||
expect(formatDistance(10, 'imperial')).toBe('6.2 mi')
|
||||
})
|
||||
it('shows <0.1 for a tiny imperial distance', () => {
|
||||
expect(formatDistance(0.05, 'imperial')).toBe('<0.1 mi')
|
||||
})
|
||||
it('clamps negative and non-finite input to 0', () => {
|
||||
expect(formatDistance(-1, 'metric')).toBe('0 m')
|
||||
expect(formatDistance(NaN, 'imperial')).toBe('0 mi')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,22 +1,35 @@
|
||||
import type { DistanceUnit } from '../types'
|
||||
|
||||
const KM_TO_MI = 0.621371
|
||||
const M_TO_FT = 3.28084
|
||||
|
||||
export function getDistanceUnitLabel(unit: DistanceUnit): 'km' | 'mi' {
|
||||
return unit === 'imperial' ? 'mi' : 'km'
|
||||
}
|
||||
|
||||
/** Formats an elevation in metres as feet for imperial, so it doesn't mix with mi distances. */
|
||||
export function formatElevation(meters: number, unit: DistanceUnit): string {
|
||||
const safe = Number.isFinite(meters) ? meters : 0
|
||||
return unit === 'imperial' ? `${Math.round(safe * M_TO_FT)} ft` : `${Math.round(safe)} m`
|
||||
}
|
||||
|
||||
export function convertDistance(km: number, unit: DistanceUnit): number {
|
||||
const safeKm = Number.isFinite(km) ? Math.max(0, km) : 0
|
||||
return unit === 'imperial' ? safeKm * KM_TO_MI : safeKm
|
||||
}
|
||||
|
||||
export function formatDistance(km: number, unit: DistanceUnit): string {
|
||||
const value = convertDistance(km, unit)
|
||||
const safeKm = Number.isFinite(km) ? Math.max(0, km) : 0
|
||||
// Metric keeps a metres reading below 1 km (e.g. "300 m"), matching the route
|
||||
// connectors; imperial has no sub-mile unit, so short hops just show "0.x mi".
|
||||
if (unit === 'metric' && safeKm < 1) {
|
||||
return `${Math.round(safeKm * 1000)} m`
|
||||
}
|
||||
const value = convertDistance(safeKm, unit)
|
||||
const label = getDistanceUnitLabel(unit)
|
||||
const rounded = Math.round(value * 10) / 10
|
||||
const text = value > 0 && rounded === 0
|
||||
? '<0.1'
|
||||
: rounded.toLocaleString(undefined, { maximumFractionDigits: 1 })
|
||||
// String() keeps a '.' decimal regardless of locale, matching the rest of the app
|
||||
// (toFixed elsewhere) and avoiding "1,5 km" in non-English environments.
|
||||
const text = value > 0 && rounded === 0 ? '<0.1' : String(rounded)
|
||||
return `${text} ${label}`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user