Skip to content

Commit

Permalink
Ultima atualização do dia 25
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Jul 25, 2023
1 parent 9d3ebea commit 0ec0f24
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 60 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ isEmail,
isEmpty,
isMACAddress,
isMD5,
isPassaportNumber,
validatePassportNumber,
isPort,
isPostalCode,
isTime,
Expand All @@ -46,6 +46,8 @@ validatePassword,
validatePhoneNumber,
validateUsername,
validateUSPhoneNumber,
isNumber,
passwordStrengthTester


validateEmail example:
Expand Down
11 changes: 0 additions & 11 deletions index.html

This file was deleted.

5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ const isTime = require('./src/isTime');
const validateBRPhoneNumber = require('./src/validateBRPhoneNumber');
const validateUSPhoneNumber = require('./src/validateUSPhoneNumber');
const validatePhoneNumber = require('./src/validatePhoneNumber');

const isNumber = require('./src/isNumber');
const passwordStrengthTester = require('./src/passwordStrengthTester');

// Nao feitos ainda
// eslint-disable-next-line no-unused-vars
const comparePass = require('./src/comparePass');
const validateName = require('./src/validateName');

Expand Down Expand Up @@ -56,5 +54,6 @@ module.exports = {
validateBRPhoneNumber,
validateUSPhoneNumber,
validatePhoneNumber,
isNumber
isNumber,
passwordStrengthTester,
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "multiform-validator",
"version": "1.0.12",
"version": "1.0.13",
"description": "Javascript library made to validate, several form fields, such as: email, phone, password, cpf etc.",
"main": "index.js",
"scripts": {
Expand Down
1 change: 0 additions & 1 deletion src/cnpjValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,3 @@ function cnpjIsValid(cnpj, errorMsg = defaultErrorMsg) {
}
}
module.exports = cnpjIsValid;

2 changes: 1 addition & 1 deletion src/isCEP.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @returns {boolean}
*/
function isCEP(cep) {
if (typeof cep !== 'string') {
if (typeof cep !== 'string') {
throw new TypeError('Input value must be a string.');
}
try {
Expand Down
2 changes: 1 addition & 1 deletion src/isCreditCardValid.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
function isCreditCardValid(cardNumber) {
if (typeof cardNumber !== 'string') throw new TypeError('The input should be a string.');
const digits = cardNumber.replace(/\D+/g, '').split('').map(Number);
const digits = cardNumber.replace(/\D+/g, '').split('').map(Number);
const { length } = digits;
let sum = 0;
let isEven = false;
Expand Down
19 changes: 10 additions & 9 deletions src/isDecimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,32 @@
* @returns {boolean} true or false
*/
function isDecimal(value) {
if (typeof value !== 'string') {
if (typeof value === 'number') {
value = value.toString();
let getValued = value;
if (typeof getValued !== 'string') {
if (typeof getValued === 'number') {
getValued = getValued.toString();
} else {
throw new TypeError('Input value must be a string or a number.');
}
}
if (value.trim().length === 0) {
if (getValued.trim().length === 0) {
throw new Error('Input value must not be an empty string.');
}
// Regular expression to validate decimal numbers
const decimalRegex = /^[-+]?(?:\d+(?:[,.]\d*)?|\d*[,.]\d+)$/;
if (!decimalRegex.test(value)) {
if (!decimalRegex.test(getValued)) {
return false;
}
// Check for multiple decimal separators
const decimalSeparator = value.includes('.') ? '.' : ',';
const decimalSeparator = getValued.includes('.') ? '.' : ',';
const otherSeparator = decimalSeparator === '.' ? ',' : '.';
if (value.includes(decimalSeparator) && value.includes(otherSeparator)) {
if (getValued.includes(decimalSeparator) && getValued.includes(otherSeparator)) {
return false;
}
// Additional checks for negative sign
if (value.startsWith('-')) {
if (getValued.startsWith('-')) {
// Ensure the negative sign is only at the beginning and not elsewhere
if (value.lastIndexOf('-') > 0) {
if (getValued.lastIndexOf('-') > 0) {
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/isEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
function isEmail(email) {
try {
if (typeof email !== 'string') throw new TypeError('Invalid input, must be a string');
if (!email) throw new Error('Value cannot be null or empty');
const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (!email) throw new Error('Value cannot be null or empty');
const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (Number(email[0])) return false;
if (!(regex.test(email))) return false;
const depoisDoArroba = email.indexOf('@') + 1;
Expand Down
2 changes: 1 addition & 1 deletion src/isNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ function isNumber(value) {
if (value === null || value === undefined || typeof value === 'boolean') {
return false;
}
return !isNaN(parseFloat(value)) && isFinite(value);
return !Number.isNaN(parseFloat(value)) && Number.isFinite(value);
}
module.exports = isNumber;
52 changes: 52 additions & 0 deletions src/passwordStrengthTester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @param {string} password
*
* @description Avalia a força de uma senha e retorna o tipo de força da senha.
*
* @returns {string} O tipo de força da senha ('veryWeak', 'weak', 'regular', 'strong' ou 'veryStrong').
*
* @example
* passwordStrengthTester('123'); // Output: 'veryWeak'
*
* @example
* passwordStrengthTester('abc'); // Output: 'weak'
*
* @example
* passwordStrengthTester('abc123'); // Output: 'regular'
*
* @example
* passwordStrengthTester('Abc123!'); // Output: 'strong'
*
* @example
* passwordStrengthTester('SuperSecurePassword123!'); // Output: 'veryStrong'
*
*
* @returns {string} O tipo de força da senha ('veryWeak', 'weak', 'regular', 'strong' ou 'veryStrong').
*/
function passwordStrengthTester(password) {
if (typeof password !== 'string') throw new TypeError('The input should be a string.');
// Check de comprimento da senha
const passwordLength = password.length;
let strengthType;

// Critérios para classificar a senha
if (passwordLength < 6) {
strengthType = 'veryWeak';
} else if (passwordLength < 8) {
strengthType = 'weak';
} else if (passwordLength < 10) {
strengthType = 'regular';
} else if (
/[A-Z]/.test(password)
&& /[!@#$%^&*(),.?":{}|<>]/.test(password)
&& /\d/.test(password)
&& /[a-zA-Z]/.test(password)
) {
strengthType = 'veryStrong';
} else {
strengthType = 'strong';
}

return strengthType;
}
module.exports = passwordStrengthTester;
2 changes: 1 addition & 1 deletion src/validatePhoneNumber.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const defaultErrorMsg = ['Invalid value passed','Invalid phone number','Unknown error'];
const defaultErrorMsg = ['Invalid value passed', 'Invalid phone number', 'Unknown error'];
/**
* @param {string} phoneNumber
* @param {string[]} [errorMsg=defaultErrorMsg] optional
Expand Down
51 changes: 25 additions & 26 deletions src/validateUsername.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function validateUsername(username, minLength, maxLength, errorMsg = defaultErro
}
// Função interna para obter a mensagem de erro
function getErrorMessage(index) {
if (errorMsg && index >= 0 && index < errorMsg.length && errorMsg[index] != null) {
if (errorMsg && index >= 0 && index < errorMsg.length && errorMsg[index] != null) {
return errorMsg[index];
}
return defaultErrorMsg[index];
Expand All @@ -65,31 +65,31 @@ function validateUsername(username, minLength, maxLength, errorMsg = defaultErro
}
const minLenthUsername = parseInt(minLength, 10) || 1;
const maxLenthUsername = parseInt(maxLength, 10) || Infinity;
if (minLenthUsername > maxLenthUsername) {
throw new Error('Minimum cannot be greater than maximum');
} // Verifica se o min é maior que o max
if (minLenthUsername < 1 || maxLenthUsername < 1) {
throw new Error('Size parameters cannot be less than one');
} // Nenhum dos dois pode ser menor que 1
if (minLenthUsername > maxLenthUsername) {
throw new Error('Minimum cannot be greater than maximum');
} // Verifica se o min é maior que o max
if (minLenthUsername < 1 || maxLenthUsername < 1) {
throw new Error('Size parameters cannot be less than one');
} // Nenhum dos dois pode ser menor que 1
try {
if(regexHasSpaces.test(username)){
return {
isValid: false,
errorMsg: getErrorMessage(4),
};
}
if(regexStartsWithNumber.test(username)){
return {
isValid: false,
errorMsg: getErrorMessage(5),
};
}
if(regexOnlyNumbers.test(username)){
return {
isValid: false,
errorMsg: getErrorMessage(6),
};
}
if (regexHasSpaces.test(username)) {
return {
isValid: false,
errorMsg: getErrorMessage(4),
};
}
if (regexStartsWithNumber.test(username)) {
return {
isValid: false,
errorMsg: getErrorMessage(5),
};
}
if (regexOnlyNumbers.test(username)) {
return {
isValid: false,
errorMsg: getErrorMessage(6),
};
}
if (username.length < minLenthUsername) {
return {
isValid: false,
Expand All @@ -114,4 +114,3 @@ function validateUsername(username, minLength, maxLength, errorMsg = defaultErro
}
}
module.exports = validateUsername;

0 comments on commit 0ec0f24

Please sign in to comment.