ilib
Version:
iLib is a cross-engine library of internationalization (i18n) classes written in pure JS
1 lines • 6.85 kB
JavaScript
var ilib=require("./ilib.js");var Utils=require("./Utils.js");var JSUtils=require("./JSUtils.js");var MathUtils=require("./MathUtils.js");var Locale=require("./Locale.js");var LocaleInfo=require("./LocaleInfo.js");var Currency=require("./Currency.js");var IString=require("./IString.js");var INumber=require("./INumber.js");var NumFmt=function(i){var t=true;this.locale=new Locale;this.type="number";var e=undefined;if(i){if(i.locale){this.locale=typeof i.locale==="string"?new Locale(i.locale):i.locale}if(i.type){if(i.type==="number"||i.type==="currency"||i.type==="percentage"){this.type=i.type}}if(i.currency){this.currency=i.currency}if(typeof i.maxFractionDigits==="number"){this.maxFractionDigits=this._toPrimitive(i.maxFractionDigits)}if(typeof i.minFractionDigits==="number"){this.minFractionDigits=this._toPrimitive(i.minFractionDigits);if(this.minFractionDigits<0){this.minFractionDigits=0}if(this.minFractionDigits>20){this.minFractionDigits=20}}if(i.style){this.style=i.style}if(typeof i.useNative==="boolean"){this.useNative=i.useNative}this.roundingMode=i.roundingMode;if(typeof i.sync!=="undefined"){t=i.sync==true}e=i.loadParams}this.localeInfo=undefined;new LocaleInfo(this.locale,{sync:t,loadParams:e,onLoad:ilib.bind(this,function(n){this.localeInfo=n;if(this.type==="number"){this.templateNegative=new IString(this.localeInfo.getNegativeNumberFormat()||"-{n}")}else if(this.type==="currency"){var r;if(!this.currency||typeof this.currency!="string"){throw"A currency property is required in the options to the number formatter constructor when the type property is set to currency."}new Currency({locale:this.locale,code:this.currency,sync:t,loadParams:e,onLoad:ilib.bind(this,function(t){this.currencyInfo=t;if(this.style!=="common"&&this.style!=="iso"){this.style="common"}if(typeof this.maxFractionDigits!=="number"&&typeof this.minFractionDigits!=="number"){this.minFractionDigits=this.maxFractionDigits=this.currencyInfo.getFractionDigits()}r=this.localeInfo.getCurrencyFormats();this.template=new IString(r[this.style]||r.common);this.templateNegative=new IString(r[this.style+"Negative"]||r["commonNegative"]);this.sign=this.style==="iso"?this.currencyInfo.getCode():this.currencyInfo.getSign();if(!this.roundingMode){this.roundingMode=this.currencyInfo&&this.currencyInfo.roundingMode}this._init();if(i&&typeof i.onLoad==="function"){i.onLoad(this)}})});return}else if(this.type==="percentage"){this.template=new IString(this.localeInfo.getPercentageFormat()||"{n}%");this.templateNegative=new IString(this.localeInfo.getNegativePercentageFormat()||this.localeInfo.getNegativeNumberFormat()+"%")}this._init();if(i&&typeof i.onLoad==="function"){i.onLoad(this)}})})};NumFmt.getAvailableLocales=function(){return undefined};NumFmt.zeros="0000000000000000000000000000000000000000000000000000000000000000000000";NumFmt.prototype={getUseNative:function(){if(typeof this.useNative==="boolean"){return this.useNative}return this.localeInfo.getDigitsStyle()==="native"},_init:function(){if(this.maxFractionDigits<this.minFractionDigits){this.minFractionDigits=this.maxFractionDigits}if(!this.roundingMode){this.roundingMode=this.localeInfo.getRoundingMode()}if(!this.roundingMode){this.roundingMode="halfdown"}this.round=MathUtils[this.roundingMode];if(!this.round){this.roundingMode="halfdown";this.round=MathUtils[this.roundingMode]}if(this.style==="nogrouping"){this.prigroupSize=this.secgroupSize=0}else{this.prigroupSize=this.localeInfo.getPrimaryGroupingDigits();this.secgroupSize=this.localeInfo.getSecondaryGroupingDigits();this.groupingSeparator=this.getUseNative()?this.localeInfo.getNativeGroupingSeparator():this.localeInfo.getGroupingSeparator()}this.decimalSeparator=this.getUseNative()?this.localeInfo.getNativeDecimalSeparator():this.localeInfo.getDecimalSeparator();if(this.getUseNative()){var i=this.localeInfo.getNativeDigits()||this.localeInfo.getDigits();if(i){this.digits=i.split("")}}this.exponentSymbol=this.localeInfo.getExponential()||"e"},_pad:function(i,t,e){return i.length>=t?i:e?NumFmt.zeros.substring(0,t-i.length)+i:i+NumFmt.zeros.substring(0,t-i.length)},_toPrimitive:function(i){var t=0;switch(typeof i){case"number":t=i;break;case"string":t=parseFloat(i);break;case"object":t=i.valueOf();break}return t},_formatScientific:function(i){var t=new Number(i);var e;var n,r=t.toExponential(),s=r.split("e"),o=s[0],a=s[1],h,c,g;if(this.maxFractionDigits>0){n=Math.pow(10,this.maxFractionDigits);o=this.round(o*n)/n}h=(""+o).split(".");c=h[0];g=h[1];if(typeof this.maxFractionDigits!=="undefined"){g=g.substring(0,this.maxFractionDigits)}if(typeof this.minFractionDigits!=="undefined"){g=this._pad(g||"",this.minFractionDigits,false)}e=c;if(g.length){e+=this.decimalSeparator+g}e+=this.exponentSymbol+a;return e},_formatStandard:function(i){var t;var e;if(typeof this.maxFractionDigits!=="undefined"&&this.maxFractionDigits>-1){var n=Math.pow(10,this.maxFractionDigits);i=this.round(i*n)/n}i=Math.abs(i);var r=(""+i).split("."),s=r[0],o=r[1],a,h;s=s.toString();if(this.minFractionDigits>0){o=this._pad(o||"",this.minFractionDigits,false)}if(this.secgroupSize>0){if(s.length>this.prigroupSize){var c=this.prigroupSize;var g=s.length;var u=g-c;s=s.slice(0,u)+this.groupingSeparator+s.slice(u);var l=s.substring(0,s.indexOf(this.groupingSeparator));e=l.length;while(e>this.secgroupSize){var f=this.secgroupSize;var p=l.length;var m=p-f;s=s.slice(0,m)+this.groupingSeparator+s.slice(m);l=s.substring(0,s.indexOf(this.groupingSeparator));e=l.length}}h=s}else if(this.prigroupSize!==0){a=MathUtils.mod(s.length-1,this.prigroupSize);h="";for(t=0;t<s.length-1;t++){h+=s.charAt(t);if(a===0){h+=this.groupingSeparator}a=MathUtils.mod(a-1,this.prigroupSize)}h+=s.charAt(s.length-1)}else{h=s}if(o&&(typeof this.maxFractionDigits==="undefined"||this.maxFractionDigits>0)){h+=this.decimalSeparator;h+=o}if(this.digits){h=JSUtils.mapString(h,this.digits)}return h},format:function(i){var t,e;if(typeof i==="undefined"){return""}e=this._toPrimitive(i);if(this.type==="number"){t=this.style==="scientific"?this._formatScientific(e):this._formatStandard(e);if(i<0){t=this.templateNegative.format({n:t})}}else{t=this._formatStandard(e);var n=e<0?this.templateNegative:this.template;t=n.format({n:t,s:this.sign})}return t},getType:function(){return this.type},getLocale:function(){return this.locale},isGroupingUsed:function(){return this.groupingSeparator!=="undefined"&&this.groupingSeparator.length>0},getMaxFractionDigits:function(){return typeof this.maxFractionDigits!=="undefined"?this.maxFractionDigits:-1},getMinFractionDigits:function(){return typeof this.minFractionDigits!=="undefined"?this.minFractionDigits:-1},getCurrency:function(){return this.currencyInfo&&this.currencyInfo.getCode()},getRoundingMode:function(){return this.roundingMode},getStyle:function(){return this.style}};module.exports=NumFmt;