Skip to content

Commit fe62b69

Browse files
chore: Cleaning up package-lock, fixing barreling, adding jsDoc.
1 parent 3e9f701 commit fe62b69

18 files changed

Lines changed: 21584 additions & 15985 deletions

File tree

package-lock.json

Lines changed: 20940 additions & 15969 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"@commitlint/cli": "^12.1.4",
6161
"@commitlint/config-conventional": "^12.1.4",
6262
"@release-it/conventional-changelog": "^1.1.4",
63-
"@types/jest": "^26.0.23",
63+
"@types/jest": "^30.0.0",
6464
"cz-conventional-changelog": "^3.3.0",
6565
"docsify-cli": "^4.4.2",
6666
"husky": "^6.0.0",
@@ -70,6 +70,6 @@
7070
"prismjs": "^1.23.0",
7171
"release-it": "^14.8.0",
7272
"tsdx": "^0.14.1",
73-
"typescript": "^4.1.2"
73+
"typescript": "^5.9.3"
7474
}
7575
}

src/utilities/boleto/index.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Constants and utility functions for validating and formatting Brazilian bank slip (boleto) numbers.
3+
* @module boleto
4+
*/
15
import { onlyNumbers, isLastChar } from '../../helpers';
26

37
export const PARTIALS = [
@@ -45,6 +49,11 @@ function isValidLength(digitableLine: string): boolean {
4549
return digitableLine.length === LENGTH;
4650
}
4751

52+
/**
53+
* Calculates the modulus 10 check digit for a given partial string.
54+
* @param partial - The partial string to calculate the check digit for.
55+
* @returns The calculated check digit.
56+
*/
4857
function mod10(partial: string): number {
4958
const sum = partial
5059
.split('')
@@ -60,6 +69,11 @@ function mod10(partial: string): number {
6069
return mod > 0 ? 10 - mod : 0;
6170
}
6271

72+
/**
73+
* Calculates the modulus 11 check digit for a given value.
74+
* @param value - The value to calculate the check digit for.
75+
* @returns The calculated check digit.
76+
*/
6377
function mod11(value: string): number {
6478
const { initial, end } = MOD_11_WEIGHTS;
6579

@@ -80,6 +94,11 @@ function mod11(value: string): number {
8094
return mod === 0 || mod === 1 ? 1 : 11 - mod;
8195
}
8296

97+
/**
98+
* Validates if all partial sections of the digitable line have correct check digits.
99+
* @param digitableLine - The digitable line to validate.
100+
* @returns True if all partials are valid, false otherwise.
101+
*/
83102
function isValidPartials(digitableLine: string): boolean {
84103
return PARTIALS.every(({ start, end, index }) => {
85104
const mod = mod10(digitableLine.substring(start, end));
@@ -88,13 +107,23 @@ function isValidPartials(digitableLine: string): boolean {
88107
});
89108
}
90109

110+
/**
111+
* Converts a digitable line to a boleto number format.
112+
* @param digitableLine - The digitable line to parse.
113+
* @returns The parsed boleto number.
114+
*/
91115
function parse(digitableLine: string): string {
92116
return DIGITABLE_LINE_TO_BOLETO_CONVERT_POSITIONS.reduce(
93117
(acc, pos) => acc + digitableLine.substring(pos.start, pos.end),
94118
''
95119
);
96120
}
97121

122+
/**
123+
* Validates if the check digit of the parsed digitable line is correct.
124+
* @param parsedDigitableLine - The parsed digitable line to validate.
125+
* @returns True if the check digit is valid, false otherwise.
126+
*/
98127
function isValidCheckDigit(parsedDigitableLine: string): boolean {
99128
const mod = mod11(
100129
parsedDigitableLine.slice(0, CHECK_DIGIT_POSITION) + parsedDigitableLine.slice(CHECK_DIGIT_POSITION + 1)
@@ -103,6 +132,16 @@ function isValidCheckDigit(parsedDigitableLine: string): boolean {
103132
return +parsedDigitableLine[CHECK_DIGIT_POSITION] === mod;
104133
}
105134

135+
/**
136+
* Validates if a Brazilian bank slip (boleto) number is valid.
137+
* Performs several validations including length check, partial validations,
138+
* and check digit verification.
139+
* @param digitableLine - The bank slip number to validate.
140+
* @returns True if the bank slip number is valid, false otherwise.
141+
* @example
142+
* isValid('23790123016000000005325000456708') // returns true
143+
* isValid('23790123016000000005325000456709') // returns false
144+
*/
106145
export function isValid(digitableLine: string): boolean {
107146
if (!digitableLine || typeof digitableLine !== 'string') return false;
108147

@@ -117,6 +156,18 @@ export function isValid(digitableLine: string): boolean {
117156
return isValidCheckDigit(parsedDigits);
118157
}
119158

159+
/**
160+
* Formats a boleto (Brazilian bank slip) number string by adding dots and spaces at specific positions.
161+
*
162+
* @param boleto - The boleto number string to be formatted. Can contain numbers and other characters.
163+
* @returns A formatted string containing only numbers with dots and spaces at standardized positions.
164+
*
165+
* @example
166+
* ```typescript
167+
* format('23790123016000000005325000456708')
168+
* // returns '23790.12301 60000.000053 25000.456708'
169+
* ```
170+
*/
120171
export function format(boleto: string) {
121172
const digits = onlyNumbers(boleto);
122173

src/utilities/capitalize/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ type CapitalizeOptions = {
2424
upperCaseWords?: string[];
2525
};
2626

27+
/**
28+
* Capitalizes a given string according to specific rules for lower-case and upper-case words.
29+
*
30+
* - Words listed in `lowerCaseWords` (default: `PREPOSITIONS`) will be converted to lower case, except for the first word.
31+
* - Words listed in `upperCaseWords` (default: `ACRONYMS`) will be converted to upper case.
32+
* - All other words will be capitalized (first letter upper case, rest lower case).
33+
*
34+
* @param value - The input string to be capitalized.
35+
* @param options - Optional configuration for capitalization.
36+
* @param options.lowerCaseWords - Array of words to keep in lower case (default: `PREPOSITIONS`).
37+
* @param options.upperCaseWords - Array of words to keep in upper case (default: `ACRONYMS`).
38+
* @returns The capitalized string according to the specified rules.
39+
*/
2740
export function capitalize(
2841
value: string,
2942
{ lowerCaseWords = PREPOSITIONS, upperCaseWords = ACRONYMS }: CapitalizeOptions = {}

src/utilities/cep/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,23 @@ export const LENGTH = 8;
44

55
export const HYPHEN_INDEXES = [4];
66

7+
/**
8+
* Checks if the provided CEP (postal code) has a valid length.
9+
*
10+
* @param cep - The CEP string to validate.
11+
* @returns `true` if the CEP length matches the expected `LENGTH`, otherwise `false`.
12+
*/
713
function isValidLength(cep: string) {
814
return cep.length === LENGTH;
915
}
1016

17+
/**
18+
* Formats a Brazilian CEP (postal code) string by inserting hyphens at specific positions.
19+
* Only numeric characters from the input are considered.
20+
*
21+
* @param cep - The input CEP string to be formatted.
22+
* @returns The formatted CEP string with hyphens.
23+
*/
1124
export function format(cep: string): string {
1225
const digits = onlyNumbers(cep);
1326

@@ -25,6 +38,16 @@ export function format(cep: string): string {
2538
}, '');
2639
}
2740

41+
/**
42+
* Checks if a given CEP (Brazilian postal code) is valid.
43+
*
44+
* The function validates the input by ensuring it is a string,
45+
* extracts only the numeric digits, and verifies if the length
46+
* matches the expected format for a CEP.
47+
*
48+
* @param cep - The CEP value to validate.
49+
* @returns `true` if the CEP is valid, otherwise `false`.
50+
*/
2851
export function isValid(cep: string) {
2952
if (!cep || typeof cep !== 'string') return false;
3053

src/utilities/cities/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import { StateCode, StateName, getStates } from '../states';
44

55
const sortAlphabetically = (cityA: string, cityB: string) => cityA.localeCompare(cityB);
66

7+
/**
8+
* Returns a list of city names for a given Brazilian state, or all cities if no state is specified.
9+
*
10+
* If a state name or code is provided, the function searches for the corresponding state and returns its cities sorted alphabetically.
11+
* If no state is provided, it returns all cities from all states, sorted alphabetically.
12+
*
13+
* @param state - The name or code of the Brazilian state to filter cities by. Optional.
14+
* @returns An array of city names, sorted alphabetically. Returns an empty array if the state is not found.
15+
*/
716
export function getCities(state?: StateName | StateCode): string[] {
817
if (state) {
918
const states = getStates();

src/utilities/cnpj/index.ts

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,23 @@ export const CHECK_DIGITS_INDEXES = [12, 13];
2525

2626
export 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

3030
export 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+
*/
3445
export 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+
*/
5774
export 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+
*/
6995
export 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+
*/
73108
export 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+
*/
78123
export 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+
*/
99156
export function isValid(cnpj: string): boolean {
100157
if (!cnpj || typeof cnpj !== 'string') return false;
101158

0 commit comments

Comments
 (0)