Files
HealthLog/src/lib/medications/dose-string.ts
T
Marc-André Bombeck fea1d1e0b5 chore: strip planning tags from comments and enable the rule
Output of `node scripts/strip-planning-tags.mjs --write`: 3,157 comment
lines in 1,308 files lose the work-item identifier at their head. Release
versions and issue references stay; nothing outside comment text
changes, which the script checks per file against a comment-free
printout. healthlog/no-planning-tags is enabled at error level with this
commit, so the tree it lands on is clean.
2026-10-20 09:08:40 +02:00

39 lines
1.5 KiB
TypeScript

/**
* v1.4.25 — 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;
}