Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
220 lines (195 loc) · 7.59 KB
/
Copy pathindex.js
File metadata and controls
220 lines (195 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { riskTables } from './risk-tables.js';
export const REGION = 'south-east-asia-indonesia';
export const TABLE_VERSION = 'south-east-asia-indonesia-unverified-v1';
export const MEDICAL_DISCLAIMER = 'Educational table-based estimate only; not a diagnosis or treatment recommendation.';
export const CholesterolUnit = Object.freeze({ MG_DL: 'mg/dL', MMOL_L: 'mmol/L' });
export const AgePolicy = Object.freeze({ REJECT: 'rejectOutsideTableRange', NEAREST_ENDPOINT: 'nearestEndpoint' });
export const RiskCategory = Object.freeze({
LOW: 'low',
MODERATE: 'moderate',
HIGH: 'high',
VERY_HIGH: 'very-high',
CRITICAL: 'critical',
});
function finiteNumber(value, label) {
if (typeof value !== 'number' || !Number.isFinite(value)) throw new TypeError(`${label} must be a finite number`);
return value;
}
function integer(value, label) {
if (!Number.isInteger(value)) throw new TypeError(`${label} must be an integer`);
return value;
}
function boolean(value, label) {
if (typeof value !== 'boolean') throw new TypeError(`${label} must be a boolean`);
return value;
}
export function parseSex(value) {
if (typeof value !== 'string') throw new TypeError('sex must be a string');
const normalized = value.trim().toLowerCase().replaceAll('_', ' ').replaceAll('-', ' ');
if (['pria', 'laki laki', 'male', 'man'].includes(normalized)) return 'male';
if (['wanita', 'perempuan', 'female', 'woman'].includes(normalized)) return 'female';
throw new RangeError('sex is unsupported');
}
export function classifyRisk(risk) {
finiteNumber(risk, 'risk');
if (risk < 0 || risk > 1) throw new RangeError('risk must be between 0 and 1');
if (risk < 0.05) return RiskCategory.LOW;
if (risk < 0.10) return RiskCategory.MODERATE;
if (risk < 0.20) return RiskCategory.HIGH;
if (risk < 0.30) return RiskCategory.VERY_HIGH;
return RiskCategory.CRITICAL;
}
export function parseCholesterol(value, unit = CholesterolUnit.MG_DL) {
if (!Object.values(CholesterolUnit).includes(unit)) throw new RangeError('cholesterolUnit is unsupported');
const normalized = typeof value === 'string' ? value.trim().replace(',', '.') : value;
const parsed = Number(normalized);
if (!Number.isFinite(parsed)) throw new TypeError('cholesterol must be numeric and finite');
const mgDl = unit === CholesterolUnit.MMOL_L ? parsed * 38.67 : parsed;
if (mgDl < 20 || mgDl > 600) throw new RangeError('cholesterol must be between 20 and 600 mg/dL');
return { value: parsed, mgDl };
}
function resolveAge(age, agePolicy) {
integer(age, 'age');
if (age < 1 || age > 120) throw new RangeError('age must be between 1 and 120');
if (!Object.values(AgePolicy).includes(agePolicy)) throw new RangeError('agePolicy is unsupported');
if (agePolicy === AgePolicy.REJECT && (age < 40 || age > 74)) {
throw new RangeError('age is outside the supported table range of 40-74');
}
return agePolicy === AgePolicy.NEAREST_ENDPOINT ? Math.min(74, Math.max(40, age)) : age;
}
function validateCommon(age, systolicBp, agePolicy) {
const effectiveAge = resolveAge(age, agePolicy);
integer(systolicBp, 'systolicBp');
if (systolicBp < 40 || systolicBp > 400) throw new RangeError('systolicBp must be between 40 and 400');
return effectiveAge;
}
function ageKey(age) {
if (age >= 70) return '70-74';
if (age >= 65) return '65-69';
if (age >= 60) return '60-64';
if (age >= 55) return '55-59';
if (age >= 50) return '50-54';
if (age >= 45) return '45-49';
return '40-44';
}
function sbpKey(value) {
if (value >= 180) return '>=180';
if (value >= 160) return '160-179';
if (value >= 140) return '140-159';
if (value >= 120) return '120-139';
return '<120';
}
function bmiKey(value) {
if (value >= 35) return '>=35';
if (value >= 30) return '30-34';
if (value >= 25) return '25-29';
if (value >= 20) return '20-24';
return '<20';
}
function cholesterolKey(mgDl) {
const mmol = mgDl / 38.67;
if (mmol >= 7) return '>=7';
if (mmol >= 6) return '6-6.9';
if (mmol >= 5) return '5-5.9';
if (mmol >= 4) return '4-4.9';
return '<4';
}
function result(risk, requestedAge, effectiveAge) {
return {
risk,
riskPercent: risk * 100,
timeHorizonYears: 10,
region: REGION,
tableVersion: TABLE_VERSION,
category: classifyRisk(risk),
usesAgeEndpoint: requestedAge !== effectiveAge,
requestedAge,
effectiveAge,
disclaimer: MEDICAL_DISCLAIMER,
};
}
export function calculateNonLab({
age,
sex,
systolicBp,
smoker,
bmi,
agePolicy = AgePolicy.REJECT,
}) {
const effectiveAge = validateCommon(age, systolicBp, agePolicy);
const normalizedSex = parseSex(sex);
boolean(smoker, 'smoker');
finiteNumber(bmi, 'bmi');
if (bmi <= 0 || bmi > 100) throw new RangeError('bmi must be between 0 and 100');
const prefix = normalizedSex === 'male' ? 'pria' : 'wanita';
const table = riskTables[`${prefix}${smoker ? 'Perokok' : 'BukanPerokok'}NonLab`];
const risk = table?.[`${ageKey(effectiveAge)}-${sbpKey(systolicBp)}`]?.[bmiKey(bmi)];
if (risk == null) throw new Error('Missing non-lab regional table value');
return result(risk, age, effectiveAge);
}
export function calculateLab({
age,
sex,
systolicBp,
smoker,
diabetes,
totalCholesterol,
cholesterolUnit = CholesterolUnit.MG_DL,
agePolicy = AgePolicy.REJECT,
}) {
const effectiveAge = validateCommon(age, systolicBp, agePolicy);
const normalizedSex = parseSex(sex);
boolean(smoker, 'smoker');
boolean(diabetes, 'diabetes');
const { mgDl } = parseCholesterol(totalCholesterol, cholesterolUnit);
const prefix = normalizedSex === 'male' ? 'pria' : 'wanita';
const table = riskTables[`${prefix}${smoker ? 'Perokok' : 'BukanPerokok'}${diabetes ? 'DenganDiabetes' : 'TanpaDiabetes'}Lab`];
const risk = table?.[`${ageKey(effectiveAge)}-${sbpKey(systolicBp)}`]?.[cholesterolKey(mgDl)];
if (risk == null) throw new Error('Missing lab regional table value');
return result(risk, age, effectiveAge);
}
export function calculateNonLabIfStopsSmoking(input) {
return calculateNonLab({ ...input, smoker: false });
}
export function calculateNonLabIfBpIsNormal(input) {
return calculateNonLab({ ...input, systolicBp: 119 });
}
export function calculateNonLabIfBmiIsNormal(input) {
return calculateNonLab({ ...input, bmi: 19 });
}
export function calculateLabIfStopsSmoking(input) {
return calculateLab({ ...input, smoker: false });
}
export function calculateLabIfBpIsNormal(input) {
return calculateLab({ ...input, systolicBp: 119 });
}
export function calculateLabIfCholesterolIsNormal(input) {
return calculateLab({ ...input, totalCholesterol: 130, cholesterolUnit: CholesterolUnit.MG_DL });
}
export function classifyBloodPressure(value) {
integer(value, 'systolicBp');
if (value < 40 || value > 400) throw new RangeError('systolicBp must be between 40 and 400');
if (value < 90) return 'low';
if (value <= 120) return 'normal';
if (value <= 139) return 'high';
return 'very-high';
}
export function classifyBmi(value) {
finiteNumber(value, 'bmi');
if (value <= 0 || value > 100) throw new RangeError('bmi must be between 0 and 100');
if (value < 18.5) return 'low';
if (value < 25) return 'normal';
if (value < 30) return 'high';
return 'very-high';
}
export function classifyCholesterol(value, age = 30) {
integer(value, 'cholesterol');
integer(age, 'age');
if (value < 20 || value > 600) throw new RangeError('cholesterol must be between 20 and 600 mg/dL');
if (age < 1 || age > 120) throw new RangeError('age must be between 1 and 120');
const normal = age <= 19 ? 170 : 200;
const borderline = age <= 19 ? 199 : 239;
if (value < normal) return 'normal';
if (value <= borderline) return 'borderline';
return 'very-high';
}