num-beauty
Version:
An ultra lightweight module for formatting numbers into human-friendly strings
1 lines • 4.94 kB
JavaScript
import{round}from"./services/rounding.service.js";import{formatNumber}from"./services/formatting.service.js";import{formatCurrency}from"./services/currency.service.js";import{formatBytes}from"./services/bytes.service.js";import{formatPercentage}from"./services/percentage.service.js";import{applyMask,getMask}from"./services/mask.service.js";import{abbreviateNumber}from"./services/abbreviation.service.js";import{toAccessibleString}from"./services/accessibility.service.js";import{unbeautify}from"./services/parsing.service.js";import{beautifyToParts}from"./services/parts.service.js";const pluginServices={round:round,formatNumber:formatNumber,formatCurrency:formatCurrency,formatBytes:formatBytes,formatPercentage:formatPercentage,applyMask:applyMask,getMask:getMask,abbreviateNumber:abbreviateNumber,toAccessibleString:toAccessibleString,unbeautify:unbeautify,beautifyToParts:beautifyToParts};export class Num{static extend(e,t){return Num.appliedPlugins.has(e)||(Num.appliedPlugins.add(e),e({Num:Num,createInstance:num,services:pluginServices,getState:e=>Num.createStateSnapshot(e),patchState(e,t){Num.applyStatePatch(e,t)}},t)),Num}static createStateSnapshot(e){return{value:e.value,locale:e._locale,decimals:e._decimals,abbreviated:e._abbreviated,stripZeros:e._stripZeros,roundingMode:e._roundingMode,mask:e._mask,currency:e._currency,showSymbol:e._showSymbol,showCode:e._showCode,bytes:e._bytes,bytesBinary:e._bytesBinary,bytesLongFormat:e._bytesLongFormat,percentage:e._percentage,percentageMultiply:e._percentageMultiply,percentageAddSpace:e._percentageAddSpace}}static applyStatePatch(e,t){void 0!==t.value&&(e.value=t.value),void 0!==t.locale&&(e._locale=t.locale),void 0!==t.decimals&&(e._decimals=t.decimals),void 0!==t.abbreviated&&(e._abbreviated=t.abbreviated),void 0!==t.stripZeros&&(e._stripZeros=t.stripZeros),void 0!==t.roundingMode&&(e._roundingMode=t.roundingMode),void 0!==t.mask&&(e._mask=t.mask),void 0!==t.currency&&(e._currency=t.currency),void 0!==t.showSymbol&&(e._showSymbol=t.showSymbol),void 0!==t.showCode&&(e._showCode=t.showCode),void 0!==t.bytes&&(e._bytes=t.bytes),void 0!==t.bytesBinary&&(e._bytesBinary=t.bytesBinary),void 0!==t.bytesLongFormat&&(e._bytesLongFormat=t.bytesLongFormat),void 0!==t.percentage&&(e._percentage=t.percentage),void 0!==t.percentageMultiply&&(e._percentageMultiply=t.percentageMultiply),void 0!==t.percentageAddSpace&&(e._percentageAddSpace=t.percentageAddSpace)}constructor(e){this._locale="en-US",this._decimals=2,this._abbreviated=!1,this._stripZeros=!1,this._roundingMode="HALF_UP",this._showSymbol=!0,this._showCode=!1,this._bytes=!1,this._bytesBinary=!0,this._bytesLongFormat=!1,this._percentage=!1,this._percentageMultiply=!0,this.value=e}static parse(e,t="en-US"){return unbeautify(e,{locale:t})}locale(e){return this._locale=e,this}decimals(e){return this._decimals=e,this}abbreviated(){return this._abbreviated=!0,this}stripZeros(){return this._stripZeros=!0,this}rounding(e){return this._roundingMode=e,this}mask(e){return this._mask=e,this}currency(e){return this._currency=e,this}hideSymbol(){return this._showSymbol=!1,this}showCode(){return this._showCode=!0,this}bytes(e=!0){return this._bytes=!0,this._bytesBinary=e,this}bytesLongFormat(){return this._bytesLongFormat=!0,this}percentage(e=!0){return this._percentage=!0,this._percentageMultiply=e,this}percentageSpace(e=!0){return this._percentageAddSpace=e,this}format(){if(this._currency)return formatCurrency(this.value,this._locale,{currency:this._currency,decimals:this._decimals,showSymbol:this._showSymbol,showCode:this._showCode,stripZeros:this._stripZeros});if(this._bytes)return formatBytes(this.value,{binary:this._bytesBinary,decimals:this._decimals,stripZeros:this._stripZeros,locale:this._locale,longFormat:this._bytesLongFormat});if(this._percentage)return formatPercentage(this.value,{multiply:this._percentageMultiply,decimals:this._decimals,stripZeros:this._stripZeros,locale:this._locale,addSpace:this._percentageAddSpace});if(this._mask){let e;return e=this._mask.includes("#")?this._mask:getMask(this._locale,this._mask)||this._mask,applyMask(this.value,e)}const e=round(this.value,this._decimals,this._roundingMode),t=formatNumber(e,this._decimals,this._locale,this._stripZeros);return this._abbreviated?abbreviateNumber(this.value,t,this._locale):t}toString(){return this.format()}valueOf(){return this.value}toAccessible(){const e=this.format();return toAccessibleString(e,{locale:this._locale})}toParts(){return beautifyToParts(this.value,{locale:this._locale,decimals:this._decimals,stripZeros:this._stripZeros,roundingMode:this._roundingMode,currency:this._currency,showSymbol:this._showSymbol,showCode:this._showCode,percentage:this._percentage,percentageMultiply:this._percentageMultiply,bytes:this._bytes,bytesBinary:this._bytesBinary,bytesLongFormat:this._bytesLongFormat,abbreviated:this._abbreviated})}}Num.appliedPlugins=new Set;export function num(e){return new Num(e)}