UNPKG

ilib

Version:

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

2 lines 7.95 kB
// !data localeinfo currency var ilib=require("../index.js"),JSUtils=require("./JSUtils.js"),MathUtils=require("./MathUtils.js"),Locale=require("./Locale.js"),LocaleInfo=require("./LocaleInfo.js"),Currency=require("./Currency.js"),IString=require("./IString.js"),NumFmt=function(options){var sync=!0,loadParams=(this.locale=new Locale(),this.type="number",void 0);options&&(options.locale&&(this.locale="string"==typeof options.locale?new Locale(options.locale):options.locale),!options.type||"number"!==options.type&&"currency"!==options.type&&"percentage"!==options.type||(this.type=options.type),options.currency&&(this.currency=options.currency),void 0!==options.maxFractionDigits&&(this.maxFractionDigits=Number(options.maxFractionDigits)),void 0!==options.minFractionDigits&&(this.minFractionDigits=Number(options.minFractionDigits),this.minFractionDigits<0&&(this.minFractionDigits=0),20<this.minFractionDigits)&&(this.minFractionDigits=20),void 0!==options.significantDigits&&(this.significantDigits=Number(options.significantDigits),this.significantDigits<1&&(this.significantDigits=1),20<this.significantDigits)&&(this.significantDigits=20),options.style&&(this.style=options.style),"boolean"==typeof options.useNative&&(this.useNative=options.useNative),this.roundingMode=options.roundingMode,"boolean"==typeof options.sync&&(sync=options.sync),loadParams=options.loadParams),this.localeInfo=void 0,new LocaleInfo(this.locale,{sync:sync,loadParams:loadParams,onLoad:ilib.bind(this,function(li){if(this.localeInfo=li,"number"===this.type)this.templateNegative=new IString(this.localeInfo.getNegativeNumberFormat()||"-{n}");else{if("currency"===this.type){var templates;if(this.currency&&"string"==typeof this.currency)return void new Currency({locale:this.locale,code:this.currency,sync:sync,loadParams:loadParams,onLoad:ilib.bind(this,function(cur){this.currencyInfo=cur,"common"!==this.style&&"iso"!==this.style&&(this.style="common"),"number"!=typeof this.maxFractionDigits&&"number"!=typeof this.minFractionDigits&&(this.minFractionDigits=this.maxFractionDigits=this.currencyInfo.getFractionDigits()),templates=this.localeInfo.getCurrencyFormats(),this.template=new IString(templates[this.style]||templates.common),this.templateNegative=new IString(templates[this.style+"Negative"]||templates.commonNegative),this.sign="iso"===this.style?this.currencyInfo.getCode():this.currencyInfo.getSign(),this.roundingMode||(this.roundingMode=this.currencyInfo&&this.currencyInfo.roundingMode),this._init(),options&&"function"==typeof options.onLoad&&options.onLoad(this)})});throw"A currency property is required in the options to the number formatter constructor when the type property is set to currency."}"percentage"===this.type&&(this.template=new IString(this.localeInfo.getPercentageFormat()||"{n}%"),this.templateNegative=new IString(this.localeInfo.getNegativePercentageFormat()||this.localeInfo.getNegativeNumberFormat()+"%"))}this._init(),options&&"function"==typeof options.onLoad&&options.onLoad(this)})})};NumFmt.getAvailableLocales=function(){},NumFmt.zeros="0000000000000000000000000000000000000000000000000000000000000000000000",NumFmt.prototype={getUseNative:function(){return"boolean"==typeof this.useNative?this.useNative:"native"===this.localeInfo.getDigitsStyle()},_init:function(){var nd;this.maxFractionDigits<this.minFractionDigits&&(this.minFractionDigits=this.maxFractionDigits),this.roundingMode||(this.roundingMode=this.localeInfo.getRoundingMode()),this.roundingMode||(this.roundingMode="halfdown"),this.round=MathUtils[this.roundingMode],this.round||(this.roundingMode="halfdown",this.round=MathUtils[this.roundingMode]),"nogrouping"===this.style?this.prigroupSize=this.secgroupSize=0:(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(),this.getUseNative()&&(nd=this.localeInfo.getNativeDigits()||this.localeInfo.getDigits())&&(this.digits=nd.split("")),this.exponentSymbol=this.localeInfo.getExponential()||"e"},constrain:function(num){var parts=(""+num).split(".");return void 0!==this.significantDigits&&0<this.significantDigits&&(void 0===this.maxFractionDigits||this.maxFractionDigits<0||parts[0].length+this.maxFractionDigits>this.significantDigits)&&(num=MathUtils.significant(num,this.significantDigits,this.round)),num=void 0!==this.maxFractionDigits&&-1<this.maxFractionDigits?MathUtils.shiftDecimal(this.round(MathUtils.shiftDecimal(num,this.maxFractionDigits)),-this.maxFractionDigits):num},_formatScientific:function(num){var maxDigits,num=new Number(num).toExponential().split("e"),significant=num[0],num=num[1];return(0<this.maxFractionDigits||0<this.significantDigits)&&(maxDigits=(this.maxFractionDigits||25)+1,0<this.significantDigits&&(maxDigits=Math.min(maxDigits,this.significantDigits)),significant=MathUtils.significant(Number(significant),maxDigits,this.round)),significant=(maxDigits=(""+significant).split("."))[0],maxDigits=maxDigits[1],void 0!==this.maxFractionDigits&&(maxDigits=maxDigits.substring(0,this.maxFractionDigits)),significant=significant,(maxDigits=void 0!==this.minFractionDigits?JSUtils.pad(maxDigits||"",this.minFractionDigits,!0):maxDigits).length&&(significant+=this.decimalSeparator+maxDigits),significant+=this.exponentSymbol+num},_formatStandard:function(num){var i,cycle,formatted;if(integral=(num=(""+(num=Math.abs(this.constrain(num)))).split("."))[0].toString(),num=num[1],0<this.minFractionDigits&&(num=JSUtils.pad(num||"",this.minFractionDigits,!0)),0<this.secgroupSize){if(integral.length>this.prigroupSize){var size1=this.prigroupSize,size1=integral.length-size1,num_sec=(integral=integral.slice(0,size1)+this.groupingSeparator+integral.slice(size1)).substring(0,integral.indexOf(this.groupingSeparator));for(k=num_sec.length;k>this.secgroupSize;)var integral,secsize1=this.secgroupSize,secsize1=num_sec.length-secsize1,k=(num_sec=(integral=integral.slice(0,secsize1)+this.groupingSeparator+integral.slice(secsize1)).substring(0,integral.indexOf(this.groupingSeparator))).length}formatted=integral}else if(0!==this.prigroupSize){for(cycle=MathUtils.mod(integral.length-1,this.prigroupSize),formatted="",i=0;i<integral.length-1;i++)formatted+=integral.charAt(i),0===cycle&&(formatted+=this.groupingSeparator),cycle=MathUtils.mod(cycle-1,this.prigroupSize);formatted+=integral.charAt(integral.length-1)}else formatted=integral;return num&&(void 0===this.maxFractionDigits&&void 0===this.significantDigits||0<this.maxFractionDigits||0<this.significantDigits)&&(formatted=formatted+this.decimalSeparator+num),formatted=this.digits?JSUtils.mapString(formatted,this.digits):formatted},format:function(num){var n,formatted;return void 0===num?"":(n=Number(num),"number"===this.type?(formatted="scientific"===this.style?this._formatScientific(n):this._formatStandard(n),num<0&&(formatted=this.templateNegative.format({n:formatted}))):(formatted=this._formatStandard(n),formatted=(n<0?this.templateNegative:this.template).format({n:formatted,s:this.sign})),formatted)},getType:function(){return this.type},getLocale:function(){return this.locale},isGroupingUsed:function(){return"undefined"!==this.groupingSeparator&&0<this.groupingSeparator.length},getMaxFractionDigits:function(){return void 0!==this.maxFractionDigits?this.maxFractionDigits:-1},getMinFractionDigits:function(){return void 0!==this.minFractionDigits?this.minFractionDigits:-1},getSignificantDigits:function(){return void 0!==this.significantDigits?this.significantDigits:-1},getCurrency:function(){return this.currencyInfo&&this.currencyInfo.getCode()},getRoundingMode:function(){return this.roundingMode},getStyle:function(){return this.style}},module.exports=NumFmt;