]> Softwares of Agnibho - pdosage.git/blob - src/Dosage.js
Typo correction
[pdosage.git] / src / Dosage.js
1 /**********************************************************************
2 * Title: PDosage
3 * Description: Pediatric Calculator
4 * Author: Agnibho Mondal
5 * Website: http://code.agnibho.com
6 **********************************************************************
7 Copyright (c) 2016 Agnibho Mondal
8 All rights reserved
9 **********************************************************************
10 This file is part of PDosage.
11
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.
16
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.
21
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 **********************************************************************/
25
26 export default function Dosage(data){
27
28 Object.defineProperty(data, "prop", {
29 writable: false
30 });
31
32 this.get=function(){
33 return data;
34 }
35
36 this.listDrugs=function(){
37 var list=data.map(function(obj){
38 return obj.name;
39 });
40 return list;
41 }
42
43 this.getDrug=function(name, patient){
44 if(this.listDrugs().indexOf(name)==-1){
45 return null;
46 }
47 var drug=JSON.parse(JSON.stringify(data.filter(extract)[0]));
48 function extract(obj){
49 return (obj.name == name);
50 }
51 for(var i=0; i<drug.form.length; i++){
52 drug.form[i]=calculateDose(drug.form[i], patient);
53 }
54 return drug;
55 }
56
57 function calculateDose(form, patient){
58 while(form.hasOwnProperty("range")){
59 var res=isolateRange(form, patient);
60 delete form.range;
61 if(res!=null){
62 if(res.hasOwnProperty("range")){
63 form.range=res.range;
64 }
65 else{
66 form.dose=res.dose;
67 }
68 }
69 }
70 if(form.dose){
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);
75 }
76 else if(form.dose[i].hasOwnProperty("perKg")){
77 form.dose[i].perKg=[].concat(form.dose[i].perKg);
78 form.dose[i].val=[];
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]);
82 }
83 else if(form.dose[i].hasOwnProperty("approx")){
84 form.dose[i].val=[];
85 form.dose[i].val=findApprox(form.dose[i].approx);
86 form.dose[i]=setLimit(form.dose[i]);
87 }
88 else if(form.dose[i].hasOwnProperty("eq")){
89 form.dose[i].eq=[].concat(form.dose[i].eq);
90 form.dose[i].val=[];
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]);
94 }
95 else{
96 form.dose[i].val=[];
97 form.dose[i].val[0]=null;
98 form.dose[i].val[1]=null;
99 }
100 }
101 }
102 return form;
103
104 function isolateRange(form, patient){
105 var min=0;
106 var max=0;
107 var type="";
108 if(patient.age.y===null && patient.age.m===null && patient.age.d===null){
109 return null;
110 }
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"));
116 type="wt";
117 }
118 else{
119 min=strToDays(form.range[i].min);
120 type="age";
121 }
122 }
123 else{
124 min=0;
125 }
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"));
129 type="wt";
130 }
131 else{
132 max=strToDays(form.range[i].max);
133 type="age";
134 }
135 }
136 else{
137 max=Infinity;
138 }
139 if(type=="wt"){
140 if(patient.wt>=min && patient.wt<max){
141 return form.range[i];
142 }
143 }
144 else if(type=="age"){
145 if(pDays>=min && pDays<max){
146 return form.range[i];
147 }
148 }
149 }
150 return null;
151
152 function strToDays(str){
153 var age={y: 0, m: 0, d: 0};
154 var days=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));
159 }
160 if(str.indexOf("m")!=-1){
161 age.m=str.slice(0, str.indexOf("m"));
162 str=str.slice(str.indexOf("m", -1));
163 }
164 if(str.indexOf("d")!=-1){
165 age.d=str.slice(0, str.indexOf("d"));
166 str=str.slice(str.indexOf("d", -1));
167 }
168 days=age.y*365+age.m*30+age.d;
169 return days;
170 }
171 }
172
173 function findApprox(arr){
174 var ret=[];
175 arr[0]=arr[0]/2;
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;
179 }
180 if(!ret[0] || (ret[0]==ret[1])){
181 ret[0]=ret[1];
182 ret[1]=null;
183 }
184 return ret;
185 }
186
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;
192 var ret=eval(str);
193 ret=Math.round(ret*100)/100;
194 return ret;
195 }
196
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];
201 }
202 if(dose.val[1]>dose.adult[1]){
203 dose.val[1]=dose.adult[1];
204 }
205 }
206 if(dose.limit && dose.val){
207 if(dose.val[0]>dose.limit[0]){
208 dose.val[0]=dose.limit[0];
209 }
210 if(dose.val[1]>dose.limit[1]){
211 dose.val[1]=dose.limit[1];
212 }
213 }
214 if(dose.val[1]==dose.val[0]){
215 dose.val[1]=null;
216 }
217 return dose;
218 }
219 }
220 }