Created
May 29, 2020 17:35
-
-
Save lmichailian/c81e504ef4189d987c8776923458598d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { SET_PASSWORD_VALIDATION, CLEAR_PASSWORD_VALIDATION } from '../actions/ui/password' | |
const passwordReducer = (state = {}, actions) => { | |
switch (actions.type) { | |
case CLEAR_PASSWORD_VALIDATION: | |
return {} | |
case SET_PASSWORD_VALIDATION: | |
const {value, user} = actions.payload | |
const notEmpty = value !== '' && value !== undefined | |
let validations = { | |
uppercase: /^(?=.*[A-Z])(?=.*\w).+$/.test(value) && notEmpty, | |
lowercase: /^(?=.*[a-z])(?=.*\w).+$/.test(value) && notEmpty, | |
number: /^(?=.*[0-9])(?=.*\d).+$/.test(value) && notEmpty, | |
special: /[*@!#%&()^~{}]/.test(value) && notEmpty | |
} | |
const includePhoneTest = new RegExp(`${user.phone.toString()}`).exec(value) || false | |
const includePhone = includePhoneTest && includePhoneTest.length > 0 | |
const includeDNITest = new RegExp(`${user.dni.toString()}`).exec(value) || false | |
const includeDNI = includeDNITest && includeDNITest.length > 0 | |
const includeNameTest = user.name.split(' ').find( | |
(el) => new RegExp(`${el}`, 'gi').exec(value) | |
) | |
const isTrivial = includePhone || includeDNI || includeNameTest | |
console.log(includePhone, includeDNI, includeNameTest) | |
const unique = !/(.)\1{2,}/.test(value) && notEmpty | |
const minchar = (value.length >= 8) && notEmpty | |
const countValid = Object.keys(validations) | |
.filter((value) => { | |
return validations[value] === true | |
}) | |
if (countValid.length >= 3) { | |
validations = { | |
uppercase: true, | |
lowercase: true, | |
number: true, | |
special: true | |
} | |
} | |
return { | |
...state, | |
uppercase: validations.uppercase, | |
lowercase: validations.lowercase, | |
number: validations.number, | |
special: validations.special, | |
unique: unique, | |
minchar: minchar, | |
isTrivial: !isTrivial, | |
isValid: countValid.length >= 3 && unique && minchar && !isTrivial | |
} | |
default: | |
return state | |
} | |
} | |
export default passwordReducer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment