const isCurrencyWithDecimals = ( currency ) => { return currency === "USD" ; } ; export const textToCurrency = ( value , currency , separator = "." ) : string => { if ( isCurrencyWithDecimals ( currency ) ) { const digits = getDigitsFromValue ( value ) ; return addDecimalToNumber ( digits , separator ) ; } else { return value ; } } ; export const currencyToTextWithUnits = ( moneyAmount ) : string => { if ( moneyAmount . currency === "BTC" ) { if ( moneyAmount . value === 1 ) { return "1 sat" ; } return currencyToText ( moneyAmount . value . toString ( ) , moneyAmount . currency ) + " sats" ; } if ( moneyAmount . currency === "USD" ) { return "$" + currencyToText ( moneyAmount . value . toString ( ) , moneyAmount . currency ) ; } throw Error ( "wrong currency" ) ; } ; export const currencyToText = ( value , currency , locale = "en-US" ) : string => isCurrencyWithDecimals ( currency ) ? Number ( value ) . toLocaleString ( locale , { style : "decimal" , maximumFractionDigits : 2 , minimumFractionDigits : 2 , } ) : Number ( value ) . toLocaleString ( locale , { style : "decimal" , maximumFractionDigits : 0 , minimumFractionDigits : 0 , } ) ; const getDigitsFromValue = ( value = "" ) => value . replace ( /(-(?!\d))|[^0-9|-]/g , "" ) || "" ; const removeLeadingZeros = ( number ) => number . replace ( /^0+([0-9]+)/ , "$1" ) ; const addDecimalToNumber = ( number , separator ) => { const fractionsStartingPosition = number . length - 2 ; const integerDigits = removeLeadingZeros ( number . substring ( 0 , fractionsStartingPosition ) ) ; const fractionDigits = number . substring ( fractionsStartingPosition ) ; return integerDigits + separator + fractionDigits ; } ;