@@ -27,7 +27,6 @@ const defaultErrorMsg: string[] = [
2727 "CNPJ invalid" ,
2828 "CNPJ must have 14 numerical digits" ,
2929 "CNPJ is not valid" ,
30- "Unknown error" ,
3130] ;
3231
3332/**
@@ -39,7 +38,7 @@ const defaultErrorMsg: string[] = [
3938 * If you want to use a default parameter, use null or leave Empty.
4039 *
4140 * Default:
42- * ['CNPJ invalid', 'CNPJ must have 14 numerical digits', 'CNPJ is not valid', 'Unknown error' ]
41+ * ['CNPJ invalid', 'CNPJ must have 14 numerical digits', 'CNPJ is not valid']
4342 * .
4443 *
4544 * Create a list of errors separated by commas in strings
@@ -75,48 +74,39 @@ function cnpjIsValid(
7574 return errorMessage != null ? errorMessage : defaultErrorMsg [ index ] ;
7675 }
7776
78- try {
79- if ( ! cnpj ) {
80- return {
81- isValid : false ,
82- errorMsg : getErrorMessage ( 0 ) , // 'CNPJ invalid'
83- } ;
84- }
85- // Check if the CNPJ has 14 digits
86- if ( cnpj . length !== 14 && cnpj . length !== 18 ) {
87- return {
88- isValid : false ,
89- errorMsg : getErrorMessage ( 1 ) , // 'CNPJ must have 14 numerical digits'
90- } ;
91- }
92- // Remove any non-digit characters from the CNPJ string
93- const cnpjClean : string = cnpj . replace ( / \D + / g, "" ) ;
94- // Convert the CNPJ string to an array of digits
95- const cnpjArray : number [ ] = cnpjClean . split ( "" ) . map ( Number ) ;
96- // Calculate the first and second verifiers
97- const firstVerifier : number = calculateFirstVerifier (
98- cnpjArray . slice ( 0 , 12 ) ,
99- ) ;
100- const secondVerifier : number = calculateSecondVerifier (
101- cnpjArray . slice ( 0 , 12 ) . concat ( firstVerifier ) ,
102- firstVerifier ,
103- ) ;
104- // Check if the calculated verifiers match the ones in the CNPJ
105- if ( cnpjArray [ 12 ] === firstVerifier && cnpjArray [ 13 ] === secondVerifier ) {
106- return {
107- isValid : true ,
108- errorMsg : null ,
109- } ;
110- }
77+ if ( ! cnpj ) {
11178 return {
11279 isValid : false ,
113- errorMsg : getErrorMessage ( 2 ) , // 'CNPJ is not valid '
80+ errorMsg : getErrorMessage ( 0 ) , // 'CNPJ invalid '
11481 } ;
115- } catch ( error ) {
82+ }
83+ // Check if the CNPJ has 14 digits
84+ if ( cnpj . length !== 14 && cnpj . length !== 18 ) {
11685 return {
11786 isValid : false ,
118- errorMsg : getErrorMessage ( 3 ) , // 'Unknown error'
87+ errorMsg : getErrorMessage ( 1 ) , // 'CNPJ must have 14 numerical digits'
88+ } ;
89+ }
90+ // Remove any non-digit characters from the CNPJ string
91+ const cnpjClean : string = cnpj . replace ( / \D + / g, "" ) ;
92+ // Convert the CNPJ string to an array of digits
93+ const cnpjArray : number [ ] = cnpjClean . split ( "" ) . map ( Number ) ;
94+ // Calculate the first and second verifiers
95+ const firstVerifier : number = calculateFirstVerifier ( cnpjArray . slice ( 0 , 12 ) ) ;
96+ const secondVerifier : number = calculateSecondVerifier (
97+ cnpjArray . slice ( 0 , 12 ) . concat ( firstVerifier ) ,
98+ firstVerifier ,
99+ ) ;
100+ // Check if the calculated verifiers match the ones in the CNPJ
101+ if ( cnpjArray [ 12 ] === firstVerifier && cnpjArray [ 13 ] === secondVerifier ) {
102+ return {
103+ isValid : true ,
104+ errorMsg : null ,
119105 } ;
120106 }
107+ return {
108+ isValid : false ,
109+ errorMsg : getErrorMessage ( 2 ) , // 'CNPJ is not valid'
110+ } ;
121111}
122112export default cnpjIsValid ;
0 commit comments