UNPKG

ilib

Version:

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

1 lines 5.81 kB
var Measurement=require("./Measurement.js");var DigitalStorageUnit=function(t){this.unit="byte";this.amount=0;this.aliases=DigitalStorageUnit.aliases;if(t){if(typeof t.unit!=="undefined"){this.originalUnit=t.unit;this.unit=this.aliases[t.unit]||t.unit}if(typeof t.amount==="object"){if(t.amount.getMeasure()==="digitalStorage"){this.amount=DigitalStorageUnit.convert(this.unit,t.amount.getUnit(),t.amount.getAmount())}else{throw"Cannot convert unit "+t.amount.unit+" to a digitalStorage"}}else if(typeof t.amount!=="undefined"){this.amount=parseFloat(t.amount)}}if(typeof DigitalStorageUnit.ratios[this.unit]==="undefined"){throw"Unknown unit: "+t.unit}};DigitalStorageUnit.prototype=new Measurement;DigitalStorageUnit.prototype.parent=Measurement;DigitalStorageUnit.prototype.constructor=DigitalStorageUnit;DigitalStorageUnit.ratios={bit:[1,1,.125,.0009765625,.0001220703125,9.536743164e-7,1.192092896e-7,9.313225746e-10,1.164153218e-10,9.094947017e-13,1.136868377e-13,8.881784197e-16,1.110223025e-16],"byte":[2,8,1,.0078125,.0009765625,7629394531e-15,9.536743164e-7,7.450580597e-9,9.313225746e-10,7.275957614e-12,9.094947017e-13,7.105427358e-15,8.881784197e-16],kilobit:[3,1024,128,1,.125,.0009765625,.0001220703125,9.536743164e-7,1.192092896e-7,9.313225746e-10,1.164153218e-10,9.094947017e-13,1.136868377e-13],kilobyte:[4,8192,1024,8,1,.0078125,.0009765625,7629394531e-15,9.536743164e-7,7.450580597e-9,9.313225746e-10,7.275957614e-12,9.094947017e-13],megabit:[5,1048576,131072,1024,128,1,.125,.0009765625,.0001220703125,9.536743164e-7,1.192092896e-7,9.313225746e-10,1.164153218e-10],megabyte:[6,8388608,1048576,8192,1024,8,1,.0078125,.0009765625,7629394531e-15,9.536743164e-7,7.450580597e-9,9.313225746e-10],gigabit:[7,1073741824,134217728,1048576,131072,1024,128,1,.125,.0009765625,.0001220703125,9.536743164e-7,1.192092896e-7],gigabyte:[8,8589934592,1073741824,8388608,1048576,8192,1024,8,1,.0078125,.0009765625,7629394531e-15,9.536743164e-7],terabit:[9,1099511628e3,137438953472,1073741824,134217728,1048576,131072,1024,128,1,.125,.0009765625,.0001220703125],terabyte:[10,8796093022e3,1099511628e3,8589934592,1073741824,8388608,1048576,8192,1024,8,1,.0078125,.0009765625],petabit:[11,1125899907e6,1407374884e5,1099511628e3,137438953472,1073741824,134217728,1048576,131072,1024,128,1,.125],petabyte:[12,9007199255e6,1125899907e6,8796093022e3,1099511628e3,8589934592,1073741824,8388608,1048576,8192,1024,8,1]};DigitalStorageUnit.bitSystem={bit:1,kilobit:3,megabit:5,gigabit:7,terabit:9,petabit:11};DigitalStorageUnit.byteSystem={"byte":2,kilobyte:4,megabyte:6,gigabyte:8,terabyte:10,petabyte:12};DigitalStorageUnit.prototype.getMeasure=function(){return"digitalStorage"};DigitalStorageUnit.prototype.convert=function(t){if(!t||typeof DigitalStorageUnit.ratios[this.normalizeUnits(t)]==="undefined"){return undefined}return new DigitalStorageUnit({unit:t,amount:this})};DigitalStorageUnit.prototype.localize=function(t){return new DigitalStorageUnit({unit:this.unit,amount:this.amount})};DigitalStorageUnit.prototype.scale=function(t){var e;if(this.unit in DigitalStorageUnit.bitSystem){e=DigitalStorageUnit.bitSystem}else{e=DigitalStorageUnit.byteSystem}var i=this.amount;var a=this.unit;var g=DigitalStorageUnit.ratios[this.unit];i=0x10000000000000000;for(var b in e){var o=this.amount*g[e[b]];if(o>=1&&o<i){i=o;a=b}}return new DigitalStorageUnit({unit:a,amount:i})};DigitalStorageUnit.aliases={bits:"bit",bit:"bit",Bits:"bit",Bit:"bit","byte":"byte",bytes:"byte",Byte:"byte",Bytes:"byte",kilobits:"kilobit",Kilobits:"kilobit",KiloBits:"kilobit",kiloBits:"kilobit",kilobit:"kilobit",Kilobit:"kilobit",kiloBit:"kilobit",KiloBit:"kilobit",kb:"kilobit",Kb:"kilobit",kilobyte:"kilobyte",Kilobyte:"kilobyte",kiloByte:"kilobyte",KiloByte:"kilobyte",kilobytes:"kilobyte",Kilobytes:"kilobyte",kiloBytes:"kilobyte",KiloBytes:"kilobyte",kB:"kilobyte",KB:"kilobyte",megabit:"megabit",Megabit:"megabit",megaBit:"megabit",MegaBit:"megabit",megabits:"megabit",Megabits:"megabit",megaBits:"megabit",MegaBits:"megabit",Mb:"megabit",mb:"megabit",megabyte:"megabyte",Megabyte:"megabyte",megaByte:"megabyte",MegaByte:"megabyte",megabytes:"megabyte",Megabytes:"megabyte",megaBytes:"megabyte",MegaBytes:"megabyte",MB:"megabyte",mB:"megabyte",gigabit:"gigabit",Gigabit:"gigabit",gigaBit:"gigabit",GigaBit:"gigabit",gigabits:"gigabit",Gigabits:"gigabit",gigaBits:"gigabyte",GigaBits:"gigabit",Gb:"gigabit",gb:"gigabit",gigabyte:"gigabyte",Gigabyte:"gigabyte",gigaByte:"gigabyte",GigaByte:"gigabyte",gigabytes:"gigabyte",Gigabytes:"gigabyte",gigaBytes:"gigabyte",GigaBytes:"gigabyte",GB:"gigabyte",gB:"gigabyte",terabit:"terabit",Terabit:"terabit",teraBit:"terabit",TeraBit:"terabit",terabits:"terabit",Terabits:"terabit",teraBits:"terabit",TeraBits:"terabit",tb:"terabit",Tb:"terabit",terabyte:"terabyte",Terabyte:"terabyte",teraByte:"terabyte",TeraByte:"terabyte",terabytes:"terabyte",Terabytes:"terabyte",teraBytes:"terabyte",TeraBytes:"terabyte",TB:"terabyte",tB:"terabyte",petabit:"petabit",Petabit:"petabit",petaBit:"petabit",PetaBit:"petabit",petabits:"petabit",Petabits:"petabit",petaBits:"petabit",PetaBits:"petabit",pb:"petabit",Pb:"petabit",petabyte:"petabyte",Petabyte:"petabyte",petaByte:"petabyte",PetaByte:"petabyte",petabytes:"petabyte",Petabytes:"petabyte",petaBytes:"petabyte",PetaBytes:"petabyte",PB:"petabyte",pB:"petabyte"};DigitalStorageUnit.convert=function(t,e,i){e=DigitalStorageUnit.aliases[e]||e;t=DigitalStorageUnit.aliases[t]||t;var a=DigitalStorageUnit.ratios[e];var g=DigitalStorageUnit.ratios[t];if(typeof e==="undefined"||typeof t==="undefined"){return undefined}var b=i*a[g[0]];return b};DigitalStorageUnit.getMeasures=function(){var t=[];for(var e in DigitalStorageUnit.ratios){t.push(e)}return t};Measurement._constructors["digitalStorage"]=DigitalStorageUnit;module.exports=DigitalStorageUnit;