1 | # LacusSoft :: cpf-fmt
|
2 |
|
3 | ![NPM Latest Version](https://img.shields.io/npm/v/@lacussoft/cpf-fmt)
|
4 | ![Downloads Count](https://img.shields.io/npm/dm/@lacussoft/cpf-fmt.svg)
|
5 | ![Bundle Size](https://packagephobia.now.sh/badge?p=@lacussoft/cpf-fmt)
|
6 | ![Test Status](https://img.shields.io/travis/juliolmuller/cpf-utils-js/main.svg)
|
7 | ![Last Update Date](https://img.shields.io/github/last-commit/juliolmuller/cpf-utils-js)
|
8 | ![Project License](https://img.shields.io/github/license/juliolmuller/cpf-utils-js)
|
9 |
|
10 | Basic function to format CPF strings (Brazilian ID document).
|
11 |
|
12 | ## Browser Support
|
13 |
|
14 | ![Chrome](https://raw.github.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png) | ![Firefox](https://raw.github.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png) | ![Safari](https://raw.github.com/alrra/browser-logos/master/src/safari/safari_48x48.png) | ![Opera](https://raw.github.com/alrra/browser-logos/master/src/opera/opera_48x48.png) | ![Edge](https://raw.github.com/alrra/browser-logos/master/src/edge/edge_48x48.png) | ![IE](https://raw.github.com/alrra/browser-logos/master/src/archive/internet-explorer_9-11/internet-explorer_9-11_48x48.png) |
|
15 | --- | --- | --- | --- | --- | --- |
|
16 | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |
|
17 |
|
18 | ## Installation
|
19 |
|
20 | ```bash
|
21 | $ npm install @lacussoft/cpf-fmt
|
22 | ```
|
23 |
|
24 | ## Import
|
25 |
|
26 | ```js
|
27 | // ES Modules
|
28 | import cpfFmt from '@lacussoft/cpf-fmt'
|
29 |
|
30 | // Common JS
|
31 | const cpfFmt = require('@lacussoft/cpf-fmt')
|
32 | ```
|
33 |
|
34 | or import it through your HTML file, using CDN:
|
35 |
|
36 | ```html
|
37 | <script src="https://cdn.jsdelivr.net/npm/@lacussoft/cpf-fmt@latest/dist/cpf-fmt.min.js"></script>
|
38 | ```
|
39 |
|
40 | ## Usage
|
41 |
|
42 | ```js
|
43 | const cpf = '47844241055'
|
44 |
|
45 | cpfFmt(cpf) // returns '478.442.410-55'
|
46 |
|
47 | cpfFmt(cpf, { // returns '478.***.***-**'
|
48 | hidden: true
|
49 | })
|
50 |
|
51 | cpfFmt(cpf, { // returns '478442410_55'
|
52 | delimiters: {
|
53 | dot: '',
|
54 | dash: '_'
|
55 | }
|
56 | })
|
57 | ```
|
58 |
|
59 | ### Formatting options
|
60 |
|
61 | ```js
|
62 | cpfFmt(cpf, {
|
63 | delimiters: {
|
64 | dot: '.', // string to replace the dot characters
|
65 | dash: '-', // string to replace the dash character
|
66 | },
|
67 | escape: false, // boolean to define if the result should be HTML escaped
|
68 | hidden: false, // boolean to define if digits should be hidden
|
69 | hiddenKey: '*', // string to replace hidden digits
|
70 | hiddenRange: {
|
71 | start: 3, // starting index of the numeric sequence to be hidden (min 0)
|
72 | end: 10, // ending index of the numeric sequence to be hidden (max 10)
|
73 | },
|
74 | onFail(value) { // fallback function to be invoked in case a non-11-digits is passed
|
75 | return value
|
76 | }
|
77 | })
|
78 | ```
|