fix(tfl): handle near-1km boundary in formatDistance

Values like 0.9999km rounded to 1000m and displayed as
'1000m away'. Now converts to meters first and switches
to km format when rounded meters >= 1000.

Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
2026-03-15 00:20:12 +00:00
parent adbc2c17ac
commit 78fc028ee5
2 changed files with 13 additions and 3 deletions

View File

@@ -61,6 +61,15 @@ describe("renderTflAlert", () => {
expect(caption.props.content).toBe("Nearest station: 2.5km away")
})
test("formats near-1km boundary as km not meters", () => {
const node = renderTflAlert(makeItem({ closestStationDistance: 0.9999 }))
const spec = render(node)
const root = spec.elements[spec.root]!
const caption = spec.elements[root.children![2]!]!
expect(caption.props.content).toBe("Nearest station: 1.0km away")
})
test("omits station distance when null", () => {
const node = renderTflAlert(makeItem({ closestStationDistance: null }))
const spec = render(node)

View File

@@ -14,10 +14,11 @@ const SEVERITY_LABEL: Record<TflAlertSeverity, string> = {
}
function formatDistance(km: number): string {
if (km < 1) {
return `${Math.round(km * 1000)}m away`
const meters = Math.round(km * 1000)
if (meters < 1000) {
return `${meters}m away`
}
return `${km.toFixed(1)}km away`
return `${(meters / 1000).toFixed(1)}km away`
}
export const renderTflAlert: FeedItemRenderer<"tfl-alert", TflAlertData> = (item) => {