]>
Softwares of Agnibho - pdosage.git/blob - src/Dosage.js
1 /**********************************************************************
3 * Description: Pediatric Calculator
4 * Author: Agnibho Mondal
5 * Website: http://code.agnibho.com
6 **********************************************************************
7 Copyright (c) 2016 Agnibho Mondal
9 **********************************************************************
10 This file is part of PDosage.
12 PDosage is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 PDosage is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with PDosage. If not, see <http://www.gnu.org/licenses/>.
24 **********************************************************************/
26 export default function Dosage(data
){
28 Object
.defineProperty(data
, "prop", {
36 this.listDrugs=function(){
37 var list
=data
.map(function(obj
){
43 this.getDrug=function(name
, patient
){
44 if(this.listDrugs().indexOf(name
)==-1){
47 var drug
=JSON
.parse(JSON
.stringify(data
.filter(extract
)[0]));
48 function extract(obj
){
49 return (obj
.name
== name
);
51 for(var i
=0; i
<drug
.form
.length
; i
++){
52 drug
.form
[i
]=calculateDose(drug
.form
[i
], patient
);
57 function calculateDose(form
, patient
){
58 while(form
.hasOwnProperty("range")){
59 var res
=isolateRange(form
, patient
);
62 if(res
.hasOwnProperty("range")){
71 form
.dose
=[].concat(form
.dose
);
72 for(var i
=0; i
<form
.dose
.length
; i
++){
73 if(form
.dose
[i
].hasOwnProperty("val")){
74 form
.dose
[i
].val
=[].concat(form
.dose
[i
].val
);
76 else if(form
.dose
[i
].hasOwnProperty("perKg")){
77 form
.dose
[i
].perKg
=[].concat(form
.dose
[i
].perKg
);
79 form
.dose
[i
].val
[0]=Math
.round(parseFloat(form
.dose
[i
].perKg
[0])*parseFloat(patient
.wt
)*100)/100;
80 form
.dose
[i
].val
[1]=Math
.round(parseFloat(form
.dose
[i
].perKg
[1])*parseFloat(patient
.wt
)*100)/100;
81 form
.dose
[i
]=setLimit(form
.dose
[i
]);
83 else if(form
.dose
[i
].hasOwnProperty("approx")){
85 form
.dose
[i
].val
=findApprox(form
.dose
[i
].approx
);
86 form
.dose
[i
]=setLimit(form
.dose
[i
]);
88 else if(form
.dose
[i
].hasOwnProperty("eq")){
89 form
.dose
[i
].eq
=[].concat(form
.dose
[i
].eq
);
91 form
.dose
[i
].val
[0]=solveEq(form
.dose
[i
].eq
[0]);
92 form
.dose
[i
].val
[1]=solveEq(form
.dose
[i
].eq
[1]);
93 form
.dose
[i
]=setLimit(form
.dose
[i
]);
97 form
.dose
[i
].val
[0]=null;
98 form
.dose
[i
].val
[1]=null;
104 function isolateRange(form
, patient
){
108 if(patient
.age
.y
===null && patient
.age
.m
===null && patient
.age
.d
===null){
111 var pDays
=patient
.age
.y
*365+patient
.age
.m
*30+patient
.age
.d
;
112 for(var i
=0; i
<form
.range
.length
; i
++){
113 if(form
.range
[i
].hasOwnProperty("min")){
114 if(form
.range
[i
].min
.indexOf("kg")!=-1){
115 min
=form
.range
[i
].min
.slice(0, str
.indexOf("kg"));
119 min
=strToDays(form
.range
[i
].min
);
126 if(form
.range
[i
].hasOwnProperty("max")){
127 if(form
.range
[i
].max
.indexOf("kg")!=-1){
128 max
=form
.range
[i
].max
.slice(0, str
.indexOf("kg"));
132 max
=strToDays(form
.range
[i
].max
);
140 if(patient
.wt
>=min
&& patient
.wt
<max
){
141 return form
.range
[i
];
144 else if(type
=="age"){
145 if(pDays
>=min
&& pDays
<max
){
146 return form
.range
[i
];
152 function strToDays(str
){
153 var age
={y
: 0, m
: 0, d
: 0};
155 str
=str
.replace(/\s+/g, "");
156 if(str
.indexOf("y")!=-1){
157 age
.y
=str
.slice(0, str
.indexOf("y"));
158 str
=str
.slice(str
.indexOf("y", -1));
160 if(str
.indexOf("m")!=-1){
161 age
.m
=str
.slice(0, str
.indexOf("m"));
162 str
=str
.slice(str
.indexOf("m", -1));
164 if(str
.indexOf("d")!=-1){
165 age
.d
=str
.slice(0, str
.indexOf("d"));
166 str
=str
.slice(str
.indexOf("d", -1));
168 days
=age
.y
*365+age
.m
*30+age
.d
;
173 function findApprox(arr
){
176 for(var i
=1; i
<arr
.length
; i
++){
177 arr
[i
]=solveEq(arr
[i
]);
178 ret
[i
-1]=Math
.round(arr
[i
]/arr[0])/2;
180 if(!ret
[0] || (ret
[0]==ret
[1])){
187 function solveEq(str
){
188 var bw
=parseFloat(patient
.wt
);
189 var ageY
=patient
.age
.y
;
190 var ageM
=ageY
*12+patient
.age
.m
;
191 var ageD
=ageM
*30+patient
.age
.d
;
193 ret
=Math
.round(ret
*100)/100;
197 function setLimit(dose
){
198 if(dose
.adult
&& dose
.val
){
199 if(dose
.val
[0]>dose
.adult
[0]){
200 dose
.val
[0]=dose
.adult
[0];
202 if(dose
.val
[1]>dose
.adult
[1]){
203 dose
.val
[1]=dose
.adult
[1];
206 if(dose
.limit
&& dose
.val
){
207 if(dose
.val
[0]>dose
.limit
[0]){
208 dose
.val
[0]=dose
.limit
[0];
210 if(dose
.val
[1]>dose
.limit
[1]){
211 dose
.val
[1]=dose
.limit
[1];
214 if(dose
.val
[1]==dose
.val
[0]){