-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related to issue #4 Signed-off-by: antoniocoj <[email protected]> Signed-off-by: Lucas Amoêdo <[email protected]>
- Loading branch information
1 parent
f858791
commit 3801639
Showing
3 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export const formatCPF = (cpf, prevCpf) => { | ||
if (!cpf) { | ||
return cpf; | ||
} | ||
|
||
const CPFNumbersOnly = cpf.replace(/[^\d]/g, ''); | ||
if (!prevCpf || cpf.length > prevCpf.length) { | ||
if (CPFNumbersOnly.length === 3) | ||
return `${CPFNumbersOnly}.`; | ||
if (CPFNumbersOnly.length === 6) | ||
return `${CPFNumbersOnly.slice(0,3)}.${CPFNumbersOnly.slice(3,6)}.`; | ||
if (CPFNumbersOnly.length === 9) | ||
return `${CPFNumbersOnly.slice(0,3)}.${CPFNumbersOnly.slice(3,6)}.${CPFNumbersOnly.slice(6,9)}-`; | ||
} | ||
if (CPFNumbersOnly.length <= 3) | ||
return `${CPFNumbersOnly}`; | ||
if (CPFNumbersOnly.length <= 6) | ||
return `${CPFNumbersOnly.slice(0,3)}.${CPFNumbersOnly.slice(3)}`; | ||
if (CPFNumbersOnly.length <= 9) | ||
return `${CPFNumbersOnly.slice(0,3)}.${CPFNumbersOnly.slice(3,6)}.${CPFNumbersOnly.slice(6)}`; | ||
|
||
return `${CPFNumbersOnly.slice(0,3)}.${CPFNumbersOnly.slice(3,6)}.${CPFNumbersOnly.slice(6,9)}-${CPFNumbersOnly.slice(9,11)}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { formatCPF } from './formatCPF'; |