1 /* 2 * EnergyUnit.js - Unit conversions for energy measurements 3 * 4 * Copyright © 2014-2015, 2018 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 energy 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 EnergyUnit = function (options) { 37 this.unit = "joule"; 38 this.amount = 0; 39 40 this.ratios = EnergyUnit.ratios; 41 this.aliases = EnergyUnit.aliases; 42 this.aliasesLower = EnergyUnit.aliasesLower; 43 this.systems = EnergyUnit.systems; 44 45 this.parent.call(this, options); 46 }; 47 48 EnergyUnit.prototype = new Measurement(); 49 EnergyUnit.prototype.parent = Measurement; 50 EnergyUnit.prototype.constructor = EnergyUnit; 51 52 EnergyUnit.ratios = { 53 /* index mJ J BTU kJ Wh Cal MJ kWh gJ MWh GWh */ 54 "millijoule": [ 1, 1, 0.001, 9.4781707775e-7, 1e-6, 2.7777777778e-7, 2.3884589663e-7, 1.0e-9, 2.7777777778e-10, 1.0e-12, 2.7777777778e-13, 2.7777777778e-16 ], 55 "joule": [ 2, 1000, 1, 9.4781707775e-4, 0.001, 2.7777777778e-4, 2.3884589663e-4, 1.0e-6, 2.7777777778e-7, 1.0e-9, 2.7777777778e-10, 2.7777777778e-13 ], 56 "BTU": [ 3, 1055055.9, 1055.0559, 1, 1.0550559, 0.29307108333, 0.25199577243, 1.0550559e-3, 2.9307108333e-4, 1.0550559e-6, 2.9307108333e-7, 2.9307108333e-10 ], 57 "kilojoule": [ 4, 1000000, 1000, 0.94781707775, 1, 0.27777777778, 0.23884589663, 0.001, 2.7777777778e-4, 1.0e-6, 2.7777777778e-7, 2.7777777778e-10 ], 58 "watt-hour": [ 5, 3.6e+6, 3600, 3.4121414799, 3.6, 1, 0.85984522786, 0.0036, 0.001, 3.6e-6, 1.0e-6, 1.0e-9 ], 59 "foodcalorie": [ 6, 4.868e+5, 4186.8, 3.9683205411, 4.1868, 1.163, 1, 4.1868e-3, 1.163e-3, 4.1868e-6, 1.163e-6, 1.163e-9 ], 60 "megajoule": [ 7, 1e+9, 1e+6, 947.81707775, 1000, 277.77777778, 238.84589663, 1, 0.27777777778, 0.001, 2.7777777778e-4, 2.7777777778e-7 ], 61 "kilowatt-hour":[ 8, 3.6e+9, 3.6e+6, 3412.1414799, 3600, 1000, 859.84522786, 3.6, 1, 3.6e-3, 0.001, 1e-6 ], 62 "gigajoule": [ 9, 1e+12, 1e+9, 947817.07775, 1e+6, 277777.77778, 238845.89663, 1000, 277.77777778, 1, 0.27777777778, 2.7777777778e-4 ], 63 "megawatt-hour":[ 10, 3.6e+12, 3.6e+9, 3412141.4799, 3.6e+6, 1e+6, 859845.22786, 3600, 1000, 3.6, 1, 0.001 ], 64 "gigawatt-hour":[ 11, 3.6e+15, 3.6e+12, 3412141479.9, 3.6e+9, 1e+9, 859845227.86, 3.6e+6, 1e+6, 3600, 1000, 1 ] 65 }; 66 67 /** 68 * Return the type of this measurement. Examples are "mass", 69 * "length", "speed", etc. Measurements can only be converted 70 * to measurements of the same type.<p> 71 * 72 * The type of the units is determined automatically from the 73 * units. For example, the unit "grams" is type "mass". Use the 74 * static call {@link Measurement.getAvailableUnits} 75 * to find out what units this version of ilib supports. 76 * 77 * @return {string} the name of the type of this measurement 78 */ 79 EnergyUnit.prototype.getMeasure = function() { 80 return "energy"; 81 }; 82 83 /** 84 * Return a new instance of this type of measurement. 85 * 86 * @param {Object} params parameters to the constructor 87 * @return {Measurement} a measurement subclass instance 88 */ 89 EnergyUnit.prototype.newUnit = function(params) { 90 return new EnergyUnit(params); 91 }; 92 93 EnergyUnit.aliases = { 94 "milli joule": "millijoule", 95 "millijoule": "millijoule", 96 "milliJ": "millijoule", 97 "mJ": "millijoule", 98 "joule": "joule", 99 "joules": "joule", 100 "J": "joule", 101 "BTU": "BTU", 102 "British Thermal Unit": "BTU", 103 "British Thermal Units": "BTU", 104 "kilo joule": "kilojoule", 105 "kilojoule": "kilojoule", 106 "kilojoules": "kilojoule", 107 "kjoule": "kilojoule", 108 "kJ": "kilojoule", 109 "watt hour": "watt-hour", 110 "watt hours": "watt-hour", 111 "Wh": "watt-hour", 112 "food calorie": "foodcalorie", 113 "food calories": "foodcalorie", 114 "calorie": "foodcalorie", 115 "calories": "foodcalorie", 116 "Cal": "foodcalorie", 117 "mega joule": "megajoule", 118 "mega joules": "megajoule", 119 "megajoule": "megajoule", 120 "megajoules": "megajoule", 121 "MJ": "megajoule", 122 "kilo watt hour": "kilowatt-hour", 123 "kilo watt hours": "kilowatt-hour", 124 "kiloWh": "kilowatt-hour", 125 "kilowatt hour": "kilowatt-hour", 126 "kilowatt hours": "kilowatt-hour", 127 "kilowatt-hour": "kilowatt-hour", 128 "kilowatt-hours": "kilowatt-hour", 129 "kilowatthour": "kilowatt-hour", 130 "kilowatthours": "kilowatt-hour", 131 "kW hour": "kilowatt-hour", 132 "kW hours": "kilowatt-hour", 133 "kW-hour": "kilowatt-hour", 134 "kW-hours": "kilowatt-hour", 135 "kWh": "kilowatt-hour", 136 "giga joule": "gigajoule", 137 "Gj": "gigajoule", 138 "gigajoule": "gigajoule", 139 "gigajoules": "gigajoule", 140 "mega watt hour": "megawatt-hour", 141 "mega watt hours": "megawatt-hour", 142 "megawatt hour": "megawatt-hour", 143 "megawatt hours": "megawatt-hour", 144 "megawatt-hour": "megawatt-hour", 145 "megawatt-hours": "megawatt-hour", 146 "MW hour": "megawatt-hour", 147 "MW hours": "megawatt-hour", 148 "MW-hour": "megawatt-hour", 149 "MW-hours": "megawatt-hour", 150 "megaWh": "megawatt-hour", 151 "MWh": "megawatt-hour", 152 "giga watt hour": "gigawatt-hour", 153 "giga watt hours": "gigawatt-hour", 154 "gigawatt hour": "gigawatt-hour", 155 "gigawatt hours": "gigawatt-hour", 156 "gigawatt-hours": "gigawatt-hour", 157 "gigawatthour": "gigawatt-hour", 158 "GW hour": "gigawatt-hour", 159 "GW hours": "gigawatt-hour", 160 "GW-hour": "gigawatt-hour", 161 "GW-hours": "gigawatt-hour", 162 "gigaWh": "gigawatt-hour", 163 "GWh": "gigawatt-hour" 164 }; 165 166 (function() { 167 EnergyUnit.aliasesLower = {}; 168 for (var a in EnergyUnit.aliases) { 169 EnergyUnit.aliasesLower[a.toLowerCase()] = EnergyUnit.aliases[a]; 170 } 171 })(); 172 173 /** 174 * Convert a energy to another measure. 175 * @static 176 * @param to {string} unit to convert to 177 * @param from {string} unit to convert from 178 * @param energy {number} amount to be convert 179 * @returns {number|undefined} the converted amount 180 */ 181 EnergyUnit.convert = function(to, from, energy) { 182 from = Measurement.getUnitIdCaseInsensitive(EnergyUnit, from) || from; 183 to = Measurement.getUnitIdCaseInsensitive(EnergyUnit, to) || to; 184 var fromRow = EnergyUnit.ratios[from]; 185 var toRow = EnergyUnit.ratios[to]; 186 if (typeof(from) === 'undefined' || typeof(to) === 'undefined') { 187 return undefined; 188 } 189 return energy * fromRow[toRow[0]]; 190 }; 191 192 EnergyUnit.systems = { 193 "metric": [ 194 "millijoule", 195 "joule", 196 "kilojoule", 197 "watt-hour", 198 "megajoule", 199 "kilowatt-hour", 200 "gigajoule", 201 "megawatt-hour", 202 "gigawatt-hour" 203 ], 204 "imperial": [ 205 "BTU", 206 "foodcalorie" 207 ], 208 "uscustomary": [ 209 "BTU", 210 "foodcalorie" 211 ], 212 "conversions": { 213 "metric": { 214 "uscustomary": { 215 "millijoule": "BTU", 216 "joule": "BTU", 217 "kilojoule": "BTU", 218 "watt-hour": "BTU", 219 "megajoule": "BTU", 220 "kilowatt-hour": "BTU", 221 "gigajoule": "BTU", 222 "megawatt-hour": "BTU", 223 "gigawatt-hour": "BTU" 224 }, 225 "imperial": { 226 "millijoule": "BTU", 227 "joule": "BTU", 228 "kilojoule": "BTU", 229 "watt-hour": "BTU", 230 "megajoule": "BTU", 231 "kilowatt-hour": "BTU", 232 "gigajoule": "BTU", 233 "megawatt-hour": "BTU", 234 "gigawatt-hour": "BTU" 235 } 236 }, 237 "uscustomary": { 238 "metric": { 239 "BTU": "joule", 240 "foodcalorie": "joule" 241 } 242 }, 243 "imperial": { 244 "metric": { 245 "BTU": "joule", 246 "foodcalorie": "joule" 247 } 248 } 249 } 250 }; 251 252 /** 253 * @private 254 * @static 255 */ 256 EnergyUnit.getMeasures = function () { 257 return Object.keys(EnergyUnit.ratios); 258 }; 259 260 //register with the factory method 261 Measurement._constructors["energy"] = EnergyUnit; 262 263 module.exports = EnergyUnit; 264