From 009d9c058b2f12a1e3102ade770f063a354b13d9 Mon Sep 17 00:00:00 2001 From: Lindsay Ferreira Date: Mon, 23 Mar 2026 16:40:45 -0300 Subject: [PATCH 1/3] fix: corrects sanitization to accept the entire alphabet in alphanumeric CNPJ --- src/format-cnpj/format-cnpj.ts | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/format-cnpj/format-cnpj.ts b/src/format-cnpj/format-cnpj.ts index 270bafe..4f5f94b 100644 --- a/src/format-cnpj/format-cnpj.ts +++ b/src/format-cnpj/format-cnpj.ts @@ -4,25 +4,15 @@ import { sanitizeToDigits } from "../_internals/sanitize-to-digits/sanitize-to-d export type FormatCnpjOptions = Pick & { version?: 1 | 2 }; const sanitize = ( - value: string | number, - version?: FormatCnpjOptions["version"], + value: string | number, + version?: FormatCnpjOptions["version"], ) => { - if (version === 2) { - const allowedChars = "0123456789ABCDFGHIJKLMNPQRSVWXYZ"; - const enhancedValue = value.toString(); - - let result = ""; - - for (let i = 0; i < enhancedValue.length; i++) { - if (allowedChars.includes(enhancedValue[i].toUpperCase())) { - result += enhancedValue[i].toUpperCase(); - } - } - - return result; - } + if (version === 2) { + const enhancedValue = value.toString(); + return enhancedValue.replace(/[^A-Za-z0-9]/g, '').toUpperCase(); + } - return sanitizeToDigits(value); + return sanitizeToDigits(value); }; /** From 272ece0a7c3de61546ece0a86504eb0f5deff179 Mon Sep 17 00:00:00 2001 From: Lindsay Ferreira Date: Mon, 23 Mar 2026 16:52:52 -0300 Subject: [PATCH 2/3] fix: adds test cases using the official Receita Federal example (12.ABC.345/01DE-35) to prevent future regressions --- src/format-cnpj/format-cnpj.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/format-cnpj/format-cnpj.test.ts b/src/format-cnpj/format-cnpj.test.ts index ab15077..e6092b4 100644 --- a/src/format-cnpj/format-cnpj.test.ts +++ b/src/format-cnpj/format-cnpj.test.ts @@ -116,4 +116,16 @@ describe("formatCnpj", () => { "46.ABC.843/4850-00", ); }); + + it("should allow all alphabet letters for version 2 (including E, O, T, U)", () => { + expect(formatCnpj("12ABC34501DE35", { version: 2 })).toBe( + "12.ABC.345/01DE-35" + ); + expect(formatCnpj("12.ABC.345/01DE-35", { version: 2 })).toBe( + "12.ABC.345/01DE-35" + ); + expect(formatCnpj("12OUT345000199", { version: 2 })).toBe( + "12.OUT.345/0001-99" + ); + }); }); From 12d025030feb7972eed895a5b834a0d4025a6408 Mon Sep 17 00:00:00 2001 From: Lindsay Ferreira Date: Thu, 26 Mar 2026 16:19:31 -0300 Subject: [PATCH 3/3] style: format code with biome --- src/format-cnpj/format-cnpj.test.ts | 20 ++++++++++---------- src/format-cnpj/format-cnpj.ts | 14 +++++++------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/format-cnpj/format-cnpj.test.ts b/src/format-cnpj/format-cnpj.test.ts index e6092b4..faf9902 100644 --- a/src/format-cnpj/format-cnpj.test.ts +++ b/src/format-cnpj/format-cnpj.test.ts @@ -118,14 +118,14 @@ describe("formatCnpj", () => { }); it("should allow all alphabet letters for version 2 (including E, O, T, U)", () => { - expect(formatCnpj("12ABC34501DE35", { version: 2 })).toBe( - "12.ABC.345/01DE-35" - ); - expect(formatCnpj("12.ABC.345/01DE-35", { version: 2 })).toBe( - "12.ABC.345/01DE-35" - ); - expect(formatCnpj("12OUT345000199", { version: 2 })).toBe( - "12.OUT.345/0001-99" - ); - }); + expect(formatCnpj("12ABC34501DE35", { version: 2 })).toBe( + "12.ABC.345/01DE-35", + ); + expect(formatCnpj("12.ABC.345/01DE-35", { version: 2 })).toBe( + "12.ABC.345/01DE-35", + ); + expect(formatCnpj("12OUT345000199", { version: 2 })).toBe( + "12.OUT.345/0001-99", + ); + }); }); diff --git a/src/format-cnpj/format-cnpj.ts b/src/format-cnpj/format-cnpj.ts index 4f5f94b..d69f356 100644 --- a/src/format-cnpj/format-cnpj.ts +++ b/src/format-cnpj/format-cnpj.ts @@ -4,15 +4,15 @@ import { sanitizeToDigits } from "../_internals/sanitize-to-digits/sanitize-to-d export type FormatCnpjOptions = Pick & { version?: 1 | 2 }; const sanitize = ( - value: string | number, - version?: FormatCnpjOptions["version"], + value: string | number, + version?: FormatCnpjOptions["version"], ) => { - if (version === 2) { - const enhancedValue = value.toString(); - return enhancedValue.replace(/[^A-Za-z0-9]/g, '').toUpperCase(); - } + if (version === 2) { + const enhancedValue = value.toString(); + return enhancedValue.replace(/[^A-Za-z0-9]/g, "").toUpperCase(); + } - return sanitizeToDigits(value); + return sanitizeToDigits(value); }; /**