mirror of
https://github.com/kennethnym/aris.git
synced 2026-03-20 00:51:20 +00:00
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:
@@ -61,6 +61,15 @@ describe("renderTflAlert", () => {
|
|||||||
expect(caption.props.content).toBe("Nearest station: 2.5km away")
|
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", () => {
|
test("omits station distance when null", () => {
|
||||||
const node = renderTflAlert(makeItem({ closestStationDistance: null }))
|
const node = renderTflAlert(makeItem({ closestStationDistance: null }))
|
||||||
const spec = render(node)
|
const spec = render(node)
|
||||||
|
|||||||
@@ -14,10 +14,11 @@ const SEVERITY_LABEL: Record<TflAlertSeverity, string> = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function formatDistance(km: number): string {
|
function formatDistance(km: number): string {
|
||||||
if (km < 1) {
|
const meters = Math.round(km * 1000)
|
||||||
return `${Math.round(km * 1000)}m away`
|
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) => {
|
export const renderTflAlert: FeedItemRenderer<"tfl-alert", TflAlertData> = (item) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user