1.2.3 • Published 9 months ago

@qiwi/common-formatters v1.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@qiwi/common-formatters

CI Maintainability Test Coverage npm (scoped)

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)   // '12 345,68'
    formatMoney(12300.45, {currencyCode: 'RUB', fractionDelimiter: '.'}) // '12 300.45 ₽'
    formatMoney(123.45, {currencySymbol: 'Foo'}) // '123,45 Foo'
    
    // Available opts
    {
      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)  //  '12 345,6789'
    formatNumber(12345.6789, {digitDelimiter: ',', fractionDelimiter: '.'}) // '12,345.6789'
    
    // Opts
    {
      digitDelimiter: string;
      fractionDelimiter: string;
      fractionLength?: number;
      strict: boolean;
      sign: boolean;
    }
Percents
        
    formatPercent(0.123)  //  '12,30%'
    formatPercent(567, {sign: true, fractionLength: 0}) // '56 700%'
    
    // Opts
    {
      strict: boolean;
      digitDelimiter: string;
      fractionDelimiter: string;
      fractionLength: number;
      multiplier: number;
      sign: boolean;
    }
Phone numbers
    // Basic cases
    formatPhone('1234567')     // 123-45-67
    formatPhone('12345678')    // 1234-5678
    formatPhone('12345')       // 1-23-45
    formatPhone('1234567890')  // 1234567890
    
    // Format by mask
    formatPhone('79876543210', {mask: '+* *** ***-**-**'}) // +7 987 654-32-10
    
    // Format by opts
    formatPhone('223344', {countryCode: '7', areaCode: '8443', areaBrackets: true, phoneNumberDelimiter: '_'}) // +7 (8443) 22_33_44

    // Opts
    {
        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: '-'}) // '1234-5678-1234-5678'

License

MIT

1.2.3

9 months ago

1.2.2

1 year ago

1.2.1

2 years ago

1.2.0

3 years ago

1.1.0

4 years ago

1.0.0

4 years ago

0.7.9

5 years ago

0.7.8

5 years ago

0.7.7

5 years ago

0.7.6

5 years ago

0.7.5

5 years ago

0.7.4

5 years ago

0.7.3

5 years ago

0.7.2

5 years ago

0.8.0

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.2

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.0

6 years ago

0.3.11

6 years ago

0.3.10

6 years ago

0.3.9

6 years ago

0.3.8

6 years ago

0.3.7

6 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago