suf-password
Version:
Password Check utility.
148 lines (147 loc) • 5.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var Password;
(function (Password_1) {
/**
* Validates a password or other strings with checks that have to be provided in the checks array,
* if the **`passed`** key of the returned object is true
* then all checks have been passed successfully.
*
* @param password password or other string to be checked.
* @param checks array of checks that will be performed.
* @param options min and max length and other stuff.
*/
function Validate(password, checks, options = { maxLength: 100, minLength: 0, passData: false }) {
const errors = [];
const data = [];
let passed = true;
if (password.length > options.maxLength || password.length < options.minLength) {
const isLong = password.length > options.maxLength;
errors.push(`password is to ${isLong ? 'long' : 'short'}, has to be ${isLong ? 'less than' : 'at least'} ${isLong ? options.maxLength : options.minLength} characters long.`);
passed = false;
}
for (let i = 0; i < checks.length; i++) {
const check = validateCheck.call({}, checks[i], password);
data.push(check.data);
if (check.err) {
errors.push(check.err);
}
if (check.passed === false) {
passed = false;
}
}
if (options.passData) {
return { passed, errors, validationData: data };
}
else {
return { passed, errors };
}
}
Password_1.Validate = Validate;
/**
* The password has to contain an uppercase letter, number and cannot contain any spaces.
* @param password password or string to check.
*/
function ValidateSimple(password) {
return Password.Validate(password, [
{ type: 'uppercase' },
{ type: 'numbers' },
{ type: 'spaces', invertCheck: true }
]).passed;
}
Password_1.ValidateSimple = ValidateSimple;
function validateCheck(Check, Password) {
this.check = Check;
this.password = Password;
this.error = undefined;
this.passed = true;
this.data = {
invertCheck: this.check.invertCheck,
errType: undefined
};
switch (this.check.type) {
case 'custom':
WrapCustomFuncOrCustomRegex.call(this, 'custom');
break;
case 'customRegex':
WrapCustomFuncOrCustomRegex.call(this, 'customRegex');
break;
case 'numbers':
WrapValidateCheckCase.call(this, /\d/g, ['number']);
break;
case 'letters':
WrapValidateCheckCase.call(this, /[a-z][A-Z]/g, ['letter']);
break;
case 'lowercase':
WrapValidateCheckCase.call(this, /[a-z]/g, ['lowercase letter']);
break;
case 'uppercase':
WrapValidateCheckCase.call(this, /[A-Z]/g, ['uppercase letter']);
break;
case 'spaces':
WrapValidateCheckCase.call(this, /\s/g, ['space']);
break;
case 'symbols':
WrapValidateCheckCase.call(this, /[`~\!@#\$%\^\&\*\(\)\-_\=\+\[\{\}\]\\\|;:'",<.>\/\?€£¥₹]/g, 'symbol');
break;
default:
this.error = 'checking type not valid.';
break;
}
return { err: this.error, passed: this.passed, data: this.data };
}
function WrapCustomFuncOrCustomRegex(type) {
if (this.check[type]) {
this.passed =
type === 'custom'
? this.check.custom(this.password)
: this.check.customRegex.test(this.password);
if (this.check.invertCheck) {
this.passed = !this.passed;
}
this.error = this.passed ? undefined : this.check.customError;
}
else {
this.error = `${type} has to be defined.`;
}
this.data.errType = type;
}
function WrapValidateCheckCase(regex, type) {
this.regexMatch = checkRegexMatch(this.password.match(regex), this.check.invertCheck, this.check.times);
this.data.errType = this.regexMatch.errType;
this.passed = this.regexMatch.passed;
if (this.check.customError && this.passed !== true) {
this.error = this.check.customError;
}
else if (!this.passed) {
this.error = validateHandleErr.call(this, type);
}
}
function validateHandleErr(type) {
if (!this.regexMatch.passed) {
if (this.regexMatch.errType === 'normal') {
return this.check.invertCheck
? `password cannot contain ${type}s.`
: `password has to contain at least one ${type}.`;
}
else {
return this.check.invertCheck
? `password cannot contain more than ${this.check.times} ${type}s.`
: `password has to contain ${this.check.times} or more ${type}s.`;
}
}
return undefined;
}
function checkRegexMatch(match, invertCheck, times) {
if (match === null) {
return { errType: times ? 'times' : 'normal', passed: invertCheck ? true : false };
}
if (times) {
return {
errType: 'times',
passed: invertCheck ? match.length <= times : match.length >= times
};
}
return { errType: 'normal', passed: invertCheck ? match.length < 1 : match.length > 0 };
}
})(Password = exports.Password || (exports.Password = {}));