import { IntPretty , IntPrettyOptions } from "./int-pretty" ; import { Dec } from "./decimal" ; import { FiatCurrency } from "@keplr-wallet/types" ; import { DeepReadonly } from "utility-types" ; import { DecUtils } from "./dec-utils" ; import bigInteger from "big-integer" ; export type PricePrettyOptions = { separator : string ; upperCase : boolean ; lowerCase : boolean ; locale : string ; } ; export class PricePretty { protected intPretty : IntPretty ; protected _options : PricePrettyOptions = { separator : "" , upperCase : false , lowerCase : false , locale : "en-US" , } ; constructor ( protected _fiatCurrency , protected amount ) { this . intPretty = new IntPretty ( amount ) . maxDecimals ( _fiatCurrency . maxDecimals ) . shrink ( true ) . trim ( true ) . locale ( false ) . inequalitySymbol ( true ) ; this . _options . locale = _fiatCurrency . locale ; } get options ( ) : DeepReadonly < Omit < IntPrettyOptions , "locale" > & PricePrettyOptions > { return { ... this . intPretty . options , ... this . _options , } ; } get symbol ( ) : string { return this . _fiatCurrency . symbol ; } get fiatCurrency ( ) : FiatCurrency { return this . _fiatCurrency ; } separator ( str ) { const pretty = this . clone ( ) ; pretty . _options . separator = str ; return pretty ; } upperCase ( bool ) { const pretty = this . clone ( ) ; pretty . _options . upperCase = bool ; pretty . _options . lowerCase = ! bool ; return pretty ; } lowerCase ( bool ) { const pretty = this . clone ( ) ; pretty . _options . lowerCase = bool ; pretty . _options . upperCase = ! bool ; return pretty ; } moveDecimalPointLeft ( delta ) { const pretty = this . clone ( ) ; pretty . intPretty = pretty . intPretty . moveDecimalPointLeft ( delta ) ; return pretty ; } moveDecimalPointRight ( delta ) { const pretty = this . clone ( ) ; pretty . intPretty = pretty . intPretty . moveDecimalPointRight ( delta ) ; return pretty ; } deprecated increasePrecision ( delta ) { return this . moveDecimalPointLeft ( delta ) ; } deprecated decreasePrecision ( delta ) { return this . moveDecimalPointRight ( delta ) ; } maxDecimals ( max ) { const pretty = this . clone ( ) ; pretty . intPretty = pretty . intPretty . maxDecimals ( max ) ; return pretty ; } inequalitySymbol ( bool ) { const pretty = this . clone ( ) ; pretty . intPretty = pretty . intPretty . inequalitySymbol ( bool ) ; return pretty ; } inequalitySymbolSeparator ( str ) { const pretty = this . clone ( ) ; pretty . intPretty = pretty . intPretty . inequalitySymbolSeparator ( str ) ; return pretty ; } trim ( bool ) { const pretty = this . clone ( ) ; pretty . intPretty = pretty . intPretty . trim ( bool ) ; return pretty ; } shrink ( bool ) { const pretty = this . clone ( ) ; pretty . intPretty = pretty . intPretty . shrink ( bool ) ; return pretty ; } locale ( locale ) { const pretty = this . clone ( ) ; pretty . _options . locale = locale ; return pretty ; } param bool ready ( bool ) { const pretty = this . clone ( ) ; pretty . intPretty = pretty . intPretty . ready ( bool ) ; return pretty ; } get isReady ( ) : boolean { return this . intPretty . isReady ; } add ( target ) { const pretty = this . clone ( ) ; pretty . intPretty = pretty . intPretty . add ( target ) ; return pretty ; } sub ( target ) { const pretty = this . clone ( ) ; pretty . intPretty = pretty . intPretty . sub ( target ) ; return pretty ; } mul ( target ) { const pretty = this . clone ( ) ; pretty . intPretty = pretty . intPretty . mul ( target ) ; return pretty ; } quo ( target ) { const pretty = this . clone ( ) ; pretty . intPretty = pretty . intPretty . quo ( target ) ; return pretty ; } toDec ( ) { return this . intPretty . toDec ( ) ; } toString ( ) { let symbol = this . symbol ; if ( this . _options . upperCase ) { symbol = symbol . toUpperCase ( ) ; } if ( this . _options . lowerCase ) { symbol = symbol . toLowerCase ( ) ; } const dec = this . toDec ( ) ; const options = this . options ; if ( options . inequalitySymbol && ! dec . isZero ( ) && dec . abs ( ) . lt ( DecUtils . getTenExponentN ( - options . maxDecimals ) ) ) { return this . intPretty . toStringWithSymbols ( `${ symbol }${ this . _options . separator }` , "" ) ; } let localeString = parseFloat ( this . intPretty . toString ( ) ) . toLocaleString ( options . locale , { maximumFractionDigits : options . maxDecimals , } ) ; const isNeg = localeString . charAt ( 0 ) === "-" ; if ( isNeg ) { localeString = localeString . slice ( 1 ) ; } return `${ isNeg ? "-" : "" }${ symbol }${ this . _options . separator }${ localeString }` ; } clone ( ) { const pretty = new PricePretty ( this . _fiatCurrency , this . amount ) ; pretty . _options = { ... this . _options , } ; pretty . intPretty = this . intPretty . clone ( ) ; return pretty ; } }