diabetic-utils
Version:
Zero-bloat TypeScript utilities for diabetes data: glucose, A1C, conversions, time-in-range, and more.
2 lines • 4.76 kB
JavaScript
function C(e){return `${e.toFixed(1)}%`}function m(e){return typeof e=="number"&&Number.isFinite(e)&&e>0&&e<20}function U(e){return m(e)?e<5.7?"normal":e<6.5?"prediabetes":"diabetes":"invalid"}function D(e,n=[6.5,7]){return m(e)&&e>=n[0]&&e<=n[1]}function T(e,n){if(!m(e)||!m(n))throw new Error("Invalid A1C value");return +(e-n).toFixed(2)}function h(e){if(!Array.isArray(e)||e.length<2)return "insufficient data";let n=e[e.length-1]-e[0];return Math.abs(n)<.1?"stable":n>0?"increasing":"decreasing"}var p=70,b=180,g=3.9,L=10,l=28.7,c=46.7,a=18.0182,o="mg/dL",u="mmol/L",G="#D32F2F",O="#388E3C",_="#4CAF50",M="#2E7D32",E="#FBC02D",A="#F57C00",F={LOW:G,NORMAL:O,ELEVATED:E,HIGH:A,NORMAL_UP:_,NORMAL_DOWN:M},y={STEADY:"\u2192",RISING:"\u2197",FALLING:"\u2198",RAPIDRISE:"\u2191",RAPIDFALL:"\u2193"};function R(e){return typeof e=="object"&&e!==null&&"value"in e&&"unit"in e&&typeof e.value=="number"&&typeof e.unit=="string"}function d(e){return typeof e!="string"?false:/^\d+(\.\d+)?\s+(mg\/dL|mmol\/L)$/i.test(e.trim())}function w(e,n=o){return n===o?e<70:e<3.9}function I(e,n=o){return n===o?e>180:e>10}function V(e,n=o){return w(e,n)?"low":I(e,n)?"high":"normal"}function x(e){if(!d(e))throw new Error('Invalid glucose string format. Use "100 mg/dL" or "5.5 mmol/L".');let t=e.trim().replace(/\s+/g," ").match(/^([\d.]+) (mg\/dL|mmol\/L)$/i),[,r,i]=t;return {value:parseFloat(r),unit:i.toLowerCase()==="mg/dl"?o:u}}function $(e,n){return typeof e=="number"&&Number.isFinite(e)&&e>0&&(n===o||n===u)}function j(e){return +((e+46.7)/28.7).toFixed(2)}function q(e){return Math.round(e*28.7-46.7)}function Z(e){if(e<0)throw new Error("A1C must be positive");let n=Number((e*28.7-46.7).toFixed(10));return Math.round(n)}function z(e,n=o){return +(((n===u?e*18.0182:e)+46.7)/28.7).toFixed(2)}function J(e){return +(3.31+.02392*e).toFixed(2)}function K(e,n){let t,r;if(R(e))t=e.value,r=e.unit;else if(typeof e=="string"){let s=x(e);t=s.value,r=s.unit;}else {if(!n)throw new Error("Unit is required when input is a number.");t=e,r=n;}if(![o,u].includes(r))throw new Error(`Unsupported glucose unit: ${r}`);if(t<=0||!Number.isFinite(t))throw new Error("Glucose value must be a positive number.");let i=r===u?1.57*t+3.5:.03*t+2.4;return parseFloat(i.toFixed(1))}function Q(e){if(!Number.isFinite(e)||e<=0)throw new Error("Invalid glucose value");return +(e/18.0182).toFixed(1)}function X(e){if(!Number.isFinite(e)||e<=0)throw new Error("Invalid glucose value");return Math.round(e*18.0182)}function ee({value:e,unit:n}){if(!Number.isFinite(e)||e<=0)throw new Error("Invalid glucose value");if(![o,u].includes(n))throw new Error("Invalid unit");return n===o?{value:Math.round(e/18.0182*10)/10,unit:u}:{value:Math.round(e*18.0182),unit:o}}function te(e,n,t={}){let r=t.digits??0,i=t.suffix??true,s=e.toFixed(r);return i?`${s} ${n}`:s}function re(e,n=1){return `${e.toFixed(n)}%`}function oe(e,n){if(isNaN(Date.parse(e)))throw new RangeError("Invalid ISO timestamp");return new Date(e).toLocaleString("en-US",{timeZone:n,year:"numeric",month:"short",day:"2-digit",hour:"2-digit",minute:"2-digit"})}function ue(e,n){if(e.length===0)return {inRange:0,belowRange:0,aboveRange:0};let t=0,r=0,i=0;for(let f of e)f.value<n.min?r++:f.value>n.max?i++:t++;let s=e.length;return {inRange:+(t/s*100).toFixed(1),belowRange:+(r/s*100).toFixed(1),aboveRange:+(i/s*100).toFixed(1)}}function se(e){return `In Range: ${e.inRange}%, Below: ${e.belowRange}%, Above: ${e.aboveRange}%`}function ae(e){return e.reduce((n,t)=>{let r=t.timestamp.split("T")[0];return n[r]=n[r]||[],n[r].push(t),n},{})}function me(e,n,t){return e.length===0?0:e.filter(i=>i>=n&&i<=t).length/e.length*100}var fe=[o,u];export{c as A1C_TO_EAG_CONSTANT,l as A1C_TO_EAG_MULTIPLIER,fe as AllowedGlucoseUnits,E as GLUCOSE_COLOR_ELEVATED,A as GLUCOSE_COLOR_HIGH,G as GLUCOSE_COLOR_LOW,O as GLUCOSE_COLOR_NORMAL,M as GLUCOSE_COLOR_NORMAL_DOWN,_ as GLUCOSE_COLOR_NORMAL_UP,F as GLUCOSE_ZONE_COLORS,b as HYPER_THRESHOLD_MGDL,L as HYPER_THRESHOLD_MMOLL,p as HYPO_THRESHOLD_MGDL,g as HYPO_THRESHOLD_MMOLL,a as MGDL_MMOLL_CONVERSION,o as MG_DL,u as MMOL_L,y as TREND_ARROWS,T as a1cDelta,J as a1cToGMI,h as a1cTrend,ue as calculateTIR,me as calculateTimeInRange,ee as convertGlucoseUnit,z as estimateA1CFromAverage,j as estimateA1CFromAvgGlucose,q as estimateAvgGlucoseFromA1C,Z as estimateEAG,K as estimateGMI,C as formatA1C,oe as formatDate,te as formatGlucose,re as formatPercentage,U as getA1CCategory,V as getGlucoseLabel,se as getTIRSummary,ae as groupByDay,D as isA1CInTarget,R as isEstimateGMIOptions,I as isHyper,w as isHypo,m as isValidA1C,d as isValidGlucoseString,$ as isValidGlucoseValue,Q as mgDlToMmolL,X as mmolLToMgDl,x as parseGlucoseString};//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map