1 /* 2 * Power.js - Unit conversions for Power 3 * 4 * Copyright © 2018-2022, JEDLSoft 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 /* 21 !depends 22 Measurement.js 23 */ 24 25 var Measurement = require("./Measurement.js"); 26 27 /** 28 * @class 29 * Create a new power measurement instance. 30 * 31 * @constructor 32 * @extends Measurement 33 * @param options {{unit:string,amount:number|string|undefined}} Options controlling 34 * the construction of this instance 35 */ 36 var PowerUnit = function (options) { 37 this.unit = "watt"; 38 this.amount = 0; 39 40 this.ratios = PowerUnit.ratios; 41 this.aliases = PowerUnit.aliases; 42 this.aliasesLower = PowerUnit.aliasesLower; 43 this.systems = PowerUnit.systems; 44 45 this.parent(options); 46 }; 47 48 PowerUnit.prototype = new Measurement(); 49 PowerUnit.prototype.parent = Measurement; 50 PowerUnit.prototype.constructor = PowerUnit; 51 52 PowerUnit.ratios = { 53 /* index mW W kW MW GW ft-lb/h HP BTU/h */ 54 "milliwatt": [ 1, 1, 1e-3, 1e-6, 1e-9, 1e-12, 737.562149277e-4, 1.34102209e-6, 3.412142450123e-3 ], 55 "watt": [ 2, 1e3, 1, 1e-3, 1e-6, 1e-9, 0.737562149277, 1.34102209e-3, 3.412142450123 ], 56 "kilowatt": [ 3, 1e6, 1e3, 1, 1e-3, 1e-6, 7.37562149277e+2, 1.34102209, 3.412142450123e+3 ], 57 "megawatt": [ 4, 1e9, 1e6, 1e3, 1, 1e-3, 7.37562149277e+5, 1.34102209e+3, 3.412142450123e+6 ], 58 "gigawatt": [ 5, 1e12, 1e9, 1e6, 1e3, 1, 7.37562149277e+8, 1.34102209e+6, 3.412142450123e+9 ], 59 "foot-pound-per-hour": [ 6, 1.35581795e+3, 1.35581795, 1.35581795e-3, 1.35581795e-6, 1.35581795e-9, 1, 5.05051e-7, 1.285067782e-3 ], 60 "horsepower": [ 7, 745701.0335416, 745.7010335416, 0.7457010335416, 7.457010335416e-4, 7.457010335416e-7, 1.9799999829594917e+6, 1, 2.54443418687714e+3 ], 61 "btu-per-hour": [ 8, 293.071, 0.293071, 2.93071e-3, 2.93071e-6, 2.93071e-9, 778.169069245845, 3.93014685e-4, 1 ] 62 }; 63 64 /** 65 * Return the type of this measurement. Examples are "mass", 66 * "length", "speed", etc. Measurements can only be converted 67 * to measurements of the same type.<p> 68 * 69 * The type of the units is determined automatically from the 70 * units. For example, the unit "grams" is type "mass". Use the 71 * static call {@link Measurement.getAvailableUnits} 72 * to find out what units this version of ilib supports. 73 * 74 * @return {string} the name of the type of this measurement 75 */ 76 PowerUnit.prototype.getMeasure = function() { 77 return "power"; 78 }; 79 80 /** 81 * Return a new instance of this type of measurement. 82 * 83 * @param {Object} params parameters to the constructor 84 * @return {Measurement} a measurement subclass instance 85 */ 86 PowerUnit.prototype.newUnit = function(params) { 87 return new PowerUnit(params); 88 }; 89 90 PowerUnit.systems = { 91 "metric": [ 92 "milliwatt", 93 "watt", 94 "kilowatt", 95 "megawatt", 96 "gigawatt" 97 ], 98 "uscustomary": [ 99 "foot-pound-per-hour", 100 "btu-per-hour", 101 "horsepower" 102 ], 103 "imperial": [ 104 "foot-pound-per-hour", 105 "btu-per-hour", 106 "horsepower" 107 ], 108 "conversions": { 109 "uscustomary": { 110 "metric": { 111 "foot-pound-per-hour": "watt", 112 "btu-per-hour": "watt", 113 "refridgeration-ton": "kilowatt", 114 "horsepower": "kilowatt" 115 } 116 }, 117 "imperial": { 118 "metric": { 119 "foot-pound-per-hour": "watt", 120 "btu-per-hour": "watt", 121 "refridgeration-ton": "kilowatt", 122 "horsepower": "kilowatt" 123 } 124 }, 125 "metric": { 126 "uscustomary": { 127 "milliwatt": "horsepower", 128 "watt": "horsepower", 129 "kilowatt": "horsepower", 130 "megawatt": "horsepower", 131 "gigawatt": "horsepower" 132 }, 133 "imperial": { 134 "milliwatt": "horsepower", 135 "watt": "horsepower", 136 "kilowatt": "horsepower", 137 "megawatt": "horsepower", 138 "gigawatt": "horsepower" 139 } 140 } 141 } 142 }; 143 144 PowerUnit.aliases = { 145 "milli joule/second": "milliwatt", 146 "milli joule/s": "milliwatt", 147 "millijoule/second": "milliwatt", 148 "millijoule/s": "milliwatt", 149 "milliJ/second": "milliwatt", 150 "milliJ/s": "milliwatt", 151 "mJ/s": "milliwatt", 152 "joule per second": "watt", 153 "joules per second": "watt", 154 "joule/second": "watt", 155 "joules/second": "watt", 156 "joule/s": "watt", 157 "joules/s": "watt", 158 "J/s": "watt", 159 "kilojoule/second": "kilowatt", 160 "kilojoule/s": "kilowatt", 161 "kiloJ/second": "kilowatt", 162 "kiloJ/s": "kilowatt", 163 "kJ/s": "kilowatt", 164 "BTU/h": "btu-per-hour", 165 "British Thermal Unit per hour": "btu-per-hour", 166 "British Thermal Units per hour": "btu-per-hour", 167 "British Thermal Unit/hour": "btu-per-hour", 168 "British Thermal Units/hour": "btu-per-hour", 169 "BTU per hour": "btu-per-hour", 170 "BTUs per hour": "btu-per-hour", 171 "BTU/hour": "btu-per-hour", 172 "BTUs/hour": "btu-per-hour", 173 "BTU/h": "btu-per-hour", 174 "milli watt": "milliwatt", 175 "mW": "milliwatt", 176 "kilo watt": "kilowatt", 177 "kW": "kilowatt", 178 "W": "watt", 179 "mega watt": "megawatt", 180 "mW": "megawatt", 181 "giga watt": "gigawatt", 182 "gW": "gigawatt", 183 "foot pound per hour": "foot-pound-per-hour", 184 "foot pound/hour": "foot-pound-per-hour", 185 "foot pound/h": "foot-pound-per-hour", 186 "foot lb/hour": "foot-pound-per-hour", 187 "foot lb/h": "foot-pound-per-hour", 188 "ft lbf/h": "foot-pound-per-hour", 189 "ft lb/h": "foot-pound-per-hour", 190 "ft lbs/h": "foot-pound-per-hour", 191 "ft-lb/h": "foot-pound-per-hour", 192 "ft-lbs/h": "foot-pound-per-hour", 193 "f lb/h": "foot-pound-per-hour", 194 "horse power": "horsepower", 195 "hp": "horsepower", 196 "british thermal units per hour": "btu-per-hour", 197 "BTU per hour": "btu-per-hour", 198 "BTUs per hour": "btu-per-hour", 199 "british thermal units/hour": "btu-per-hour", 200 "british thermal unit/hour": "btu-per-hour", 201 "british thermal units/hr": "btu-per-hour", 202 "british thermal unit/hr": "btu-per-hour", 203 "BTU/hour": "btu-per-hour", 204 "BTUs/hour": "btu-per-hour", 205 "BTU/hr": "btu-per-hour", 206 "BTUs/hr": "btu-per-hour", 207 "BTU/h": "btu-per-hour", 208 "BTUs/h": "btu-per-hour" 209 }; 210 211 PowerUnit.aliasesLower = {}; 212 for (var a in PowerUnit.aliases) { 213 PowerUnit.aliasesLower[a.toLowerCase()] = PowerUnit.aliases[a]; 214 } 215 216 /** 217 * Convert a power to another measure. 218 * @static 219 * @param to {string} unit to convert to 220 * @param from {string} unit to convert from 221 * @param power {number} amount to be convert 222 * @returns {number|undefined} the converted amount 223 */ 224 PowerUnit.convert = function(to, from, power) { 225 from = Measurement.getUnitIdCaseInsensitive(PowerUnit, from) || from; 226 to = Measurement.getUnitIdCaseInsensitive(PowerUnit, to) || to; 227 var fromRow = PowerUnit.ratios[from]; 228 var toRow = PowerUnit.ratios[to]; 229 if (typeof(from) === 'undefined' || typeof(to) === 'undefined') { 230 return undefined; 231 } 232 return power * fromRow[toRow[0]]; 233 }; 234 235 /** 236 * @private 237 * @static 238 */ 239 PowerUnit.getMeasures = function () { 240 return Object.keys(PowerUnit.ratios); 241 }; 242 243 //register with the factory method 244 Measurement._constructors["power"] = PowerUnit; 245 246 module.exports = PowerUnit; 247