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

From WikiAnesthesia
Tag: Manual revert
 
(567 intermediate revisions by the same user not shown)
Line 3: Line 3:
  */
  */
( function() {
( function() {
     var DEFAULT_DRUG_COLOR = 'default';
     mw.calculators.setOptionValue( 'defaultDrugColor', 'default' );
     var DEFAULT_DRUG_POPULATION = 'general';
     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' );
        }
    };


     /**
     /**
Line 12: Line 22:
         concentration: {
         concentration: {
             toString: function( units ) {
             toString: function( units ) {
                 units = units.replace( ' pct', '%' );
                 units = units.replace( 'pct', '%' );
                units = units.replace( 'ug', 'mcg' );
 
                return units;
            }
        },
        mass: {
            toString: function( units ) {
                units = units.replace( 'ug', 'mcg' );


                 return units;
                 return units;
Line 20: Line 38:


     mw.calculators.addUnits( {
     mw.calculators.addUnits( {
        Eq: {
            baseName: 'mass_eq',
            prefixes: 'short'
        },
        mcg: {
            baseName: 'mass',
            definition: '1 ug'
        },
        patch: {
            baseName: 'volume_patch'
        },
         pct: {
         pct: {
             baseName: 'concentration',
             baseName: 'concentration',
             definition: '10 mg/mL'
             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'
         }
         }
     } );
     } );
Line 70: Line 125:
         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );


         if( !this.primaryColor && !this.parentColor ) {
         this.parentColor = this.parentColor || this.id === mw.calculators.getOptionValue( 'defaultDrugColor' ) ? this.parentColor : mw.calculators.getOptionValue( 'defaultDrugColor' );
            throw new Error( 'Drug color "' + this.id + '" must define either a primary color or a parent color.' );
        }
     };
     };


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


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


     mw.calculators.objectClasses.DrugColor.getHighlightColor = function() {
     mw.calculators.objectClasses.DrugColor.prototype.getHighlightColor = function() {
         if( this.highlightColor ) {
         if( this.highlightColor ) {
             return this.highlightColor;
             return this.highlightColor;
Line 99: Line 152:
     };
     };


     mw.calculators.objectClasses.DrugColor.getPrimaryColor = function() {
     mw.calculators.objectClasses.DrugColor.prototype.getPrimaryColor = function() {
         if( this.primaryColor ) {
         if( this.primaryColor ) {
             return this.primaryColor;
             return this.primaryColor;
Line 107: Line 160:
     };
     };


     mw.calculators.objectClasses.DrugColor.isStriped = function() {
     mw.calculators.objectClasses.DrugColor.prototype.isStriped = function() {
         if( this.striped !== null ) {
         if( this.striped !== null ) {
             return this.striped;
             return this.striped;
Line 168: Line 221:
                     throw new Error( 'DrugPopulation variable "' + variableId + '" not defined' );
                     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 = 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;
    };




Line 198: Line 355:
         }
         }
     };
     };


     /**
     /**
Line 208: Line 363:
     */
     */
     mw.calculators.objectClasses.DrugIndication = function( propertyValues ) {
     mw.calculators.objectClasses.DrugIndication = function( propertyValues ) {
         var properties = {
         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: [
             required: [
                 'id',
                 'id',
Line 214: Line 375:
             ],
             ],
             optional: [
             optional: [
                 'abbreviation'
                 'abbreviation',
                'default',
                'searchData'
             ]
             ]
         };
         };
    };


        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
    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 = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
     mw.calculators.objectClasses.DrugIndication.prototype.toString = function() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };




Line 238: Line 410:
             mw.calculators.drugs[ drugId ] = drugs[ drugId ];
             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 );
     };
     };


Line 257: Line 439:
     */
     */
     mw.calculators.objectClasses.Drug = function( propertyValues ) {
     mw.calculators.objectClasses.Drug = function( propertyValues ) {
         var properties = {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
             required: [
 
                'id',
        if( !this.color ) {
                'name'
            this.color = mw.calculators.getOptionValue( 'defaultDrugColor' );
             ],
        }
             optional: [
 
                'color'
         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;


        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
            this.dosages = [];


        if( !this.color ) {
            this.addDosages( dosageData );
             this.color = DEFAULT_DRUG_COLOR;
        } else {
             this.dosages = [];
         }
         }


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


     mw.calculators.objectClasses.Drug.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
     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 ) {
    * DrugPreparation
            preparations[ preparationId ].id = this.preparations.length;
    */
 
     mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) {
            this.preparations.push( preparations[ preparationId ] );
         if( !mw.calculators.getDrug( drugId ) ) {
        }
            throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' );
     };
 
     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 );
            }
         }
         }


         for( var drugPreparationId in drugPreparationData ) {
        return indications.filter( mw.calculators.uniqueValues );
             drugPreparationData[ drugPreparationId ].drug = drugId;
    };
 
    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 );
            }
         }
         }


         var drugPreparations = mw.calculators.createCalculatorObjects( 'DrugPreparation', drugPreparationData );
         return populations.filter( mw.calculators.uniqueValues );
    };
 
    mw.calculators.objectClasses.Drug.prototype.getRoutes = function( indicationId ) {
        var routes = [];


         for( var drugPreparationId in drugPreparations ) {
         for( var iDosage in this.dosages ) {
             mw.calculators.drugs[ drugId ].preparations[ drugPreparationId ] = drugPreparations[ drugPreparationId ];
             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;
    * Class DrugPreparation
     };
    * @param {Object} propertyValues
 
    * @returns {mw.calculators.objectClasses.DrugPreparation}
    mw.calculators.objectClasses.Drug.prototype.getProperties = function() {
    * @constructor
         return {
    */
    mw.calculators.objectClasses.DrugPreparation = function( propertyValues ) {
         var properties = {
             required: [
             required: [
                'drug',
                 'id',
                 'id',
                 'concentration'
                 'name'
             ],
             ],
             optional: [
             optional: [
                 'dilutionRequired',
                 'color',
                 'commonDilution'
                 'description',
                'dosages',
                'formula',
                'preparations',
                'references',
                'searchData',
                'tradeNames'
             ]
             ]
         };
         };
        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
     };
     };
    mw.calculators.objectClasses.DrugPreparation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );




Line 333: Line 577:


     /**
     /**
     * DrugDosage
     * DrugPreparation
     */
     */
     mw.calculators.addDrugDosages = function( drugId, drugDosageData ) {
     mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) {
         if( !mw.calculators.getDrug( drugId ) ) {
         var drug = mw.calculators.getDrug( drugId );
            throw new Error( 'DrugDosase references drug "' + drugId + '" which is not defined' );
        }


         for( var drugDosageId in drugDosageData ) {
         if( !drug ) {
             drugDosageData[ drugDosageId ].drug = drugId;
             throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' );
         }
         }


         var drugDosages = mw.calculators.createCalculatorObjects( 'DrugDosage', drugDosageData );
         drug.addPreparations( drugPreparationData );
 
        for( var drugDosageId in drugDosages ) {
            mw.calculators.drugs[ drugId ].dosages[ drugDosageId ] = drugDosages[ drugDosageId ];
        }
     };
     };


Line 354: Line 592:


     /**
     /**
     * Class DrugDosage
     * Class DrugPreparation
     * @param {Object} propertyValues
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.DrugDosage}
     * @returns {mw.calculators.objectClasses.DrugPreparation}
     * @constructor
     * @constructor
     */
     */
     mw.calculators.objectClasses.DrugDosage = function( propertyValues ) {
     mw.calculators.objectClasses.DrugPreparation = function( propertyValues ) {
         var properties = {
         var properties = {
             required: [
             required: [
                'dose',
                'drug',
                 'id',
                 'id',
                 'indication'
                 'concentration'
             ],
             ],
             optional: [
             optional: [
                 'population'
                 'default',
                'dilutionRequired',
                'commonDilution'
             ]
             ]
         };
         };
Line 374: Line 612:
         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );


        if( !this.population ) {
            this.population = DEFAULT_DRUG_POPULATION;
        }


         mw.calculators.addDrugDoseCalculations( this.drug, this.dose );
         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 );
     };
     };


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






     /**
     /**
     * DrugDosageCalculation
     * Class DrugDosage
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugDosage}
    * @constructor
     */
     */
     mw.calculators.addDrugDoseCalculations = function( drugId, drugDoseCalculationData ) {
     mw.calculators.objectClasses.DrugDosage = function( propertyValues ) {
         if( !mw.calculators.getDrug( drugId ) ) {
         mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
            throw new Error( 'DrugDosase references drug "' + drugId + '" which is not defined' );
 
        }
        var drugIndication = mw.calculators.getDrugIndication( this.indication );


         if( !Array.isArray( this.dose ) ) {
         if( !drugIndication ) {
            drugDoseCalculationData = [ drugDoseCalculationData ];
            throw new Error( 'Invalid indication "' + this.indication + '" for drug dosage' );
         }
         }


         var doseProperties = [
         this.indication = drugIndication;
            'dose',
            'min',
            'max',
            'absoluteMin',
            'absoluteMax'
        ];


         var calculationData = {};
         this.population = this.population ? this.population : mw.calculators.getOptionValue( 'defaultDrugPopulation' );


         for( var doseGroupId in drugDoseCalculationData ) {
         var drugPopulation = mw.calculators.getDrugPopulation( this.population );
            for( var doseId in drugDoseCalculationData[ doseGroupId ] ) {
                drugDoseCalculationData[ doseGroupId ][ doseId ].drug = drugId;
                drugDoseCalculationData[ doseGroupId ][ doseId ].calculate = mw.calculators.objectClasses.DrugDoseCalculation.prototype.calculate;


                var data = {
        if( !drugPopulation ) {
                    calculations: {
            throw new Error( 'Invalid population "' + this.population + '" for drug dosage' );
                        required: [],
        }
                        optional: []
                    },
                    variables: {
                        required: [],
                        optional: []
                    }
                };


                // Look at dose properties to identify any variable dependence (e.g. weight-dependence)
        this.population = drugPopulation;
                for( var iDoseProperty in doseProperties ) {
                    var dosePropertyValue = drugDoseCalculationData[ doseGroupId ][ doseId ][ doseProperties[ iDoseProperty ] ];


                    // For now, this only supports weight dependence, unclear if it will need to be more generalizable in the future
        this.references = this.references ? mw.calculators.prepareReferences( this.references ) : [];
                    if( dosePropertyValue &&
                        dosePropertyValue.match( /\/\s*?kg/ ) &&
                        data.variables.required.indexOf( 'weight' ) === -1 ) {
                        data.variables.required.push( 'weight' );
                    }
                }


                if( drugDoseCalculationData[ doseGroupId ][ doseId ].hasOwnProperty( 'weightCalculation' ) ) {
        this.routes = this.routes ? this.routes : [ mw.calculators.getOptionValue( 'defaultDrugRoute' ) ];
                    var weightCalculationId = drugDoseCalculationData[ doseGroupId ][ doseId ].weightCalculation;
                    var weightCalculation = mw.calculators.getCalculation( weightCalculationId );


                    if( !weightCalculation ) {
        if( !Array.isArray( this.routes ) ) {
                        throw new Error( 'Drug "' + drugId + '" dose ' + doseGroupId + '.' + doseId + ': weightCalculation "' + weightCalculationId + '" which is not defined' );
            this.routes = [ this.routes ];
                    }
        }


                    data.calculations.optional.push( weightCalculationId );
        drugRoutes = [];
                    data.variables.optional = data.variables.optional.concat( weightCalculation.data.variables.required.concat( weightCalculation.data.variables.optional ) );
                }


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


                 calculationData[ drugId + '-' + doseGroupId + '-' + doseId ] = drugDoseCalculationData[ doseGroupId ][ doseId ];
            if( !drugRoute ) {
                 throw new Error( 'Invalid route "' + drugRouteId + '" for drug dosage' );
             }
             }
            drugRoutes[ iRoute ] = drugRoute;
         }
         }


         mw.calculators.addCalculations( calculationData, 'DrugDoseCalculation' );
         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 ) {
    * Class DrugDoseCalculation
         if( !drugDoseData ) {
    * @param {Object} propertyValues
            return;
    * @returns {mw.calculators.objectClasses.DrugDoseCalculation}
        } else if( !Array.isArray( drugDoseData ) ) {
    * @constructor
            // Each dosage can have one or more associated doses. Ensure this value is an array.
    */
            drugDoseData = [ drugDoseData ];
    mw.calculators.objectClasses.DrugDoseCalculation = function( propertyValues ) {
        }
         mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );


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


    mw.calculators.objectClasses.DrugDoseCalculation.prototype = Object.create( mw.calculators.objectClasses.Calculation.prototype );
        for( var doseId in doses ) {
 
            doses[ doseId ].id = this.dose.length;
    mw.calculators.objectClasses.DrugDoseCalculation.prototype.calculate = function() {


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


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


    mw.calculators.objectClasses.DrugDoseCalculation.prototype.getLabelHtml = function() {
        inputData = inputData.merge( this.population.getCalculationData() );
        var labelHtml = this.name;


         labelHtml = $( '<a>', {
         for( var iDose in this.dose ) {
             href: mw.util.getUrl( this.name ),
             inputData = inputData.merge( this.dose[ iDose ].getCalculationData() );
            text: labelHtml
         }
         } )[ 0 ].outerHTML;


         return labelHtml;
         return inputData;
     };
     };


     mw.calculators.objectClasses.DrugDoseCalculation.prototype.getProperties = function() {
     mw.calculators.objectClasses.DrugDosage.prototype.getProperties = function() {
         return {
         return {
             required: [
             required: [
                 'id',
                 'id'
                'drug'
             ],
             ],
             optional: [
             optional: [
                 'absoluteMin',
                 'description',
                'absoluteMax',
                 'dose',
                 'dose',
                 'min',
                 'indication',
                 'max',
                 'population',
                 'route',
                 'routes',
                 'weightCalculation'
                 'references'
             ]
             ]
         };
         };
     };
     };


     mw.calculators.objectClasses.DrugDoseCalculation.prototype.initialize = function() {
     mw.calculators.objectClasses.DrugDosage.prototype.getRouteString = function() {
         mw.calculators.objectClasses.Calculation.prototype.initialize.call( this );
         var routeString = '';


         this.route = this.route ? this.route : 'IV';
         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;
    };




    /*******
    * BEGIN DRUG DATA
    *******/






     /**
     /**
     * DrugColor data
     * Class DrugDose
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugDose}
    * @constructor
     */
     */
     mw.calculators.addDrugColors( {
     mw.calculators.objectClasses.DrugDose = function( propertyValues ) {
         anticholinergic: {
         mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
            primaryColor: '#00ac8c'
        },
        benzodiazepine: {
            primaryColor: '#ff6c2f'
        },
        benzodiazepineReversal: {
            parentColor: 'benzodiazepine',
            striped: true
        },
        cardiovascularAgonist: {
            primaryColor: '#ba93df'
        },
        cardiovascularAntagonist: {
            parentColor: 'cardiovascularAgonist',
            striped: true
        },
        default: {
            primaryColor: '#fff'
        },
        desflurane: {
            primaryColor: '#0ab8fd'
        },
        enflurane: {
            primaryColor: '#f58733'
        },
        epinephrine: {
            parentColor: 'cardiovascularAntagonist',
            highlightColor: '#000'
        },
        halothane: {
            primaryColor: '#b20107'
        },
        isoflurane: {
            primaryColor: '#ca7fc0'
        },
        localAnesthetic: {
            primaryColor: '#dad9d6'
        },
        neuromuscularBlocker: {
            primaryColor: '#fe5442'
        },
        neuromuscularBlockerReversal: {
            parentColor: 'neuromuscularBlocker',
            striped: true
        },
        nitrousOxide: {
            primaryColor: '#2d549f'
        },
        opioid: {
            primaryColor: '#6cd1ef'
        },
        opioidReversal: {
            parentColor: 'opioid',
            striped: true
        },
        sedativeHypnotic: {
            primaryColor: '#ffe800'
        },
        sevoflurane: {
            primaryColor: '#f8da00'
        },
        succinylcholine: {
            parentColor: 'neuromuscularBlocker',
            highlightColor: '#000'
        }
    } );


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


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


    /**
            if( !Array.isArray( weightCalculationIds ) ) {
    * DrugPopulation data
                 weightCalculationIds = [ weightCalculationIds ];
    */
    mw.calculators.addDrugPopulations( {
        general: {
            name: 'General',
            abbreviation: 'Gen.'
        },
        neonatal: {
            name: 'Neonatal',
            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'
                }
            }
        }
    } );


            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 );
    * DrugIndication data
            }
    */
         } else {
    mw.calculators.addDrugIndications( {
             this.weightCalculation = [];
         generalAnesthesia: {
             name: 'General anesthesia',
            abbreviation: 'GA'
         }
         }
    } );


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


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


    /**
            if( this[ mathProperty ] ) {
    * Drug data
                // 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' ) ) {
    * Cefazolin
                    isWeightDependent = true;
    */
                }
    mw.calculators.addDrugs( {
            } else {
        cefazolin: {
                this[ mathProperty ] = null;
             name: 'Cefazolin'
             }
         }
         }
    } );


    /**
        if( isWeightDependent ) {
    * Ketamine
            // Default is tbw
    */
            this.weightCalculation.push( mw.calculators.getCalculation( 'tbw' ) );
    mw.calculators.addDrugs( {
        ketamine: {
            name: 'Ketamine',
            color: 'sedativeHypnotic'
         }
         }
     } );
     };


     mw.calculators.addDrugPreparations( 'ketamine', [
     mw.calculators.objectClasses.DrugDose.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
        {
            concentration: '10 mg/mL'
        }, {
            concentration: '50 mg/mL'
        }, {
            concentration: '100 mg/mL'
        }
    ] );


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


    /**
        if( this.frequency ) {
    * Lidocaine
             administration += administration ? ' ' : '';
    */
             administration += this.frequency;
    mw.calculators.addDrugs( {
        lidocaine: {
             name: 'Lidocaine',
             color: 'localAnesthetic'
         }
         }
    } );


    mw.calculators.addDrugPreparations( 'lidocaine', [
        if( this.duration ) {
        {
             administration += administration ? ' ' : '';
             concentration: '1 pct'
             administration += 'over ' + this.duration;
        }, {
             concentration: '2 pct'
         }
         }
    ] );


        return administration;
    };


    /**
     mw.calculators.objectClasses.DrugDose.prototype.getCalculationData = function() {
    * Propofol
         var calculationData = new mw.calculators.objectClasses.CalculationData();
    */
     mw.calculators.addDrugs( {
         propofol: {
            name: 'Propofol',
            color: 'sedativeHypnotic'
        }
    } );


    mw.calculators.addDrugPreparations( 'propofol', [
        for( var iWeightCalculation in this.weightCalculation ) {
        {
            calculationData.calculations.optional.push( this.weightCalculation[ iWeightCalculation ].id );
            concentration: '10 mg/mL'
         }
         }
    ] );


    mw.calculators.addDrugDosages( 'propofol', [
         return calculationData;
         {
    };
            indication: 'generalAnesthesia',
            population: 'general',
            dose: [
                {
                    name: 'Induction',
                    min: '1 mg/kg',
                    max: '2.5 mg/kg',
                    weightCalculation: 'lbw'
                }, {
                    name: 'Maintenance',
                    min: '100 mcg/kg/min',
                    max: '200 mcg/kg/min',
                    route: 'IV'
                }
            ]
        }, {
            indication: 'generalAnesthesia',
            population: 'pediatric',
            dose: [
                {
                    name: 'Induction',
                    min: '2.5 mg/kg',
                    max: '3.5 mg/kg',
                    weightCalculation: 'lbw'
                }, {
                    name: 'Maintenance',
                    min: '125 mcg/kg/min',
                    max: '300 mcg/kg/min'
                }
            ]
        }, {
            indication: 'generalAnesthesia',
            population: 'elderly',
            dose: [
                {
                    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'
                }
            ]
        }
    ] );


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


 
     mw.calculators.objectClasses.DrugDose.prototype.getProperties = function() {
 
         return {
    /**
             required: [
    * DrugDosage
                 'id'
    *
            ],
    * Structure is:
            optional: [
    *
                'absoluteMax',
    * drugId: {
                 'absoluteMin',
    *    indicationId: {
                 'dose',
    *        doseId: {
                'duration',
    *
                'frequency',
    *        }
                 'min',
    *     }
                'max',
    * }
                 'name',
    */
                'text',
 
                'weightCalculation'
    var drugDosages = {
             ]
         cefazolin: {
         };
             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'
                    }
                }
             }
         }
     };
     };


}() );
}() );

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'
            ]
        };
    };

}() );