Difference between revisions of "MediaWiki:Gadget-calculator-drugs-core.js"

From WikiAnesthesia
Tag: Manual revert
 
(377 intermediate revisions by the same user not shown)
Line 3: Line 3:
  */
  */
( function() {
( function() {
    mw.calculators.setOptionValue( 'defaultDrugColor', 'default' );
    mw.calculators.setOptionValue( 'defaultDrugPopulation', 'general' );
    mw.calculators.setOptionValue( 'defaultDrugRoute', 'iv' );
    mw.calculators.isValueDependent = function( value, variableId ) {
        // This may need generalized to support other variables in the future
        if( variableId === 'weight' ) {
            return value && value.formatUnits().match( /\/[\s(]*?kg/ );
        } else {
            throw new Error( 'Dependence "' + variableId + '" not supported by isValueDependent' );
        }
    };
     /**
     /**
     * DrugColor data
     * Define units
     */
     */
     mw.calculators.addDrugColors( {
     mw.calculators.addUnitsBases( {
         anticholinergic: {
         concentration: {
             primaryColor: '#00ac8c'
             toString: function( units ) {
                units = units.replace( 'pct', '%' );
                units = units.replace( 'ug', 'mcg' );
 
                return units;
            }
         },
         },
         benzodiazepine: {
         mass: {
             primaryColor: '#ff6c2f'
             toString: function( units ) {
                units = units.replace( 'ug', 'mcg' );
 
                return units;
            }
        }
    } );
 
    mw.calculators.addUnits( {
        Eq: {
            baseName: 'mass_eq',
            prefixes: 'short'
         },
         },
         benzodiazepineReversal: {
         mcg: {
             parentColor: 'benzodiazepine',
             baseName: 'mass',
             striped: true
             definition: '1 ug'
         },
         },
         cardiovascularAgonist: {
         patch: {
             primaryColor: '#ba93df'
             baseName: 'volume_patch'
         },
         },
         cardiovascularAntagonist: {
         pct: {
             parentColor: 'cardiovascularAgonist',
             baseName: 'concentration',
             striped: true
             definition: '10 mg/mL',
            formatValue: function( value ) {
                var pctMatch = value.match( /([\d.]+)\s*?%/ );
 
                if( pctMatch ) {
                    var pctValue = pctMatch[ 1 ];
 
                    value = pctValue + '% (' + 10 * pctValue + ' mg/mL)';
                }
 
                return value;
            }
         },
         },
         default: {
         pill: {
             primaryColor: '#fff'
             baseName: 'volume_pill'
         },
         },
         desflurane: {
         spray: {
             primaryColor: '#0ab8fd'
             baseName: 'volume_spray'
         },
         },
         enflurane: {
         units: {
             primaryColor: '#f58733'
             baseName: 'mass_units',
        },
             aliases: [
        epinephrine: {
                'unit'
             parentColor: 'cardiovascularAntagonist',
             ]
            highlightColor: '#000'
        },
        halothane: {
             primaryColor: '#b20107'
        },
        isoflurane: {
            primaryColor: '#ca7fc0'
        },
        localAnesthetic: {
            primaryColor: '#dad9d6'
        },
        neuromuscularBlocker: {
            primaryColor: '#fe5442'
        },
        neuromuscularBlockerReversal: {
            parentColor: 'neuromuscularBlocker',
            striped: true
         },
         },
         nitrousOxide: {
         vial: {
             primaryColor: '#2d549f'
             baseName: 'volume_vial'
        },
        opioid: {
            primaryColor: '#6cd1ef'
        },
        opioidReversal: {
            parentColor: 'opioid',
            striped: true
        },
        sedativeHypnotic: {
            primaryColor: '#ffe800'
        },
        sevoflurane: {
            primaryColor: '#f8da00'
        },
        succinylcholine: {
            parentColor: 'neuromuscularBlocker',
            highlightColor: '#000'
         }
         }
     } );
     } );
Line 78: Line 84:


     /**
     /**
     * DrugPopulation data
     * DrugColor
     */
     */
     mw.calculators.addDrugPopulations( {
     mw.calculators.drugColors = {};
        general: {
 
            name: 'General',
    mw.calculators.addDrugColors = function( drugColorData ) {
            abbreviation: 'Gen.'
         var drugColors = mw.calculators.createCalculatorObjects( 'DrugColor', drugColorData );
        },
 
        neonatal: {
         for( var drugColorId in drugColors ) {
            name: 'Neonatal',
             mw.calculators.drugColors[ drugColorId ] = drugColors[ drugColorId ];
            abbreviation: 'Neo.',
            variables: {
                age: {
                    max: '0 yo'
                }
            }
         },
        pediatric: {
            name: 'Pediatric',
            abbreviation: 'Ped.',
            variables: {
                age: {
                    min: '0 yo',
                    max: '17.9 yo'
                }
            }
         },
        elderly: {
             name: 'Elderly',
            abbreviation: 'Eld.',
            variables: {
                age: {
                    min: '65 yo'
                }
            }
         }
         }
     } );
     };
 


    mw.calculators.getDrugColor = function( drugColorId ) {
        if( mw.calculators.drugColors.hasOwnProperty( drugColorId ) ) {
            return mw.calculators.drugColors[ drugColorId ];
        } else {
            return null;
        }
    };


     /**
     /**
     * DrugIndication data
     * Class DrugColor
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugColor}
    * @constructor
     */
     */
     mw.calculators.addDrugIndications( {
     mw.calculators.objectClasses.DrugColor = function( propertyValues ) {
         abxProphylaxis: {
         var properties = {
             name: 'Antimicrobial prophylaxis',
             required: [
             abbreviation: 'Abx.'
                'id'
        },
            ],
        anxiolysis: {
             optional: [
            name: 'Anxiolysis',
                'parentColor',
            abbreviation: 'Anxiety'
                'primaryColor',
         },
                'highlightColor',
         generalAnesthesia: {
                'striped'
            name: 'General anesthesia',
            ]
            abbreviation: 'GA'
         };
        },
 
        neuromuscularBlockade: {
        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
            name: 'Neuromuscular blockade (non-RSI)',
 
            abbreviation: 'NMB (non-RSI)'
         this.parentColor = this.parentColor || this.id === mw.calculators.getOptionValue( 'defaultDrugColor' ) ? this.parentColor : mw.calculators.getOptionValue( 'defaultDrugColor' );
         },
    };
         neuromuscularBlockadeRsi: {
 
            name: 'Neuromuscular blockade (Rapid sequence induction)',
    mw.calculators.objectClasses.DrugColor.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
            abbreviation: 'NMB (RSI)'
 
        },
    mw.calculators.objectClasses.DrugColor.prototype.getParentDrugColor = function() {
         neuromuscularBlockadeReversal: {
         if( !this.parentColor ) {
             name: 'Neuromuscular blockade reversal',
            return null;
            abbreviation: 'NMB reversal'
         }
         },
 
         mac: {
        var parentDrugColor = mw.calculators.getDrugColor( this.parentColor );
            name: 'Monitored anesthesia care',
 
             abbreviation: 'MAC'
         if( !parentDrugColor ) {
         },
             throw new Error( 'Parent drug color "' + this.parentColor + '" not found for drug color "' + this.id + '"' );
        ponv: {
         }
             name: 'Postoperative nausea & vomiting',
 
            abbreviation: 'PONV'
         return parentDrugColor;
        },
    };
         tiva: {
 
             name: 'Total intravenous anesthesia',
    mw.calculators.objectClasses.DrugColor.prototype.getHighlightColor = function() {
             abbreviation: 'TIVA'
        if( this.highlightColor ) {
             return this.highlightColor;
         } else if( this.parentColor ) {
             return this.getParentDrugColor().getHighlightColor();
        }
    };
 
    mw.calculators.objectClasses.DrugColor.prototype.getPrimaryColor = function() {
         if( this.primaryColor ) {
             return this.primaryColor;
        } else if( this.parentColor ) {
             return this.getParentDrugColor().getPrimaryColor();
         }
         }
     } );
     };


    mw.calculators.objectClasses.DrugColor.prototype.isStriped = function() {
        if( this.striped !== null ) {
            return this.striped;
        } else if( this.parentColor ) {
            return this.getParentDrugColor().isStriped();
        }
    };




    /**
    * Drug data
    */
    var drugId;






     /**
     /**
     * Cefazolin
     * DrugPopulation
     */
     */
    drugId = 'cefazolin';


     mw.calculators.addDrugs( [
     mw.calculators.drugPopulations = {};
         {
 
            id: drugId,
    mw.calculators.addDrugPopulations = function( drugPopulationData ) {
            name: 'Cefazolin'
         var drugPopulations = mw.calculators.createCalculatorObjects( 'DrugPopulation', drugPopulationData );
        }
    ] );


    mw.calculators.addDrugDosages( drugId, [
        for( var drugPopulationId in drugPopulations ) {
        {
            mw.calculators.drugPopulations[ drugPopulationId ] = drugPopulations[ drugPopulationId ];
            indication: 'abxProphylaxis',
            population: 'general',
            dose: {
                dose: '2 g'
            }
         }
         }
     ] );
     };


     mw.calculators.addDrugPreparations( drugId, [
     mw.calculators.getDrugPopulation = function( drugPopulationId ) {
         {
        if( mw.calculators.drugPopulations.hasOwnProperty( drugPopulationId ) ) {
             concentration: '1 g/vial'
            return mw.calculators.drugPopulations[ drugPopulationId ];
         } else {
             return null;
         }
         }
     ] );
     };






     /**
     /**
     * Ketamine
     * Class DrugPopulation
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugPopulation}
    * @constructor
     */
     */
     drugId = 'ketamine';
     mw.calculators.objectClasses.DrugPopulation = function( propertyValues ) {
        var properties = {
            required: [
                'id',
                'name'
            ],
            optional: [
                'abbreviation',
                'variables'
            ]
        };
 
        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );


    mw.calculators.addDrugs( [
        if( this.variables ) {
        {
            for( var variableId in this.variables ) {
            id: drugId,
                if( !mw.calculators.getVariable( variableId ) ) {
            name: 'Ketamine',
                    throw new Error( 'DrugPopulation variable "' + variableId + '" not defined' );
            color: 'sedativeHypnotic'
                }
 
                this.variables[ variableId ].min = this.variables[ variableId ].hasOwnProperty( 'min' ) ?
                    math.unit( this.variables[ variableId ].min ) : null;
 
                this.variables[ variableId ].max = this.variables[ variableId ].hasOwnProperty( 'max' ) ?
                    math.unit( this.variables[ variableId ].max ) : null;
            }
        } else {
            this.variables = {};
         }
         }
     ] );
     };
 
    mw.calculators.objectClasses.DrugPopulation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
 
    mw.calculators.objectClasses.DrugPopulation.prototype.getCalculationData = function() {
        var inputData = new mw.calculators.objectClasses.CalculationData();


    mw.calculators.addDrugPreparations( drugId, [
        for( var variableId in this.variables ) {
        {
             inputData.variables.required.push( variableId );
             concentration: '10 mg/mL'
        }, {
            concentration: '50 mg/mL'
        }, {
            concentration: '100 mg/mL'
         }
         }
    ] );


     mw.calculators.addDrugDosages( drugId, [
        return inputData;
         {
    };
             indication: 'generalAnesthesia',
 
             population: 'general',
     mw.calculators.objectClasses.DrugPopulation.prototype.getCalculationDataScore = function( dataValues ) {
             dose: {
        // A return value of -1 indicates the data did not match the population definition
                 min: '1 mg/kg',
 
                max: '2 mg/kg',
         for( var variableId in this.variables ) {
                 weightCalculation: 'lbw'
             if( !dataValues.hasOwnProperty( variableId ) ) {
                return -1;
             }
 
             if( this.variables[ variableId ].min &&
                 ( !dataValues[ variableId ] ||
                    !math.largerEq( dataValues[ variableId ], this.variables[ variableId ].min ) ) ) {
                 return -1;
             }
             }
        }, {
 
             indication: 'generalAnesthesia',
             if( this.variables[ variableId ].max &&
            population: 'general',
                ( !dataValues[ variableId ] ||
            route: 'im',
                    !math.smallerEq( dataValues[ variableId ], this.variables[ variableId ].max ) ) ) {
            dose: {
                 return -1;
                 min: '4 mg/kg',
                max: '6 mg/kg'
             }
             }
         }
         }
     ] );
 
        // If the data matches the population definition, the score corresponds to the number of variables in the
        // population definition. This should roughly correspond to the specificity of the population.
        return Object.keys( this.variables ).length;
    };
 
     mw.calculators.objectClasses.DrugPopulation.prototype.toString = function() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };






     /**
     /**
     * Lidocaine
     * DrugRoute
     */
     */
     drugId = 'lidocaine';
     mw.calculators.drugRoutes = {};
 
    mw.calculators.addDrugRoutes = function( drugRouteData ) {
        var drugRoutes = mw.calculators.createCalculatorObjects( 'DrugRoute', drugRouteData );


    mw.calculators.addDrugs( [
        for( var drugRouteId in drugRoutes ) {
        {
            mw.calculators.drugRoutes[ drugRouteId ] = drugRoutes[ drugRouteId ];
            id: drugId,
            name: 'Lidocaine',
            color: 'localAnesthetic'
         }
         }
     ] );
     };


     mw.calculators.addDrugPreparations( drugId, [
     mw.calculators.getDrugRoute = function( drugRouteId ) {
         {
         if( mw.calculators.drugRoutes.hasOwnProperty( drugRouteId ) ) {
             concentration: '1 pct'
             return mw.calculators.drugRoutes[ drugRouteId ];
         }, {
         } else {
             concentration: '2 pct'
             return null;
         }
         }
     ] );
     };
 
    /**
    * Class DrugRoute
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugRoute}
    * @constructor
    */
    mw.calculators.objectClasses.DrugRoute = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
 
        this.abbreviation = this.abbreviation ? this.abbreviation : this.name;
    };
 
    mw.calculators.objectClasses.DrugRoute.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
 
    mw.calculators.objectClasses.DrugRoute.prototype.getProperties = function() {
        return {
            required: [
                'id',
                'name'
            ],
            optional: [
                'abbreviation',
                'default'
            ]
        };
    };
 
    mw.calculators.objectClasses.DrugRoute.prototype.toString = function() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };
 
 
 
 






     /**
     /**
     * Midazolam
     * DrugIndication
     */
     */
     drugId = 'midazolam';
     mw.calculators.drugIndications = {};


     mw.calculators.addDrugs( [
     mw.calculators.addDrugIndications = function( drugIndicationData ) {
         {
         var drugIndications = mw.calculators.createCalculatorObjects( 'DrugIndication', drugIndicationData );
            id: drugId,
 
            name: 'Midazolam',
        for( var drugIndicationId in drugIndications ) {
             color: 'benzodiazepine'
             mw.calculators.drugIndications[ drugIndicationId ] = drugIndications[ drugIndicationId ];
         }
         }
     ] );
     };


     mw.calculators.addDrugDosages( drugId, [
     mw.calculators.getDrugIndication = function( drugIndicationId ) {
         {
         if( mw.calculators.drugIndications.hasOwnProperty( drugIndicationId ) ) {
            indication: 'anxiolysis',
             return mw.calculators.drugIndications[ drugIndicationId ];
            population: 'general',
         } else {
            dose: {
             return null;
                min: '0.01 mg/kg',
                max: '0.03 mg/kg'
            }
        }, {
             indication: 'anxiolysis',
            population: 'general',
            route: 'im',
            dose: {
                min: '0.07 mg/kg',
                max: '0.08 mg/kg'
            }
         }, {
             indication: 'anxiolysis',
            population: 'pediatric',
            route: 'po',
            dose: {
                dose: '0.5 mg/kg',
                absoluteMax: '20 mg'
            }
        }, {
            indication: 'generalAnesthesia',
            population: 'general',
            dose: {
                min: '0.1 mg/kg',
                max: '0.3 mg/kg',
                weightCalculation: 'lbw'
            }
         }
         }
     ] );
     };
 
    /**
    * Class DrugIndication
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugIndication}
    * @constructor
    */
    mw.calculators.objectClasses.DrugIndication = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
    };
 
    mw.calculators.objectClasses.DrugIndication.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
 
    mw.calculators.objectClasses.DrugIndication.prototype.getProperties = function() {
        return {
            required: [
                'id',
                'name'
            ],
            optional: [
                'abbreviation',
                'default',
                'searchData'
            ]
        };
    };
 
    mw.calculators.objectClasses.DrugIndication.prototype.getSearchString = function() {
        var searchString = this.name;
 
        searchString += this.abbreviation ? ' ' + this.abbreviation : '';
        searchString += this.searchData ? ' ' + this.searchData : '';
 
        return searchString.trim();
    };
 
    mw.calculators.objectClasses.DrugIndication.prototype.toString = function() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };
 


    mw.calculators.addDrugPreparations( drugId, [
        {
            concentration: '1 mg/mL'
        }, {
            concentration: '5 mg/mL'
        }
    ] );






     /**
     /**
     * Neostigmine
     * Drug
     */
     */
     drugId = 'neostigmine';
     mw.calculators.drugs = {};
 
    mw.calculators.addDrugs = function( drugData ) {
        var drugs = mw.calculators.createCalculatorObjects( 'Drug', drugData );


    mw.calculators.addDrugs( [
        for( var drugId in drugs ) {
        {
            mw.calculators.drugs[ drugId ] = drugs[ drugId ];
            id: drugId,
            name: 'Neostigmine',
            color: 'neuromuscularBlockerReversal'
         }
         }
     ] );
     };
 
    mw.calculators.addDrugDosages = function( drugId, drugDosageData ) {
        var drug = mw.calculators.getDrug( drugId );


    mw.calculators.addDrugDosages( drugId, [
        if( !drug ) {
        {
             throw new Error( 'DrugDosage references drug "' + drugId + '" which is not defined' );
             indication: 'neuromuscularBlockadeReversal',
            population: 'general',
            dose: {
                min: '0.03 mg/kg',
                max: '0.07 mg/kg',
                absoluteMax: '5 mg'
            },
            description: 'For each 1 mg of neostigmine, give 0.2 mg of glycopyrrolate to avoid bradycardia'
        }, {
            indication: 'ponv',
            population: 'pediatric',
            dose: {
                dose: '0.1 mg/kg',
                absoluteMax: '4 mg'
            }
         }
         }
    ] );


     mw.calculators.addDrugPreparations( drugId, [
        drug.addDosages( drugDosageData );
         {
    };
             concentration: '0.5 mg/mL'
 
         }, {
     mw.calculators.getDrug = function( drugId ) {
             concentration: '1 mg/mL'
         if( mw.calculators.drugs.hasOwnProperty( drugId ) ) {
             return mw.calculators.drugs[ drugId ];
         } else {
             return null;
         }
         }
     ] );
     };






     /**
     /**
     * Ondansetron
     * Class Drug
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.Drug}
    * @constructor
     */
     */
     drugId = 'ondansetron';
     mw.calculators.objectClasses.Drug = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
 
        if( !this.color ) {
            this.color = mw.calculators.getOptionValue( 'defaultDrugColor' );
        }
 
        var color = mw.calculators.getDrugColor( this.color );
 
        if( !color ) {
            throw new Error( 'Invalid drug color "' + this.color + '" for drug "' + this.id + '"' );
        }
 
        this.color = color;
 
        if( this.preparations ) {
            var preparationData = this.preparations;
 
            this.preparations = [];
 
            this.addPreparations( preparationData );
        } else {
            this.preparations = [];
        }
 
        if( this.dosages ) {
            var dosageData = this.dosages;
 
            this.dosages = [];
 
            this.addDosages( dosageData );
        } else {
            this.dosages = [];
        }
 
        this.references = this.references ? mw.calculators.prepareReferences( this.references ) : [];
        this.tradeNames = this.tradeNames ? this.tradeNames : [];
    };
 
    mw.calculators.objectClasses.Drug.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
 
    mw.calculators.objectClasses.Drug.prototype.addDosages = function( dosageData ) {
        var dosages = mw.calculators.createCalculatorObjects( 'DrugDosage', dosageData );
 
        for( var dosageId in dosages ) {
            dosages[ dosageId ].id = this.dosages.length;
 
            this.dosages.push( dosages[ dosageId ] );
        }
    };
 
    mw.calculators.objectClasses.Drug.prototype.addPreparations = function( preparationData ) {
        var preparations = mw.calculators.createCalculatorObjects( 'DrugPreparation', preparationData );
 
        for( var preparationId in preparations ) {
            preparations[ preparationId ].id = this.preparations.length;


    mw.calculators.addDrugs( [
            this.preparations.push( preparations[ preparationId ] );
        {
            id: drugId,
            name: 'Ondansetron'
         }
         }
     ] );
     };
 
    mw.calculators.objectClasses.Drug.prototype.getIndications = function() {
        var indications = [];


    mw.calculators.addDrugDosages( drugId, [
        for( var iDosage in this.dosages ) {
        {
             if( this.dosages[ iDosage ].indication ) {
             indication: 'ponv',
                 indications.push( this.dosages[ iDosage ].indication );
            population: 'general',
            dose: {
                 dose: '4 mg'
             }
             }
         }, {
         }
             indication: 'ponv',
 
             population: 'pediatric',
        return indications.filter( mw.calculators.uniqueValues );
             dose: {
    };
                 dose: '0.1 mg/kg',
 
                 absoluteMax: '4 mg'
    mw.calculators.objectClasses.Drug.prototype.getPopulations = function( indicationId ) {
        var populations = [];
 
        for( var iDosage in this.dosages ) {
             if( this.dosages[ iDosage ].population &&
                ( !indicationId || ( this.dosages[ iDosage ].indication && this.dosages[ iDosage ].indication.id === indicationId ) ) ) {
                populations.push( this.dosages[ iDosage ].population );
             }
        }
 
        return populations.filter( mw.calculators.uniqueValues );
    };
 
    mw.calculators.objectClasses.Drug.prototype.getRoutes = function( indicationId ) {
        var routes = [];
 
        for( var iDosage in this.dosages ) {
             if( this.dosages[ iDosage ].routes.length &&
                ( !indicationId || ( this.dosages[ iDosage ].indication && this.dosages[ iDosage ].indication.id === indicationId ) ) ) {
                 for( var iRoute in this.dosages[ iDosage ].routes ) {
                    routes.push( this.dosages[ iDosage ].routes[ iRoute ] );
                 }
             }
             }
         }
         }
    ] );


     mw.calculators.addDrugPreparations( drugId, [
        return routes.filter( mw.calculators.uniqueValues );
         {
    };
             concentration: '2 mg/mL'
 
     mw.calculators.objectClasses.Drug.prototype.getPreparations = function( excludeDilutionRequired ) {
        var preparations = this.preparations.filter( mw.calculators.uniqueValues );
 
         if( excludeDilutionRequired ) {
             for( var iPreparation in preparations ) {
                if( preparations[ iPreparation ].dilutionRequired ) {
                    delete preparations[ iPreparation ];
                }
            }
         }
         }
     ] );
 
        return preparations;
    };
 
     mw.calculators.objectClasses.Drug.prototype.getProperties = function() {
        return {
            required: [
                'id',
                'name'
            ],
            optional: [
                'color',
                'description',
                'dosages',
                'formula',
                'preparations',
                'references',
                'searchData',
                'tradeNames'
            ]
        };
    };
 
 






     /**
     /**
     * Propofol
     * DrugPreparation
     */
     */
     drugId = 'propofol';
     mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) {
        var drug = mw.calculators.getDrug( drugId );


    mw.calculators.addDrugs( [
        if( !drug ) {
        {
             throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' );
             id: drugId,
            name: 'Propofol',
            color: 'sedativeHypnotic'
         }
         }
    ] );


    mw.calculators.addDrugPreparations( drugId, [
        drug.addPreparations( drugPreparationData );
        {
    };
            concentration: '10 mg/mL'
 
        }
 
    ] );


     mw.calculators.addDrugDosages( drugId, [
     /**
         {
    * Class DrugPreparation
             indication: 'generalAnesthesia',
    * @param {Object} propertyValues
            population: 'general',
    * @returns {mw.calculators.objectClasses.DrugPreparation}
            dose: [
    * @constructor
                 {
    */
                    name: 'Induction',
    mw.calculators.objectClasses.DrugPreparation = function( propertyValues ) {
                    min: '1 mg/kg',
         var properties = {
                    max: '2.5 mg/kg',
             required: [
                    weightCalculation: 'lbw'
                 'id',
                 }, {
                'concentration'
                    name: 'Maintenance',
            ],
                    min: '100 mcg/kg/min',
            optional: [
                    max: '200 mcg/kg/min'
                 'default',
                }
                'dilutionRequired',
                'commonDilution'
             ]
             ]
         }, {
         };
            indication: 'generalAnesthesia',
 
            population: 'pediatric',
        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
            dose: [
 
                {
 
                    name: 'Induction',
        this.concentration = this.concentration.replace( 'mcg', 'ug' );
                    min: '2.5 mg/kg',
 
                    max: '3.5 mg/kg',
        this.concentration = math.unit( this.concentration );
                    weightCalculation: 'lbw'
    };
                }, {
 
                    name: 'Maintenance',
    mw.calculators.objectClasses.DrugPreparation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
                    min: '125 mcg/kg/min',
 
                    max: '300 mcg/kg/min'
    mw.calculators.objectClasses.DrugPreparation.prototype.getVolumeUnits = function() {
                }
        // The units of concentration will always be of the form "mass / volume"
            ]
        // The regular expression matches all text leading up to the volume units
        }, {
        return mw.calculators.getUnitsByBase( this.concentration ).volume;
            indication: 'generalAnesthesia',
    };
            population: 'elderly',
 
            dose: [
    mw.calculators.objectClasses.DrugPreparation.prototype.toString = function() {
                {
        return mw.calculators.getValueString( this.concentration );
                    name: 'Induction',
    };
                    min: '1 mg/kg',
 
                    max: '1.5 mg/kg',
 
                    weightCalculation: 'lbw'
                }, {
                    name: 'Maintenance',
                    min: '50 mcg/kg/min',
                    max: '100 mcg/kg/min'
                }
            ]
        }, {
            indication: 'mac',
            population: 'general',
            dose: [
                {
                    min: '25 mcg/kg/min',
                    max: '75 mcg/kg/min'
                }
            ]
        }
    ] );






     /**
     /**
     * Rocuronium
     * Class DrugDosage
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugDosage}
    * @constructor
     */
     */
     drugId = 'rocuronium';
     mw.calculators.objectClasses.DrugDosage = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
 
        var drugIndication = mw.calculators.getDrugIndication( this.indication );


    mw.calculators.addDrugs( [
        if( !drugIndication ) {
        {
             throw new Error( 'Invalid indication "' + this.indication + '" for drug dosage' );
             id: drugId,
            name: 'Rocuronium',
            color: 'neuromuscularBlocker'
         }
         }
    ] );


    mw.calculators.addDrugPreparations( drugId, [
        this.indication = drugIndication;
         {
 
             concentration: '10 mg/mL'
        this.population = this.population ? this.population : mw.calculators.getOptionValue( 'defaultDrugPopulation' );
 
         var drugPopulation = mw.calculators.getDrugPopulation( this.population );
 
        if( !drugPopulation ) {
             throw new Error( 'Invalid population "' + this.population + '" for drug dosage' );
         }
         }
    ] );


    mw.calculators.addDrugDosages( drugId, [
        this.population = drugPopulation;
         {
 
            indication: 'intubation',
        this.references = this.references ? mw.calculators.prepareReferences( this.references ) : [];
            population: 'general',
 
            dose: {
         this.routes = this.routes ? this.routes : [ mw.calculators.getOptionValue( 'defaultDrugRoute' ) ];
                dose: '0.6 mg/kg'
 
            }
        if( !Array.isArray( this.routes ) ) {
         }, {
            this.routes = [ this.routes ];
             indication: 'intubationRsi',
        }
             population: 'general',
 
             dose: {
         drugRoutes = [];
                 dose: '1.2 mg/kg'
 
        for( var iRoute in this.routes ) {
             var drugRouteId = this.routes[ iRoute ];
             var drugRoute = mw.calculators.getDrugRoute( drugRouteId );
 
             if( !drugRoute ) {
                 throw new Error( 'Invalid route "' + drugRouteId + '" for drug dosage' );
             }
             }
            drugRoutes[ iRoute ] = drugRoute;
         }
         }
    ] );


        this.routes = drugRoutes;
        // Add the dose objects to the drug
        var drugDoseData = this.dose;
        this.dose = [];
        this.addDoses( drugDoseData );
    };
    mw.calculators.objectClasses.DrugDosage.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
    mw.calculators.objectClasses.DrugDosage.prototype.addDoses = function( drugDoseData ) {
        if( !drugDoseData ) {
            return;
        } else if( !Array.isArray( drugDoseData ) ) {
            // Each dosage can have one or more associated doses. Ensure this value is an array.
            drugDoseData = [ drugDoseData ];
        }


        var doses = mw.calculators.createCalculatorObjects( 'DrugDose', drugDoseData );


    /**
        for( var doseId in doses ) {
    * Sufentanil
            doses[ doseId ].id = this.dose.length;
    */
    drugId = 'sufentanil';


    mw.calculators.addDrugs( [
            this.dose.push( doses[ doseId ] );
        {
            id: drugId,
            name: 'Sufentanil',
            color: 'opioid'
         }
         }
     ] );
     };


     mw.calculators.addDrugPreparations( drugId, [
     mw.calculators.objectClasses.DrugDosage.prototype.getCalculationData = function() {
         {
         var inputData = new mw.calculators.objectClasses.CalculationData();
            concentration: '5 mcg/mL'
 
         },
         inputData = inputData.merge( this.population.getCalculationData() );
         {
 
             concentration: '50 mcg/mL'
         for( var iDose in this.dose ) {
             inputData = inputData.merge( this.dose[ iDose ].getCalculationData() );
         }
         }
    ] );


     mw.calculators.addDrugDosages( drugId, [
        return inputData;
         {
    };
             indication: 'tiva',
 
             population: 'general',
     mw.calculators.objectClasses.DrugDosage.prototype.getProperties = function() {
             dose: [
         return {
                 {
             required: [
                    name: 'Load',
                'id'
                    min: '0.25 mcg/kg',
             ],
                    max: '2 mcg/kg',
             optional: [
                    weightCalculation: 'lbw'
                 'description',
                 }, {
                'dose',
                    name: 'Maintenance',
                'indication',
                    min: '0.5 mcg/kg/hr',
                 'population',
                    max: '1.5 mcg/kg/hr'
                'routes',
                }
                'references'
             ]
             ]
        };
    };
    mw.calculators.objectClasses.DrugDosage.prototype.getRouteString = function() {
        var routeString = '';
        for( var iRoute in this.routes ) {
            routeString += routeString ? '/' : '';
            routeString += this.routes[ iRoute ].abbreviation;
         }
         }
    ] );


        return routeString;
    };


    mw.calculators.objectClasses.DrugDosage.prototype.hasInfo = function() {
        return this.description;
    };




Line 569: Line 758:


     /**
     /**
     * DrugDosage
     * Class DrugDose
     *
     * @param {Object} propertyValues
    * Structure is:
     * @returns {mw.calculators.objectClasses.DrugDose}
    *
     * @constructor
    * drugId: {
     *     indicationId: {
    *        doseId: {
    *
    *        }
    *    }
     * }
     */
     */
    mw.calculators.objectClasses.DrugDose = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
        if( this.weightCalculation ) {
            var weightCalculationIds = this.weightCalculation;
            // weightCalculation property will contain references to the actual objects, so reinitialize
            this.weightCalculation = [];
            if( !Array.isArray( weightCalculationIds ) ) {
                weightCalculationIds = [ weightCalculationIds ];
            }
            for( var iWeightCalculation in weightCalculationIds ) {
                var weightCalculationId = weightCalculationIds[ iWeightCalculation ];
                var weightCalculation = mw.calculators.getCalculation( weightCalculationId );


    var drugDosages = {
                 if( !weightCalculation ) {
        cefazolin: {
                     throw new Error( 'Drug dose references weight calculation "' + weightCalculationId + '" which is not defined' );
            abxProphylaxis: {
                general: {
                    population: 'general',
                    dose: '2 g'
                },
                general120kg: {
                    population: {
                        id: 'general',
                        variables: {
                            weight: {
                                min: '120 kg'
                            }
                        }
                    },
                    dose: '3 g'
                },
                 pediatric: {
                     dose: {
                        absoluteMax: '2 g',
                        dose: '30 mg/kg'
                    }
                },
                pediatric120kg: {
                    population: {
                        id: 'pediatric',
                        variables: {
                            weight: {
                                min: '120 kg'
                            }
                        }
                    },
                    dose: {
                        dose: '3 g'
                    }
                 }
                 }
                this.weightCalculation.push( weightCalculation );
             }
             }
        } else {
            this.weightCalculation = [];
        }
        var mathProperties = this.getMathProperties();
        var isWeightDependent = false;
        for( var iMathProperty in mathProperties ) {
            var mathProperty = mathProperties[ iMathProperty ];
            if( this[ mathProperty ] ) {
                // TODO consider making a UnitsBase.weight.fromString()
                this[ mathProperty ] = this[ mathProperty ].replace( 'kg', 'kgwt' );
                this[ mathProperty ] = this[ mathProperty ].replace( 'mcg', 'ug' );
                this[ mathProperty ] = math.unit( this[ mathProperty ] );
                if( mw.calculators.isValueDependent( this[ mathProperty ], 'weight' ) ) {
                    isWeightDependent = true;
                }
            } else {
                this[ mathProperty ] = null;
            }
        }
        if( isWeightDependent ) {
            // Default is tbw
            this.weightCalculation.push( mw.calculators.getCalculation( 'tbw' ) );
        }
    };
    mw.calculators.objectClasses.DrugDose.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
    mw.calculators.objectClasses.DrugDose.prototype.getAdministration = function() {
        var administration = '';
        if( this.frequency ) {
            administration += administration ? ' ' : '';
            administration += this.frequency;
        }
        if( this.duration ) {
            administration += administration ? ' ' : '';
            administration += 'over ' + this.duration;
         }
         }
        return administration;
    };
    mw.calculators.objectClasses.DrugDose.prototype.getCalculationData = function() {
        var calculationData = new mw.calculators.objectClasses.CalculationData();
        for( var iWeightCalculation in this.weightCalculation ) {
            calculationData.calculations.optional.push( this.weightCalculation[ iWeightCalculation ].id );
        }
        return calculationData;
    };
    mw.calculators.objectClasses.DrugDose.prototype.getMathProperties = function() {
        return [
            'dose',
            'min',
            'max',
            'absoluteMin',
            'absoluteMax'
        ];
    };
    mw.calculators.objectClasses.DrugDose.prototype.getProperties = function() {
        return {
            required: [
                'id'
            ],
            optional: [
                'absoluteMax',
                'absoluteMin',
                'dose',
                'duration',
                'frequency',
                'min',
                'max',
                'name',
                'text',
                'weightCalculation'
            ]
        };
     };
     };


}() );
}() );

Latest revision as of 20:55, 29 March 2022

/**
 * @author Chris Rishel
 */
( function() {
    mw.calculators.setOptionValue( 'defaultDrugColor', 'default' );
    mw.calculators.setOptionValue( 'defaultDrugPopulation', 'general' );
    mw.calculators.setOptionValue( 'defaultDrugRoute', 'iv' );

    mw.calculators.isValueDependent = function( value, variableId ) {
        // This may need generalized to support other variables in the future
        if( variableId === 'weight' ) {
            return value && value.formatUnits().match( /\/[\s(]*?kg/ );
        } else {
            throw new Error( 'Dependence "' + variableId + '" not supported by isValueDependent' );
        }
    };

    /**
     * Define units
     */
    mw.calculators.addUnitsBases( {
        concentration: {
            toString: function( units ) {
                units = units.replace( 'pct', '%' );
                units = units.replace( 'ug', 'mcg' );

                return units;
            }
        },
        mass: {
            toString: function( units ) {
                units = units.replace( 'ug', 'mcg' );

                return units;
            }
        }
    } );

    mw.calculators.addUnits( {
        Eq: {
            baseName: 'mass_eq',
            prefixes: 'short'
        },
        mcg: {
            baseName: 'mass',
            definition: '1 ug'
        },
        patch: {
            baseName: 'volume_patch'
        },
        pct: {
            baseName: 'concentration',
            definition: '10 mg/mL',
            formatValue: function( value ) {
                var pctMatch = value.match( /([\d.]+)\s*?%/ );

                if( pctMatch ) {
                    var pctValue = pctMatch[ 1 ];

                    value = pctValue + '% (' + 10 * pctValue + ' mg/mL)';
                }

                return value;
            }
        },
        pill: {
            baseName: 'volume_pill'
        },
        spray: {
            baseName: 'volume_spray'
        },
        units: {
            baseName: 'mass_units',
            aliases: [
                'unit'
            ]
        },
        vial: {
            baseName: 'volume_vial'
        }
    } );



    /**
     * DrugColor
     */
    mw.calculators.drugColors = {};

    mw.calculators.addDrugColors = function( drugColorData ) {
        var drugColors = mw.calculators.createCalculatorObjects( 'DrugColor', drugColorData );

        for( var drugColorId in drugColors ) {
            mw.calculators.drugColors[ drugColorId ] = drugColors[ drugColorId ];
        }
    };

    mw.calculators.getDrugColor = function( drugColorId ) {
        if( mw.calculators.drugColors.hasOwnProperty( drugColorId ) ) {
            return mw.calculators.drugColors[ drugColorId ];
        } else {
            return null;
        }
    };

    /**
     * Class DrugColor
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.DrugColor}
     * @constructor
     */
    mw.calculators.objectClasses.DrugColor = function( propertyValues ) {
        var properties = {
            required: [
                'id'
            ],
            optional: [
                'parentColor',
                'primaryColor',
                'highlightColor',
                'striped'
            ]
        };

        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );

        this.parentColor = this.parentColor || this.id === mw.calculators.getOptionValue( 'defaultDrugColor' ) ? this.parentColor : mw.calculators.getOptionValue( 'defaultDrugColor' );
    };

    mw.calculators.objectClasses.DrugColor.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );

    mw.calculators.objectClasses.DrugColor.prototype.getParentDrugColor = function() {
        if( !this.parentColor ) {
            return null;
        }

        var parentDrugColor = mw.calculators.getDrugColor( this.parentColor );

        if( !parentDrugColor ) {
            throw new Error( 'Parent drug color "' + this.parentColor + '" not found for drug color "' + this.id + '"' );
        }

        return parentDrugColor;
    };

    mw.calculators.objectClasses.DrugColor.prototype.getHighlightColor = function() {
        if( this.highlightColor ) {
            return this.highlightColor;
        } else if( this.parentColor ) {
            return this.getParentDrugColor().getHighlightColor();
        }
    };

    mw.calculators.objectClasses.DrugColor.prototype.getPrimaryColor = function() {
        if( this.primaryColor ) {
            return this.primaryColor;
        } else if( this.parentColor ) {
            return this.getParentDrugColor().getPrimaryColor();
        }
    };

    mw.calculators.objectClasses.DrugColor.prototype.isStriped = function() {
        if( this.striped !== null ) {
            return this.striped;
        } else if( this.parentColor ) {
            return this.getParentDrugColor().isStriped();
        }
    };





    /**
     * DrugPopulation
     */

    mw.calculators.drugPopulations = {};

    mw.calculators.addDrugPopulations = function( drugPopulationData ) {
        var drugPopulations = mw.calculators.createCalculatorObjects( 'DrugPopulation', drugPopulationData );

        for( var drugPopulationId in drugPopulations ) {
            mw.calculators.drugPopulations[ drugPopulationId ] = drugPopulations[ drugPopulationId ];
        }
    };

    mw.calculators.getDrugPopulation = function( drugPopulationId ) {
        if( mw.calculators.drugPopulations.hasOwnProperty( drugPopulationId ) ) {
            return mw.calculators.drugPopulations[ drugPopulationId ];
        } else {
            return null;
        }
    };



    /**
     * Class DrugPopulation
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.DrugPopulation}
     * @constructor
     */
    mw.calculators.objectClasses.DrugPopulation = function( propertyValues ) {
        var properties = {
            required: [
                'id',
                'name'
            ],
            optional: [
                'abbreviation',
                'variables'
            ]
        };

        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );

        if( this.variables ) {
            for( var variableId in this.variables ) {
                if( !mw.calculators.getVariable( variableId ) ) {
                    throw new Error( 'DrugPopulation variable "' + variableId + '" not defined' );
                }

                this.variables[ variableId ].min = this.variables[ variableId ].hasOwnProperty( 'min' ) ?
                    math.unit( this.variables[ variableId ].min ) : null;

                this.variables[ variableId ].max = this.variables[ variableId ].hasOwnProperty( 'max' ) ?
                    math.unit( this.variables[ variableId ].max ) : null;
            }
        } else {
            this.variables = {};
        }
    };

    mw.calculators.objectClasses.DrugPopulation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );

    mw.calculators.objectClasses.DrugPopulation.prototype.getCalculationData = function() {
        var inputData = new mw.calculators.objectClasses.CalculationData();

        for( var variableId in this.variables ) {
            inputData.variables.required.push( variableId );
        }

        return inputData;
    };

    mw.calculators.objectClasses.DrugPopulation.prototype.getCalculationDataScore = function( dataValues ) {
        // A return value of -1 indicates the data did not match the population definition

        for( var variableId in this.variables ) {
            if( !dataValues.hasOwnProperty( variableId ) ) {
                return -1;
            }

            if( this.variables[ variableId ].min &&
                ( !dataValues[ variableId ] ||
                    !math.largerEq( dataValues[ variableId ], this.variables[ variableId ].min ) ) ) {
                return -1;
            }

            if( this.variables[ variableId ].max &&
                ( !dataValues[ variableId ] ||
                    !math.smallerEq( dataValues[ variableId ], this.variables[ variableId ].max ) ) ) {
                return -1;
            }
        }

        // If the data matches the population definition, the score corresponds to the number of variables in the
        // population definition. This should roughly correspond to the specificity of the population.
        return Object.keys( this.variables ).length;
    };

    mw.calculators.objectClasses.DrugPopulation.prototype.toString = function() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };



    /**
     * DrugRoute
     */
    mw.calculators.drugRoutes = {};

    mw.calculators.addDrugRoutes = function( drugRouteData ) {
        var drugRoutes = mw.calculators.createCalculatorObjects( 'DrugRoute', drugRouteData );

        for( var drugRouteId in drugRoutes ) {
            mw.calculators.drugRoutes[ drugRouteId ] = drugRoutes[ drugRouteId ];
        }
    };

    mw.calculators.getDrugRoute = function( drugRouteId ) {
        if( mw.calculators.drugRoutes.hasOwnProperty( drugRouteId ) ) {
            return mw.calculators.drugRoutes[ drugRouteId ];
        } else {
            return null;
        }
    };

    /**
     * Class DrugRoute
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.DrugRoute}
     * @constructor
     */
    mw.calculators.objectClasses.DrugRoute = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );

        this.abbreviation = this.abbreviation ? this.abbreviation : this.name;
    };

    mw.calculators.objectClasses.DrugRoute.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );

    mw.calculators.objectClasses.DrugRoute.prototype.getProperties = function() {
        return {
            required: [
                'id',
                'name'
            ],
            optional: [
                'abbreviation',
                'default'
            ]
        };
    };

    mw.calculators.objectClasses.DrugRoute.prototype.toString = function() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };







    /**
     * DrugIndication
     */
    mw.calculators.drugIndications = {};

    mw.calculators.addDrugIndications = function( drugIndicationData ) {
        var drugIndications = mw.calculators.createCalculatorObjects( 'DrugIndication', drugIndicationData );

        for( var drugIndicationId in drugIndications ) {
            mw.calculators.drugIndications[ drugIndicationId ] = drugIndications[ drugIndicationId ];
        }
    };

    mw.calculators.getDrugIndication = function( drugIndicationId ) {
        if( mw.calculators.drugIndications.hasOwnProperty( drugIndicationId ) ) {
            return mw.calculators.drugIndications[ drugIndicationId ];
        } else {
            return null;
        }
    };

    /**
     * Class DrugIndication
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.DrugIndication}
     * @constructor
     */
    mw.calculators.objectClasses.DrugIndication = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
    };

    mw.calculators.objectClasses.DrugIndication.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );

    mw.calculators.objectClasses.DrugIndication.prototype.getProperties = function() {
        return {
            required: [
                'id',
                'name'
            ],
            optional: [
                'abbreviation',
                'default',
                'searchData'
            ]
        };
    };

    mw.calculators.objectClasses.DrugIndication.prototype.getSearchString = function() {
        var searchString = this.name;

        searchString += this.abbreviation ? ' ' + this.abbreviation : '';
        searchString += this.searchData ? ' ' + this.searchData : '';

        return searchString.trim();
    };

    mw.calculators.objectClasses.DrugIndication.prototype.toString = function() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };





    /**
     * Drug
     */
    mw.calculators.drugs = {};

    mw.calculators.addDrugs = function( drugData ) {
        var drugs = mw.calculators.createCalculatorObjects( 'Drug', drugData );

        for( var drugId in drugs ) {
            mw.calculators.drugs[ drugId ] = drugs[ drugId ];
        }
    };

    mw.calculators.addDrugDosages = function( drugId, drugDosageData ) {
        var drug = mw.calculators.getDrug( drugId );

        if( !drug ) {
            throw new Error( 'DrugDosage references drug "' + drugId + '" which is not defined' );
        }

        drug.addDosages( drugDosageData );
    };

    mw.calculators.getDrug = function( drugId ) {
        if( mw.calculators.drugs.hasOwnProperty( drugId ) ) {
            return mw.calculators.drugs[ drugId ];
        } else {
            return null;
        }
    };



    /**
     * Class Drug
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.Drug}
     * @constructor
     */
    mw.calculators.objectClasses.Drug = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );

        if( !this.color ) {
            this.color = mw.calculators.getOptionValue( 'defaultDrugColor' );
        }

        var color = mw.calculators.getDrugColor( this.color );

        if( !color ) {
            throw new Error( 'Invalid drug color "' + this.color + '" for drug "' + this.id + '"' );
        }

        this.color = color;

        if( this.preparations ) {
            var preparationData = this.preparations;

            this.preparations = [];

            this.addPreparations( preparationData );
        } else {
            this.preparations = [];
        }

        if( this.dosages ) {
            var dosageData = this.dosages;

            this.dosages = [];

            this.addDosages( dosageData );
        } else {
            this.dosages = [];
        }

        this.references = this.references ? mw.calculators.prepareReferences( this.references ) : [];
        this.tradeNames = this.tradeNames ? this.tradeNames : [];
    };

    mw.calculators.objectClasses.Drug.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );

    mw.calculators.objectClasses.Drug.prototype.addDosages = function( dosageData ) {
        var dosages = mw.calculators.createCalculatorObjects( 'DrugDosage', dosageData );

        for( var dosageId in dosages ) {
            dosages[ dosageId ].id = this.dosages.length;

            this.dosages.push( dosages[ dosageId ] );
        }
    };

    mw.calculators.objectClasses.Drug.prototype.addPreparations = function( preparationData ) {
        var preparations = mw.calculators.createCalculatorObjects( 'DrugPreparation', preparationData );

        for( var preparationId in preparations ) {
            preparations[ preparationId ].id = this.preparations.length;

            this.preparations.push( preparations[ preparationId ] );
        }
    };

    mw.calculators.objectClasses.Drug.prototype.getIndications = function() {
        var indications = [];

        for( var iDosage in this.dosages ) {
            if( this.dosages[ iDosage ].indication ) {
                indications.push( this.dosages[ iDosage ].indication );
            }
        }

        return indications.filter( mw.calculators.uniqueValues );
    };

    mw.calculators.objectClasses.Drug.prototype.getPopulations = function( indicationId ) {
        var populations = [];

        for( var iDosage in this.dosages ) {
            if( this.dosages[ iDosage ].population &&
                ( !indicationId || ( this.dosages[ iDosage ].indication && this.dosages[ iDosage ].indication.id === indicationId ) ) ) {
                populations.push( this.dosages[ iDosage ].population );
            }
        }

        return populations.filter( mw.calculators.uniqueValues );
    };

    mw.calculators.objectClasses.Drug.prototype.getRoutes = function( indicationId ) {
        var routes = [];

        for( var iDosage in this.dosages ) {
            if( this.dosages[ iDosage ].routes.length &&
                ( !indicationId || ( this.dosages[ iDosage ].indication && this.dosages[ iDosage ].indication.id === indicationId ) ) ) {
                for( var iRoute in this.dosages[ iDosage ].routes ) {
                    routes.push( this.dosages[ iDosage ].routes[ iRoute ] );
                }
            }
        }

        return routes.filter( mw.calculators.uniqueValues );
    };

    mw.calculators.objectClasses.Drug.prototype.getPreparations = function( excludeDilutionRequired ) {
        var preparations = this.preparations.filter( mw.calculators.uniqueValues );

        if( excludeDilutionRequired ) {
            for( var iPreparation in preparations ) {
                if( preparations[ iPreparation ].dilutionRequired ) {
                    delete preparations[ iPreparation ];
                }
            }
        }

        return preparations;
    };

    mw.calculators.objectClasses.Drug.prototype.getProperties = function() {
        return {
            required: [
                'id',
                'name'
            ],
            optional: [
                'color',
                'description',
                'dosages',
                'formula',
                'preparations',
                'references',
                'searchData',
                'tradeNames'
            ]
        };
    };





    /**
     * DrugPreparation
     */
    mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) {
        var drug = mw.calculators.getDrug( drugId );

        if( !drug ) {
            throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' );
        }

        drug.addPreparations( drugPreparationData );
    };



    /**
     * Class DrugPreparation
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.DrugPreparation}
     * @constructor
     */
    mw.calculators.objectClasses.DrugPreparation = function( propertyValues ) {
        var properties = {
            required: [
                'id',
                'concentration'
            ],
            optional: [
                'default',
                'dilutionRequired',
                'commonDilution'
            ]
        };

        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );


        this.concentration = this.concentration.replace( 'mcg', 'ug' );

        this.concentration = math.unit( this.concentration );
    };

    mw.calculators.objectClasses.DrugPreparation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );

    mw.calculators.objectClasses.DrugPreparation.prototype.getVolumeUnits = function() {
        // The units of concentration will always be of the form "mass / volume"
        // The regular expression matches all text leading up to the volume units
        return mw.calculators.getUnitsByBase( this.concentration ).volume;
    };

    mw.calculators.objectClasses.DrugPreparation.prototype.toString = function() {
        return mw.calculators.getValueString( this.concentration );
    };





    /**
     * Class DrugDosage
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.DrugDosage}
     * @constructor
     */
    mw.calculators.objectClasses.DrugDosage = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );

        var drugIndication = mw.calculators.getDrugIndication( this.indication );

        if( !drugIndication ) {
            throw new Error( 'Invalid indication "' + this.indication + '" for drug dosage' );
        }

        this.indication = drugIndication;

        this.population = this.population ? this.population : mw.calculators.getOptionValue( 'defaultDrugPopulation' );

        var drugPopulation = mw.calculators.getDrugPopulation( this.population );

        if( !drugPopulation ) {
            throw new Error( 'Invalid population "' + this.population + '" for drug dosage' );
        }

        this.population = drugPopulation;

        this.references = this.references ? mw.calculators.prepareReferences( this.references ) : [];

        this.routes = this.routes ? this.routes : [ mw.calculators.getOptionValue( 'defaultDrugRoute' ) ];

        if( !Array.isArray( this.routes ) ) {
            this.routes = [ this.routes ];
        }

        drugRoutes = [];

        for( var iRoute in this.routes ) {
            var drugRouteId = this.routes[ iRoute ];
            var drugRoute = mw.calculators.getDrugRoute( drugRouteId );

            if( !drugRoute ) {
                throw new Error( 'Invalid route "' + drugRouteId + '" for drug dosage' );
            }

            drugRoutes[ iRoute ] = drugRoute;
        }

        this.routes = drugRoutes;

        // Add the dose objects to the drug
        var drugDoseData = this.dose;
        this.dose = [];

        this.addDoses( drugDoseData );
    };

    mw.calculators.objectClasses.DrugDosage.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );

    mw.calculators.objectClasses.DrugDosage.prototype.addDoses = function( drugDoseData ) {
        if( !drugDoseData ) {
            return;
        } else if( !Array.isArray( drugDoseData ) ) {
            // Each dosage can have one or more associated doses. Ensure this value is an array.
            drugDoseData = [ drugDoseData ];
        }

        var doses = mw.calculators.createCalculatorObjects( 'DrugDose', drugDoseData );

        for( var doseId in doses ) {
            doses[ doseId ].id = this.dose.length;

            this.dose.push( doses[ doseId ] );
        }
    };

    mw.calculators.objectClasses.DrugDosage.prototype.getCalculationData = function() {
        var inputData = new mw.calculators.objectClasses.CalculationData();

        inputData = inputData.merge( this.population.getCalculationData() );

        for( var iDose in this.dose ) {
            inputData = inputData.merge( this.dose[ iDose ].getCalculationData() );
        }

        return inputData;
    };

    mw.calculators.objectClasses.DrugDosage.prototype.getProperties = function() {
        return {
            required: [
                'id'
            ],
            optional: [
                'description',
                'dose',
                'indication',
                'population',
                'routes',
                'references'
            ]
        };
    };

    mw.calculators.objectClasses.DrugDosage.prototype.getRouteString = function() {
        var routeString = '';

        for( var iRoute in this.routes ) {
            routeString += routeString ? '/' : '';
            routeString += this.routes[ iRoute ].abbreviation;
        }

        return routeString;
    };

    mw.calculators.objectClasses.DrugDosage.prototype.hasInfo = function() {
        return this.description;
    };





    /**
     * Class DrugDose
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.DrugDose}
     * @constructor
     */
    mw.calculators.objectClasses.DrugDose = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );

        if( this.weightCalculation ) {
            var weightCalculationIds = this.weightCalculation;

            // weightCalculation property will contain references to the actual objects, so reinitialize
            this.weightCalculation = [];

            if( !Array.isArray( weightCalculationIds ) ) {
                weightCalculationIds = [ weightCalculationIds ];
            }

            for( var iWeightCalculation in weightCalculationIds ) {
                var weightCalculationId = weightCalculationIds[ iWeightCalculation ];
                var weightCalculation = mw.calculators.getCalculation( weightCalculationId );

                if( !weightCalculation ) {
                    throw new Error( 'Drug dose references weight calculation "' + weightCalculationId + '" which is not defined' );
                }

                this.weightCalculation.push( weightCalculation );
            }
        } else {
            this.weightCalculation = [];
        }

        var mathProperties = this.getMathProperties();
        var isWeightDependent = false;

        for( var iMathProperty in mathProperties ) {
            var mathProperty = mathProperties[ iMathProperty ];

            if( this[ mathProperty ] ) {
                // TODO consider making a UnitsBase.weight.fromString()
                this[ mathProperty ] = this[ mathProperty ].replace( 'kg', 'kgwt' );
                this[ mathProperty ] = this[ mathProperty ].replace( 'mcg', 'ug' );

                this[ mathProperty ] = math.unit( this[ mathProperty ] );

                if( mw.calculators.isValueDependent( this[ mathProperty ], 'weight' ) ) {
                    isWeightDependent = true;
                }
            } else {
                this[ mathProperty ] = null;
            }
        }

        if( isWeightDependent ) {
            // Default is tbw
            this.weightCalculation.push( mw.calculators.getCalculation( 'tbw' ) );
        }
    };

    mw.calculators.objectClasses.DrugDose.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );

    mw.calculators.objectClasses.DrugDose.prototype.getAdministration = function() {
        var administration = '';

        if( this.frequency ) {
            administration += administration ? ' ' : '';
            administration += this.frequency;
        }

        if( this.duration ) {
            administration += administration ? ' ' : '';
            administration += 'over ' + this.duration;
        }

        return administration;
    };

    mw.calculators.objectClasses.DrugDose.prototype.getCalculationData = function() {
        var calculationData = new mw.calculators.objectClasses.CalculationData();

        for( var iWeightCalculation in this.weightCalculation ) {
            calculationData.calculations.optional.push( this.weightCalculation[ iWeightCalculation ].id );
        }

        return calculationData;
    };

    mw.calculators.objectClasses.DrugDose.prototype.getMathProperties = function() {
        return [
            'dose',
            'min',
            'max',
            'absoluteMin',
            'absoluteMax'
        ];
    };

    mw.calculators.objectClasses.DrugDose.prototype.getProperties = function() {
        return {
            required: [
                'id'
            ],
            optional: [
                'absoluteMax',
                'absoluteMin',
                'dose',
                'duration',
                'frequency',
                'min',
                'max',
                'name',
                'text',
                'weightCalculation'
            ]
        };
    };

}() );