pediatric-ai-scribe-v3/public/js/drugs-loader.js
Daniel ad90b88734 feat: B — extract drug data to public/data/drugs.json (schema v1.0)
Moved 43 weight-based drug entries out of calculators.js string literals
into a structured JSON reference. Renderers now iterate JSON → S.drugRow.

Sections extracted (43 drugs):
- anaphylaxis (7), sedation (11), agitation (11), antiemetics (7), seizure (7)

Out of scope: seizure refractory drips (compound strings), NRP, antimicrobial
empiric regimens (already structured), airway/cardiac/tox/trauma/respiratory
/sepsis/burns sections (fine as-is or no drugs).

New:
- public/data/drugs.json — { version, last_reviewed, sections.<key>.drugs[] }
- public/js/drugs-loader.js — fetches JSON on boot, exposes window._DRUGS +
  window._DRUGS_READY Promise. Non-fatal: each calc function has a matching
  *_FALLBACK constant so a 404 on drugs.json doesn't break anything.

Schema:
- dose_mg_per_kg | dose_mg_per_kg_low/high (for ranges) | max_mg | unit |
  route | notes | source | optional special-cases (weight bands, age-dep text)

Added one unit test asserting drugs.json loads + has all 5 sections with
non-empty drugs arrays. 37/37 unit tests + 26/26 Playwright e2e tests pass.
2026-04-20 04:23:24 +02:00

31 lines
1.3 KiB
JavaScript

// ============================================================
// drugs-loader.js
// Fetches /data/drugs.json at page load and exposes:
// window._DRUGS — the parsed JSON (after fetch resolves)
// window._DRUGS_READY — a Promise that resolves to the JSON
//
// Calculator functions in calculators.js look up drugs via a small
// helper (pickDrug) and fall back to hardcoded inline data if the
// JSON isn't loaded yet. This means the app keeps working even if
// the fetch fails — the JSON is an authoritative data source but
// not a hard dependency at runtime.
// ============================================================
(function() {
// Default to empty shape so lookups don't throw before fetch resolves.
window._DRUGS = window._DRUGS || { version: null, sections: {} };
window._DRUGS_READY = fetch('/data/drugs.json', { credentials: 'same-origin' })
.then(function(r) {
if (!r.ok) throw new Error('drugs.json HTTP ' + r.status);
return r.json();
})
.then(function(j) {
window._DRUGS = j;
return j;
})
.catch(function(err) {
// Non-fatal — calc functions have inline fallbacks.
console.warn('[drugs-loader] could not load /data/drugs.json:', err);
return window._DRUGS;
});
})();