Skip to content

Commit fe7d04d

Browse files
weillercarvalhotiagoporto
authored andcommitted
fix(cnpj): avoid zero-only cnpj generation
Merge #133
1 parent 117ee3f commit fe7d04d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

packages/gerador-validador-cnpj/src/generate.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,17 @@ describe('generate cnpj', () => {
6666
expect(cnpj).toBe(expected)
6767
expect(cnpj).toBeTruthy()
6868
})
69+
70+
it('should not return a cnpj with all digits equal to 0', () => {
71+
expect.assertions(1)
72+
73+
mockRandom([
74+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // first attempt -> 000000000000
75+
0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0, 0.1, 0.2, // retry -> 123456789012
76+
])
77+
78+
const cnpj = generate()
79+
80+
expect(cnpj).toBe('12345678901230')
81+
})
6982
})

packages/gerador-validador-cnpj/src/generate.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export const generate = ({
1818
format?: boolean
1919
alphanumeric?: boolean
2020
} = {}): string => {
21-
const firstTwelveDigits = generateCharacters(alphanumeric)
21+
let firstTwelveDigits = ''
22+
23+
do {
24+
firstTwelveDigits = generateCharacters(alphanumeric)
25+
} while (firstTwelveDigits === '000000000000')
2226

2327
const checkDigits = calcCheckDigits(firstTwelveDigits)
2428
const generatedCNPJ = `${firstTwelveDigits}${checkDigits}`

0 commit comments

Comments
 (0)