]> Softwares of Agnibho - pdosage.git/blob - src/dosage.js
9d7c15c2a2561c231f97964584b6eade5cee62c7
[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 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"));
113 type="wt";
114 }
115 else{
116 min=strToDays(form.range[i].min);
117 type="age";
118 }
119 }
120 else{
121 min=0;
122 }
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"));
126 type="wt";
127 }
128 else{
129 max=strToDays(form.range[i].max);
130 type="age";
131 }
132 }
133 else{
134 max=Infinity;
135 }
136 if(type=="wt"){
137 if(patient.wt>=min && patient.wt<max){
138 return form.range[i];
139 }
140 }
141 else if(type=="age"){
142 if(pDays>=min && pDays<max){
143 return form.range[i];
144 }
145 }
146 }
147 return null;
148
149 function strToDays(str){
150 var age={y: 0, m: 0, d: 0};
151 var days=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));
156 }
157 if(str.indexOf("m")!=-1){
158 age.m=str.slice(0, str.indexOf("m"));
159 str=str.slice(str.indexOf("m", -1));
160 }
161 if(str.indexOf("d")!=-1){
162 age.d=str.slice(0, str.indexOf("d"));
163 str=str.slice(str.indexOf("d", -1));
164 }
165 days=age.y*365+age.m*30+age.d;
166 return days;
167 }
168 }
169
170 function findApprox(arr){
171 var ret=[];
172 arr[0]=arr[0]/2;
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;
176 }
177 if(!ret[0] || (ret[0]==ret[1])){
178 ret[0]=ret[1];
179 ret[1]=null;
180 }
181 return ret;
182 }
183
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;
189 var ret=eval(str);
190 ret=Math.round(ret*100)/100;
191 return ret;
192 }
193
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];
199 }
200 if(dose.val[1]>dose.limit[1]){
201 dose.val[1]=dose.limit[1];
202 }
203 }
204 if(dose.val[1]==dose.val[0]){
205 dose.val[1]=null;
206 }
207 return dose;
208 }
209 }
210 }