@qiwi/common-formatters

Common string formatters
Motivation
There's no such thing as universal formatters collection, but ... Ha, and this one library is just a set of several controversial workarounds.
What can be said in defense? Smaller is better.
Usage
import {formatMoney, formatNumber, formatCardPan, formatPhone, formatPercent} from '@qiwi/common-formatters'
Money
formatMoney(12345.6789)
formatMoney(12300.45, {currencyCode: 'RUB', fractionDelimiter: '.'})
formatMoney(123.45, {currencySymbol: 'Foo'})
{
strict?: boolean
digitDelimiter?: string
fractionDelimiter?: string
fractionLength?: number
currencyCode?: string
currencySymbol?: string
currencyPosition?: 'left' | 'right'
currencySpacer?: string
sign?: boolean
fractionRemoveZeros?: boolean
}
Numbers
formatNumber(12345.6789)
formatNumber(12345.6789, {digitDelimiter: ',', fractionDelimiter: '.'})
{
digitDelimiter: string;
fractionDelimiter: string;
fractionLength?: number;
strict: boolean;
sign: boolean;
}
Percents
formatPercent(0.123)
formatPercent(567, {sign: true, fractionLength: 0})
{
strict: boolean;
digitDelimiter: string;
fractionDelimiter: string;
fractionLength: number;
multiplier: number;
sign: boolean;
}
Phone numbers
formatPhone('1234567')
formatPhone('12345678')
formatPhone('12345')
formatPhone('1234567890')
formatPhone('79876543210', {mask: '+* *** ***-**-**'})
formatPhone('223344', {countryCode: '7', areaCode: '8443', areaBrackets: true, phoneNumberDelimiter: '_'})
{
blocksDelimiter: string;
countryCode: string;
areaCode: string;
areaBrackets: boolean;
areaCodeLength: number;
countryCodePrefix: string;
countryCodeLength: number;
phoneNumberLength: number;
phoneNumberDelimiter: string;
mask: string;
}
Card PAN
formatCardPan('1234567812345678', {digitDelimiter: '-'})
License
MIT