Created
September 17, 2021 12:46
-
-
Save lavary/559256c8a23a0a54fc8ed01e57e44615 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export const validateCC = function (value) { | |
return { | |
valid: validate(value), | |
errorMessage: 'O número do Cartão de Cidadão não é válido' | |
} | |
} | |
const CHAR = { | |
'0': 0, | |
'1': 1, | |
'2': 2, | |
'3': 3, | |
'4': 4, | |
'5': 5, | |
'6': 6, | |
'7': 7, | |
'8': 8, | |
'9': 9, | |
'A': 10, | |
'B': 11, | |
'C': 12, | |
'D': 13, | |
'E': 14, | |
'F': 15, | |
'G': 16, | |
'H': 17, | |
'I': 18, | |
'J': 19, | |
'K': 20, | |
'L': 21, | |
'M': 22, | |
'N': 23, | |
'O': 24, | |
'P': 25, | |
'Q': 26, | |
'R': 27, | |
'S': 28, | |
'T': 29, | |
'U': 30, | |
'V': 31, | |
'W': 32, | |
'X': 33, | |
'Y': 34, | |
'Z': 35, | |
}; | |
const CHARS = Object.keys(CHAR); | |
function calculateSum(value) { | |
var sum = 0; | |
for (var i = value.length - 1; i >= 0; i--) { | |
var d = CHAR[value[i]]; | |
if (d === undefined || (i < 9 && d > 9)) { | |
return false; | |
} | |
if (i % 2 === 0) { | |
d *= 2; | |
if (d > 9) { | |
d -= 9; | |
} | |
} | |
sum += d; | |
} | |
return sum % 10; | |
} | |
function validate(number) { | |
if (typeof number !== 'string' || number.length !== 12) { | |
return false; | |
} | |
return calculateSum(number) === 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment