11// Função para calcular o primeiro dígito verificador
22function calculateFirstVerifier ( cnpjBase : number [ ] ) : number {
3- const weight : Array < number > = [ 5 , 4 , 3 , 2 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 ] ;
4- let sum : number = 0 ;
5- for ( let i : number = 0 ; i < 12 ; i += 1 ) {
6- sum += cnpjBase [ i ] * weight [ i ] ;
7- }
8- const remainder : number = sum % 11 ;
9- return remainder < 2 ? 0 : 11 - remainder ;
3+ const weight : Array < number > = [ 5 , 4 , 3 , 2 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 ] ;
4+ let sum : number = 0 ;
5+ for ( let i : number = 0 ; i < 12 ; i += 1 ) {
6+ sum += cnpjBase [ i ] * weight [ i ] ;
7+ }
8+ const remainder : number = sum % 11 ;
9+ return remainder < 2 ? 0 : 11 - remainder ;
1010}
1111// Função para calcular o segundo dígito verificador
1212function calculateSecondVerifier (
13- cnpjBase : number [ ] ,
14- firstVerifier : number ,
13+ cnpjBase : number [ ] ,
14+ firstVerifier : number ,
1515) : number {
16- const weight : number [ ] = [ 6 , 5 , 4 , 3 , 2 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 ] ;
17- let sum : number = 0 ;
18- for ( let i : number = 0 ; i < 12 ; i += 1 ) {
19- sum += cnpjBase [ i ] * weight [ i ] ;
20- }
21- sum += firstVerifier * weight [ 12 ] ;
22- const remainder : number = sum % 11 ;
23- return remainder < 2 ? 0 : 11 - remainder ;
16+ const weight : number [ ] = [ 6 , 5 , 4 , 3 , 2 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 ] ;
17+ let sum : number = 0 ;
18+ for ( let i : number = 0 ; i < 12 ; i += 1 ) {
19+ sum += cnpjBase [ i ] * weight [ i ] ;
20+ }
21+ sum += firstVerifier * weight [ 12 ] ;
22+ const remainder : number = sum % 11 ;
23+ return remainder < 2 ? 0 : 11 - remainder ;
2424}
2525
2626const defaultErrorMsg : string [ ] = [
27- "CNPJ invalid" ,
28- "CNPJ must have 14 numerical digits" ,
29- "CNPJ is not valid" ,
27+ "CNPJ invalid" ,
28+ "CNPJ must have 14 numerical digits" ,
29+ "CNPJ is not valid" ,
3030] ;
3131
3232/**
@@ -46,67 +46,67 @@ const defaultErrorMsg: string[] = [
4646 */
4747
4848function cnpjIsValid (
49- cnpj : string ,
50- errorMsg : ( string | null ) [ ] | null = defaultErrorMsg ,
49+ cnpj : string ,
50+ errorMsg : ( string | null ) [ ] | null = defaultErrorMsg ,
5151) : {
52- isValid : boolean ;
53- errorMsg : string | null ;
52+ isValid : boolean ;
53+ errorMsg : string | null ;
5454} {
55- if ( typeof cnpj !== "string" ) {
56- throw new TypeError ( "The input should be a string." ) ;
57- }
58- // Check para saber se as mensagens que sao passadas sao validas
59- // caso contrario retorna um ERRO
60- if ( errorMsg ) {
61- if ( ! Array . isArray ( errorMsg ) ) throw new Error ( "Must be an Array" ) ;
62- for ( const element of errorMsg ) {
63- if ( element != null && typeof element !== "string" ) {
64- throw new TypeError (
65- "All values within the array must be strings or null/undefined." ,
66- ) ;
67- }
68- }
69- }
55+ if ( typeof cnpj !== "string" ) {
56+ throw new TypeError ( "The input should be a string." ) ;
57+ }
58+ // Check para saber se as mensagens que sao passadas sao validas
59+ // caso contrario retorna um ERRO
60+ if ( errorMsg ) {
61+ if ( ! Array . isArray ( errorMsg ) ) throw new Error ( "Must be an Array" ) ;
62+ for ( const element of errorMsg ) {
63+ if ( element != null && typeof element !== "string" ) {
64+ throw new TypeError (
65+ "All values within the array must be strings or null/undefined." ,
66+ ) ;
67+ }
68+ }
69+ }
7070
71- // Função interna para obter a mensagem de erro
72- function getErrorMessage ( index : number ) : string {
73- const errorMessage : string | null = errorMsg ? errorMsg [ index ] : null ;
74- return errorMessage ?? defaultErrorMsg [ index ] ;
75- }
71+ // Função interna para obter a mensagem de erro
72+ function getErrorMessage ( index : number ) : string {
73+ const errorMessage : string | null = errorMsg ? errorMsg [ index ] : null ;
74+ return errorMessage ?? defaultErrorMsg [ index ] ;
75+ }
7676
77- if ( ! cnpj ) {
78- return {
79- isValid : false ,
80- errorMsg : getErrorMessage ( 0 ) , // 'CNPJ invalid'
81- } ;
82- }
83- // Check if the CNPJ has 14 digits
84- if ( cnpj . length !== 14 && cnpj . length !== 18 ) {
85- return {
86- isValid : false ,
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 ,
105- } ;
106- }
107- return {
108- isValid : false ,
109- errorMsg : getErrorMessage ( 2 ) , // 'CNPJ is not valid'
110- } ;
77+ if ( ! cnpj ) {
78+ return {
79+ isValid : false ,
80+ errorMsg : getErrorMessage ( 0 ) , // 'CNPJ invalid'
81+ } ;
82+ }
83+ // Check if the CNPJ has 14 digits
84+ if ( cnpj . length !== 14 && cnpj . length !== 18 ) {
85+ return {
86+ isValid : false ,
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 ,
105+ } ;
106+ }
107+ return {
108+ isValid : false ,
109+ errorMsg : getErrorMessage ( 2 ) , // 'CNPJ is not valid'
110+ } ;
111111}
112112export default cnpjIsValid ;
0 commit comments