Created
July 29, 2023 12:32
-
-
Save psenger/d5631277de0d1d3057efc44ea4c6c9ee to your computer and use it in GitHub Desktop.
[Password Policy Check Functions contains min, min length, special characters, numbers, upper and lower case] #JavaScript #Functional
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
const minLength = (min) => (str) => (str||'').toString().trim().length >= min | |
const contains = (chars, min) => (str) => [...((str||'').toString().trim())].filter(char => (new Set(chars)).has(char)).length >= min | |
const hasLowerCase = contains('abcdefghijklmnopqrstuvwxyz', 1) | |
const hasUpperCase = contains('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 1) | |
const hasNumber = contains('0123456789', 1) | |
const hasSpecialChar = contains('!@#$%^&*()-+_', 1) | |
const compose = (...fns) => (x) => fns.map((fn) => fn(x)) | |
const passwordPolicyCheck = compose( | |
minLength(12), | |
hasLowerCase, | |
hasUpperCase, | |
hasNumber, | |
hasSpecialChar | |
) | |
const passwordCheck = (str) => passwordPolicyCheck(str).filter(Boolean).length === 5 | |
const password = "wwsj3" + "*!23X" + "Xd12a" | |
console.log(passwordCheck(password)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment