Repository URL to install this package:
|
Version:
3.5.8 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const exotic_1 = require("exotic");
const isAlphaNumeric_1 = require("./isAlphaNumeric");
const __match_1 = require("./__match");
// const matchPassword = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@$!*])(?=.{8,})/
function isValidPassword(value = '') {
// Commenting this out as the validation has to happen on the server-side
// return isString(value) && matchPassword.test(value)
const getStrength = toStrength(value);
const strengthValidation = (item) => {
switch (item) {
case 'strong':
return true;
case 'very strong':
return true;
default:
return false;
}
};
console.log('Password Strength :: ', getStrength);
return strengthValidation(getStrength);
}
exports.isValidPassword = isValidPassword;
// === strength ===
/* eslint-disable max-statements */
const strengthList = {
'0': 'very weak',
'1': 'weak',
// medium
'2': 'good',
'4': 'strong',
'5': 'very strong',
};
function toStrengthName(originalStrength = 0) {
let strength = originalStrength;
// default to very weak / 0
const wording = strengthList[0];
// start at highest
while (strength >= 0) {
const wordForStrength = strengthList[strength];
if (exotic_1.isSafe(wordForStrength)) {
return wordForStrength;
}
// go lower until we find the right word
strength -= 1;
}
return wording;
}
exports.toStrengthName = toStrengthName;
// .*[!,%,&,@,#,$,^,*,?,_,~]
const matchSpecialChar = /(.*[!,%,&,@,#,$,^,*,?,_,~])/g;
// if it has one special character, increase strength value
const hasAnySpecialChar = (item) => __match_1.test(matchSpecialChar, item);
// if it has two special characters, increase strength value
const hasSpecialChar = (x, min) => {
const isValidSpecialChar = hasAnySpecialChar(x);
const matchedSpecialChar = x.match(matchSpecialChar);
const getLength = isValidSpecialChar
? matchedSpecialChar[0].split('').length
: 1;
return isValidSpecialChar && getLength >= min;
};
// if password contains both lower and uppercase characters, increase strength value
const matchLowercaseAndUppercase = /([a-z].*[A-Z])|([A-Z].*[a-z])/;
const hasLowercaseAndUppercase = __match_1.test(matchLowercaseAndUppercase);
// if it has numbers and characters, increase strength value
const hasNumbersAndLetters = x => __match_1.test(isAlphaNumeric_1.matchNumbers, x) && __match_1.test(isAlphaNumeric_1.matchNumbers, x);
// if length is 8 characters or more, increase strength value
// @note @VARIATION (BY 2 - THIS IS THE BIGGEST FACTOR)
const isLongEnough = (validPassword) => validPassword.length > 7;
/**
* @todo - move to best module later
* @see toStrengthName
*
* @return configurable wording for strength at a certain Weight
*/
function toStrength(validPassword) {
let strength = 0;
if (isLongEnough(validPassword)) {
strength += 1;
}
if (hasLowercaseAndUppercase(validPassword)) {
strength += 1;
}
if (hasNumbersAndLetters(validPassword)) {
strength += 1;
}
if (hasSpecialChar(validPassword, 1)) {
strength += 1;
}
// now we have calculated strength value, we can return messages
return toStrengthName(strength);
}
exports.toStrength = toStrength;
exports.default = isValidPassword;
//# sourceMappingURL=isValidPassword.js.map