@@ -25,12 +25,23 @@ export const CHECK_DIGITS_INDEXES = [12, 13];
2525
2626export const FIRST_CHECK_DIGIT_WEIGHTS = [ 5 , 4 , 3 , 2 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 ] ;
2727
28- export const SECOND_CHECK_DIGIT_WEIGHTS = [ 6 , ... FIRST_CHECK_DIGIT_WEIGHTS ] ;
28+ export const SECOND_CHECK_DIGIT_WEIGHTS = [ 6 ] . concat ( FIRST_CHECK_DIGIT_WEIGHTS ) ;
2929
3030export interface FormatCnpjOptions {
3131 pad ?: boolean ;
3232}
3333
34+ /**
35+ * Formats a CNPJ (Cadastro Nacional da Pessoa Jurídica) number according to Brazilian standards.
36+ *
37+ * The function removes all non-numeric characters from the input, optionally pads the number to the required length,
38+ * and inserts formatting characters (dots, slashes, hyphens) at the appropriate positions.
39+ *
40+ * @param cnpj - The CNPJ number to format, as a string or number.
41+ * @param options - Optional formatting options.
42+ * @property pad - If true, pads the CNPJ with leading zeros to the required length.
43+ * @returns The formatted CNPJ string.
44+ */
3445export function format ( cnpj : string | number , options : FormatCnpjOptions = { } ) : string {
3546 let digits = onlyNumbers ( cnpj ) ;
3647
@@ -54,6 +65,12 @@ export function format(cnpj: string | number, options: FormatCnpjOptions = {}):
5465 } , '' ) ;
5566}
5667
68+ /**
69+ * Generates a valid CNPJ (Cadastro Nacional da Pessoa Jurídica) number as a string.
70+ * The generated CNPJ includes both check digits calculated according to official rules.
71+ *
72+ * @returns {string } A valid CNPJ number as a string.
73+ */
5774export function generate ( ) : string {
5875 const baseCNPJ = generateRandomNumber ( LENGTH - 2 ) ;
5976
@@ -66,17 +83,45 @@ export function generate(): string {
6683 return `${ baseCNPJ } ${ firstCheckDigit } ${ secondCheckDigit } ` ;
6784}
6885
86+ /**
87+ * Checks if the provided CNPJ string matches the valid format.
88+ *
89+ * The valid format is: `NN.NNN.NNN/NNNN-NN` where `N` is a digit.
90+ * Dots, slashes, and hyphens are optional.
91+ *
92+ * @param cnpj - The CNPJ string to validate.
93+ * @returns `true` if the CNPJ matches the expected format, otherwise `false`.
94+ */
6995export function isValidFormat ( cnpj : string ) : boolean {
7096 return / ^ \d { 2 } \. ? \d { 3 } \. ? \d { 3 } \/ ? \d { 4 } - ? \d { 2 } $ / . test ( cnpj ) ;
7197}
7298
99+ /**
100+ * Checks if the given CPF number is a reserved number.
101+ *
102+ * Reserved numbers are typically sequences of repeated digits (e.g., "00000000000", "11111111111", etc.)
103+ * that are not valid for real CPF registrations.
104+ *
105+ * @param cpf - The CPF number as a string to be checked.
106+ * @returns `true` if the CPF is a reserved number, otherwise `false`.
107+ */
73108export function isReservedNumber ( cpf : string ) : boolean {
74109 return RESERVED_NUMBERS . indexOf ( cpf ) >= 0 ;
75110}
76111
77112// TODO: move to checksum helper
113+ /**
114+ * Checks if the provided CNPJ string has a valid checksum according to Brazilian CNPJ rules.
115+ *
116+ * The function uses predefined weights and indexes to calculate and validate the check digits.
117+ * It iterates through the check digit indexes, adjusts the weights as needed, and compares
118+ * the calculated check digit with the corresponding digit in the CNPJ.
119+ *
120+ * @param cnpj - The CNPJ string to validate.
121+ * @returns `true` if the CNPJ has a valid checksum, `false` otherwise.
122+ */
78123export function isValidChecksum ( cnpj : string ) : boolean {
79- const weights = [ ... FIRST_CHECK_DIGIT_WEIGHTS ] ;
124+ const weights = FIRST_CHECK_DIGIT_WEIGHTS . slice ( ) ;
80125
81126 return CHECK_DIGITS_INDEXES . every ( ( i ) => {
82127 if ( i === CHECK_DIGITS_INDEXES [ CHECK_DIGITS_INDEXES . length - 1 ] ) {
@@ -96,6 +141,18 @@ export function isValidChecksum(cnpj: string): boolean {
96141 } ) ;
97142}
98143
144+ /**
145+ * Checks if a given CNPJ (Cadastro Nacional da Pessoa Jurídica) string is valid.
146+ *
147+ * The validation includes:
148+ * - Ensuring the input is a non-empty string.
149+ * - Verifying the format of the CNPJ.
150+ * - Checking that the CNPJ is not a reserved number.
151+ * - Validating the checksum digits.
152+ *
153+ * @param cnpj - The CNPJ string to validate.
154+ * @returns `true` if the CNPJ is valid, otherwise `false`.
155+ */
99156export function isValid ( cnpj : string ) : boolean {
100157 if ( ! cnpj || typeof cnpj !== 'string' ) return false ;
101158
0 commit comments