/** A field of a study's form, as its Observation says it: `cervical_cytology.hpv` is the study * `cervical_cytology` and the field `hpv`. */ export interface Fact { study: string; field: string; title: string; value: string; } /** What the cytology's form declares before and at the sample: statements of the form, not * diagnoses. */ export const ANTECEDENTS = [ { field: 'screening_history', title: 'Detección de cáncer de cérvix' }, { field: 'gynecologic_condition', title: 'Condición ginecoobstétrica a la detección' }, { field: 'presenting_symptom', title: 'Actualmente presenta' }, { field: 'cervix_appearance', title: 'A la exploración se observa' }, ]; const DECLARED = new Set(ANTECEDENTS.map((a) => a.field)); const HPV = new Set(['hpv', 'hpv_16', 'hpv_18']); /** A catalog code as its study and field. */ export function split(code: string): { study: string; field: string } { const [study, ...field] = code.split('.'); return field.length ? { study, field: field.join('.') } : { study: '', field: code }; } const capital = (s: string) => s.charAt(0).toUpperCase() + s.slice(1); /** A report's facts sorted by what they are: the form's antecedents (by field), the study's own * results, and the extra findings (the cytologist's text first, then each finding stated). */ export function group(facts: Fact[]) { const antecedents: Record = {}; const results: string[] = []; const findings: string[] = []; for (const f of facts) { const form = f.study === 'cervical_cytology'; if (form && DECLARED.has(f.field)) antecedents[f.field] = f.value; else if (form && f.field === 'findings') findings.unshift(`Texto del informe: ${f.value}`); else if (form && f.field.startsWith('finding_')) { const named = capital(f.title.replace(/^Hallazgo: /, '')); findings.push(f.value === 'positivo' ? named : `${named}: ${f.value}`); } // "BI-RADS 4C", not "BI-RADS: BI-RADS 4C" else results.push(f.value.startsWith(f.title) ? f.value : `${f.title}: ${f.value}`); } return { antecedents, results, findings }; } /** A cytology report's HPV results apart from the cytology's own: they are a study of their own. */ export function hpvApart(facts: Fact[]) { const hpv = (f: Fact) => f.study === 'cervical_cytology' && HPV.has(f.field); return { hpv: facts.filter(hpv), rest: facts.filter((f) => !hpv(f)) }; }