ngx-sp-infra 6.5.23 → 6.5.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -12032,27 +12032,84 @@ class CpfCnpjValidator {
|
|
|
12032
12032
|
/**
|
|
12033
12033
|
* Valida um CPF ou CNPJ de acordo com seu dígito verificador.
|
|
12034
12034
|
*/
|
|
12035
|
+
// static validate(c: AbstractControl): ValidationErrors | null {
|
|
12036
|
+
// let cpfCnpj: any = "";
|
|
12037
|
+
// if (c.value) cpfCnpj = c.value.replace(/\D/g, '');
|
|
12038
|
+
// if (cpfCnpj === '') {
|
|
12039
|
+
// return null;
|
|
12040
|
+
// }
|
|
12041
|
+
// // Verifica o tamanho da string.
|
|
12042
|
+
// if ([CpfCnpjValidator.cpfLength, CpfCnpjValidator.cnpjLength].indexOf(cpfCnpj.length) < 0) {
|
|
12043
|
+
// return { cpcnpjInvalid: true };
|
|
12044
|
+
// }
|
|
12045
|
+
// // Verifica se todos os dígitos são iguais, exceto para CPF com dígitos zerados.
|
|
12046
|
+
// if (/^([0-9])\1*$/.test(cpfCnpj) && cpfCnpj !== '00000000000') {
|
|
12047
|
+
// return { cpcnpjInvalid: true };
|
|
12048
|
+
// }
|
|
12049
|
+
// // A seguir é realizado o cálculo verificador.
|
|
12050
|
+
// const cpfCnpjArr: number[] = cpfCnpj.split('').reverse().slice(2);
|
|
12051
|
+
// cpfCnpjArr.unshift(CpfCnpjValidator.buildDigit(cpfCnpjArr));
|
|
12052
|
+
// cpfCnpjArr.unshift(CpfCnpjValidator.buildDigit(cpfCnpjArr));
|
|
12053
|
+
// if (cpfCnpj !== cpfCnpjArr.reverse().join('')) {
|
|
12054
|
+
// // Dígito verificador não é válido, resultando em falha.
|
|
12055
|
+
// return { cpcnpjInvalidDigit: true };
|
|
12056
|
+
// }
|
|
12057
|
+
// return null;
|
|
12058
|
+
// }
|
|
12035
12059
|
static validate(c) {
|
|
12036
|
-
let
|
|
12037
|
-
if (
|
|
12038
|
-
cpfCnpj = c.value.replace(/\D/g, '');
|
|
12039
|
-
if (cpfCnpj === '') {
|
|
12060
|
+
let valor = (c.value || "").toUpperCase().replace(/[^A-Z0-9]/g, "");
|
|
12061
|
+
if (!valor)
|
|
12040
12062
|
return null;
|
|
12041
|
-
|
|
12042
|
-
|
|
12043
|
-
if (
|
|
12063
|
+
const isCPF = valor.length === 11;
|
|
12064
|
+
const isCNPJ = valor.length === 14;
|
|
12065
|
+
if (!isCPF && !isCNPJ) {
|
|
12044
12066
|
return { cpcnpjInvalid: true };
|
|
12045
12067
|
}
|
|
12046
|
-
//
|
|
12047
|
-
if (
|
|
12068
|
+
// CPF ===========================================
|
|
12069
|
+
if (isCPF) {
|
|
12070
|
+
if (/^(\d)\1+$/.test(valor))
|
|
12071
|
+
return { cpcnpjInvalid: true };
|
|
12072
|
+
const calcDV = (base) => {
|
|
12073
|
+
let soma = 0;
|
|
12074
|
+
for (let i = 0; i < base.length; i++) {
|
|
12075
|
+
soma += parseInt(base[i], 10) * (base.length + 1 - i);
|
|
12076
|
+
}
|
|
12077
|
+
const resto = soma % 11;
|
|
12078
|
+
return resto < 2 ? 0 : 11 - resto;
|
|
12079
|
+
};
|
|
12080
|
+
const dv1 = calcDV(valor.substring(0, 9));
|
|
12081
|
+
const dv2 = calcDV(valor.substring(0, 9) + dv1);
|
|
12082
|
+
if (valor !== valor.substring(0, 9) + dv1.toString() + dv2.toString()) {
|
|
12083
|
+
return { cpcnpjInvalidDigit: true };
|
|
12084
|
+
}
|
|
12085
|
+
return null;
|
|
12086
|
+
}
|
|
12087
|
+
// CNPJ ALFANUMÉRICO ===========================================
|
|
12088
|
+
// Rejeita todos iguais
|
|
12089
|
+
if (/^([A-Z0-9])\1*$/.test(valor)) {
|
|
12048
12090
|
return { cpcnpjInvalid: true };
|
|
12049
12091
|
}
|
|
12050
|
-
//
|
|
12051
|
-
const
|
|
12052
|
-
|
|
12053
|
-
|
|
12054
|
-
|
|
12055
|
-
|
|
12092
|
+
// Converte as letras para numeros
|
|
12093
|
+
const valores = valor.split('').map((c) => {
|
|
12094
|
+
return c.charCodeAt(0) - 48;
|
|
12095
|
+
});
|
|
12096
|
+
const peso1 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
12097
|
+
const peso2 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
12098
|
+
// DV1
|
|
12099
|
+
let soma1 = 0;
|
|
12100
|
+
for (let i = 0; i < 12; i++)
|
|
12101
|
+
soma1 += valores[i] * peso1[i];
|
|
12102
|
+
const resto1 = soma1 % 11;
|
|
12103
|
+
const dv1 = resto1 < 2 ? 0 : 11 - resto1;
|
|
12104
|
+
// DV2
|
|
12105
|
+
let soma2 = 0;
|
|
12106
|
+
for (let i = 0; i < 12; i++)
|
|
12107
|
+
soma2 += valores[i] * peso2[i];
|
|
12108
|
+
soma2 += dv1 * peso2[12];
|
|
12109
|
+
const resto2 = soma2 % 11;
|
|
12110
|
+
const dv2 = resto2 < 2 ? 0 : 11 - resto2;
|
|
12111
|
+
// Verifica os DVs
|
|
12112
|
+
if (valores[12] !== dv1 || valores[13] !== dv2) {
|
|
12056
12113
|
return { cpcnpjInvalidDigit: true };
|
|
12057
12114
|
}
|
|
12058
12115
|
return null;
|