Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/generate-cnpj/generate-cnpj.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,20 @@ describe("generateCnpj", () => {
expect(isValidCnpj(formatted, { version: 2 })).toBe(true);
}
});

test("should generate alphanumeric CNPJs spanning the full A-Z alphabet", () => {
const usedChars = new Set<string>();
for (let i = 0; i < 1000; i++) {
for (const char of generateCnpj(2)) {
usedChars.add(char);
}
}
// E, O, T and U were previously excluded by a restricted alphabet, even
// though isValidCnpj accepts them: the RFB alphanumeric CNPJ uses the full
// [A-Z0-9] base (official example "12.ABC.345/01DE-35" contains an "E").
for (const char of ["E", "O", "T", "U"]) {
expect(usedChars.has(char)).toBe(true);
}
});
});
});
2 changes: 1 addition & 1 deletion src/generate-cnpj/generate-cnpj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const FIRST_CHECK_DIGIT_WEIGHTS = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];

const SECOND_CHECK_DIGIT_WEIGHTS = [6, ...FIRST_CHECK_DIGIT_WEIGHTS];

const VALID_CNPJ_CHARS = "0123456789ABCDFGHIJKLMNPQRSVWXYZ";
const VALID_CNPJ_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

const generateRandomCnpjChar = (): string =>
VALID_CNPJ_CHARS[Math.floor(Math.random() * VALID_CNPJ_CHARS.length)];
Expand Down