cpf_and_cnpj-generator
Version:
Este repositório contém um código JavaScript que gera um CPF (Cadastro de Pessoas Físicas) e CNPJ (Cadastro Nacional da Pessoa Jurídica) brasileiro válido. CPF é um número de identificação exclusivo atribuído a indivíduos no Brasil. O código garante que o
44 lines (43 loc) • 2.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("./constants");
function generateValidCNPJ() {
for (let index = 0; index < 12; index++) {
constants_1.uintArray14[index] = Math.trunc(Math.random() * 36);
}
let sum = 0;
let weight = 2;
for (let index = 11; index >= 0; index--) {
const value = constants_1.uintArray14[index];
const normalizedValue = value < 10 ? value : value + 7;
sum += normalizedValue * weight;
weight = weight === 9 ? 2 : weight + 1;
}
const firstRemainder = sum % 11;
constants_1.uintArray14[12] = firstRemainder < 2 ? 0 : 11 - firstRemainder;
sum = 0;
weight = 2;
for (let index = 12; index >= 0; index--) {
const value = constants_1.uintArray14[index];
const normalizedValue = value < 10 ? value : value + 7;
sum += normalizedValue * weight;
weight = weight === 9 ? 2 : weight + 1;
}
const secondRemainder = sum % 11;
constants_1.uintArray14[13] = secondRemainder < 2 ? 0 : 11 - secondRemainder;
return (constants_1.AlphaChars09AZ[constants_1.uintArray14[0]] +
constants_1.AlphaChars09AZ[constants_1.uintArray14[1]] +
constants_1.AlphaChars09AZ[constants_1.uintArray14[2]] +
constants_1.AlphaChars09AZ[constants_1.uintArray14[3]] +
constants_1.AlphaChars09AZ[constants_1.uintArray14[4]] +
constants_1.AlphaChars09AZ[constants_1.uintArray14[5]] +
constants_1.AlphaChars09AZ[constants_1.uintArray14[6]] +
constants_1.AlphaChars09AZ[constants_1.uintArray14[7]] +
constants_1.AlphaChars09AZ[constants_1.uintArray14[8]] +
constants_1.AlphaChars09AZ[constants_1.uintArray14[9]] +
constants_1.AlphaChars09AZ[constants_1.uintArray14[10]] +
constants_1.AlphaChars09AZ[constants_1.uintArray14[11]] +
constants_1.AlphaChars09AZ[constants_1.uintArray14[12]] +
constants_1.AlphaChars09AZ[constants_1.uintArray14[13]]);
}
exports.default = generateValidCNPJ;