cpf generator validator class

PHOTO EMBED

Sat May 07 2022 14:18:13 GMT+0000 (UTC)

Saved by @Dyam #typescript

class CPF {
    cpfNumber: Array<number> = [];
    
    notAcepted: Array<string> = [
        "00000000000", "11111111111", "22222222222", "33333333333", "44444444444",
        "55555555555", "66666666666", "77777777777", "88888888888", "99999999999"
    ];

    generate(): string {
        let number = [];
        for (let i = 0; i < 9; i++) {
            number.push(Math.floor(Math.random() * 10));
        }
        number.push(this.claculateFirstDigit(number));
        number.push(this.claculateSecondDigit(number));
        return this.maskCPF(number);
    }
    maskCPF(values: Array<number>): string {
        let cpf = "";
        for (let item of values) {
            cpf += item;
        }
        cpf = cpf.replace(/[^\d]/g, '').replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, '$1.$2.$3-$4');
        return cpf;
    }
    clean(values: string) {
        values = values.replace(/[^\d]/g, '');
        for (let item of values) {
            this.cpfNumber.push(parseInt(item));
        }
    }
    claculateFirstDigit(values: Array<number>): number {
        let sum = 0;
        for (let i = 0; i < 9; i++) {
            sum += values[i] * (10 - i);
        }
        let rest = (sum * 10) % 11;
        if (rest === 10 || rest === 11) {
            rest = 0;
        }
        return rest;
    }
    claculateSecondDigit(values: Array<number>): number {
        let sum = 0;
        for (let i = 0; i < 10; i++) {
            sum += values[i] * (11 - i);
        }
        let rest = (sum * 10) % 11;
        if (rest === 10 || rest === 11) {
            rest = 0;
        }
        return rest;
    }
    isValid(values: string): boolean {
        this.clean(values);
        return this.isAcepted(values) && this.length(this.cpfNumber) 
        && this.validate(this.cpfNumber);
    }
    isAcepted(values: string): boolean {
        let cpf = values.replace(/[^\d]/g, '');
        return !this.notAcepted.includes(cpf);
    }
    length(values: Array<number>): boolean {
        return values.length === 11;
    }
    validate(values: Array<number>): boolean {
        return this.claculateFirstDigit(values) === this.cpfNumber[9] 
        && this.claculateSecondDigit(values) === this.cpfNumber[10];
    }
    origin(values: string): string {
        this.clean(values);
        switch (this.cpfNumber[8]) {
            case 0:
                return "Rio Grande do Sul";
            case 1:
                return "Distrito Federal, Goiás, Mato Grosso do Sul e Tocantins";
            case 2:
                return "Pará, Amazonas, Acre, Amapá, Rondônia e Roraima";
            case 3:
                return "Ceará, Maranhão e Piauí";
            case 4:
                return "Pernambuco, Rio Grande do Norte, Paraíba e Alagoas";
            case 5:
                return "Bahia e Sergipe";
            case 6:
                return "Minas Gerais";
            case 7:
                return "Rio de Janeiro e Espírito Santo";
            case 8:
                return "São Paulo";
            case 9:
                return "Paraná e Santa Catarina";
            default:
                return "";
        }
    }
}

var cpf = new CPF();
let mockCpf = cpf.generate();      // gera um cpf mock.
let valid = cpf.isValid(mockCpf);  // verifica se o cpf é valido.
let origem = cpf.origin(mockCpf);  // verifica pelo digito de onde pertence.
console.log(mockCpf);              // mostra um o cpf gerado em formato ###.###.###-##
content_copyCOPY