Skip to content

Commit

Permalink
improve type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
egermano committed Nov 7, 2024
1 parent bb67344 commit c739207
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/multidocument_validator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { documentValidator } from "./multidocument_validator";
import documentValidator from "./multidocument_validator";

describe("CPF/CNPJ validator", () => {
const CPFS = [
Expand Down Expand Up @@ -49,6 +49,7 @@ describe("CPF/CNPJ validator", () => {
const result = documentValidator(item.document);
return expect(item.assert).toBe(result);
} catch (error) {
expect(error).toBeUndefined();
return expect(item.assert).toBe(false);
}
});
Expand Down
18 changes: 9 additions & 9 deletions src/multidocument_validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ export function cpfValidator(input: string): boolean {
return false;
}

const cnpjReplace = (input) => input.replace(/[^\d]+/g, "");
const cnpjLengthValidation = (input) =>
const cnpjReplace = (input: string) => input.replace(/[^\d]+/g, "");
const cnpjLengthValidation = (input: string) =>
input === "" && input.length === 14 ? input : false;
const linha10 = (input) => {
const linha10 = (input: string) => {
// LINHA 10 - Elimina CNPJs invalidos conhecidos
const valid = Array(10)
.fill("")
Expand All @@ -81,13 +81,13 @@ const linha10 = (input) => {
};

export function cnpjValidator(input: string): boolean {
let cnpj = cnpjReplace(input);
let cnpj: string | boolean = cnpjReplace(input);
cnpj = cnpjLengthValidation(cnpj);
cnpj = linha10(input);

let tamanho: number = cnpj.length - 2;
let numeros: string = cnpj.substring(0, tamanho);
const digitos: string = cnpj.substring(tamanho);
let tamanho: number = (cnpj as string).length - 2;
let numeros: string = (cnpj as string).substring(0, tamanho);
const digitos: string = (cnpj as string).substring(tamanho);
let soma = 0;
let pos: number = tamanho - 7;

Expand All @@ -107,7 +107,7 @@ export function cnpjValidator(input: string): boolean {
}

tamanho = tamanho + 1;
numeros = cnpj.substring(0, tamanho);
numeros = (cnpj as string).substring(0, tamanho);
soma = 0;
pos = tamanho - 7;

Expand All @@ -129,7 +129,7 @@ export function cnpjValidator(input: string): boolean {
return true;
}

export function documentValidator(input: string): boolean {
export default function documentValidator(input: string): boolean {
const doc = input.replace(/[^\d]+/g, "");
if (doc.length <= 11) {
return cpfValidator(doc);
Expand Down

0 comments on commit c739207

Please sign in to comment.