simulator/engine/library.js
// Drug-library access. The real library is simulator/data/drugs.js (SPEC §4).
// Every engine entry point accepts an injected `library` (same shape:
// { SOURCES, CONDITIONS, ALLERGIES, MEDICATIONS, DRUGS }) so tests can run
// against a fixture. When none is injected the default library is loaded
// with a guarded dynamic import, so the engine still loads if data/drugs.js
// is missing (the error is reported when a library is actually needed).
let DEFAULT_LIBRARY = null;
let DEFAULT_ERROR = null;
try {
DEFAULT_LIBRARY = await import('../data/drugs.js');
} catch (err) {
DEFAULT_ERROR = err;
}
export function getDefaultLibrary() {
if (!DEFAULT_LIBRARY) {
throw new Error('Drug library simulator/data/drugs.js could not be loaded' + (DEFAULT_ERROR ? `: ${DEFAULT_ERROR.message}` : ''));
}
return DEFAULT_LIBRARY;
}
export function hasDefaultLibrary() { return !!DEFAULT_LIBRARY; }
export function resolveLibrary(library) { return library || getDefaultLibrary(); }
const INDEX = new WeakMap();
function index(lib) {
let ix = INDEX.get(lib);
if (!ix) {
ix = { drugs: new Map((lib.DRUGS || []).map((d) => [d.id, d])), antibiotics: new Map((lib.ANTIBIOTICS || []).map((d) => [d.id, d])), fluids: new Map((lib.FLUIDS || []).map((f) => [f.id, f])) };
INDEX.set(lib, ix);
}
return ix;
}
export function getDrug(library, drugId) {
return index(resolveLibrary(library)).drugs.get(drugId) || null;
}
export function listDrugs(library) { return resolveLibrary(library).DRUGS || []; }
/** Surgical-prophylaxis antibiotics (data/drugs.js ANTIBIOTICS; timing only, no PK). */
export function getAntibiotic(library, id) { return index(resolveLibrary(library)).antibiotics.get(id) || null; }
export function listAntibiotics(library) { return resolveLibrary(library).ANTIBIOTICS || []; }
/** IV fluids (data/drugs.js FLUIDS). */
export function getFluid(library, id) { return index(resolveLibrary(library)).fluids.get(id) || null; }
export function listFluids(library) { return resolveLibrary(library).FLUIDS || []; }
/** A drug or a prophylaxis antibiotic (both use the regimen schema read by recommend()). */
export function getDosable(library, id) { return getDrug(library, id) || getAntibiotic(library, id); }
/** Find a source id whose citation contains `needle` (case-insensitive); null if none. */
export function findSourceId(library, needle) {
const src = resolveLibrary(library).SOURCES || {};
const n = needle.toLowerCase();
for (const [id, s] of Object.entries(src)) {
if (String(s && s.citation || '').toLowerCase().includes(n)) return id;
}
return null;
}
Source and scopePublished from Smart-NOA-Controller/simulator/engine/library.js at revision 6e5b205e3843. Full source text is retained; HTML presentation does not establish independent clinical review. In-memory simulation with no patient, monitor or pump interface; not clinically validated.