UNPKG

hannah-password-rules

Version:

Make validations defined from dashboard of Platform

101 lines (98 loc) 4.3 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { passwordRules: () => passwordRules }); module.exports = __toCommonJS(index_exports); // src/functions.ts function passwordRules(password, validation, props = {}) { function escapeRegex(expresion) { return expresion.replace(/[/\-\\^$*+?.()|[\]{}]/g, "\\$&"); } function prepareStringForValidation(word) { return word.replaceAll(" ", ""); } const validations = () => ({ max_length: () => password.trim().length <= validation.rules.max_length?.value, min_length: () => password.trim().length >= validation.rules.min_length?.value, mayus_quantity: () => validation.rules.mayus_quantity?.required === false || new RegExp( `(?=(.*[A-Z]){${escapeRegex(`${validation.rules.mayus_quantity?.value}`)}})`, "g" ).test(password), minus_quantity: () => validation.rules.minus_quantity?.required === false || new RegExp( `(?=(.*[a-z]){${escapeRegex(`${validation.rules.minus_quantity?.value}`)}})`, "g" ).test(password), number_quantity: () => validation.rules.number_quantity?.required === false || new RegExp( `(?=(.*[0-9]){${escapeRegex(`${validation.rules.number_quantity?.value}`)}})`, "g" ).test(password), special_char_quantity: () => validation.rules.special_char_quantity?.required === false || new RegExp( `(?=(.*[*.!@$%&#:,.?_+-]){${escapeRegex(`${validation.rules.special_char_quantity?.value}`)}})`, "g" ).test(password), with_disallowed_words: () => { const trueValues = validation.words.map( (word) => prepareStringForValidation(password.toLowerCase()).indexOf(word.toLocaleLowerCase()) >= 0 ).filter((value) => value === true); return validation.rules.with_disallowed_words?.required === false || !trueValues.length; }, with_disallowed_user_name: () => { const words = props.name?.split(" ") ?? []; const trueValues = words.map( (word) => prepareStringForValidation(password.toLowerCase()).indexOf(word.toLocaleLowerCase()) >= 0 ).filter((value) => value === true); console.log("TRUE NAME", trueValues); return validation.rules.with_disallowed_user_name?.required === false || props.name !== void 0 && !trueValues.length; }, with_disallowed_user_email: () => { const words = props.email?.split("@") ?? []; const trueValues = words.map( (word) => prepareStringForValidation(password.toLowerCase()).indexOf(word.toLocaleLowerCase()) >= 0 ).filter((value) => value === true); return validation.rules.with_disallowed_user_email?.required === false || props.email !== void 0 && !trueValues.length; }, with_disallowed_user_phone: () => { const words = props.phone?.split("@") ?? []; const trueValues = words.map( (word) => prepareStringForValidation(password.toLowerCase()).indexOf(word.toLocaleLowerCase()) >= 0 ).filter((value) => value === true); return validation.rules.with_disallowed_user_phone?.required === false || props.phone !== void 0 && !trueValues.length; } }); const test = function() { const results = {}; Object.keys(validation.rules).forEach((key) => { console.log(key); results[key] = validations()[key](); console.log(results[key]); }); return results; }; return { validations, test }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { passwordRules });