Skip to content

Commit

Permalink
test: Covering more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Aug 24, 2024
1 parent 38d78a9 commit 52643aa
Show file tree
Hide file tree
Showing 25 changed files with 520 additions and 447 deletions.
66 changes: 28 additions & 38 deletions src/cnpjValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const defaultErrorMsg: string[] = [
"CNPJ invalid",
"CNPJ must have 14 numerical digits",
"CNPJ is not valid",
"Unknown error",
];

/**
Expand All @@ -39,7 +38,7 @@ const defaultErrorMsg: string[] = [
* If you want to use a default parameter, use null or leave Empty.
*
* Default:
* ['CNPJ invalid', 'CNPJ must have 14 numerical digits', 'CNPJ is not valid', 'Unknown error']
* ['CNPJ invalid', 'CNPJ must have 14 numerical digits', 'CNPJ is not valid']
* .
*
* Create a list of errors separated by commas in strings
Expand Down Expand Up @@ -75,48 +74,39 @@ function cnpjIsValid(
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
}

try {
if (!cnpj) {
return {
isValid: false,
errorMsg: getErrorMessage(0), // 'CNPJ invalid'
};
}
// Check if the CNPJ has 14 digits
if (cnpj.length !== 14 && cnpj.length !== 18) {
return {
isValid: false,
errorMsg: getErrorMessage(1), // 'CNPJ must have 14 numerical digits'
};
}
// Remove any non-digit characters from the CNPJ string
const cnpjClean: string = cnpj.replace(/\D+/g, "");
// Convert the CNPJ string to an array of digits
const cnpjArray: number[] = cnpjClean.split("").map(Number);
// Calculate the first and second verifiers
const firstVerifier: number = calculateFirstVerifier(
cnpjArray.slice(0, 12),
);
const secondVerifier: number = calculateSecondVerifier(
cnpjArray.slice(0, 12).concat(firstVerifier),
firstVerifier,
);
// Check if the calculated verifiers match the ones in the CNPJ
if (cnpjArray[12] === firstVerifier && cnpjArray[13] === secondVerifier) {
return {
isValid: true,
errorMsg: null,
};
}
if (!cnpj) {
return {
isValid: false,
errorMsg: getErrorMessage(2), // 'CNPJ is not valid'
errorMsg: getErrorMessage(0), // 'CNPJ invalid'
};
} catch (error) {
}
// Check if the CNPJ has 14 digits
if (cnpj.length !== 14 && cnpj.length !== 18) {
return {
isValid: false,
errorMsg: getErrorMessage(3), // 'Unknown error'
errorMsg: getErrorMessage(1), // 'CNPJ must have 14 numerical digits'
};
}
// Remove any non-digit characters from the CNPJ string
const cnpjClean: string = cnpj.replace(/\D+/g, "");
// Convert the CNPJ string to an array of digits
const cnpjArray: number[] = cnpjClean.split("").map(Number);
// Calculate the first and second verifiers
const firstVerifier: number = calculateFirstVerifier(cnpjArray.slice(0, 12));
const secondVerifier: number = calculateSecondVerifier(
cnpjArray.slice(0, 12).concat(firstVerifier),
firstVerifier,
);
// Check if the calculated verifiers match the ones in the CNPJ
if (cnpjArray[12] === firstVerifier && cnpjArray[13] === secondVerifier) {
return {
isValid: true,
errorMsg: null,
};
}
return {
isValid: false,
errorMsg: getErrorMessage(2), // 'CNPJ is not valid'
};
}
export default cnpjIsValid;
75 changes: 34 additions & 41 deletions src/cpfValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const defaultErrorMsg: string[] = [
"CPF invalid",
"CPF must have 11 numerical digits",
"CPF is not valid",
"Unknown error",
];

/**
Expand All @@ -29,6 +28,7 @@ function cpfIsValid(
if (typeof cpf !== "string") {
throw new TypeError("The input should be a string.");
}

if (errorMsg) {
if (!Array.isArray(errorMsg)) throw new TypeError("Must be an Array");
for (let index: number = 0; index < errorMsg.length; index += 1) {
Expand All @@ -45,56 +45,49 @@ function cpfIsValid(
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
}

try {
if (!cpf) {
return {
isValid: false,
errorMsg: getErrorMessage(0),
};
}

const cpfClean: string = cpf.replace(/\D+/g, "");

if (/^(\d)\1{10}$/.test(cpfClean)) {
return {
isValid: false,
errorMsg: getErrorMessage(2),
};
}

if (cpfClean.length !== 11) {
return {
isValid: false,
errorMsg: getErrorMessage(1),
};
}
if (!cpf) {
return {
isValid: false,
errorMsg: getErrorMessage(0),
};
}

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

if (cpfArray[9] === validator(sum1) && cpfArray[10] === validator(sum2)) {
return {
isValid: true,
errorMsg: null,
};
}
if (/^(\d)\1{10}$/.test(cpfClean)) {
return {
isValid: false,
errorMsg: getErrorMessage(2),
};
} catch (err) {
}

if (cpfClean.length !== 11) {
return {
isValid: false,
errorMsg: getErrorMessage(3),
errorMsg: getErrorMessage(1),
};
}

const cpfArray: number[] = cpfClean.split("").map(Number);
const validator: (sum: number) => number = (sum: number) =>
sum % 11 < 2 ? 0 : 11 - (sum % 11);
const sum1: number = cpfArray
.slice(0, 9)
.reduce((acc, val, i) => acc + val * (10 - i), 0);
const sum2: number = cpfArray
.slice(0, 10)
.reduce((acc, val, i) => acc + val * (11 - i), 0);

if (cpfArray[9] === validator(sum1) && cpfArray[10] === validator(sum2)) {
return {
isValid: true,
errorMsg: null,
};
}
return {
isValid: false,
errorMsg: getErrorMessage(2),
};
}

export default cpfIsValid;
21 changes: 0 additions & 21 deletions src/getOnlyEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,6 @@ function getOnlyEmail(
}

return multiple ? matches : matches[0];
/**
* const cleanedEmails: string[] = matches.map((email) => {
for (const domain of domainsToClean) {
const index: number = email.lastIndexOf(domain);
if (index !== -1) {
return email.substring(0, index + domain.length);
}
}
return email;
});
const cleanedEmails2: string[] = cleanedEmails.map((email) => {
for (const domain of domainsToClean) {
const index: number = email.indexOf(domain);
if (index !== -1) {
return email.substring(0, index + domain.length);
}
}
return email;
});
*/
}

export default getOnlyEmail;
23 changes: 13 additions & 10 deletions src/isCEP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ function isCEP(cep: string): boolean {
if (typeof cep !== "string") {
throw new TypeError("Input value must be a string.");
}
try {
if (cep.length < 8 || cep.length > 10) return false;
// Clean the CEP and keep only the numbers
const cepString: string = cep.replace(/\D/g, ""); // The \D pattern matches any non-digit character
// Check if the cleaned CEP contains only numbers
if (cepString.length !== 8) return false;
// Check if the CEP is a valid number (all digits)
if (Number.isNaN(cepString)) return false;
return true;
} catch (error) {
if (cep.length < 8 || cep.length > 10) {
return false;
}
// Clean the CEP and keep only the numbers
const cepString: string = cep.replace(/\D/g, ""); // The \D pattern matches any non-digit character
// Check if the cleaned CEP contains only numbers
if (cepString.length !== 8) {
return false;
}
// Check if the CEP is a valid number (all digits)
if (Number.isNaN(cepString)) {
return false;
}

return true;
}
export default isCEP;
24 changes: 8 additions & 16 deletions src/validateBRPhoneNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import { ValidateFunctions } from "./types";
const defaultErrorMsg: string[] = [
"Field phone number cannot be empty",
"Invalid phone number",
"Unknown error",
];
/**
* @param phoneNumber
* @param errorMsg optional
* @example validateBRPhoneNumber('(11) 98765-4321');
* @example validateBRPhoneNumber('(11) 98765-4321', ['Invalid phone number', 'Invalid format', 'Unknown error']);
* @example validateBRPhoneNumber('(11) 98765-4321', ['Invalid phone number', 'Invalid format']);
* @description This function returns three errors in the following order:
*
* Default:
* ['Field phone number cannot be empty', 'Invalid phone number', 'Unknown error']
* ['Field phone number cannot be empty', 'Invalid phone number']
*
* Create a list of errors separated by commas in strings
* @returns An object with 'isValid' (boolean) and 'errorMsg' (string) properties.
Expand Down Expand Up @@ -51,22 +50,15 @@ function validateBRPhoneNumber(
}
// Regular expression to validate Brazilian phone numbers
const brPhoneNumberRegex: RegExp = /^\(\d{2}\) \d{5}-\d{4}$/;
try {
if (!brPhoneNumberRegex.test(phoneNumber)) {
return {
isValid: false,
errorMsg: getErrorMessage(1),
};
}
return {
isValid: true,
errorMsg: null,
};
} catch (error) {
if (!brPhoneNumberRegex.test(phoneNumber)) {
return {
isValid: false,
errorMsg: getErrorMessage(2),
errorMsg: getErrorMessage(1),
};
}
return {
isValid: true,
errorMsg: null,
};
}
export default validateBRPhoneNumber;
Loading

0 comments on commit 52643aa

Please sign in to comment.