quoslibero
Version:
FusionCharts JavaScript charting framework
1 lines • 2.69 kB
JavaScript
import FormatSpecifier from'./format-specifier';import formatTypes from'./format-types';import formatTrim from'./format-trim';import{prefixExponent}from'./format-prefix-auto';const CHAR_CODE_0=48,CHAR_CODE_9=57,CHAR_CODE_DOT=46,isNil=a=>'undefined'==typeof a||null===a;class Formatter{constructor(a,b){var c=Math.max,d=Math.min;this.specifier=new FormatSpecifier(a),this.localeInfo=b;let{symbol:e}=this.specifier;this.fill=this.specifier.fill,this.align=this.specifier.align,this.sign=this.specifier.sign,this.zero=this.specifier.zero,this.width=this.specifier.width,this.comma=this.specifier.comma,this.precision=this.specifier.precision,this.trim=this.specifier.trim,this.type=this.specifier.type,'n'===this.type?(this.comma=!0,this.type='g'):!formatTypes[this.type]&&(isNil(this.precision)&&(this.precision=12),this.trim=!0,this.type='g'),(this.zero||'0'===this.fill&&'='===this.align)&&(this.zero=!0,this.fill='0',this.align='='),this.prefix='$'===e?this.localeInfo.prefix:'#'===e&&/[boxX]/.test(this.type)?'0'+this.type.toLowerCase():'',this.suffix='$'===e?this.localeInfo.suffix:/[%p]/.test(this.type)?this.localeInfo.percent:'',this.formatType=formatTypes[this.type],this.mayHaveSuffix=/[defgprs%]/.test(this.type),this.precision=isNil(this.precision)?6:/[gprs]/.test(this.type)?c(1,d(21,this.precision)):c(0,d(20,this.precision)),this.setCustomPrefix(''),this.setCustomSuffix('')}format(a){let b,d,e,f,g,h,j=this.prefix,k=this.suffix,l=this.formatType;if('c'===this.type)k=l(a)+k,a='';else if(a=+a,b=0>a,a=l(Math.abs(a),this.precision),this.trim&&(a=formatTrim(a)),b&&0==+a&&(b=!1),j=b?('('===this.sign?this.sign:'-')+j:'-'===this.sign||'('===this.sign?''+j:this.sign+j,k='s'===this.type?this.localeInfo.prefixes[8+prefixExponent/3]+k:''+k,k+=b&&'('===this.sign?')':'',this.mayHaveSuffix)for(f=-1,g=a.length;++f<g;)if(h=a.charCodeAt(f),h<CHAR_CODE_0||h>CHAR_CODE_9){k=(h===CHAR_CODE_DOT?this.localeInfo.decimal+a.slice(f+1):a.slice(f))+k,a=a.slice(0,f);break}switch(this.comma&&!this.zero&&(a=this.localeInfo.group(a,1/0)),d=j.length+a.length+k.length,e=d<this.width?Array(this.width-d+1).join(this.fill):'',this.comma&&this.zero&&(a=this.localeInfo.group(e+a,e.length?this.width-k.length:1/0),e=''),this.align){case'<':a=j+a+k+e;break;case'=':a=j+e+a+k;break;case'^':d=e.length/2,a=e.slice(0,d)+j+a+k+e.slice(d);break;default:a=e+j+a+k;}return this.getCustomPrefix()+this.localeInfo.numerals(a)+this.getCustomSuffix()}setCustomPrefix(a){return this._customPrefix=a+'',this}getCustomPrefix(){return this._customPrefix}setCustomSuffix(a){return this._customSuffix=a+'',this}getCustomSuffix(){return this._customSuffix}toString(){return this.specifier.toString()}}export default Formatter;