Last active
November 29, 2020 23:58
-
-
Save douglas-pires/d49bc811afc393a7cc7f9cca115002af to your computer and use it in GitHub Desktop.
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
// baseado em https://gist.github.com/joaohcrangel/8bd48bcc40b9db63bef7201143303937 | |
export const cpf = (cpf: string) => { | |
const _cpf = cpf.replace(/[\s.-]*/g, ''); | |
let sum = 0; | |
let rest = 0; | |
if (_cpf.match(/([0-9])(\d\1{9})/)?.length) return false; | |
for (let i = 1; i <= 9; i += 1) { | |
sum += parseInt(_cpf.substring(i - 1, i), 10) * (11 - i); | |
} | |
rest = (sum * 10) % 11; | |
if (rest === 10 || rest === 11) rest = 0; | |
if (rest !== parseInt(_cpf.substring(9, 10), 10)) return false; | |
sum = 0; | |
for (let i = 1; i <= 10; i += 1) { | |
sum += parseInt(_cpf.substring(i - 1, i), 10) * (12 - i); | |
} | |
rest = (sum * 10) % 11; | |
if (rest === 10 || rest === 11) rest = 0; | |
if (rest !== parseInt(_cpf.substring(10, 11), 10)) return false; | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment