UNPKG

regexp-toolkit

Version:
214 lines (209 loc) 5.29 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 src_exports = {}; __export(src_exports, { regex: () => regex, validate: () => validate }); module.exports = __toCommonJS(src_exports); // src/builder/RegexBuilder.ts var RegexBuilder = class _RegexBuilder { constructor() { this.pattern = ""; } // 🔠 Character Classes digit() { this.pattern += "\\d"; return this; } letter() { this.pattern += "[a-zA-Z]"; return this; } lowercase() { this.pattern += "[a-z]"; return this; } uppercase() { this.pattern += "[A-Z]"; return this; } lettersOrNumbers() { this.pattern += "[a-zA-Z0-9]"; return this; } whitespace() { this.pattern += "\\s"; return this; } nonWhitespace() { this.pattern += "\\S"; return this; } // matches All letters(a–z and A–Z), All digits(0–9) and The underscore: _ wordChar() { this.pattern += "\\w"; return this; } // matches Anything that is not a letter, digit, or underscore nonWordChar() { this.pattern += "\\W"; return this; } anyChar() { this.pattern += "."; return this; } custom(chars) { const escaped = chars.replace(/([\\\-\]])/g, "\\$1"); this.pattern += `[${escaped}]`; return this; } // 🔁 Quantifiers one() { this.pattern += "{1}"; return this; } zeroOrMore() { this.pattern += "*"; return this; } oneOrMore() { this.pattern += "+"; return this; } optional() { this.pattern += "?"; return this; } between(min, max) { this.pattern += `{${min},${max}}`; return this; } exactly(n) { this.pattern += `{${n}}`; return this; } atLeast(n) { this.pattern += `{${n},}`; return this; } // 🔧 Anchors and Structure start() { this.pattern += "^"; return this; } end() { this.pattern += "$"; return this; } strict() { let finalPattern = this.pattern; if (!finalPattern.startsWith("^")) { finalPattern = "^" + finalPattern; } if (!finalPattern.endsWith("$")) { finalPattern = finalPattern + "$"; } this.pattern = finalPattern; return this; } group(fn) { const inner = new _RegexBuilder(); fn(inner); this.pattern += `(?:${inner.toString()})`; return this; } capture(fn) { const inner = new _RegexBuilder(); fn(inner); this.pattern += `(${inner.toString()})`; return this; } or() { this.pattern += "|"; return this; } // need the same string to match then(str) { this.pattern += str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); return this; } // insert your own regex snippet raw(str) { this.pattern += str; return this; } // checks that what comes next must NOT match str not(str) { const escaped = str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); this.pattern += `(?!${escaped})`; return this; } // checks that what comes next must match str lookahead(str) { const escaped = str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); this.pattern += `(?=${escaped})`; return this; } // 🎯 Output and Test toRegex(flags = "") { return new RegExp(this.pattern, flags); } toString() { return this.pattern; } test(input) { return this.toRegex().test(input); } match(input) { return input.match(this.toRegex()); } }; // src/validators/validators.ts var validators = { email: (val) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val), phone: (val) => /^\+?[0-9]{10,15}$/.test(val), url: (val) => /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/i.test(val), ipv4: (val) => /^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.(?!$)|$){4}$/.test(val), hex: (val) => /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(val), username: (val) => /^[a-zA-Z0-9_]+$/.test(val), password: (val) => /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*#?&]{6,}$/.test(val), slug: (val) => /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(val), date: (val) => /^\d{4}-\d{2}-\d{2}$/.test(val), time: (val) => /^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$/.test(val), uuid: (val) => /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test( val ), htmlTag: (val) => /^<\/?[a-z][\s\S]*>$/i.test(val) }; var validators_default = validators; // src/index.ts function regex() { return new RegexBuilder(); } function validate(type, value) { return validators_default[type](value); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { regex, validate });