Files
HealthLog/src/lib/medications/dose-string.ts
T
Marc-André Bombeck 9445d89b76 refactor(api,tz): one typed client for every call and one home for wall-clock math
A typed apiFetch wrapper owns the envelope unwrap, the ok-check and the
error read for every client call — two hundred call sites across the
app migrate onto it and a lint rule keeps raw api fetches out for good.
The legacy timezone helpers fold into the tz module and test fixtures
drop personal names.
2026-06-11 02:19:27 +02:00

39 lines
1.5 KiB
TypeScript

/**
* v1.4.25 W21 Fix-N — unified dose-string → mg parser.
*
* Replaces two near-identical helpers previously hand-rolled inside
* `<DrugLevelChart>` (`parseDoseMg`) and the titration route
* (`parseDoseString`). The DrugLevelChart variant returned `NaN` for
* unparseable strings; the titration variant returned `null`. Both
* code paths now go through this module — the legacy null-returning
* shape is preserved as `parseDoseMgOrNull` so the titration route
* keeps its "no current step" semantics.
*
* Accepts comma OR dot as decimal separator (maintainer note: the German
* UI lets the user type "0,25 mg"). Trailing unit text after the
* number is ignored — the parser only owns the numeric extraction.
*/
/**
* Parse a dose string like "7.5 mg" → 7.5 or "0,25" → 0.25. Returns
* `NaN` when the string carries no recognisable number (e.g. "as
* needed") so callers can use `Number.isFinite` for the gate.
*/
export function parseDoseMg(input: string): number {
if (!input) return Number.NaN;
const match = input.match(/([0-9]+(?:[.,][0-9]+)?)/);
if (!match) return Number.NaN;
return Number.parseFloat(match[1].replace(",", "."));
}
/**
* Same as `parseDoseMg` but returns `null` instead of `NaN` for the
* unparseable case. The titration route reads this as "no current
* step" — null signals "we couldn't resolve a dose" rather than "the
* dose is zero".
*/
export function parseDoseMgOrNull(input: string): number | null {
const value = parseDoseMg(input);
return Number.isFinite(value) ? value : null;
}