human-regex
Advanced tools
@@ -1,2 +0,2 @@ | ||
| "use strict";const t=new Map,r={GLOBAL:"g",NON_SENSITIVE:"i",MULTILINE:"m",DOT_ALL:"s",UNICODE:"u",STICKY:"y"},e=Object.freeze({digit:"0-9",lowercaseLetter:"a-z",uppercaseLetter:"A-Z",letter:"a-zA-Z",alphanumeric:"a-zA-Z0-9",anyCharacter:"."}),a=Object.freeze({zeroOrMore:"*",oneOrMore:"+",optional:"?"});class n{constructor(){this.parts=[],this.flags=new Set}digit(){return this.add("\\d")}special(){return this.add("(?=.*[!@#$%^&*])")}word(){return this.add("\\w")}whitespace(){return this.add("\\s")}literal(r){return this.add(function(r){t.has(r)||t.set(r,r.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"));return t.get(r)}(r))}or(){return this.add("|")}range(t){const r=e[t];if(!r)throw new Error(`Unknown range: ${t}`);return this.add(`[${r}]`)}letter(){return this.add("[a-zA-Z]")}anyCharacter(){return this.add(".")}hasSpecialCharacter(){return this.add("(?=.*[!@#$%^&*])")}hasDigit(){return this.add("(?=.*\\d)")}hasLetter(){return this.add("(?=.*[a-zA-Z])")}exactly(t){return this.add(`{${t}}`)}atLeast(t){return this.add(`{${t},}`)}atMost(t){return this.add(`{0,${t}}`)}between(t,r){return this.add(`{${t},${r}}`)}oneOrMore(){return this.add(a.oneOrMore)}optional(){return this.add(a.optional)}zeroOrMore(){return this.add(a.zeroOrMore)}startGroup(){return this.add("(?:")}endGroup(){return this.add(")")}startAnchor(){return this.add("^")}endAnchor(){return this.add("$")}global(){return this.flags.add(r.GLOBAL),this}nonSensitive(){return this.flags.add(r.NON_SENSITIVE),this}protocol(){return this.add("https?://")}www(){return this.add("(www\\.)?")}tld(){return this.add("(com|org|net)")}path(){return this.add("(/\\w+)*")}add(t){return this.parts.push(t),this}toString(){return this.parts.join("")}toRegExp(){const t=this.toString();return new RegExp(t,[...this.flags].join(""))}}const s=()=>new n,o=(()=>{const t=t=>{const r=t().toRegExp();return()=>r};return{email:t((()=>s().startAnchor().word().oneOrMore().literal("@").word().oneOrMore().startGroup().literal(".").word().oneOrMore().endGroup().zeroOrMore().literal(".").letter().atLeast(2).endAnchor())),url:t((()=>s().startAnchor().protocol().optional().www().word().oneOrMore().literal(".").tld().path().optional().endAnchor())),phoneInternational:t((()=>s().startAnchor().literal("+").digit().between(1,3).literal("-").digit().between(3,14).endAnchor()))}})();exports.Flags=r,exports.Patterns=o,exports.Quantifiers=a,exports.Ranges=e,exports.createRegex=s; | ||
| "use strict";const t=new Map,r={GLOBAL:"g",NON_SENSITIVE:"i",MULTILINE:"m",DOT_ALL:"s",UNICODE:"u",STICKY:"y"},e=Object.freeze({digit:"0-9",lowercaseLetter:"a-z",uppercaseLetter:"A-Z",letter:"a-zA-Z",alphanumeric:"a-zA-Z0-9",anyCharacter:"."}),a=Object.freeze({zeroOrMore:"*",oneOrMore:"+",optional:"?"});class n{constructor(){this.parts=[],this.flags=new Set}digit(){return this.add("\\d")}special(){return this.add("(?=.*[!@#$%^&*])")}word(){return this.add("\\w")}whitespace(){return this.add("\\s")}nonWhitespace(){return this.add("\\S")}literal(r){return this.add(function(r){t.has(r)||t.set(r,r.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"));return t.get(r)}(r))}or(){return this.add("|")}range(t){const r=e[t];if(!r)throw new Error(`Unknown range: ${t}`);return this.add(`[${r}]`)}notRange(t){return this.add(`[^${t}]`)}lazy(){const t=this.parts.pop();if(!t)throw new Error("No quantifier to make lazy");return this.add(`${t}?`)}startNamedGroup(t){return this.add(`(?<${t}>`)}letter(){return this.add("[a-zA-Z]")}anyCharacter(){return this.add(".")}negativeLookahead(t){return this.add(`(?!${t})`)}positiveLookahead(t){return this.add(`(?=${t})`)}positiveLookbehind(t){return this.add(`(?<=${t})`)}negativeLookbehind(t){return this.add(`(?<!${t})`)}hasSpecialCharacter(){return this.add("(?=.*[!@#$%^&*])")}hasDigit(){return this.add("(?=.*\\d)")}hasLetter(){return this.add("(?=.*[a-zA-Z])")}exactly(t){return this.add(`{${t}}`)}atLeast(t){return this.add(`{${t},}`)}atMost(t){return this.add(`{0,${t}}`)}between(t,r){return this.add(`{${t},${r}}`)}oneOrMore(){return this.add(a.oneOrMore)}optional(){return this.add(a.optional)}zeroOrMore(){return this.add(a.zeroOrMore)}startGroup(){return this.add("(?:")}startCaptureGroup(){return this.add("(")}wordBoundary(){return this.add("\\b")}nonWordBoundary(){return this.add("\\B")}endGroup(){return this.add(")")}startAnchor(){return this.add("^")}endAnchor(){return this.add("$")}global(){return this.flags.add(r.GLOBAL),this}nonSensitive(){return this.flags.add(r.NON_SENSITIVE),this}multiline(){return this.flags.add(r.MULTILINE),this}dotAll(){return this.flags.add(r.DOT_ALL),this}sticky(){return this.flags.add(r.STICKY),this}unicodeChar(t){this.flags.add(r.UNICODE);const e=new Set(["u","l","t","m","o"]);if(void 0!==t&&!e.has(t))throw new Error(`Invalid Unicode letter variant: ${t}`);return this.add(`\\p{L${null!=t?t:""}}`)}unicodeDigit(){return this.flags.add(r.UNICODE),this.add("\\p{N}")}unicodePunctuation(){return this.flags.add(r.UNICODE),this.add("\\p{P}")}unicodeSymbol(){return this.flags.add(r.UNICODE),this.add("\\p{S}")}repeat(t){if(0===this.parts.length)throw new Error("No pattern to repeat");const r=this.parts.pop();return this.parts.push(`(${r}){${t}}`),this}ipv4Octet(){return this.add("(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)")}protocol(){return this.add("https?://")}www(){return this.add("(www\\.)?")}tld(){return this.add("(com|org|net)")}path(){return this.add("(/\\w+)*")}add(t){return this.parts.push(t),this}toString(){return this.parts.join("")}toRegExp(){return new RegExp(this.toString(),[...this.flags].join(""))}}const d=()=>new n,s=(()=>{const t=t=>{const r=t().toRegExp();return()=>new RegExp(r.source,r.flags)};return{email:t((()=>d().startAnchor().word().oneOrMore().literal("@").word().oneOrMore().startGroup().literal(".").word().oneOrMore().endGroup().zeroOrMore().literal(".").letter().atLeast(2).endAnchor())),url:t((()=>d().startAnchor().protocol().www().word().oneOrMore().literal(".").tld().path().endAnchor())),phoneInternational:t((()=>d().startAnchor().literal("+").digit().between(1,3).literal("-").digit().between(3,14).endAnchor()))}})();exports.Flags=r,exports.Patterns=s,exports.Quantifiers=a,exports.Ranges=e,exports.createRegex=d; | ||
| //# sourceMappingURL=human-regex.cjs.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"human-regex.cjs.js","sources":["../src/human-regex.ts"],"sourcesContent":["// human-regex.ts\n// Static cache for escaped literals\nconst escapeCache = new Map<string, string>();\n\n/**\n * Regex flags (optimized as Set-based)\n */\nconst Flags = {\n GLOBAL: \"g\",\n NON_SENSITIVE: \"i\",\n MULTILINE: \"m\",\n DOT_ALL: \"s\",\n UNICODE: \"u\",\n STICKY: \"y\",\n} as const;\n\n/**\n * Precomputed character ranges\n */\nconst Ranges = Object.freeze({\n digit: \"0-9\",\n lowercaseLetter: \"a-z\",\n uppercaseLetter: \"A-Z\",\n letter: \"a-zA-Z\",\n alphanumeric: \"a-zA-Z0-9\",\n anyCharacter: \".\",\n});\n\ntype RangeKeys = keyof typeof Ranges;\n\n/**\n * Quantifier symbols\n */\nconst Quantifiers = Object.freeze({\n zeroOrMore: \"*\",\n oneOrMore: \"+\",\n optional: \"?\",\n});\n\nclass HumanRegex {\n private parts: string[];\n private flags: Set<string>;\n\n constructor() {\n this.parts = [];\n this.flags = new Set<string>();\n }\n\n // Core components\n digit(): this {\n return this.add(\"\\\\d\");\n }\n\n special(): this {\n return this.add(\"(?=.*[!@#$%^&*])\");\n }\n\n word(): this {\n return this.add(\"\\\\w\");\n }\n\n whitespace(): this {\n return this.add(\"\\\\s\");\n }\n\n literal(text: string): this {\n return this.add(escapeLiteral(text));\n }\n\n or(): this {\n return this.add(\"|\");\n }\n\n range(name: RangeKeys): this {\n const range = Ranges[name];\n if (!range) throw new Error(`Unknown range: ${name}`);\n return this.add(`[${range}]`);\n }\n\n letter(): this {\n return this.add(\"[a-zA-Z]\");\n }\n\n anyCharacter(): this {\n return this.add(\".\");\n }\n\n // Lookaheads for validation\n hasSpecialCharacter(): this {\n return this.add(\"(?=.*[!@#$%^&*])\");\n }\n\n hasDigit(): this {\n return this.add(\"(?=.*\\\\d)\");\n }\n\n hasLetter(): this {\n return this.add(\"(?=.*[a-zA-Z])\");\n }\n\n // Quantifiers\n exactly(n: number): this {\n return this.add(`{${n}}`);\n }\n\n atLeast(n: number): this {\n return this.add(`{${n},}`);\n }\n\n atMost(n: number): this {\n return this.add(`{0,${n}}`);\n }\n\n between(min: number, max: number): this {\n return this.add(`{${min},${max}}`);\n }\n\n oneOrMore(): this {\n return this.add(Quantifiers.oneOrMore);\n }\n\n optional(): this {\n return this.add(Quantifiers.optional);\n }\n\n zeroOrMore(): this {\n return this.add(Quantifiers.zeroOrMore);\n }\n\n // Grouping and anchors\n startGroup(): this {\n return this.add(\"(?:\");\n }\n\n endGroup(): this {\n return this.add(\")\");\n }\n\n startAnchor(): this {\n return this.add(\"^\");\n }\n\n endAnchor(): this {\n return this.add(\"$\");\n }\n\n // Flags (optimized string operations)\n global(): this {\n this.flags.add(Flags.GLOBAL);\n return this;\n }\n\n nonSensitive(): this {\n this.flags.add(Flags.NON_SENSITIVE);\n return this;\n }\n\n // Protocol optimization using optional 's'\n protocol(): this {\n return this.add(\"https?://\");\n }\n\n www(): this {\n return this.add(\"(www\\\\.)?\");\n }\n\n tld(): this {\n return this.add(\"(com|org|net)\");\n }\n\n path(): this {\n return this.add(\"(/\\\\w+)*\");\n }\n\n // Internal methods\n private add(part: string): this {\n this.parts.push(part);\n return this;\n }\n\n toString(): string {\n return this.parts.join(\"\");\n }\n\n toRegExp(): RegExp {\n const pattern = this.toString();\n return new RegExp(pattern, [...this.flags].join(\"\"));\n }\n}\n\nfunction escapeLiteral(text: string): string {\n if (!escapeCache.has(text)) {\n escapeCache.set(text, text.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\"));\n }\n return escapeCache.get(text) as string;\n}\n\n/**\n * Memoized predefined patterns\n */\nconst createRegex = (): HumanRegex => new HumanRegex();\n\nconst Patterns = (() => {\n const createCachedPattern = (builder: () => HumanRegex): (() => RegExp) => {\n const regex = builder().toRegExp();\n return () => regex;\n };\n\n return {\n email: createCachedPattern(() =>\n createRegex()\n .startAnchor()\n .word()\n .oneOrMore()\n .literal(\"@\")\n .word()\n .oneOrMore()\n .startGroup()\n .literal(\".\")\n .word()\n .oneOrMore()\n .endGroup()\n .zeroOrMore()\n .literal(\".\")\n .letter()\n .atLeast(2)\n .endAnchor()\n ),\n url: createCachedPattern(() =>\n createRegex()\n .startAnchor()\n .protocol()\n .optional()\n .www()\n .word()\n .oneOrMore()\n .literal(\".\")\n .tld()\n .path()\n .optional()\n .endAnchor()\n ),\n phoneInternational: createCachedPattern(() =>\n createRegex()\n .startAnchor()\n .literal(\"+\")\n .digit()\n .between(1, 3)\n .literal(\"-\")\n .digit()\n .between(3, 14)\n .endAnchor()\n ),\n };\n})();\n\n// Public API\nexport { createRegex, Patterns, Flags, Ranges, Quantifiers };\n"],"names":["escapeCache","Map","Flags","GLOBAL","NON_SENSITIVE","MULTILINE","DOT_ALL","UNICODE","STICKY","Ranges","Object","freeze","digit","lowercaseLetter","uppercaseLetter","letter","alphanumeric","anyCharacter","Quantifiers","zeroOrMore","oneOrMore","optional","HumanRegex","constructor","this","parts","flags","Set","add","special","word","whitespace","literal","text","has","set","replace","get","escapeLiteral","or","range","name","Error","hasSpecialCharacter","hasDigit","hasLetter","exactly","n","atLeast","atMost","between","min","max","startGroup","endGroup","startAnchor","endAnchor","global","nonSensitive","protocol","www","tld","path","part","push","toString","join","toRegExp","pattern","RegExp","createRegex","Patterns","createCachedPattern","builder","regex","email","url","phoneInternational"],"mappings":"aAEA,MAAMA,EAAc,IAAIC,IAKlBC,EAAQ,CACZC,OAAQ,IACRC,cAAe,IACfC,UAAW,IACXC,QAAS,IACTC,QAAS,IACTC,OAAQ,KAMJC,EAASC,OAAOC,OAAO,CAC3BC,MAAO,MACPC,gB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AQ,KACRpB,QACAsC,QAAQ,EAAG,GACXlB,QAAQ,KACRpB,QACAsC,QAAQ,EAAG,IACXM,cAGR,EApDgB"} | ||
| {"version":3,"file":"human-regex.cjs.js","sources":["../src/human-regex.ts"],"sourcesContent":["/**\n * @module human-regex\n * A utility for building regular expressions with a human-readable syntax.\n */\n\n/** Static cache for escaped literals */\nconst escapeCache = new Map<string, string>();\n\n/** Regular expression flags */\nconst Flags = {\n /** Global match */\n GLOBAL: \"g\",\n /** Case-insensitive matching */\n NON_SENSITIVE: \"i\",\n /** Multi-line matching */\n MULTILINE: \"m\",\n /** Dot matches newlines */\n DOT_ALL: \"s\",\n /** Enable Unicode features */\n UNICODE: \"u\",\n /** Sticky matching */\n STICKY: \"y\",\n} as const;\n\n/** Predefined character ranges */\nconst Ranges = Object.freeze({\n digit: \"0-9\",\n lowercaseLetter: \"a-z\",\n uppercaseLetter: \"A-Z\",\n letter: \"a-zA-Z\",\n alphanumeric: \"a-zA-Z0-9\",\n anyCharacter: \".\",\n});\n\ntype RangeKeys = keyof typeof Ranges;\n\n/** Regular expression quantifiers */\nconst Quantifiers = Object.freeze({\n zeroOrMore: \"*\",\n oneOrMore: \"+\",\n optional: \"?\",\n});\n\n/**\n * Class for building regular expressions using a chainable API\n * @class\n */\nclass HumanRegex {\n private parts: string[];\n private flags: Set<string>;\n\n constructor() {\n this.parts = [];\n this.flags = new Set<string>();\n }\n\n // Basic character types\n /** Matches any digit (0-9) */\n digit(): this {\n return this.add(\"\\\\d\");\n }\n\n /** Positive lookahead for special characters */\n special(): this {\n return this.add(\"(?=.*[!@#$%^&*])\");\n }\n\n /** Matches any word character (a-z, A-Z, 0-9, and _) */\n word(): this {\n return this.add(\"\\\\w\");\n }\n\n /** Matches any whitespace character */\n whitespace(): this {\n return this.add(\"\\\\s\");\n }\n\n /** Matches any non-whitespace character */\n nonWhitespace(): this {\n return this.add(\"\\\\S\");\n }\n\n /** Escapes and matches a literal string */\n literal(text: string): this {\n return this.add(escapeLiteral(text));\n }\n\n /** Alternation operator (OR) */\n or(): this {\n return this.add(\"|\");\n }\n\n /** Matches any character in a predefined range */\n range(name: RangeKeys): this {\n const range = Ranges[name];\n if (!range) throw new Error(`Unknown range: ${name}`);\n return this.add(`[${range}]`);\n }\n\n /** Matches any character not in the specified set */\n notRange(chars: string): this {\n return this.add(`[^${chars}]`);\n }\n\n /** Makes the previous quantifier lazy */\n lazy(): this {\n const lastPart = this.parts.pop();\n if (!lastPart) throw new Error(\"No quantifier to make lazy\");\n return this.add(`${lastPart}?`);\n }\n\n // Grouping constructs\n /** Starts a named capturing group */\n startNamedGroup(name: string): this {\n return this.add(`(?<${name}>`);\n }\n\n /** Matches any uppercase or lowercase letter */\n letter(): this {\n return this.add(\"[a-zA-Z]\");\n }\n\n /** Matches any character except newlines */\n anyCharacter(): this {\n return this.add(\".\");\n }\n\n // Lookaround assertions\n /** Negative lookahead assertion */\n negativeLookahead(pattern: string): this {\n return this.add(`(?!${pattern})`);\n }\n\n /** Positive lookahead assertion */\n positiveLookahead(pattern: string): this {\n return this.add(`(?=${pattern})`);\n }\n\n /** Positive lookbehind assertion */\n positiveLookbehind(pattern: string): this {\n return this.add(`(?<=${pattern})`);\n }\n\n /** Negative lookbehind assertion */\n negativeLookbehind(pattern: string): this {\n return this.add(`(?<!${pattern})`);\n }\n\n // Validation helpers\n /** Positive lookahead for special characters */\n hasSpecialCharacter(): this {\n return this.add(\"(?=.*[!@#$%^&*])\");\n }\n\n /** Positive lookahead for digits */\n hasDigit(): this {\n return this.add(\"(?=.*\\\\d)\");\n }\n\n /** Positive lookahead for letters */\n hasLetter(): this {\n return this.add(\"(?=.*[a-zA-Z])\");\n }\n\n // Quantifiers\n /** Matches exactly n occurrences */\n exactly(n: number): this {\n return this.add(`{${n}}`);\n }\n\n /** Matches at least n occurrences */\n atLeast(n: number): this {\n return this.add(`{${n},}`);\n }\n\n /** Matches at most n occurrences */\n atMost(n: number): this {\n return this.add(`{0,${n}}`);\n }\n\n /** Matches between min and max occurrences */\n between(min: number, max: number): this {\n return this.add(`{${min},${max}}`);\n }\n\n /** Matches one or more occurrences */\n oneOrMore(): this {\n return this.add(Quantifiers.oneOrMore);\n }\n\n /** Matches zero or one occurrences */\n optional(): this {\n return this.add(Quantifiers.optional);\n }\n\n /** Matches zero or more occurrences */\n zeroOrMore(): this {\n return this.add(Quantifiers.zeroOrMore);\n }\n\n // Anchors and boundaries\n /** Starts a non-capturing group */\n startGroup(): this {\n return this.add(\"(?:\");\n }\n\n /** Starts a capturing group */\n startCaptureGroup(): this {\n return this.add(\"(\");\n }\n\n /** Word boundary assertion */\n wordBoundary(): this {\n return this.add(\"\\\\b\");\n }\n\n /** Non-word boundary assertion */\n nonWordBoundary(): this {\n return this.add(\"\\\\B\");\n }\n\n /** Ends the current group */\n endGroup(): this {\n return this.add(\")\");\n }\n\n /** Start of string/line anchor */\n startAnchor(): this {\n return this.add(\"^\");\n }\n\n /** End of string/line anchor */\n endAnchor(): this {\n return this.add(\"$\");\n }\n\n // Flag management\n /** Adds global match flag */\n global(): this {\n this.flags.add(Flags.GLOBAL);\n return this;\n }\n\n /** Adds case-insensitive match flag */\n nonSensitive(): this {\n this.flags.add(Flags.NON_SENSITIVE);\n return this;\n }\n\n /** Adds multiline match flag */\n multiline(): this {\n this.flags.add(Flags.MULTILINE);\n return this;\n }\n\n /** Adds dot-all match flag */\n dotAll(): this {\n this.flags.add(Flags.DOT_ALL);\n return this;\n }\n\n /** Adds sticky match flag */\n sticky(): this {\n this.flags.add(Flags.STICKY);\n return this;\n }\n\n // Unicode properties\n /** Matches Unicode characters */\n unicodeChar(variant?: \"u\" | \"l\" | \"t\" | \"m\" | \"o\"): this {\n this.flags.add(Flags.UNICODE);\n const validVariants = new Set([\"u\", \"l\", \"t\", \"m\", \"o\"] as const);\n\n if (variant !== undefined && !validVariants.has(variant)) {\n throw new Error(`Invalid Unicode letter variant: ${variant}`);\n }\n\n return this.add(`\\\\p{L${variant ?? \"\"}}`);\n }\n\n /** Matches Unicode digits */\n unicodeDigit(): this {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{N}\");\n }\n\n /** Matches Unicode punctuation */\n unicodePunctuation(): this {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{P}\");\n }\n\n /** Matches Unicode symbols */\n unicodeSymbol(): this {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{S}\");\n }\n\n // Complex patterns\n /** Repeats the previous pattern exactly count times */\n repeat(count: number): this {\n if (this.parts.length === 0) {\n throw new Error(\"No pattern to repeat\");\n }\n\n const lastPart = this.parts.pop();\n this.parts.push(`(${lastPart}){${count}}`);\n return this;\n }\n\n /** Matches an IPv4 octet */\n ipv4Octet(): this {\n return this.add(\"(25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]\\\\d|\\\\d)\");\n }\n\n /** Matches URL protocol */\n protocol(): this {\n return this.add(\"https?://\");\n }\n\n /** Matches optional www subdomain */\n www(): this {\n return this.add(\"(www\\\\.)?\");\n }\n\n /** Matches common TLDs */\n tld(): this {\n return this.add(\"(com|org|net)\");\n }\n\n /** Matches URL path segments */\n path(): this {\n return this.add(\"(/\\\\w+)*\");\n }\n\n // Internal utilities\n private add(part: string): this {\n this.parts.push(part);\n return this;\n }\n\n /** Returns the constructed pattern string */\n toString(): string {\n return this.parts.join(\"\");\n }\n\n /** Compiles the pattern to a RegExp object */\n toRegExp(): RegExp {\n return new RegExp(this.toString(), [...this.flags].join(\"\"));\n }\n}\n\n/** Escapes special characters in a literal string */\nfunction escapeLiteral(text: string): string {\n if (!escapeCache.has(text)) {\n escapeCache.set(text, text.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\"));\n }\n return escapeCache.get(text)!;\n}\n\n/** Factory function for creating HumanRegex instances */\nconst createRegex = (): HumanRegex => new HumanRegex();\n\n/** Predefined common pattern templates */\nconst Patterns = (() => {\n const createCachedPattern = (builder: () => HumanRegex) => {\n const regex = builder().toRegExp();\n return () => new RegExp(regex.source, regex.flags);\n };\n\n return {\n /** Email address pattern */\n email: createCachedPattern(() =>\n createRegex()\n .startAnchor()\n .word()\n .oneOrMore()\n .literal(\"@\")\n .word()\n .oneOrMore()\n .startGroup()\n .literal(\".\")\n .word()\n .oneOrMore()\n .endGroup()\n .zeroOrMore()\n .literal(\".\")\n .letter()\n .atLeast(2)\n .endAnchor()\n ),\n /** URL pattern */\n url: createCachedPattern(() =>\n createRegex()\n .startAnchor()\n .protocol()\n .www()\n .word()\n .oneOrMore()\n .literal(\".\")\n .tld()\n .path()\n .endAnchor()\n ),\n /** International phone number pattern */\n phoneInternational: createCachedPattern(() =>\n createRegex()\n .startAnchor()\n .literal(\"+\")\n .digit()\n .between(1, 3)\n .literal(\"-\")\n .digit()\n .between(3, 14)\n .endAnchor()\n ),\n };\n})();\n\nexport { createRegex, Patterns, Flags, Ranges, Quantifiers };\n"],"names":["escapeCache","Map","Flags","GLOBAL","NON_SENSITIVE","MULTILINE","DOT_ALL","UNICODE","STICKY","Ranges","Object","freeze","digit","lowercaseLetter","uppercaseLetter","letter","alphanumeric","anyCharacter","Quantifiers","zeroOrMore","oneOrMore","optional","HumanRegex","constructor","this","parts","flags","Set","add","special","word","whitespace","nonWhitespace","literal","text","has","set","replace","get","escapeLiteral","or","range","name","Error","notRange","chars","lazy","lastPart","pop","startNamedGroup","negativeLookahead","pattern","positiveLookahead","positiveLookbehind","negativeLookbehind","hasSpecialCharacter","hasDigit","hasLetter","exactly","n","atLeast","atMost","between","min","max","startGroup","startCaptureGroup","wordBoundary","nonWordBoundary","endGroup","startAnchor","endAnchor","global","nonSensitive","multiline","dotAll","sticky","unicodeChar","variant","validVariants","undefined","unicodeDigit","unicodePunctuation","unicodeSymbol","repeat","count","length","push","ipv4Octet","protocol","www","tld","path","part","toString","join","toRegExp","RegExp","createRegex","Patterns","createCachedPattern","builder","regex","source","email","url","phoneInternational"],"mappings":"aAMA,MAAMA,EAAc,IAAIC,IAGlBC,EAAQ,CAEZC,OAAQ,I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,EAGpC6E,EAAW,MACf,MAAMC,EAAuBC,IAC3B,MAAMC,EAAQD,IAAUL,WACxB,MAAO,IAAM,IAAIC,OAAOK,EAAMC,OAAQD,EAAM5E,MAAM,EAGpD,MAAO,CAEL8E,MAAOJ,GAAoB,IACzBF,IACG5B,cACAxC,OACAV,YACAa,QAAQ,KACRH,OACAV,YACA6C,aACAhC,QAAQ,KACRH,OACAV,YACAiD,WACAlD,aACAc,QAAQ,KACRlB,SACA6C,QAAQ,GACRW,cAGLkC,IAAKL,GAAoB,IACvBF,IACG5B,cACAmB,WACAC,MACA5D,OACAV,YACAa,QAAQ,KACR0D,MACAC,OACArB,cAGLmC,mBAAoBN,GAAoB,IACtCF,IACG5B,cACArC,QAAQ,KACRrB,QACAkD,QAAQ,EAAG,GACX7B,QAAQ,KACRrB,QACAkD,QAAQ,EAAG,IACXS,cAGR,EArDgB"} |
@@ -1,2 +0,2 @@ | ||
| const t=new Map,r={GLOBAL:"g",NON_SENSITIVE:"i",MULTILINE:"m",DOT_ALL:"s",UNICODE:"u",STICKY:"y"},e=Object.freeze({digit:"0-9",lowercaseLetter:"a-z",uppercaseLetter:"A-Z",letter:"a-zA-Z",alphanumeric:"a-zA-Z0-9",anyCharacter:"."}),n=Object.freeze({zeroOrMore:"*",oneOrMore:"+",optional:"?"});class a{constructor(){this.parts=[],this.flags=new Set}digit(){return this.add("\\d")}special(){return this.add("(?=.*[!@#$%^&*])")}word(){return this.add("\\w")}whitespace(){return this.add("\\s")}literal(r){return this.add(function(r){t.has(r)||t.set(r,r.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"));return t.get(r)}(r))}or(){return this.add("|")}range(t){const r=e[t];if(!r)throw new Error(`Unknown range: ${t}`);return this.add(`[${r}]`)}letter(){return this.add("[a-zA-Z]")}anyCharacter(){return this.add(".")}hasSpecialCharacter(){return this.add("(?=.*[!@#$%^&*])")}hasDigit(){return this.add("(?=.*\\d)")}hasLetter(){return this.add("(?=.*[a-zA-Z])")}exactly(t){return this.add(`{${t}}`)}atLeast(t){return this.add(`{${t},}`)}atMost(t){return this.add(`{0,${t}}`)}between(t,r){return this.add(`{${t},${r}}`)}oneOrMore(){return this.add(n.oneOrMore)}optional(){return this.add(n.optional)}zeroOrMore(){return this.add(n.zeroOrMore)}startGroup(){return this.add("(?:")}endGroup(){return this.add(")")}startAnchor(){return this.add("^")}endAnchor(){return this.add("$")}global(){return this.flags.add(r.GLOBAL),this}nonSensitive(){return this.flags.add(r.NON_SENSITIVE),this}protocol(){return this.add("https?://")}www(){return this.add("(www\\.)?")}tld(){return this.add("(com|org|net)")}path(){return this.add("(/\\w+)*")}add(t){return this.parts.push(t),this}toString(){return this.parts.join("")}toRegExp(){const t=this.toString();return new RegExp(t,[...this.flags].join(""))}}const o=()=>new a,d=(()=>{const t=t=>{const r=t().toRegExp();return()=>r};return{email:t((()=>o().startAnchor().word().oneOrMore().literal("@").word().oneOrMore().startGroup().literal(".").word().oneOrMore().endGroup().zeroOrMore().literal(".").letter().atLeast(2).endAnchor())),url:t((()=>o().startAnchor().protocol().optional().www().word().oneOrMore().literal(".").tld().path().optional().endAnchor())),phoneInternational:t((()=>o().startAnchor().literal("+").digit().between(1,3).literal("-").digit().between(3,14).endAnchor()))}})();export{r as Flags,d as Patterns,n as Quantifiers,e as Ranges,o as createRegex}; | ||
| const t=new Map,r={GLOBAL:"g",NON_SENSITIVE:"i",MULTILINE:"m",DOT_ALL:"s",UNICODE:"u",STICKY:"y"},e=Object.freeze({digit:"0-9",lowercaseLetter:"a-z",uppercaseLetter:"A-Z",letter:"a-zA-Z",alphanumeric:"a-zA-Z0-9",anyCharacter:"."}),a=Object.freeze({zeroOrMore:"*",oneOrMore:"+",optional:"?"});class n{constructor(){this.parts=[],this.flags=new Set}digit(){return this.add("\\d")}special(){return this.add("(?=.*[!@#$%^&*])")}word(){return this.add("\\w")}whitespace(){return this.add("\\s")}nonWhitespace(){return this.add("\\S")}literal(r){return this.add(function(r){t.has(r)||t.set(r,r.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"));return t.get(r)}(r))}or(){return this.add("|")}range(t){const r=e[t];if(!r)throw new Error(`Unknown range: ${t}`);return this.add(`[${r}]`)}notRange(t){return this.add(`[^${t}]`)}lazy(){const t=this.parts.pop();if(!t)throw new Error("No quantifier to make lazy");return this.add(`${t}?`)}startNamedGroup(t){return this.add(`(?<${t}>`)}letter(){return this.add("[a-zA-Z]")}anyCharacter(){return this.add(".")}negativeLookahead(t){return this.add(`(?!${t})`)}positiveLookahead(t){return this.add(`(?=${t})`)}positiveLookbehind(t){return this.add(`(?<=${t})`)}negativeLookbehind(t){return this.add(`(?<!${t})`)}hasSpecialCharacter(){return this.add("(?=.*[!@#$%^&*])")}hasDigit(){return this.add("(?=.*\\d)")}hasLetter(){return this.add("(?=.*[a-zA-Z])")}exactly(t){return this.add(`{${t}}`)}atLeast(t){return this.add(`{${t},}`)}atMost(t){return this.add(`{0,${t}}`)}between(t,r){return this.add(`{${t},${r}}`)}oneOrMore(){return this.add(a.oneOrMore)}optional(){return this.add(a.optional)}zeroOrMore(){return this.add(a.zeroOrMore)}startGroup(){return this.add("(?:")}startCaptureGroup(){return this.add("(")}wordBoundary(){return this.add("\\b")}nonWordBoundary(){return this.add("\\B")}endGroup(){return this.add(")")}startAnchor(){return this.add("^")}endAnchor(){return this.add("$")}global(){return this.flags.add(r.GLOBAL),this}nonSensitive(){return this.flags.add(r.NON_SENSITIVE),this}multiline(){return this.flags.add(r.MULTILINE),this}dotAll(){return this.flags.add(r.DOT_ALL),this}sticky(){return this.flags.add(r.STICKY),this}unicodeChar(t){this.flags.add(r.UNICODE);const e=new Set(["u","l","t","m","o"]);if(void 0!==t&&!e.has(t))throw new Error(`Invalid Unicode letter variant: ${t}`);return this.add(`\\p{L${null!=t?t:""}}`)}unicodeDigit(){return this.flags.add(r.UNICODE),this.add("\\p{N}")}unicodePunctuation(){return this.flags.add(r.UNICODE),this.add("\\p{P}")}unicodeSymbol(){return this.flags.add(r.UNICODE),this.add("\\p{S}")}repeat(t){if(0===this.parts.length)throw new Error("No pattern to repeat");const r=this.parts.pop();return this.parts.push(`(${r}){${t}}`),this}ipv4Octet(){return this.add("(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)")}protocol(){return this.add("https?://")}www(){return this.add("(www\\.)?")}tld(){return this.add("(com|org|net)")}path(){return this.add("(/\\w+)*")}add(t){return this.parts.push(t),this}toString(){return this.parts.join("")}toRegExp(){return new RegExp(this.toString(),[...this.flags].join(""))}}const d=()=>new n,i=(()=>{const t=t=>{const r=t().toRegExp();return()=>new RegExp(r.source,r.flags)};return{email:t((()=>d().startAnchor().word().oneOrMore().literal("@").word().oneOrMore().startGroup().literal(".").word().oneOrMore().endGroup().zeroOrMore().literal(".").letter().atLeast(2).endAnchor())),url:t((()=>d().startAnchor().protocol().www().word().oneOrMore().literal(".").tld().path().endAnchor())),phoneInternational:t((()=>d().startAnchor().literal("+").digit().between(1,3).literal("-").digit().between(3,14).endAnchor()))}})();export{r as Flags,i as Patterns,a as Quantifiers,e as Ranges,d as createRegex}; | ||
| //# sourceMappingURL=human-regex.esm.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"human-regex.esm.js","sources":["../src/human-regex.ts"],"sourcesContent":["// human-regex.ts\n// Static cache for escaped literals\nconst escapeCache = new Map<string, string>();\n\n/**\n * Regex flags (optimized as Set-based)\n */\nconst Flags = {\n GLOBAL: \"g\",\n NON_SENSITIVE: \"i\",\n MULTILINE: \"m\",\n DOT_ALL: \"s\",\n UNICODE: \"u\",\n STICKY: \"y\",\n} as const;\n\n/**\n * Precomputed character ranges\n */\nconst Ranges = Object.freeze({\n digit: \"0-9\",\n lowercaseLetter: \"a-z\",\n uppercaseLetter: \"A-Z\",\n letter: \"a-zA-Z\",\n alphanumeric: \"a-zA-Z0-9\",\n anyCharacter: \".\",\n});\n\ntype RangeKeys = keyof typeof Ranges;\n\n/**\n * Quantifier symbols\n */\nconst Quantifiers = Object.freeze({\n zeroOrMore: \"*\",\n oneOrMore: \"+\",\n optional: \"?\",\n});\n\nclass HumanRegex {\n private parts: string[];\n private flags: Set<string>;\n\n constructor() {\n this.parts = [];\n this.flags = new Set<string>();\n }\n\n // Core components\n digit(): this {\n return this.add(\"\\\\d\");\n }\n\n special(): this {\n return this.add(\"(?=.*[!@#$%^&*])\");\n }\n\n word(): this {\n return this.add(\"\\\\w\");\n }\n\n whitespace(): this {\n return this.add(\"\\\\s\");\n }\n\n literal(text: string): this {\n return this.add(escapeLiteral(text));\n }\n\n or(): this {\n return this.add(\"|\");\n }\n\n range(name: RangeKeys): this {\n const range = Ranges[name];\n if (!range) throw new Error(`Unknown range: ${name}`);\n return this.add(`[${range}]`);\n }\n\n letter(): this {\n return this.add(\"[a-zA-Z]\");\n }\n\n anyCharacter(): this {\n return this.add(\".\");\n }\n\n // Lookaheads for validation\n hasSpecialCharacter(): this {\n return this.add(\"(?=.*[!@#$%^&*])\");\n }\n\n hasDigit(): this {\n return this.add(\"(?=.*\\\\d)\");\n }\n\n hasLetter(): this {\n return this.add(\"(?=.*[a-zA-Z])\");\n }\n\n // Quantifiers\n exactly(n: number): this {\n return this.add(`{${n}}`);\n }\n\n atLeast(n: number): this {\n return this.add(`{${n},}`);\n }\n\n atMost(n: number): this {\n return this.add(`{0,${n}}`);\n }\n\n between(min: number, max: number): this {\n return this.add(`{${min},${max}}`);\n }\n\n oneOrMore(): this {\n return this.add(Quantifiers.oneOrMore);\n }\n\n optional(): this {\n return this.add(Quantifiers.optional);\n }\n\n zeroOrMore(): this {\n return this.add(Quantifiers.zeroOrMore);\n }\n\n // Grouping and anchors\n startGroup(): this {\n return this.add(\"(?:\");\n }\n\n endGroup(): this {\n return this.add(\")\");\n }\n\n startAnchor(): this {\n return this.add(\"^\");\n }\n\n endAnchor(): this {\n return this.add(\"$\");\n }\n\n // Flags (optimized string operations)\n global(): this {\n this.flags.add(Flags.GLOBAL);\n return this;\n }\n\n nonSensitive(): this {\n this.flags.add(Flags.NON_SENSITIVE);\n return this;\n }\n\n // Protocol optimization using optional 's'\n protocol(): this {\n return this.add(\"https?://\");\n }\n\n www(): this {\n return this.add(\"(www\\\\.)?\");\n }\n\n tld(): this {\n return this.add(\"(com|org|net)\");\n }\n\n path(): this {\n return this.add(\"(/\\\\w+)*\");\n }\n\n // Internal methods\n private add(part: string): this {\n this.parts.push(part);\n return this;\n }\n\n toString(): string {\n return this.parts.join(\"\");\n }\n\n toRegExp(): RegExp {\n const pattern = this.toString();\n return new RegExp(pattern, [...this.flags].join(\"\"));\n }\n}\n\nfunction escapeLiteral(text: string): string {\n if (!escapeCache.has(text)) {\n escapeCache.set(text, text.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\"));\n }\n return escapeCache.get(text) as string;\n}\n\n/**\n * Memoized predefined patterns\n */\nconst createRegex = (): HumanRegex => new HumanRegex();\n\nconst Patterns = (() => {\n const createCachedPattern = (builder: () => HumanRegex): (() => RegExp) => {\n const regex = builder().toRegExp();\n return () => regex;\n };\n\n return {\n email: createCachedPattern(() =>\n createRegex()\n .startAnchor()\n .word()\n .oneOrMore()\n .literal(\"@\")\n .word()\n .oneOrMore()\n .startGroup()\n .literal(\".\")\n .word()\n .oneOrMore()\n .endGroup()\n .zeroOrMore()\n .literal(\".\")\n .letter()\n .atLeast(2)\n .endAnchor()\n ),\n url: createCachedPattern(() =>\n createRegex()\n .startAnchor()\n .protocol()\n .optional()\n .www()\n .word()\n .oneOrMore()\n .literal(\".\")\n .tld()\n .path()\n .optional()\n .endAnchor()\n ),\n phoneInternational: createCachedPattern(() =>\n createRegex()\n .startAnchor()\n .literal(\"+\")\n .digit()\n .between(1, 3)\n .literal(\"-\")\n .digit()\n .between(3, 14)\n .endAnchor()\n ),\n };\n})();\n\n// Public API\nexport { createRegex, Patterns, Flags, Ranges, Quantifiers };\n"],"names":["escapeCache","Map","Flags","GLOBAL","NON_SENSITIVE","MULTILINE","DOT_ALL","UNICODE","STICKY","Ranges","Object","freeze","digit","lowercaseLetter","uppercaseLetter","letter","alphanumeric","anyCharacter","Quantifiers","zeroOrMore","oneOrMore","optional","HumanRegex","constructor","this","parts","flags","Set","add","special","word","whitespace","literal","text","has","set","replace","get","escapeLiteral","or","range","name","Error","hasSpecialCharacter","hasDigit","hasLetter","exactly","n","atLeast","atMost","between","min","max","startGroup","endGroup","startAnchor","endAnchor","global","nonSensitive","protocol","www","tld","path","part","push","toString","join","toRegExp","pattern","RegExp","createRegex","Patterns","createCachedPattern","builder","regex","email","url","phoneInternational"],"mappings":"AAEA,MAAMA,EAAc,IAAIC,IAKlBC,EAAQ,CACZC,OAAQ,IACRC,cAAe,IACfC,UAAW,IACXC,QAAS,IACTC,QAAS,IACTC,OAAQ,KAMJC,EAASC,OAAOC,OAAO,CAC3BC,MAAO,MACPC,gBAAiB,MACjBC,gBAAiB,MACjBC,OAAQ,SACRC,aAAc,YACdC,aAAc,MAQVC,EAAcR,OAAOC,OAAO,CAChCQ,WAAY,IACZC,UAAW,IACXC,SAAU,MAGZ,MAAMC,EAIJ,WAAAC,GACEC,KAAKC,MAAQ,GACbD,KAAKE,MAAQ,IAAIC,IAInB,KAAAf,GACE,OAAOY,KAAKI,IAAI,OAGlB,OAAAC,GACE,OAAOL,KAAKI,IAAI,oBAGlB,IAAAE,GACE,OAAON,KAAKI,IAAI,OAGlB,UAAAG,GACE,OAAOP,KAAKI,IAAI,OAGlB,OAAAI,CAAQC,GACN,OAAOT,KAAKI,IA4HhB,SAAuBK,GAChBjC,EAAYkC,IAAID,IACnBjC,EAAYmC,IAAIF,EAAMA,EAAKG,QAAQ,sBAAuB,SAE5D,OAAOpC,EAAYqC,IAAIJ,EACzB,CAjIoBK,CAAcL,IAGhC,EAAAM,GACE,OAAOf,KAAKI,IAAI,KAGlB,KAAAY,CAAMC,GACJ,MAAMD,EAAQ/B,EAAOgC,GACrB,IAAKD,EAAO,MAAM,IAAIE,MAAM,kBAAkBD,KAC9C,OAAOjB,KAAKI,IAAI,IAAIY,MAGtB,MAAAz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S,IAAI5C,KAAKE,OAAOwC,KAAK,MAc9C,MAAAI,EAAc,IAAkB,IAAIhD,EAEpCiD,EAAW,MACf,MAAMC,EAAuBC,IAC3B,MAAMC,EAAQD,IAAUN,WACxB,MAAO,IAAMO,CAAK,EAGpB,MAAO,CACLC,MAAOH,GAAoB,IACzBF,IACGf,cACAzB,OACAV,YACAY,QAAQ,KACRF,OACAV,YACAiC,aACArB,QAAQ,KACRF,OACAV,YACAkC,WACAnC,aACAa,QAAQ,KACRjB,SACAiC,QAAQ,GACRQ,cAELoB,IAAKJ,GAAoB,IACvBF,IACGf,cACAI,WACAtC,WACAuC,MACA9B,OACAV,YACAY,QAAQ,KACR6B,MACAC,OACAzC,WACAmC,cAELqB,mBAAoBL,GAAoB,IACtCF,IACGf,cACAvB,QAAQ,KACRpB,QACAsC,QAAQ,EAAG,GACXlB,QAAQ,KACRpB,QACAsC,QAAQ,EAAG,IACXM,cAGR,EApDgB"} | ||
| {"version":3,"file":"human-regex.esm.js","sources":["../src/human-regex.ts"],"sourcesContent":["/**\n * @module human-regex\n * A utility for building regular expressions with a human-readable syntax.\n */\n\n/** Static cache for escaped literals */\nconst escapeCache = new Map<string, string>();\n\n/** Regular expression flags */\nconst Flags = {\n /** Global match */\n GLOBAL: \"g\",\n /** Case-insensitive matching */\n NON_SENSITIVE: \"i\",\n /** Multi-line matching */\n MULTILINE: \"m\",\n /** Dot matches newlines */\n DOT_ALL: \"s\",\n /** Enable Unicode features */\n UNICODE: \"u\",\n /** Sticky matching */\n STICKY: \"y\",\n} as const;\n\n/** Predefined character ranges */\nconst Ranges = Object.freeze({\n digit: \"0-9\",\n lowercaseLetter: \"a-z\",\n uppercaseLetter: \"A-Z\",\n letter: \"a-zA-Z\",\n alphanumeric: \"a-zA-Z0-9\",\n anyCharacter: \".\",\n});\n\ntype RangeKeys = keyof typeof Ranges;\n\n/** Regular expression quantifiers */\nconst Quantifiers = Object.freeze({\n zeroOrMore: \"*\",\n oneOrMore: \"+\",\n optional: \"?\",\n});\n\n/**\n * Class for building regular expressions using a chainable API\n * @class\n */\nclass HumanRegex {\n private parts: string[];\n private flags: Set<string>;\n\n constructor() {\n this.parts = [];\n this.flags = new Set<string>();\n }\n\n // Basic character types\n /** Matches any digit (0-9) */\n digit(): this {\n return this.add(\"\\\\d\");\n }\n\n /** Positive lookahead for special characters */\n special(): this {\n return this.add(\"(?=.*[!@#$%^&*])\");\n }\n\n /** Matches any word character (a-z, A-Z, 0-9, and _) */\n word(): this {\n return this.add(\"\\\\w\");\n }\n\n /** Matches any whitespace character */\n whitespace(): this {\n return this.add(\"\\\\s\");\n }\n\n /** Matches any non-whitespace character */\n nonWhitespace(): this {\n return this.add(\"\\\\S\");\n }\n\n /** Escapes and matches a literal string */\n literal(text: string): this {\n return this.add(escapeLiteral(text));\n }\n\n /** Alternation operator (OR) */\n or(): this {\n return this.add(\"|\");\n }\n\n /** Matches any character in a predefined range */\n range(name: RangeKeys): this {\n const range = Ranges[name];\n if (!range) throw new Error(`Unknown range: ${name}`);\n return this.add(`[${range}]`);\n }\n\n /** Matches any character not in the specified set */\n notRange(chars: string): this {\n return this.add(`[^${chars}]`);\n }\n\n /** Makes the previous quantifier lazy */\n lazy(): this {\n const lastPart = this.parts.pop();\n if (!lastPart) throw new Error(\"No quantifier to make lazy\");\n return this.add(`${lastPart}?`);\n }\n\n // Grouping constructs\n /** Starts a named capturing group */\n startNamedGroup(name: string): this {\n return this.add(`(?<${name}>`);\n }\n\n /** Matches any uppercase or lowercase letter */\n letter(): this {\n return this.add(\"[a-zA-Z]\");\n }\n\n /** Matches any character except newlines */\n anyCharacter(): this {\n return this.add(\".\");\n }\n\n // Lookaround assertions\n /** Negative lookahead assertion */\n negativeLookahead(pattern: string): this {\n return this.add(`(?!${pattern})`);\n }\n\n /** Positive lookahead assertion */\n positiveLookahead(pattern: string): this {\n return this.add(`(?=${pattern})`);\n }\n\n /** Positive lookbehind assertion */\n positiveLookbehind(pattern: string): this {\n return this.add(`(?<=${pattern})`);\n }\n\n /** Negative lookbehind assertion */\n negativeLookbehind(pattern: string): this {\n return this.add(`(?<!${pattern})`);\n }\n\n // Validation helpers\n /** Positive lookahead for special characters */\n hasSpecialCharacter(): this {\n return this.add(\"(?=.*[!@#$%^&*])\");\n }\n\n /** Positive lookahead for digits */\n hasDigit(): this {\n return this.add(\"(?=.*\\\\d)\");\n }\n\n /** Positive lookahead for letters */\n hasLetter(): this {\n return this.add(\"(?=.*[a-zA-Z])\");\n }\n\n // Quantifiers\n /** Matches exactly n occurrences */\n exactly(n: number): this {\n return this.add(`{${n}}`);\n }\n\n /** Matches at least n occurrences */\n atLeast(n: number): this {\n return this.add(`{${n},}`);\n }\n\n /** Matches at most n occurrences */\n atMost(n: number): this {\n return this.add(`{0,${n}}`);\n }\n\n /** Matches between min and max occurrences */\n between(min: number, max: number): this {\n return this.add(`{${min},${max}}`);\n }\n\n /** Matches one or more occurrences */\n oneOrMore(): this {\n return this.add(Quantifiers.oneOrMore);\n }\n\n /** Matches zero or one occurrences */\n optional(): this {\n return this.add(Quantifiers.optional);\n }\n\n /** Matches zero or more occurrences */\n zeroOrMore(): this {\n return this.add(Quantifiers.zeroOrMore);\n }\n\n // Anchors and boundaries\n /** Starts a non-capturing group */\n startGroup(): this {\n return this.add(\"(?:\");\n }\n\n /** Starts a capturing group */\n startCaptureGroup(): this {\n return this.add(\"(\");\n }\n\n /** Word boundary assertion */\n wordBoundary(): this {\n return this.add(\"\\\\b\");\n }\n\n /** Non-word boundary assertion */\n nonWordBoundary(): this {\n return this.add(\"\\\\B\");\n }\n\n /** Ends the current group */\n endGroup(): this {\n return this.add(\")\");\n }\n\n /** Start of string/line anchor */\n startAnchor(): this {\n return this.add(\"^\");\n }\n\n /** End of string/line anchor */\n endAnchor(): this {\n return this.add(\"$\");\n }\n\n // Flag management\n /** Adds global match flag */\n global(): this {\n this.flags.add(Flags.GLOBAL);\n return this;\n }\n\n /** Adds case-insensitive match flag */\n nonSensitive(): this {\n this.flags.add(Flags.NON_SENSITIVE);\n return this;\n }\n\n /** Adds multiline match flag */\n multiline(): this {\n this.flags.add(Flags.MULTILINE);\n return this;\n }\n\n /** Adds dot-all match flag */\n dotAll(): this {\n this.flags.add(Flags.DOT_ALL);\n return this;\n }\n\n /** Adds sticky match flag */\n sticky(): this {\n this.flags.add(Flags.STICKY);\n return this;\n }\n\n // Unicode properties\n /** Matches Unicode characters */\n unicodeChar(variant?: \"u\" | \"l\" | \"t\" | \"m\" | \"o\"): this {\n this.flags.add(Flags.UNICODE);\n const validVariants = new Set([\"u\", \"l\", \"t\", \"m\", \"o\"] as const);\n\n if (variant !== undefined && !validVariants.has(variant)) {\n throw new Error(`Invalid Unicode letter variant: ${variant}`);\n }\n\n return this.add(`\\\\p{L${variant ?? \"\"}}`);\n }\n\n /** Matches Unicode digits */\n unicodeDigit(): this {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{N}\");\n }\n\n /** Matches Unicode punctuation */\n unicodePunctuation(): this {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{P}\");\n }\n\n /** Matches Unicode symbols */\n unicodeSymbol(): this {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{S}\");\n }\n\n // Complex patterns\n /** Repeats the previous pattern exactly count times */\n repeat(count: number): this {\n if (this.parts.length === 0) {\n throw new Error(\"No pattern to repeat\");\n }\n\n const lastPart = this.parts.pop();\n this.parts.push(`(${lastPart}){${count}}`);\n return this;\n }\n\n /** Matches an IPv4 octet */\n ipv4Octet(): this {\n return this.add(\"(25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]\\\\d|\\\\d)\");\n }\n\n /** Matches URL protocol */\n protocol(): this {\n return this.add(\"https?://\");\n }\n\n /** Matches optional www subdomain */\n www(): this {\n return this.add(\"(www\\\\.)?\");\n }\n\n /** Matches common TLDs */\n tld(): this {\n return this.add(\"(com|org|net)\");\n }\n\n /** Matches URL path segments */\n path(): this {\n return this.add(\"(/\\\\w+)*\");\n }\n\n // Internal utilities\n private add(part: string): this {\n this.parts.push(part);\n return this;\n }\n\n /** Returns the constructed pattern string */\n toString(): string {\n return this.parts.join(\"\");\n }\n\n /** Compiles the pattern to a RegExp object */\n toRegExp(): RegExp {\n return new RegExp(this.toString(), [...this.flags].join(\"\"));\n }\n}\n\n/** Escapes special characters in a literal string */\nfunction escapeLiteral(text: string): string {\n if (!escapeCache.has(text)) {\n escapeCache.set(text, text.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\"));\n }\n return escapeCache.get(text)!;\n}\n\n/** Factory function for creating HumanRegex instances */\nconst createRegex = (): HumanRegex => new HumanRegex();\n\n/** Predefined common pattern templates */\nconst Patterns = (() => {\n const createCachedPattern = (builder: () => HumanRegex) => {\n const regex = builder().toRegExp();\n return () => new RegExp(regex.source, regex.flags);\n };\n\n return {\n /** Email address pattern */\n email: createCachedPattern(() =>\n createRegex()\n .startAnchor()\n .word()\n .oneOrMore()\n .literal(\"@\")\n .word()\n .oneOrMore()\n .startGroup()\n .literal(\".\")\n .word()\n .oneOrMore()\n .endGroup()\n .zeroOrMore()\n .literal(\".\")\n .letter()\n .atLeast(2)\n .endAnchor()\n ),\n /** URL pattern */\n url: createCachedPattern(() =>\n createRegex()\n .startAnchor()\n .protocol()\n .www()\n .word()\n .oneOrMore()\n .literal(\".\")\n .tld()\n .path()\n .endAnchor()\n ),\n /** International phone number pattern */\n phoneInternational: createCachedPattern(() =>\n createRegex()\n .startAnchor()\n .literal(\"+\")\n .digit()\n .between(1, 3)\n .literal(\"-\")\n .digit()\n .between(3, 14)\n .endAnchor()\n ),\n };\n})();\n\nexport { createRegex, Patterns, Flags, Ranges, Quantifiers };\n"],"names":["escapeCache","Map","Flags","GLOBAL","NON_SENSITIVE","MULTILINE","DOT_ALL","UNICODE","STICKY","Ranges","Object","freeze","digit","lowercaseLetter","uppercaseLetter","letter","alphanumeric","anyCharacter","Quantifiers","zeroOrMore","oneOrMore","optional","HumanRegex","constructor","this","parts","flags","Set","add","special","word","whitespace","nonWhitespace","literal","text","has","set","replace","get","escapeLiteral","or","range","name","Error","notRange","chars","lazy","lastPart","pop","startNamedGroup","negativeLookahead","pattern","positiveLookahead","positiveLookbehind","negativeLookbehind","hasSpecialCharacter","hasDigit","hasLetter","exactly","n","atLeast","atMost","between","min","max","startGroup","startCaptureGroup","wordBoundary","nonWordBoundary","endGroup","startAnchor","endAnchor","global","nonSensitive","multiline","dotAll","sticky","unicodeChar","variant","validVariants","undefined","unicodeDigit","unicodePunctuation","unicodeSymbol","repeat","count","length","push","ipv4Octet","protocol","www","tld","path","part","toString","join","toRegExp","RegExp","createRegex","Patterns","createCachedPattern","builder","regex","source","email","url","phoneInternational"],"mappings":"AAMA,MAAMA,EAAc,IAAIC,IAGlBC,EAAQ,CAEZC,OAAQ,IAERC,cAAe,IAEfC,UAAW,IAEXC,QAAS,IAETC,QAAS,IAETC,OAAQ,KAIJC,EAASC,OAAOC,OAAO,CAC3BC,MAAO,MACPC,gBAAiB,MACjBC,gBAAiB,MACjBC,OAAQ,SACRC,aAAc,YACdC,aAAc,MAMVC,EAAcR,OAAOC,OAAO,CAChCQ,WAAY,IACZC,UAAW,IACXC,SAAU,MAOZ,MAAMC,EAIJ,WAAAC,GACEC,KAAKC,MAAQ,GACbD,KAAKE,MAAQ,IAAIC,IAKnB,KAAAf,GACE,OAAOY,KAAKI,IAAI,OAIlB,OAAAC,GACE,OAAOL,KAAKI,IAAI,oBAIlB,IAAAE,GACE,OAAON,KAAKI,IAAI,OAIlB,UAAAG,GACE,OAAOP,KAAKI,IAAI,OAIlB,aAAAI,GACE,OAAOR,KAAKI,IAAI,OAIlB,OAAAK,CAAQC,GACN,OAAOV,KAAKI,IA6QhB,SAAuBM,GAChBlC,EAAYmC,IAAID,IACnBlC,EAAYoC,IAAIF,EAAMA,EAAKG,QAAQ,sBAAuB,SAE5D,OAAOrC,EAAYsC,IAAIJ,EACzB,CAlRoBK,CAAcL,IAIhC,EAAAM,GACE,OAAOhB,KAAKI,IAA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KAAKC,MAAM8D,KAAK,IAAIxC,MAAasC,MAC1B7D,KAIT,SAAAgE,GACE,OAAOhE,KAAKI,IAAI,4CAIlB,QAAA6D,GACE,OAAOjE,KAAKI,IAAI,aAIlB,GAAA8D,GACE,OAAOlE,KAAKI,IAAI,aAIlB,GAAA+D,GACE,OAAOnE,KAAKI,IAAI,iBAIlB,IAAAgE,GACE,OAAOpE,KAAKI,IAAI,YAIV,GAAAA,CAAIiE,GAEV,OADArE,KAAKC,MAAM8D,KAAKM,GACTrE,KAIT,QAAAsE,GACE,OAAOtE,KAAKC,MAAMsE,KAAK,IAIzB,QAAAC,GACE,OAAO,IAAIC,OAAOzE,KAAKsE,WAAY,IAAItE,KAAKE,OAAOqE,KAAK,MAatD,MAAAG,EAAc,IAAkB,IAAI5E,EAGpC6E,EAAW,MACf,MAAMC,EAAuBC,IAC3B,MAAMC,EAAQD,IAAUL,WACxB,MAAO,IAAM,IAAIC,OAAOK,EAAMC,OAAQD,EAAM5E,MAAM,EAGpD,MAAO,CAEL8E,MAAOJ,GAAoB,IACzBF,IACG5B,cACAxC,OACAV,YACAa,QAAQ,KACRH,OACAV,YACA6C,aACAhC,QAAQ,KACRH,OACAV,YACAiD,WACAlD,aACAc,QAAQ,KACRlB,SACA6C,QAAQ,GACRW,cAGLkC,IAAKL,GAAoB,IACvBF,IACG5B,cACAmB,WACAC,MACA5D,OACAV,YACAa,QAAQ,KACR0D,MACAC,OACArB,cAGLmC,mBAAoBN,GAAoB,IACtCF,IACG5B,cACArC,QAAQ,KACRrB,QACAkD,QAAQ,EAAG,GACX7B,QAAQ,KACRrB,QACAkD,QAAQ,EAAG,IACXS,cAGR,EArDgB"} |
+44
-2
@@ -1,2 +0,2 @@ | ||
| export interface Flags { | ||
| export const Flags: { | ||
| GLOBAL: string; | ||
@@ -8,3 +8,3 @@ NON_SENSITIVE: string; | ||
| STICKY: string; | ||
| } | ||
| }; | ||
@@ -34,2 +34,3 @@ export declare const Ranges: { | ||
| // Character classes and literals | ||
| digit(): this; | ||
@@ -39,8 +40,13 @@ special(): this; | ||
| whitespace(): this; | ||
| nonWhitespace(): this; | ||
| literal(text: string): this; | ||
| or(): this; | ||
| // Ranges and character sets | ||
| range(name: RangeKeys): this; | ||
| notRange(chars: string): this; | ||
| letter(): this; | ||
| anyCharacter(): this; | ||
| // Quantifiers | ||
| exactly(n: number): this; | ||
@@ -53,10 +59,45 @@ atLeast(n: number): this; | ||
| zeroOrMore(): this; | ||
| lazy(): this; | ||
| // Grouping and capturing | ||
| startGroup(): this; | ||
| startCaptureGroup(): this; | ||
| startNamedGroup(name: string): this; | ||
| endGroup(): this; | ||
| // Boundaries and anchors | ||
| wordBoundary(): this; | ||
| nonWordBoundary(): this; | ||
| startAnchor(): this; | ||
| endAnchor(): this; | ||
| // Lookaround assertions | ||
| negativeLookahead(pattern: string): this; | ||
| positiveLookahead(pattern: string): this; | ||
| positiveLookbehind(pattern: string): this; | ||
| negativeLookbehind(pattern: string): this; | ||
| // Validation helpers | ||
| hasSpecialCharacter(): this; | ||
| hasDigit(): this; | ||
| hasLetter(): this; | ||
| // Flag management | ||
| global(): this; | ||
| nonSensitive(): this; | ||
| multiline(): this; | ||
| dotAll(): this; | ||
| sticky(): this; | ||
| // Unicode properties | ||
| unicodeChar(variant?: "u" | "l" | "t" | "m" | "o"): this; | ||
| unicodeDigit(): this; | ||
| unicodePunctuation(): this; | ||
| unicodeSymbol(): this; | ||
| // Repetition and complex patterns | ||
| repeat(count: number): this; | ||
| ipv4Octet(): this; | ||
| // URL related helpers | ||
| protocol(): this; | ||
@@ -67,2 +108,3 @@ www(): this; | ||
| // Final output | ||
| toString(): string; | ||
@@ -69,0 +111,0 @@ toRegExp(): RegExp; |
+1
-1
| { | ||
| "name": "human-regex", | ||
| "version": "2.0.5", | ||
| "version": "2.1.0", | ||
| "description": "Human-friendly regex builder with English-like syntax", | ||
@@ -5,0 +5,0 @@ "main": "dist/human-regex.cjs.js", |
+51
-30
@@ -31,2 +31,3 @@ [](https://www.npmjs.com/package/human-regex) | ||
| - [Flags](#flags) | ||
| - [Unicode Properties](#unicode-properties) | ||
| - [Predefined Patterns](#predefined-patterns) | ||
@@ -87,32 +88,40 @@ - [Examples](#examples) | ||
| | Method | Description | Example Output | | ||
| | ------------------ | ------------------------------------------- | -------------- | ---- | | ||
| | `.digit()` | Adds a digit pattern (`\d`). | `\d` | | ||
| | `.word()` | Adds a word character pattern (`\w`). | `\w` | | ||
| | `.whitespace()` | Adds a whitespace character pattern (`\s`). | `\s` | | ||
| | `.anyCharacter()` | Adds a pattern for any character (`.`). | `.` | | ||
| | `.literal("text")` | Adds a literal text pattern. | `["text"]` | | ||
| | `.or()` | Adds an OR pattern (` | `). | `\|` | | ||
| | `.range("digit")` | Adds a range pattern for digits (`0-9`). | `[0-9]` | | ||
| | Method | Description | Example Output | | ||
| | -------------------- | ----------------------------------------------- | -------------- | | ||
| | `.digit()` | Adds a digit pattern (`\d`). | `\d` | | ||
| | `.word()` | Adds a word character pattern (`\w`). | `\w` | | ||
| | `.whitespace()` | Adds a whitespace character pattern (`\s`). | `\s` | | ||
| | `.nonWhitespace()` | Adds a non-whitespace character pattern (`\S`). | `\S` | | ||
| | `.anyCharacter()` | Adds a pattern for any character (`.`). | `.` | | ||
| | `.literal("text")` | Adds a literal text pattern. | `["text"]` | | ||
| | `.or()` | Adds an OR pattern. | `\|` | | ||
| | `.range("digit")` | Adds a range pattern for digits (`0-9`). | `[0-9]` | | ||
| | `.notRange("aeiou")` | Adds a range pattern for digits (`0-9`). | `[0-9]` | | ||
| ### Quantifiers | ||
| | Method | Description | Example Output | | ||
| | -------------------- | -------------------------------------- | -------------- | | ||
| | `.exactly(n)` | Adds an exact quantifier (`{n}`). | `{n}` | | ||
| | `.atLeast(n)` | Adds a minimum quantifier (`{n,}`). | `{n,}` | | ||
| | `.atMost(n)` | Adds a maximum quantifier (`{0,n}`). | `{0,n}` | | ||
| | `.between(min, max)` | Adds a range quantifier (`{min,max}`). | `{min,max}` | | ||
| | `.oneOrMore()` | Adds a one-or-more quantifier (`+`). | `+` | | ||
| | `.optional()` | Adds an optional quantifier (`?`). | `?` | | ||
| | `.zeroOrMore()` | Adds a zero-or-more quantifier (`*`). | `*` | | ||
| | Method | Description | Example Output | | ||
| | -------------------- | ------------------------------------------------- | -------------- | | ||
| | `.exactly(n)` | Adds an exact quantifier (`{n}`). | `{n}` | | ||
| | `.atLeast(n)` | Adds a minimum quantifier (`{n,}`). | `{n,}` | | ||
| | `.atMost(n)` | Adds a maximum quantifier (`{0,n}`). | `{0,n}` | | ||
| | `.between(min, max)` | Adds a range quantifier (`{min,max}`). | `{min,max}` | | ||
| | `.oneOrMore()` | Adds a one-or-more quantifier (`+`). | `+` | | ||
| | `.optional()` | Adds an optional quantifier (`?`). | `?` | | ||
| | `.zeroOrMore()` | Adds a zero-or-more quantifier (`*`). | `*` | | ||
| | `.lazy()` | Makes the previous quantifier lazy. | `?` | | ||
| | `.repeat(count)` | Repeats the previous pattern exactly count times. | `{count}` | | ||
| ### Anchors & Groups | ||
| | Method | Description | Example Output | | ||
| | ---------------- | ------------------------------------- | -------------- | | ||
| | `.startGroup()` | Starts a non-capturing group (`(?:`). | `(?:` | | ||
| | `.endGroup()` | Ends a group (`)`). | `)` | | ||
| | `.startAnchor()` | Adds a start anchor (`^`). | `^` | | ||
| | `.endAnchor()` | Adds an end anchor (`$`). | `$` | | ||
| | Method | Description | Example Output | | ||
| | -------------------------- | ------------------------------------------ | -------------- | | ||
| | `.startGroup()` | Starts a non-capturing group (`(?:`). | `(?:` | | ||
| | `.startCaptureGroup()` | Starts a capturing group (`(`). | `(` | | ||
| | `.startNamedGroup("name")` | Starts a named capturing group. | `(?<name>` | | ||
| | `.endGroup()` | Ends a group (`)`). | `)` | | ||
| | `.startAnchor()` | Adds a start anchor (`^`). | `^` | | ||
| | `.endAnchor()` | Adds an end anchor (`$`). | `$` | | ||
| | `.wordBoundary()` | Adds a word boundary assertion (`\b`). | `\b` | | ||
| | `.nonWordBoundary()` | Adds a non-word boundary assertion (`\B`). | `\B` | | ||
@@ -129,8 +138,8 @@ ### Validation Helpers | ||
| | Method | Description | Example Output | | ||
| | ------------- | -------------------------------------- | --------------------- | | ||
| | `.protocol()` | Adds a protocol pattern (`https?://`). | `https?://` | | ||
| | `.www()` | Adds a www pattern (`(www\.)?`). | `(www\.)?` | | ||
| | `.tld()` | Adds a top-level domain pattern. | `["(com\|org\|net)"]` | | ||
| | `.path()` | Adds a path pattern (`(/\w+)*`). | `(/\w+)*` | | ||
| | Method | Description | Example Output | | ||
| | ------------- | -------------------------------------- | ------------------------- | | ||
| | `.protocol()` | Adds a protocol pattern (`https?://`). | `https?://` | | ||
| | `.www()` | Adds a www pattern (`(www\.)?`). | `(www\.)?` | | ||
| | `.path()` | Adds a path pattern (`(/\w+)*`). | `(/\w+)*` | | ||
| | `.tld()` | Adds a top-level domain pattern. | \[\"\(com\|org\|net\)\"\] | | ||
@@ -143,3 +152,15 @@ ### Flags | ||
| | `.nonSensitive()` | Adds the case-insensitive flag (`i`). | `i` | | ||
| | `.multiline()` | Adds the multiline flag (`m`). | `m` | | ||
| | `.dotAll()` | Adds the dot-all flag (`s`). | `s` | | ||
| | `.sticky()` | Adds the sticky flag (`y`). | `y` | | ||
| ### Unicode Properties | ||
| | Method | Description | Example Output | | ||
| | ----------------------- | ---------------------------- | -------------- | | ||
| | `.unicodeChar()` | Matches Unicode characters. | `\p{L}` | | ||
| | `.unicodeDigit()` | Matches Unicode digits. | `\p{N}` | | ||
| | `.unicodePunctuation()` | Matches Unicode punctuation. | `\p{P}` | | ||
| | `.unicodeSymbol()` | Matches Unicode symbols. | `\p{S}` | | ||
| ### Predefined Patterns | ||
@@ -146,0 +167,0 @@ |
52272
55.87%203
28.48%200
11.73%