Repository URL to install this package:
|
Version:
0.9.5 ▾
|
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const exotic_1 = require("exotic");
const isValidLength_1 = __importDefault(require("./isValidLength"));
const isAlphaNumeric_1 = require("./isAlphaNumeric");
const __match_1 = require("./__match");
// const matchPassword = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@$!*])(?=.{8,})/
/**
* @param {string} [value='']
* @return {boolean}
*/
function isValidPassword(value = '') {
// Commenting this out as the validation has to happen on the server-side
// return isString(value) && matchPassword.test(value)
// Performing on the length validation
return isValidLength_1.default(value, 5);
}
exports.isValidPassword = isValidPassword;
// === strength ===
/* eslint-disable max-statements */
const strengthList = {
'0': 'very weak',
'1': 'weak',
// medium
'2': 'good',
'3': 'strong',
'5': 'very strong',
};
/**
* @param {Number} strength
* @return {String}
*/
function toStrengthName(strength = 0) {
// 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 = __match_1.test(matchSpecialChar);
// if it has two special characters, increase strength value
const hasSpecialChar = (x, min = 1) => hasAnySpecialChar(x) && x.match(min).length > 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 > 8;
/**
* @todo - move to best module later
* @see toStrengthName
*
* @param {String} validPassword
* @return {String} configurable wording for strength at a certain Weight
*/
function toStrength(validPassword) {
let strength = 0;
if (isLongEnough(validPassword)) {
strength += 2;
}
if (hasLowercaseAndUppercase(validPassword)) {
strength += 1;
}
if (hasNumbersAndLetters(validPassword)) {
strength += 1;
}
if (hasSpecialChar(validPassword)) {
strength += 1;
}
if (hasSpecialChar(validPassword, 2)) {
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