Skip to content

Commit 52643aa

Browse files
committed
test: Covering more tests
1 parent 38d78a9 commit 52643aa

25 files changed

+520
-447
lines changed

src/cnpjValidator.ts

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
122112
export default cnpjIsValid;

src/cpfValidator.ts

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const defaultErrorMsg: string[] = [
22
"CPF invalid",
33
"CPF must have 11 numerical digits",
44
"CPF is not valid",
5-
"Unknown error",
65
];
76

87
/**
@@ -29,6 +28,7 @@ function cpfIsValid(
2928
if (typeof cpf !== "string") {
3029
throw new TypeError("The input should be a string.");
3130
}
31+
3232
if (errorMsg) {
3333
if (!Array.isArray(errorMsg)) throw new TypeError("Must be an Array");
3434
for (let index: number = 0; index < errorMsg.length; index += 1) {
@@ -45,56 +45,49 @@ function cpfIsValid(
4545
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
4646
}
4747

48-
try {
49-
if (!cpf) {
50-
return {
51-
isValid: false,
52-
errorMsg: getErrorMessage(0),
53-
};
54-
}
55-
56-
const cpfClean: string = cpf.replace(/\D+/g, "");
57-
58-
if (/^(\d)\1{10}$/.test(cpfClean)) {
59-
return {
60-
isValid: false,
61-
errorMsg: getErrorMessage(2),
62-
};
63-
}
64-
65-
if (cpfClean.length !== 11) {
66-
return {
67-
isValid: false,
68-
errorMsg: getErrorMessage(1),
69-
};
70-
}
48+
if (!cpf) {
49+
return {
50+
isValid: false,
51+
errorMsg: getErrorMessage(0),
52+
};
53+
}
7154

72-
const cpfArray: number[] = cpfClean.split("").map(Number);
73-
const validator: (sum: number) => number = (sum: number) =>
74-
sum % 11 < 2 ? 0 : 11 - (sum % 11);
75-
const sum1: number = cpfArray
76-
.slice(0, 9)
77-
.reduce((acc, val, i) => acc + val * (10 - i), 0);
78-
const sum2: number = cpfArray
79-
.slice(0, 10)
80-
.reduce((acc, val, i) => acc + val * (11 - i), 0);
55+
const cpfClean: string = cpf.replace(/\D+/g, "");
8156

82-
if (cpfArray[9] === validator(sum1) && cpfArray[10] === validator(sum2)) {
83-
return {
84-
isValid: true,
85-
errorMsg: null,
86-
};
87-
}
57+
if (/^(\d)\1{10}$/.test(cpfClean)) {
8858
return {
8959
isValid: false,
9060
errorMsg: getErrorMessage(2),
9161
};
92-
} catch (err) {
62+
}
63+
64+
if (cpfClean.length !== 11) {
9365
return {
9466
isValid: false,
95-
errorMsg: getErrorMessage(3),
67+
errorMsg: getErrorMessage(1),
68+
};
69+
}
70+
71+
const cpfArray: number[] = cpfClean.split("").map(Number);
72+
const validator: (sum: number) => number = (sum: number) =>
73+
sum % 11 < 2 ? 0 : 11 - (sum % 11);
74+
const sum1: number = cpfArray
75+
.slice(0, 9)
76+
.reduce((acc, val, i) => acc + val * (10 - i), 0);
77+
const sum2: number = cpfArray
78+
.slice(0, 10)
79+
.reduce((acc, val, i) => acc + val * (11 - i), 0);
80+
81+
if (cpfArray[9] === validator(sum1) && cpfArray[10] === validator(sum2)) {
82+
return {
83+
isValid: true,
84+
errorMsg: null,
9685
};
9786
}
87+
return {
88+
isValid: false,
89+
errorMsg: getErrorMessage(2),
90+
};
9891
}
9992

10093
export default cpfIsValid;

src/getOnlyEmail.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,27 +96,6 @@ function getOnlyEmail(
9696
}
9797

9898
return multiple ? matches : matches[0];
99-
/**
100-
* const cleanedEmails: string[] = matches.map((email) => {
101-
for (const domain of domainsToClean) {
102-
const index: number = email.lastIndexOf(domain);
103-
if (index !== -1) {
104-
return email.substring(0, index + domain.length);
105-
}
106-
}
107-
return email;
108-
});
109-
110-
const cleanedEmails2: string[] = cleanedEmails.map((email) => {
111-
for (const domain of domainsToClean) {
112-
const index: number = email.indexOf(domain);
113-
if (index !== -1) {
114-
return email.substring(0, index + domain.length);
115-
}
116-
}
117-
return email;
118-
});
119-
*/
12099
}
121100

122101
export default getOnlyEmail;

src/isCEP.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ function isCEP(cep: string): boolean {
77
if (typeof cep !== "string") {
88
throw new TypeError("Input value must be a string.");
99
}
10-
try {
11-
if (cep.length < 8 || cep.length > 10) return false;
12-
// Clean the CEP and keep only the numbers
13-
const cepString: string = cep.replace(/\D/g, ""); // The \D pattern matches any non-digit character
14-
// Check if the cleaned CEP contains only numbers
15-
if (cepString.length !== 8) return false;
16-
// Check if the CEP is a valid number (all digits)
17-
if (Number.isNaN(cepString)) return false;
18-
return true;
19-
} catch (error) {
10+
if (cep.length < 8 || cep.length > 10) {
2011
return false;
2112
}
13+
// Clean the CEP and keep only the numbers
14+
const cepString: string = cep.replace(/\D/g, ""); // The \D pattern matches any non-digit character
15+
// Check if the cleaned CEP contains only numbers
16+
if (cepString.length !== 8) {
17+
return false;
18+
}
19+
// Check if the CEP is a valid number (all digits)
20+
if (Number.isNaN(cepString)) {
21+
return false;
22+
}
23+
24+
return true;
2225
}
2326
export default isCEP;

src/validateBRPhoneNumber.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ import { ValidateFunctions } from "./types";
33
const defaultErrorMsg: string[] = [
44
"Field phone number cannot be empty",
55
"Invalid phone number",
6-
"Unknown error",
76
];
87
/**
98
* @param phoneNumber
109
* @param errorMsg optional
1110
* @example validateBRPhoneNumber('(11) 98765-4321');
12-
* @example validateBRPhoneNumber('(11) 98765-4321', ['Invalid phone number', 'Invalid format', 'Unknown error']);
11+
* @example validateBRPhoneNumber('(11) 98765-4321', ['Invalid phone number', 'Invalid format']);
1312
* @description This function returns three errors in the following order:
1413
*
1514
* Default:
16-
* ['Field phone number cannot be empty', 'Invalid phone number', 'Unknown error']
15+
* ['Field phone number cannot be empty', 'Invalid phone number']
1716
*
1817
* Create a list of errors separated by commas in strings
1918
* @returns An object with 'isValid' (boolean) and 'errorMsg' (string) properties.
@@ -51,22 +50,15 @@ function validateBRPhoneNumber(
5150
}
5251
// Regular expression to validate Brazilian phone numbers
5352
const brPhoneNumberRegex: RegExp = /^\(\d{2}\) \d{5}-\d{4}$/;
54-
try {
55-
if (!brPhoneNumberRegex.test(phoneNumber)) {
56-
return {
57-
isValid: false,
58-
errorMsg: getErrorMessage(1),
59-
};
60-
}
61-
return {
62-
isValid: true,
63-
errorMsg: null,
64-
};
65-
} catch (error) {
53+
if (!brPhoneNumberRegex.test(phoneNumber)) {
6654
return {
6755
isValid: false,
68-
errorMsg: getErrorMessage(2),
56+
errorMsg: getErrorMessage(1),
6957
};
7058
}
59+
return {
60+
isValid: true,
61+
errorMsg: null,
62+
};
7163
}
7264
export default validateBRPhoneNumber;

0 commit comments

Comments
 (0)