]>
Softwares of Agnibho - pdosage.git/blob - src/dosage.js
9d7c15c2a2561c231f97964584b6eade5cee62c7
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 var pDays
=patient
.age
.y
*365+patient
.age
.m
*30+patient
.age
.d
;
109 for(var i
=0; i
<form
.range
.length
; i
++){
110 if(form
.range
[i
].hasOwnProperty("min")){
111 if(form
.range
[i
].min
.indexOf("kg")!=-1){
112 min
=form
.range
[i
].min
.slice(0, str
.indexOf("kg"));
116 min
=strToDays(form
.range
[i
].min
);
123 if(form
.range
[i
].hasOwnProperty("max")){
124 if(form
.range
[i
].max
.indexOf("kg")!=-1){
125 max
=form
.range
[i
].max
.slice(0, str
.indexOf("kg"));
129 max
=strToDays(form
.range
[i
].max
);
137 if(patient
.wt
>=min
&& patient
.wt
<max
){
138 return form
.range
[i
];
141 else if(type
=="age"){
142 if(pDays
>=min
&& pDays
<max
){
143 return form
.range
[i
];
149 function strToDays(str
){
150 var age
={y
: 0, m
: 0, d
: 0};
152 str
=str
.replace(/\s+/g, "");
153 if(str
.indexOf("y")!=-1){
154 age
.y
=str
.slice(0, str
.indexOf("y"));
155 str
=str
.slice(str
.indexOf("y", -1));
157 if(str
.indexOf("m")!=-1){
158 age
.m
=str
.slice(0, str
.indexOf("m"));
159 str
=str
.slice(str
.indexOf("m", -1));
161 if(str
.indexOf("d")!=-1){
162 age
.d
=str
.slice(0, str
.indexOf("d"));
163 str
=str
.slice(str
.indexOf("d", -1));
165 days
=age
.y
*365+age
.m
*30+age
.d
;
170 function findApprox(arr
){
173 for(var i
=1; i
<arr
.length
; i
++){
174 arr
[i
]=solveEq(arr
[i
]);
175 ret
[i
-1]=Math
.round(arr
[i
]/arr[0])/2;
177 if(!ret
[0] || (ret
[0]==ret
[1])){
184 function solveEq(str
){
185 var bw
=parseFloat(patient
.wt
);
186 var ageY
=patient
.age
.y
;
187 var ageM
=ageY
*12+patient
.age
.m
;
188 var ageD
=ageM
*30+patient
.age
.d
;
190 ret
=Math
.round(ret
*100)/100;
194 function setLimit(dose
){
195 //console.log(">>"+JSON.stringify(dose));
196 if(dose
.limit
&& dose
.val
){
197 if(dose
.val
[0]>dose
.limit
[0]){
198 dose
.val
[0]=dose
.limit
[0];
200 if(dose
.val
[1]>dose
.limit
[1]){
201 dose
.val
[1]=dose
.limit
[1];
204 if(dose
.val
[1]==dose
.val
[0]){