-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.js
More file actions
126 lines (99 loc) · 2.68 KB
/
document.js
File metadata and controls
126 lines (99 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const formatCPF = (value) => {
if (!value) return 'Não informado';
const cpf = String(value);
if (!cpf) return '';
return cpf.toString().replace(/(\d{3})(\d{3})(\d{3})(\d{2})/g, '$1.$2.$3-$4');
};
const formatCNPJ = (value) => {
if (!value) return 'Não informado';
const cnpj = String(value).slice(0, 14);
if (!cnpj) return '';
return cnpj.replace(/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/g, '$1.$2.$3/$4-$5');
};
const formatDocument = (value) => {
if (!value) return 'Não informado';
const document = String(value);
if (!document) return '';
if (document.length < 14) {
return formatCPF(document);
}
return formatCNPJ(document);
};
const isCPFValid = (value) => {
const cpf = String(value).replace(/\D/g, '');
if (cpf.length !== 11 || /^(\d)\1{10}$/.test(cpf)) {
return false;
}
let soma = 0;
let resto;
let i = 1;
for (const char of cpf) {
if (i > 9) break;
soma += parseInt(char) * (11 - i);
i++;
}
resto = (soma * 10) % 11;
if (resto === 10 || resto === 11) {
resto = 0;
}
if (resto !== parseInt(cpf.charAt(9))) {
return false;
}
soma = 0;
i = 1;
for (const char of cpf) {
if (i > 10) break;
soma += parseInt(char) * (12 - i);
i++;
}
resto = (soma * 10) % 11;
if (resto === 10 || resto === 11) {
resto = 0;
}
if (resto !== parseInt(cpf.charAt(10))) {
return false;
}
return true;
};
const isCNPJValid = (value) => {
const cnpj = String(value).replace(/\D/g, '');
if (cnpj.length !== 14 || /^(\d)\1{13}$/.test(cnpj)) {
return false; // Verifica se o CNPJ possui 14 dígitos ou se todos os dígitos são iguais
}
const calcularDigitoVerificador = (digitos, pesoInicial) => {
let soma = 0;
let peso = pesoInicial;
for (const digito of digitos) {
soma += parseInt(digito) * peso;
peso = peso === 2 ? 9 : peso - 1;
}
const resto = soma % 11;
const digitoVerificador = resto < 2 ? 0 : 11 - resto;
return digitoVerificador;
};
const digitos = cnpj.slice(0, -2);
const digitoVerificador1 = calcularDigitoVerificador(digitos, 5);
const digitoVerificador2 = calcularDigitoVerificador(
digitos + digitoVerificador1,
6
);
return (
parseInt(cnpj.charAt(12)) === digitoVerificador1 &&
parseInt(cnpj.charAt(13)) === digitoVerificador2
);
};
const isDocumentValid = (value) => {
const document = String(value).replace(/\D/g, '');
if (!document) return '';
if (document.length === 14 && isCNPJValid(document)) return true;
if (document.length === 11 && isCPFValid(document)) return true;
return false;
};
module.exports = {
formatCPF,
formatCNPJ,
formatDocument,
isCPFValid,
isCNPJValid,
isDocumentValid,
};