Files
HealthLog/src/lib/labs/format-value.ts
T
Marc-André Bombeck fe13fa966b fix(labs): print range bars and lab values in the reader's number format
Range bars and the lab list, detail rows, chart tooltip and reference
ranges printed numbers with a plain `String()`, so a German reader saw
"1.8 mIU/L" and "61.3 kg" under a German interface. The same was true
of the mean, GMI, eA1C and the advanced indices on the glucose panel.

`RangeBar` now prints value, bounds and delta through the locale number
formatter and takes an optional `formatValue` for surfaces with their
own precision. Lab values keep their trim rule (whole numbers bare, at
most two decimals) in `formatLabValue`, which now hands the digits to a
caller-supplied locale formatter; `useLabNumber` wires it to the
reader's locale for every lab surface. Inputs are unchanged.
2026-10-01 22:09:30 +02:00

43 lines
1.4 KiB
TypeScript

/**
* v1.18.1 — shared lab-value number formatting.
*
* The list card, the detail history rows, and the chart tooltip all trim a
* lab reading the same way: whole numbers render bare, fractional values keep
* up to two decimals (no trailing-zero noise). Centralised so the three
* surfaces stay byte-identical.
*
* The digits themselves come from the caller's locale number formatter
* (`useFormatters().number`), so a German reader sees "1,8 mIU/L" where an
* English one sees "1.8 mIU/L". This module only decides the precision.
*/
export type LabNumberFormat = (value: number) => string;
export function formatLabValue(
value: number,
formatNumber: LabNumberFormat,
): string {
return formatNumber(
Number.isInteger(value) ? value : Number(value.toFixed(2)),
);
}
/**
* v1.18.9 — render a reading's display value: a qualitative row (numeric
* `value` is null) shows its `valueText` verbatim; a numeric row shows the
* formatted number plus its unit. One helper so the list card, the history
* rows, and the detail header stay consistent.
*/
export function formatLabReading(
reading: {
value: number | null;
valueText: string | null;
unit: string;
},
formatNumber: LabNumberFormat,
): string {
if (reading.value === null) {
return reading.valueText ?? "";
}
return `${formatLabValue(reading.value, formatNumber)} ${reading.unit}`.trim();
}