UNPKG

ilib

Version:

iLib is a cross-engine library of internationalization (i18n) classes written in pure JS

1 lines 5.99 kB
var Measurement=require("./Measurement.js");var EnergyUnit=function(e){this.unit="joule";this.amount=0;this.aliases=EnergyUnit.aliases;if(e){if(typeof e.unit!=="undefined"){this.originalUnit=e.unit;this.unit=this.aliases[e.unit]||e.unit}if(typeof e.amount==="object"){if(e.amount.getMeasure()==="energy"){this.amount=EnergyUnit.convert(this.unit,e.amount.getUnit(),e.amount.getAmount())}else{throw"Cannot convert units "+e.amount.unit+" to a energy"}}else if(typeof e.amount!=="undefined"){this.amount=parseFloat(e.amount)}}if(typeof EnergyUnit.ratios[this.unit]==="undefined"){throw"Unknown unit: "+e.unit}};EnergyUnit.prototype=new Measurement;EnergyUnit.prototype.parent=Measurement;EnergyUnit.prototype.constructor=EnergyUnit;EnergyUnit.ratios={millijoule:[1,1,.001,9.4781707775e-7,1e-6,2.7777777778e-7,2.3884589663e-7,1e-9,2.7777777778e-10,1e-12,2.7777777778e-13,2.7777777778e-16],joule:[2,1e3,1,.00094781707775,.001,.00027777777778,.00023884589663,1e-6,2.7777777778e-7,1e-9,2.7777777778e-10,2.7777777778e-13],BTU:[3,1055055.9,1055.0559,1,1.0550559,.29307108333,.25199577243,.0010550559,.00029307108333,10550559e-13,2.9307108333e-7,2.9307108333e-10],kilojoule:[4,1e6,1e3,.94781707775,1,.27777777778,.23884589663,.001,.00027777777778,1e-6,2.7777777778e-7,2.7777777778e-10],"watt hour":[5,36e5,3600,3.4121414799,3.6,1,.85984522786,.0036,.001,36e-7,1e-6,1e-9],calorie:[6,486800,4186.8,3.9683205411,4.1868,1.163,1,.0041868,.001163,41868e-10,1163e-9,1.163e-9],megajoule:[7,1e9,1e6,947.81707775,1e3,277.77777778,238.84589663,1,.27777777778,.001,.00027777777778,2.7777777778e-7],"kilowatt hour":[8,36e8,36e5,3412.1414799,3600,1e3,859.84522786,3.6,1,.0036,.001,1e-6],gigajoule:[9,1e12,1e9,947817.07775,1e6,277777.77778,238845.89663,1e3,277.77777778,1,.27777777778,.00027777777778],"megawatt hour":[10,36e11,36e8,3412141.4799,36e5,1e6,859845.22786,3600,1e3,3.6,1,.001],"gigawatt hour":[11,36e14,36e11,3412141479.9,36e8,1e9,859845227.86,36e5,1e6,3600,1e3,1]};EnergyUnit.prototype.getMeasure=function(){return"energy"};EnergyUnit.prototype.convert=function(e){if(!e||typeof EnergyUnit.ratios[this.normalizeUnits(e)]==="undefined"){return undefined}return new EnergyUnit({unit:e,amount:this})};EnergyUnit.aliases={"milli joule":"millijoule",millijoule:"millijoule",MilliJoule:"millijoule",milliJ:"millijoule",joule:"joule",J:"joule",j:"joule",Joule:"joule",Joules:"joule",joules:"joule",BTU:"BTU",btu:"BTU","British thermal unit":"BTU","british thermal unit":"BTU","kilo joule":"kilojoule",kJ:"kilojoule",kj:"kilojoule",Kj:"kilojoule",kiloJoule:"kilojoule",kilojoule:"kilojoule",kjoule:"kilojoule","watt hour":"watt hour",Wh:"watt hour",wh:"watt hour","watt-hour":"watt hour",calorie:"calorie",Cal:"calorie",cal:"calorie",Calorie:"calorie",calories:"calorie","mega joule":"megajoule",MJ:"megajoule",megajoule:"megajoule",megajoules:"megajoule",Megajoules:"megajoule",megaJoules:"megajoule",MegaJoules:"megajoule",megaJoule:"megajoule",MegaJoule:"megajoule","kilo Watt hour":"kilowatt hour",kWh:"kilowatt hour",kiloWh:"kilowatt hour",KiloWh:"kilowatt hour","KiloWatt-hour":"kilowatt hour","kilowatt hour":"kilowatt hour","kilowatt-hour":"kilowatt hour","KiloWatt-hours":"kilowatt hour","kilowatt-hours":"kilowatt hour","Kilo Watt-hour":"kilowatt hour","Kilo Watt-hours":"kilowatt hour","giga joule":"gigajoule",gJ:"gigajoule",GJ:"gigajoule",GigaJoule:"gigajoule",gigaJoule:"gigajoule",gigajoule:"gigajoule",GigaJoules:"gigajoule",gigaJoules:"gigajoule",Gigajoules:"gigajoule",gigajoules:"gigajoule","mega watt hour":"megawatt hour",MWh:"megawatt hour",MegaWh:"megawatt hour",megaWh:"megawatt hour",megaWatthour:"megawatt hour","megaWatt-hour":"megawatt hour","mega Watt-hour":"megawatt hour","megaWatt hour":"megawatt hour","megawatt hour":"megawatt hour","mega Watt hour":"megawatt hour","giga watt hour":"gigawatt hour",gWh:"gigawatt hour",GWh:"gigawatt hour",gigaWh:"gigawatt hour","gigaWatt-hour":"gigawatt hour","gigawatt-hour":"gigawatt hour","gigaWatt hour":"gigawatt hour","gigawatt hour":"gigawatt hour",gigawatthour:"gigawatt hour"};EnergyUnit.convert=function(e,t,i){t=EnergyUnit.aliases[t]||t;e=EnergyUnit.aliases[e]||e;var o=EnergyUnit.ratios[t];var u=EnergyUnit.ratios[e];if(typeof t==="undefined"||typeof e==="undefined"){return undefined}return i*o[u[0]]};EnergyUnit.getMeasures=function(){var e=[];for(var t in EnergyUnit.ratios){e.push(t)}return e};EnergyUnit.metricJouleSystem={millijoule:1,joule:2,kilojoule:4,megajoule:7,gigajoule:9};EnergyUnit.metricWattHourSystem={"watt hour":5,"kilowatt hour":8,"megawatt hour":10,"gigawatt hour":11};EnergyUnit.imperialSystem={BTU:3};EnergyUnit.uscustomarySystem={calorie:6};EnergyUnit.metricToImperial={millijoule:"BTU",joule:"BTU",kilojoule:"BTU",megajoule:"BTU",gigajoule:"BTU"};EnergyUnit.imperialToMetric={BTU:"joule"};EnergyUnit.prototype.localize=function(e){var t;if(e==="en-GB"){t=EnergyUnit.metricToImperial[this.unit]||this.unit}else{t=EnergyUnit.imperialToMetric[this.unit]||this.unit}return new EnergyUnit({unit:t,amount:this})};EnergyUnit.prototype.scale=function(e){var t=EnergyUnit.ratios[this.unit];var i;if(e==="metric"&&typeof EnergyUnit.metricJouleSystem[this.unit]!=="undefined"||typeof e==="undefined"&&typeof EnergyUnit.metricJouleSystem[this.unit]!=="undefined"){i=EnergyUnit.metricJouleSystem}else if(e==="metric"&&typeof EnergyUnit.metricWattHourSystem[this.unit]!=="undefined"||typeof e==="undefined"&&typeof EnergyUnit.metricWattHourSystem[this.unit]!=="undefined"){i=EnergyUnit.metricWattHourSystem}else if(e==="uscustomary"||typeof e==="undefined"&&typeof EnergyUnit.uscustomarySystem[this.unit]!=="undefined"){i=EnergyUnit.uscustomarySystem}else if(e==="imperial"||typeof e==="undefined"&&typeof EnergyUnit.imperialSystem[this.unit]!=="undefined"){i=EnergyUnit.imperialSystem}var o=this.amount;var u=this.unit;o=0x10000000000000000;for(var a in i){var r=this.amount*t[i[a]];if(r>=1&&r<o){o=r;u=a}}return new EnergyUnit({unit:u,amount:o})};Measurement._constructors["energy"]=EnergyUnit;module.exports=EnergyUnit;