Skip to content

Commit 555ead5

Browse files
committed
refactor: convert CNPJ validation class to functional approach
1 parent bc1a833 commit 555ead5

1 file changed

Lines changed: 57 additions & 59 deletions

File tree

src/cnpjValidator/cnpjValidator2.ts

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-extraneous-class */
2-
31
import type { ValidateFunctions } from "../types";
42

53
const defaultErrorMsg: string[] = [
@@ -8,67 +6,67 @@ const defaultErrorMsg: string[] = [
86
"CNPJ is not valid",
97
];
108

11-
export class CNPJ {
12-
private static readonly tamanhoCNPJSemDV: number = 12;
13-
private static readonly regexCNPJSemDV: RegExp = /^([A-Z\d]){12}$/;
14-
private static readonly regexCNPJ: RegExp = /^([A-Z\d]){12}(\d){2}$/;
15-
private static readonly regexCaracteresMascara: RegExp = /[./-]/g;
16-
private static readonly regexCaracteresNaoPermitidos: RegExp = /[^A-Z\d./-]/i;
17-
private static readonly valorBase: number = "0".charCodeAt(0); // nosonar
18-
private static readonly pesosDV: number[] = [
19-
6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2,
20-
];
21-
private static readonly cnpjZerado: string = "00000000000000";
22-
public static isValid(cnpj: string): boolean {
23-
if (!this.regexCaracteresNaoPermitidos.test(cnpj)) {
24-
const cnpjSemMascara: string = this.removeMascaraCNPJ(cnpj);
25-
if (
26-
this.regexCNPJ.test(cnpjSemMascara) &&
27-
cnpjSemMascara !== CNPJ.cnpjZerado
28-
) {
29-
const dvInformado: string = cnpjSemMascara.substring(
30-
this.tamanhoCNPJSemDV,
31-
);
32-
const dvCalculado: string = this.calculaDV(
33-
cnpjSemMascara.substring(0, this.tamanhoCNPJSemDV),
34-
);
35-
return dvInformado === dvCalculado;
36-
}
9+
const tamanhoCNPJSemDV: number = 12;
10+
const regexCNPJSemDV: RegExp = /^([A-Z\d]){12}$/;
11+
const regexCNPJ: RegExp = /^([A-Z\d]){12}(\d){2}$/;
12+
const regexCaracteresMascara: RegExp = /[./-]/g;
13+
const regexCaracteresNaoPermitidos: RegExp = /[^A-Z\d./-]/i;
14+
const valorBase: number = "0".charCodeAt(0); // nosonar
15+
const pesosDV: number[] = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
16+
const cnpjZerado: string = "00000000000000";
17+
18+
function isValid(cnpj: string): boolean {
19+
if (!regexCaracteresNaoPermitidos.test(cnpj)) {
20+
const cnpjSemMascara: string = removeMascaraCNPJ(cnpj);
21+
22+
if (regexCNPJ.test(cnpjSemMascara) && cnpjSemMascara !== cnpjZerado) {
23+
const dvInformado: string = cnpjSemMascara.substring(tamanhoCNPJSemDV);
24+
25+
const dvCalculado: string = calculaDV(
26+
cnpjSemMascara.substring(0, tamanhoCNPJSemDV),
27+
);
28+
29+
return dvInformado === dvCalculado;
3730
}
38-
return false;
3931
}
40-
public static calculaDV(cnpj: string): string {
41-
if (!this.regexCaracteresNaoPermitidos.test(cnpj)) {
42-
const cnpjSemMascara: string = this.removeMascaraCNPJ(cnpj);
43-
if (
44-
this.regexCNPJSemDV.test(cnpjSemMascara) &&
45-
cnpjSemMascara !== this.cnpjZerado.substring(0, this.tamanhoCNPJSemDV)
46-
) {
47-
let somatorioDV1: number = 0;
48-
let somatorioDV2: number = 0;
49-
for (let i: number = 0; i < this.tamanhoCNPJSemDV; i++) {
50-
const asciiDigito: number =
51-
cnpjSemMascara.charCodeAt(i) - this.valorBase; // nosonar
52-
somatorioDV1 += asciiDigito * this.pesosDV[i + 1];
53-
somatorioDV2 += asciiDigito * this.pesosDV[i];
54-
}
55-
const dv1: number =
56-
somatorioDV1 % 11 < 2 ? 0 : 11 - (somatorioDV1 % 11);
57-
somatorioDV2 += dv1 * this.pesosDV[this.tamanhoCNPJSemDV];
58-
59-
const dv2: number =
60-
somatorioDV2 % 11 < 2 ? 0 : 11 - (somatorioDV2 % 11);
61-
62-
return `${String(dv1)}${String(dv2)}`;
32+
33+
return false;
34+
}
35+
36+
function calculaDV(cnpj: string): string {
37+
if (!regexCaracteresNaoPermitidos.test(cnpj)) {
38+
const cnpjSemMascara: string = removeMascaraCNPJ(cnpj);
39+
40+
if (
41+
regexCNPJSemDV.test(cnpjSemMascara) &&
42+
cnpjSemMascara !== cnpjZerado.substring(0, tamanhoCNPJSemDV)
43+
) {
44+
let somatorioDV1: number = 0;
45+
let somatorioDV2: number = 0;
46+
47+
for (let i: number = 0; i < tamanhoCNPJSemDV; i++) {
48+
const asciiDigito: number = cnpjSemMascara.charCodeAt(i) - valorBase; // nosonar
49+
50+
somatorioDV1 += asciiDigito * pesosDV[i + 1];
51+
somatorioDV2 += asciiDigito * pesosDV[i];
6352
}
53+
54+
const dv1: number = somatorioDV1 % 11 < 2 ? 0 : 11 - (somatorioDV1 % 11);
55+
somatorioDV2 += dv1 * pesosDV[tamanhoCNPJSemDV];
56+
57+
const dv2: number = somatorioDV2 % 11 < 2 ? 0 : 11 - (somatorioDV2 % 11);
58+
59+
return `${String(dv1)}${String(dv2)}`;
6460
}
65-
throw new Error(
66-
"Não é possível calcular o DV pois o CNPJ fornecido é inválido",
67-
);
68-
}
69-
private static removeMascaraCNPJ(cnpj: string): string {
70-
return cnpj.replace(this.regexCaracteresMascara, "");
7161
}
62+
63+
throw new Error(
64+
"Não é possível calcular o DV pois o CNPJ fornecido é inválido",
65+
);
66+
}
67+
68+
function removeMascaraCNPJ(cnpj: string): string {
69+
return cnpj.replace(regexCaracteresMascara, ""); // nosonar
7270
}
7371

7472
function cnpjIsValid(
@@ -111,7 +109,7 @@ function cnpjIsValid(
111109
};
112110
}
113111

114-
if (CNPJ.isValid(cnpj)) {
112+
if (isValid(cnpj)) {
115113
return {
116114
isValid: true,
117115
errorMsg: null,

0 commit comments

Comments
 (0)