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

From WikiAnesthesia
Line 428: Line 428:
             calculate: function( data ) {
             calculate: function( data ) {
                 var weight = data.weight.toNumber( 'kgwt' );
                 var weight = data.weight.toNumber( 'kgwt' );
                 var age = data.weight.toNumber( 'yo' );
                 var age = data.age.toNumber( 'yo' );
                 var caseDuration = data.caseDuration ? data.caseDuration.toNumber( 'hr' ) : null;
                 var caseDuration = data.caseDuration ? data.caseDuration.toNumber( 'hr' ) : null;



Revision as of 17:36, 31 July 2021

( function() {
    var moduleId = 'patientStats';

    mw.calculators.addUnitsBases( {
        hgb: {
            toString: function( units ) {
                units = units.replace( 'hgbperdL', '/dL' );
                units = units.replace( 'pcthct', '%' );

                return units;
            }
        }
    } );

    mw.calculators.addUnits( {
        pcthct: {
            baseName: 'hgb'
        },
        ghgbperdL: {
            baseName: 'hgb',
            prefixes: 'short',
            definition: '3 pcthct'
        }
    } );

    mw.calculators.addVariables( {
        caseDuration: {
            name: 'Case duration',
            type: 'number',
            abbreviation: 'Duration',
            maxLength: 3,
            units: [
                'hr',
                'min'
            ]
        },
        hct: {
            name: 'Current hematocrit',
            type: 'number',
            abbreviation: 'Current hct',
            defaultValue: '45 pcthct',
            maxLength: 4,
            units: [
                'pcthct',
                'ghgbperdL'
            ]
        },
        minHct: {
            name: 'Minimum hematocrit',
            type: 'number',
            abbreviation: 'Min hct',
            defaultValue: '21 pcthct',
            maxLength: 4,
            units: [
                'pcthct',
                'ghgbperdL'
            ]
        }
    } );



    mw.calculators.addCalculations( {
        bladeMacSize: {
            name: 'Laryngoscope blade size (MAC)',
            abbreviation: 'Blade (MAC)',
            data: {
                variables: {
                    required: [ 'age' ]
                }
            },
            type: 'string',
            references: [
                'Smith\'s Anesthesia for Infants and Children. 8e. p356'
            ],
            calculate: function( data ) {
                var age = data.age.toNumber( 'yo' );

                if( age >= 6 ) {
                    return '3';
                } else if( age >= 1 ) {
                    // ( age + 16 ) / 4
                    // To nicely display in increments of 0.5, double the calculation, round (floor), then divide by 2
                    return '2';
                } else {
                    return '-';
                }
            }
        },
        bladeMillerSize: {
            name: 'Laryngoscope blade size (Miller)',
            abbreviation: 'Blade (Miller)',
            data: {
                variables: {
                    required: [ 'age' ]
                }
            },
            type: 'string',
            references: [
                'Smith\'s Anesthesia for Infants and Children. 8e. p356'
            ],
            calculate: function( data ) {
                var age = data.age.toNumber( 'yo' );

                if( age >= 2 ) {
                    return '2';
                } else if( age >= 1 / 12 ) {
                    return '1';
                } else if( age >= 0 ) {
                    return '0-1';
                } else {
                    return '0';
                }
            }
        },
        bmi: {
            name: 'Body mass index',
            abbreviation: 'BMI',
            data: {
                variables: {
                    required: [ 'weight', 'height' ]
                }
            },
            digits: 0,
            units: 'kg/m^2',
            formula: '<math>\\mathrm{BMI} = \\frac{\\text{mass}_\\text{kg}}{{\\text{height}_\\text{m}}^2}</math>',
            link: '[[Body mass index]]',
            references: [],
            calculate: function( data ) {
                return data.weight.toNumber( 'kgwt' ) / Math.pow( data.height.toNumber( 'm' ), 2 );
            }
        },
        bsa: {
            name: 'Body surface area',
            abbreviation: 'BSA',
            data: {
                variables: {
                    required: [ 'weight', 'height' ]
                }
            },
            digits: 2,
            units: 'm^2',
            formula: '<math>\\mathrm{BSA} = \\sqrt{\\frac{\\mathrm{Weight_{kg}}*\\mathrm{Height_{cm}}}{3600}}</math>',
            link: false,
            references: [
                'Mosteller RD. Simplified calculation of body-surface area. N Engl J Med. 1987 Oct 22;317(17):1098. doi: 10.1056/NEJM198710223171717. PMID: 3657876.'
            ],
            calculate: function( data ) {
                return Math.sqrt( data.height.toNumber( 'cm' ) * data.weight.toNumber( 'kgwt' ) / 3600 );
            }
        },
        ebv: {
            name: 'Estimated blood volume',
            abbreviation: 'EBV',
            data: {
                variables: {
                    required: [ 'weight', 'age' ]
                }
            },
            digits: 0,
            units: 'mL',
            formula: '',
            references: [
                'Morgan & Mikhail\'s Clinical Anesthesiology. 5e. p1168'
            ],
            calculate: function( data ) {
                var weight = data.weight.toNumber( 'kgwt' );
                var age = data.age.toNumber( 'yo' );

                var ebvPerKg;

                if( age >= 1 ) {
                    if( data.gender === 'F' ) {
                        ebvPerKg = 65;
                    } else {
                        ebvPerKg = 75;
                    }
                } else if( age >= 1/12 ) {
                    ebvPerKg = 80;
                } else if( age >= 0 ) {
                    ebvPerKg = 85;
                } else {
                    ebvPerKg = 95;
                }

                return weight * ebvPerKg;
            }
        },
        ettDepth: {
            name: 'Endotracheal tube depth',
            abbreviation: 'ETT depth',
            data: {
                variables: {
                    required: [ 'age' ]
                }
            },
            type: 'string',
            references: [
                'Smith\'s Anesthesia for Infants and Children. 8e. p356'
            ],
            calculate: function( data ) {
                var age = data.age.toNumber( 'yo' );
                var depth;

                if( age >= 12 ) {
                    depth = '20+';
                } else if( age >= 2 ) {
                    // ( age / 2 ) + 12
                    depth = Math.round( age / 2 + 12 );
                } else if( age >= 1 ) {
                    depth = 12;
                } else if( age >= 0.5 ) {
                    depth = 11;
                } else if( age >= 0 ) {
                    depth = 9;
                } else {
                    depth = '7-8';
                }

                return depth + ' cm from teeth';
            }
        },
        ettSize: {
            name: 'Endotracheal tube size',
            abbreviation: 'ETT size',
            data: {
                variables: {
                    required: [ 'age' ]
                }
            },
            type: 'string',
            references: [
                'Smith\'s Anesthesia for Infants and Children. 8e. p356'
            ],
            calculate: function( data ) {
                var age = data.age.toNumber( 'yo' );
                var size;

                if( age >= 12 ) {
                    size = '6+';
                } else if( age >= 1 ) {
                    // ( age + 16 ) / 4
                    // To nicely display in increments of 0.5, double the calculation, round (floor), then divide by 2
                    size = String( Math.floor( 2 * ( ( age + 16 ) / 4 ) ) / 2 );
                } else if( age >= 0.5 ) {
                    size = '3.5-4';
                } else if( age >= 0 ) {
                    size = '3-3.5';
                } else {
                    size = '2.5-3';
                }

                return size + ' mm (ID)';
            }
        },
        fluidMaintenanceRate: {
            name: 'Fluid maintenance rate',
            abbreviation: 'Fluid maint.',
            data: {
                variables: {
                    required: [ 'weight' ]
                }
            },
            digits: 0,
            units: 'mL/hr',
            formula: '',
            references: [
                'Miller\'s Anesthesia 7e, section IV, pg. 1728'
            ],
            calculate: function( data ) {
                var weight = data.weight.toNumber( 'kgwt' );

                // Uses 4-2-1 rule
                var maintenanceRate = 4 * Math.min( weight, 10 );

                if( weight > 10 ) {
                    maintenanceRate += 2 * Math.min( weight - 10, 10 );
                }

                if( weight > 20) {
                    maintenanceRate += weight - 20;
                }

                return maintenanceRate;
            }
        },
        ibw: {
            name: 'Ideal body weight',
            abbreviation: 'IBW',
            data: {
                variables: {
                    required: [ 'height', 'gender' ]
                }
            },
            units: 'kg',
            link: 'https://en.wikipedia.org/wiki/Human_body_weight#Ideal_body_weight',
            references: [
                'Devine BJ. Gentamicin therapy. Drug Intell Clin Pharm. 1974;8:650–655.'
            ],
            calculate: function( data ) {
                if( data.height.toNumber( 'cm' ) < 152 ) {
                    throw new Error( 'Ideal body weight may only be applied to persons 152 cm (60 inches) or taller' );
                }

                var baseWeight = data.gender === 'F' ? 45.5 : 50;

                // baseWeight + 2.3 kg for every inch over 5 feet
                return baseWeight + 2.3 * ( data.height.toNumber( 'in' ) - 60 );
            }
        },
        lbw: {
            name: 'Lean body weight',
            abbreviation: 'LBW',
            data: {
                variables: {
                    required: [ 'gender', 'height', 'weight' ]
                }
            },
            units: 'kg',
            link: 'https://en.wikipedia.org/wiki/Lean_body_mass',
            references: [
                'Janmahasatian S, Duffull SB, Ash S, Ward LC, Byrne NM, Green B. Quantification of lean bodyweight. Clin Pharmacokinet. 2005; 44(10): 1051-65.'
            ],
            calculate: function( data ) {
                if( data.gender === 'F' ) {
                    return 9270 * data.weight.toNumber( 'kgwt' ) / ( 8780 + 244 * data.weight.toNumber( 'kgwt' ) / Math.pow( data.height.toNumber( 'm' ), 2 ) );
                } else {
                    return 9270 * data.weight.toNumber( 'kgwt' ) / ( 6680 + 216 * data.weight.toNumber( 'kgwt' ) / Math.pow( data.height.toNumber( 'm' ), 2 ) );
                }
            }
        },
        lmaSize: {
            name: 'Laryngeal mask airway size',
            abbreviation: 'LMA size',
            data: {
                variables: {
                    required: [ 'weight' ]
                }
            },
            type: 'string',
            references: [
                'Smith\'s Anesthesia for Infants and Children. 8e. p356'
            ],
            calculate: function( data ) {
                var weight = data.weight.toNumber( 'kgwt' );
                var size;

                if( weight > 100 ) {
                    size = '6';
                } else if( weight > 70 ) {
                    size = '5';
                } else if( weight > 50 ) {
                    size = '4';
                } else if( weight > 30 ) {
                    size = '3';
                } else if( weight > 20 ) {
                    size = '2.5';
                } else if( weight > 10 ) {
                    size = '2';
                } else if( weight > 5 ) {
                    size = '1.5';
                } else {
                    size = '1';
                }

                return size;
            }
        },
        maxAbl: {
            name: 'Maximum allowable blood loss',
            abbreviation: 'Max ABL',
            data: {
                calculations: {
                    required: [ 'ebv' ]
                },
                variables: {
                    required: [ 'weight', 'age', 'hct', 'minHct' ]
                }
            },
            digits: 0,
            units: 'mL',
            formula: '',
            references: [
                'Morgan & Mikhail\'s Clinical Anesthesiology. 5e. p1168'
            ],
            calculate: function( data ) {
                var currentHct = data.hct.toNumber( 'pcthct' );
                var minHct = data.minHct.toNumber( 'pcthct' );

                if( currentHct < minHct ) {
                    return '-';
                }

                return data.ebv.toNumber( 'mL' ) * ( currentHct - minHct ) / currentHct;
            },
            onRendered: function( $selector ) {
                var extraClass = 'calculator-calculation-extra-maxAbl';

                if( !$( '.' + extraClass ).length ) {
                    var $extraContainer = $( '<div>', {
                        class: extraClass
                    } ).css( 'margin-left', '1rem' );

                    $extraContainer.append( mw.calculators.createInputGroup( [
                        'hct',
                        'minHct'
                    ] ) );

                    $selector.after( $extraContainer );
                }
            }
        },
        minUop: {
            name: 'Minimum urine output',
            abbreviation: 'Min UOP',
            data: {
                variables: {
                    required: [ 'weight', 'age' ],
                    optional: [ 'caseDuration' ]
                }
            },
            type: 'string',
            formula: '',
            references: [
                'Klahr S, Miller SB. Acute oliguria. N Engl J Med. 1998 Mar 5;338(10):671-5. doi: 10.1056/NEJM199803053381007. PMID: 9486997.',
                'Arant BS Jr. Postnatal development of renal function during the first year of life. Pediatr Nephrol. 1987 Jul;1(3):308-13. doi: 10.1007/BF00849229. PMID: 3153294.'
            ],
            calculate: function( data ) {
                var weight = data.weight.toNumber( 'kgwt' );
                var age = data.age.toNumber( 'yo' );
                var caseDuration = data.caseDuration ? data.caseDuration.toNumber( 'hr' ) : null;

                var minUop;

                if( age > 1 ) {
                    minUop = 0.5 * weight;
                } else {
                    minUop = 1 * weight;
                }

                if( caseDuration ) {
                    minUop = minUop * caseDuration + ' mL';
                } else {
                    minUop = minUop + ' mL/hr';
                }

                return minUop;
            },
            onRendered: function( $selector ) {
                var extraClass = 'calculator-calculation-extra-minUop';

                if( !$( '.' + extraClass ).length ) {
                    var $extraContainer = $( '<div>', {
                        class: extraClass
                    } ).css( 'margin-left', '1rem' );

                    $extraContainer.append( mw.calculators.createInputGroup( [
                        'caseDuration'
                    ] ) );

                    $selector.after( $extraContainer );
                }
            }
        }
    } );

    var tableMaxWidth = 600;

    mw.calculators.addCalculators( moduleId, {
        anatomy: {
            name: 'Patient statistics',
            calculations: [
                'bmi',
                'bsa',
                'ibw',
                'lbw'
            ],
            css: {
                'max-width': tableMaxWidth
            },
            table: true
        },
        airway: {
            name: 'Airway management',
            calculations: [
                'bladeMacSize',
                'bladeMillerSize',
                'ettSize',
                'ettDepth',
                'lmaSize'
            ],
            css: {
                'max-width': tableMaxWidth
            },
            table: true
        },
        fluidManagement: {
            name: 'Fluid management',
            calculations: [
                'ebv',
                'fluidMaintenanceRate',
                'maxAbl',
                'minUop'
            ],
            css: {
                'max-width': tableMaxWidth
            },
            table: true
        }
    } );
}() );