gerador e verificador de cpf

PHOTO EMBED

Sat May 07 2022 10:52:01 GMT+0000 (UTC)

Saved by @Dyam #typescript

const Algorithm = {
    clean: (number) => {
        if (Array.isArray(number)) {
            return number;
        }
        let array = [];
        number = number.replace(/[^\d]/g, '');

        for (let item of number) {
            array.push(parseInt(item));
        }
        return array;
    },
    notAcepted: [
        "00000000000", "11111111111", "22222222222", "33333333333", "44444444444",
        "55555555555", "66666666666", "77777777777", "88888888888", "99999999999"
    ],
    length: (number) => {
        return number.length === 11;
    },
    claculateFirstDigit: (number) => {
        let sum = 0;
        for (let i = 0; i < 9; i++) {
            sum += parseInt(number[i]) * (10 - i);
        }
        let rest = (sum * 10) % 11;
        if (rest === 10 || rest === 11) {
            rest = 0;
        }
        return rest;
    },
    claculateSecondDigit: (number) => {
        let sum = 0;
        for (let i = 0; i < 10; i++) {
            sum += parseInt(number[i]) * (11 - i);
        }
        let rest = (sum * 10) % 11;
        if (rest === 10 || rest === 11) {
            rest = 0;
        }
        return rest;
    },
    validate: (number) => {
        return Algorithm.claculateFirstDigit(number) == number[9] && Algorithm.claculateSecondDigit(number) == number[10];
    },
    isAcepted: (number) => {
        return Algorithm.notAcepted.indexOf(number) === -1;
    },
    isValid: (number) => {
        number = Algorithm.clean(number);
        return Algorithm.isAcepted(number) && Algorithm.length(number) && Algorithm.validate(number);
    },
    generate: () => {
        let number = [];
        for (let i = 0; i < 9; i++) {
            number.push(Math.floor(Math.random() * 10));
        }
        number.push(Algorithm.claculateFirstDigit(number));
        number.push(Algorithm.claculateSecondDigit(number));
        return Algorithm.maskCPF(number);
    },
    maskCPF: (number) => {
        number = Algorithm.clean(number);
        return number.slice(0, 3).join("") + "." + number.slice(3, 6).join("") + "." + number.slice(6, 9).join("") + "-" + number.slice(9, 11).join("");
    },
    origem: (number) => {
        let origem = {
            0: "Rio Grande do Sul",
            1: "Distrito Federal, Goiás, Mato Grosso do Sul e Tocantins",
            2: "Pará, Amazonas, Acre, Amapá, Rondônia e Roraima",
            3: "Ceará, Maranhão e Piauí",
            4: "Pernambuco, Rio Grande do Norte, Paraíba e Alagoas",
            5: "Bahia e Sergipe",
            6: "Minas Gerais",
            7: "Rio de Janeiro e Espírito Santo",
            8: "São Paulo",
            9: "Paraná e Santa Catarina",
        };
        return origem[number[8]];
    }
};
content_copyCOPY