mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-14 06:40:50 +00:00
47 lines
1.6 KiB
TypeScript
47 lines
1.6 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), and a value below 1 keeps three
|
|
* significant digits so a small reading (TSH 0.004, troponin 0.012) never
|
|
* reads as zero. 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 {
|
|
if (Number.isInteger(value)) return formatNumber(value);
|
|
const rounded =
|
|
Math.abs(value) < 1
|
|
? Number(value.toPrecision(3))
|
|
: Number(value.toFixed(2));
|
|
return formatNumber(rounded);
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
}
|