/**
 * Regex to detect the format.
 */ const FORMAT_REGEX = /(i([^id]+))?i+(([^id?]+)(d+)(\?)?)?/ ; /**
 * Configuration for format currency.
 */ interface CurrencyConfig { /** The character between a group of three integer 'i's and the following one. */ integerSeparator : string ; /** The character between a group of three integer 'i's and the following one. It also
     * supports more than one single character. */ decimalSeparator : string ; /** Length of decimals numbers. It counts the number of 'd's after the integer part. */ decimalsNumber : number ; /** Boolean value to hide or show the decimal part when it has 0. */ hideIntegerDecimals : boolean ; } /**
 * Parts of number: integer and decimal.
 */ interface NumberParts { /** Integer part of the number as string. */ integer : string ; /** Decimal part of the number as string. */ decimal : string ; } param value param format remarks returns public export function currencyFormatter ( value , format = 'i.iii,dd' ) { const { integer , decimal } = numberParts ( value ) ; const { decimalSeparator , decimalsNumber , integerSeparator , hideIntegerDecimals } = currencyConfig ( format ) ; const formattedInteger = formatInteger ( integer , integerSeparator ) ; const formattedDecimal = formatDecimal ( decimal , { decimalsNumber , hideIntegerDecimals , decimalSeparator } ) ; return format . replace ( FORMAT_REGEX , `${ formattedInteger }${ formattedDecimal }` ) ; } param integer param integerSeparator returns internal function formatInteger ( integer , integerSeparator ) { return integer . replace ( /\B(?=(\d{3})+(?!\d))/g , integerSeparator ) ; } param decimal param CurrencyConfig returns internal function formatDecimal ( decimal , { decimalsNumber , decimalSeparator , hideIntegerDecimals } ) { return hideIntegerDecimals && ! + decimal ? '' : `${ decimalSeparator }${ decimal . padEnd ( decimalsNumber , '0' ) . substring ( 0 , decimalsNumber ) }` ; } param format returns internal function currencyConfig ( format ) { const [ , , integerSeparator = '' , , decimalSeparator = '' , decimals = '' , hideIntegerDecimals = '' ] = FORMAT_REGEX . exec ( format ) ?? [ ] ; return { integerSeparator , decimalSeparator , decimalsNumber : decimals . length , hideIntegerDecimals : ! ! hideIntegerDecimals } ; } param value returns internal function numberParts ( value ) { const [ integer , decimal = '' ] = `${ value }` . split ( '.' ) ; return { integer , decimal } ; }