-
-
Save naider/e6e640a479573b0b04ef44389e7b1fb3 to your computer and use it in GitHub Desktop.
Validação de CNPJ
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
export function cnpjValidation(value) { | |
if (!value) return false | |
// Aceita receber o valor como string, número ou array com todos os dígitos | |
const validTypes = | |
typeof value === 'string' || Number.isInteger(value) || Array.isArray(value) | |
// Elimina valor em formato inválido | |
if (!validTypes) return false | |
// Limpa o valor passado, deixando somente os dígitos | |
const cleanNumbers = value.toString().match(/\d/g) | |
// Verifica se o valor passado é igual null | |
if(cleanNumbers === null) return false | |
// Guarda um array com todos os dígitos do valor | |
const numbers = cleanNumbers.map(Number) | |
// Valida a quantidade de dígitos | |
if (numbers.length !== 14) return false | |
// Elimina inválidos com todos os dígitos iguais | |
const items = [...new Set(numbers)] | |
if (items.length === 1) return false | |
// Cálculo validador | |
const calc = (x) => { | |
const slice = numbers.slice(0, x) | |
let factor = x - 7 | |
let sum = 0 | |
for (let i = x; i >= 1; i--) { | |
const n = slice[x - i] | |
sum += n * factor-- | |
if (factor < 2) factor = 9 | |
} | |
const result = 11 - (sum % 11) | |
return result > 9 ? 0 : result | |
} | |
// Separa os 2 últimos dígitos de verificadores | |
const digits = numbers.slice(12) | |
// Valida 1o. dígito verificador | |
const digit0 = calc(12) | |
if (digit0 !== digits[0]) return false | |
// Valida 2o. dígito verificador | |
const digit1 = calc(13) | |
return digit1 === digits[1] | |
} | |
// Referência: https://pt.wikipedia.org/wiki/Cadastro_Nacional_da_Pessoa_Jur%C3%ADdica |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment