From 030609b45eaf8462d0d38b58c746f75f9cf5beed Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Tue, 27 Aug 2024 17:53:55 +1000 Subject: [PATCH] Refactor bilirubin vs threshold calculation --- src/bilirubin_app.js | 30 ++++-------------------------- src/bilirubin_lib.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/bilirubin_app.js b/src/bilirubin_app.js index c1924eb..d7df466 100644 --- a/src/bilirubin_app.js +++ b/src/bilirubin_app.js @@ -150,35 +150,13 @@ function updateBilirubin() { return; } - // Chronological age in days - 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); + let [d, result_text, text_colour, background_colour, accent_colour] = describeBilirubin(gestation, document.getElementById('time_birth').valueAsDate, document.getElementById('time_measurement').valueAsDate, bilirubin); chart.data.datasets[2].data = [{x: d, y: bilirubin}]; chart.update(); - if (bilirubin < phototherapy_thresh_value) { - if (bilirubin < phototherapy_thresh_value - 50) { - resultDiv.className = 'border-l-4 border-sky-400 bg-sky-50 py-2 px-4'; - 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 ' + prettyPrintBilirubin(phototherapy_thresh_value - bilirubin) + ' below 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 ' + prettyPrintBilirubin(bilirubin - phototherapy_thresh_value) + ' above 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 exchange transfusion threshold.'; - } + resultDiv.className = 'border-l-4 ' + accent_colour + ' ' + background_colour + ' py-2 px-4'; + resultP.className = 'text-sm ' + text_colour; + resultP.innerHTML = result_text; } updateBilirubin(); diff --git a/src/bilirubin_lib.js b/src/bilirubin_lib.js index c55c260..1f5606e 100644 --- a/src/bilirubin_lib.js +++ b/src/bilirubin_lib.js @@ -76,6 +76,48 @@ function exchange_thresh(d, gestation) { 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 ' + prettyPrintBilirubin(phototherapy_thresh_value - bilirubin) + ' below 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 ' + prettyPrintBilirubin(bilirubin - phototherapy_thresh_value) + ' above 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 exchange transfusion threshold.'; + } + + return [d, result_text, text_colour, background_colour, accent_colour]; +} + // -------------- // Utility functions