Refactor bilirubin vs threshold calculation

This commit is contained in:
RunasSudo 2024-08-27 17:53:55 +10:00
parent f669061d65
commit 030609b45e
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 46 additions and 26 deletions

View File

@ -150,35 +150,13 @@ function updateBilirubin() {
return; return;
} }
// Chronological age in days let [d, result_text, text_colour, background_colour, accent_colour] = describeBilirubin(gestation, document.getElementById('time_birth').valueAsDate, document.getElementById('time_measurement').valueAsDate, bilirubin);
let d = (document.getElementById('time_measurement').valueAsDate.getTime() - document.getElementById('time_birth').valueAsDate.getTime()) / 1000 / 60 / 60 / 24;
let phototherapy_thresh_value = phototherapy_thresh(d, gestation);
let exchange_thresh_value = exchange_thresh(d, gestation);
chart.data.datasets[2].data = [{x: d, y: bilirubin}]; chart.data.datasets[2].data = [{x: d, y: bilirubin}];
chart.update(); chart.update();
if (bilirubin < phototherapy_thresh_value) { resultDiv.className = 'border-l-4 ' + accent_colour + ' ' + background_colour + ' py-2 px-4';
if (bilirubin < phototherapy_thresh_value - 50) { resultP.className = 'text-sm ' + text_colour;
resultDiv.className = 'border-l-4 border-sky-400 bg-sky-50 py-2 px-4'; resultP.innerHTML = result_text;
resultP.className = 'text-sm text-sky-800';
} else {
resultDiv.className = 'border-l-4 border-yellow-400 bg-yellow-50 py-2 px-4';
resultP.className = 'text-sm text-yellow-800';
}
resultP.innerHTML = 'Bilirubin of ' + bilirubin + ' at ' + prettyPrintDays(d) + ' is <span class="font-bold">' + prettyPrintBilirubin(phototherapy_thresh_value - bilirubin) + ' below</span> the phototherapy threshold.';
} else if (bilirubin < exchange_thresh_value) {
resultDiv.className = 'border-l-4 border-orange-400 bg-orange-50 py-2 px-4';
resultP.className = 'text-sm text-orange-800';
resultP.innerHTML = 'Bilirubin of ' + bilirubin + ' at ' + prettyPrintDays(d) + ' is <span class="font-bold">' + prettyPrintBilirubin(bilirubin - phototherapy_thresh_value) + ' above</span> the phototherapy threshold, ' + prettyPrintBilirubin(exchange_thresh_value - bilirubin) + ' below the exchange transfusion threshold.';
} else {
resultDiv.className = 'border-l-4 border-red-400 bg-red-50 py-2 px-4';
resultP.className = 'text-sm text-red-800';
resultP.innerHTML = 'Bilirubin of ' + bilirubin + ' at ' + prettyPrintDays(d) + ' is ' + prettyPrintBilirubin(bilirubin - exchange_thresh_value) + ' above the <span class="font-bold">exchange transfusion</span> threshold.';
}
} }
updateBilirubin(); updateBilirubin();

View File

@ -76,6 +76,48 @@ function exchange_thresh(d, gestation) {
return thresh_at_3days; return thresh_at_3days;
} }
// ----------------------------------
// Bilirubin vs threshold calculation
function describeBilirubin(gestation, dateBirth, dateMeasurement, bilirubin) {
// Chronological age in days
let d = (dateMeasurement.getTime() - dateBirth.getTime()) / 1000 / 60 / 60 / 24;
let phototherapy_thresh_value = phototherapy_thresh(d, gestation);
let exchange_thresh_value = exchange_thresh(d, gestation);
// Restate colours separately so Tailwind can detect them
let result_text, text_colour, background_colour, accent_colour;
if (bilirubin < phototherapy_thresh_value) {
if (bilirubin < phototherapy_thresh_value - 50) {
text_colour = 'text-sky-800';
background_colour = 'bg-sky-50';
accent_colour = 'border-sky-400';
} else {
text_colour = 'text-yellow-800';
background_colour = 'bg-yellow-50';
accent_colour = 'border-yellow-400';
}
result_text = 'Bilirubin of ' + bilirubin + ' at ' + prettyPrintDays(d) + ' is <span class="font-bold">' + prettyPrintBilirubin(phototherapy_thresh_value - bilirubin) + ' below</span> the phototherapy threshold.';
} else if (bilirubin < exchange_thresh_value) {
text_colour = 'text-orange-800';
background_colour = 'bg-orange-50';
accent_colour = 'border-orange-400';
result_text = 'Bilirubin of ' + bilirubin + ' at ' + prettyPrintDays(d) + ' is <span class="font-bold">' + prettyPrintBilirubin(bilirubin - phototherapy_thresh_value) + ' above</span> the phototherapy threshold, ' + prettyPrintBilirubin(exchange_thresh_value - bilirubin) + ' below the exchange transfusion threshold.';
} else {
text_colour = 'text-red-800';
background_colour = 'bg-red-50';
accent_colour = 'border-red-400';
result_text = 'Bilirubin of ' + bilirubin + ' at ' + prettyPrintDays(d) + ' is ' + prettyPrintBilirubin(bilirubin - exchange_thresh_value) + ' above the <span class="font-bold">exchange transfusion</span> threshold.';
}
return [d, result_text, text_colour, background_colour, accent_colour];
}
// -------------- // --------------
// Utility functions // Utility functions