mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-10 05:12:14 +00:00
Hoist five duplicated helpers into single source modules so each contract evolves in exactly one place: - src/lib/api/read-error.ts — replaces the four hand-rolled copies in the onboarding flow (Welcome / Goals / Source / Baseline). - src/lib/medications/research-mode-types.ts — shared ResearchModeStatus DTO + tri-state gate helper consumed by both the DrugLevelChart and the Settings advanced section. - src/lib/medications/route-guards.ts — assertMedicationOwnership for the eleven medication sub-routes, preserving the 404 leak shape across the privacy boundary. - src/lib/medications/dose-string.ts — unified parseDoseMg + null variant replacing the DrugLevelChart and titration-route parsers. - src/lib/medications/glp1-knowledge.ts — adds findDrugIdByBrand reverse lookup so the brand-to-id pattern stops being open-coded at every chart render. 28 new unit tests across the five helpers.
39 lines
1.5 KiB
TypeScript
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 (Marc-memory: 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;
|
|
}
|