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")}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; | ||
| "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}?`)}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])")}optional(){return this.add(a.optional)}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)}zeroOrMore(){return this.add(a.zeroOrMore)}startNamedGroup(t){return this.add(`(?<${t}>`)}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":["const escapeCache = new Map<string, string>();\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\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\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 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 nonWhitespace(): 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 notRange(chars: string): this {\n return this.add(`[^${chars}]`);\n }\n\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 startNamedGroup(name: string): this {\n return this.add(`(?<${name}>`);\n }\n\n letter(): this {\n return this.add(\"[a-zA-Z]\");\n }\n\n anyCharacter(): this {\n return this.add(\".\");\n }\n\n negativeLookahead(pattern: string): this {\n return this.add(`(?!${pattern})`);\n }\n\n positiveLookahead(pattern: string): this {\n return this.add(`(?=${pattern})`);\n }\n\n positiveLookbehind(pattern: string): this {\n return this.add(`(?<=${pattern})`);\n }\n\n negativeLookbehind(pattern: string): this {\n return this.add(`(?<!${pattern})`);\n }\n\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 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 startGroup(): this {\n return this.add(\"(?:\");\n }\n\n startCaptureGroup(): this {\n return this.add(\"(\");\n }\n\n wordBoundary(): this {\n return this.add(\"\\\\b\");\n }\n\n nonWordBoundary(): this {\n return this.add(\"\\\\B\");\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 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 multiline(): this {\n this.flags.add(Flags.MULTILINE);\n return this;\n }\n\n dotAll(): this {\n this.flags.add(Flags.DOT_ALL);\n return this;\n }\n\n sticky(): this {\n this.flags.add(Flags.STICKY);\n return this;\n }\n\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 unicodeDigit(): this {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{N}\");\n }\n\n unicodePunctuation(): this {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{P}\");\n }\n\n unicodeSymbol(): this {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{S}\");\n }\n\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 ipv4Octet(): this {\n return this.add(\"(25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]\\\\d|\\\\d)\");\n }\n\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 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 return new RegExp(this.toString(), [...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)!;\n}\n\nconst createRegex = (): HumanRegex => new HumanRegex();\n\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: 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 .www()\n .word()\n .oneOrMore()\n .literal(\".\")\n .tld()\n .path()\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\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":"aAAA,MAAMA,EAAc,IAAIC,IAElBC,EAAQ,CACZC,OAAQ,IACRC,cAAe,IACfC,UAAW,IACXC,QAAS,IACTC,QAAS,IACTC,OAAQ,KAGJC,EAASC,OAAOC,OAAO,CAC3BC,MAAO,MACPC,gBAAiB,MACjBC,gBAAiB,MACjBC,OAAQ,SACRC,aAAc,YACdC,aAAc,MAKVC,EAAcR,OAAOC,OAAO,CAChCQ,WAAY,IACZC,UAAW,IACXC,SAAU,MAGZ,MAAMC,EAIJ,WAAAC,GACEC,KAAKC,MAAQ,GACbD,KAAKE,MAAQ,IAAIC,IAGnB,KAAAf,GACE,OAAOY,KAAKI,IAAI,OAGlB,OAAAC,GACE,OAAOL,KAAKI,IAAI,oBAGlB,IAAAE,GACE,OAAON,KAAKI,IAAI,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F,IACG5B,cACArC,QAAQ,KACRrB,QACAkD,QAAQ,EAAG,GACX7B,QAAQ,KACRrB,QACAkD,QAAQ,EAAG,IACXS,cAGR,EAlDgB"} | ||
| {"version":3,"file":"human-regex.cjs.js","sources":["../src/human-regex.ts"],"sourcesContent":["type PartialBut<T, K extends keyof T> = Partial<T> & Pick<T, K>;\n\nconst escapeCache = new Map<string, string>();\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\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\nconst Quantifiers = Object.freeze({\n zeroOrMore: \"*\",\n oneOrMore: \"+\",\n optional: \"?\",\n});\n\ntype Quantifiers =\n | \"exactly\"\n | \"atLeast\"\n | \"atMost\"\n | \"between\"\n | \"oneOrMore\"\n | \"zeroOrMore\"\n | \"repeat\";\ntype QuantifierMethods = Quantifiers | \"optional\" | \"lazy\";\n\ntype WithLazy = HumanRegex;\ntype Base = Omit<HumanRegex, \"lazy\">;\n\ntype AtStart = Omit<Base, QuantifierMethods | \"endGroup\">;\ntype AfterAnchor = Omit<Base, QuantifierMethods | \"or\">;\ntype SimpleQuantifier = Omit<Base, Quantifiers>;\ntype LazyQuantifier = Omit<WithLazy, Quantifiers>;\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 digit(): Base {\n return this.add(\"\\\\d\");\n }\n\n special(): Base {\n return this.add(\"(?=.*[!@#$%^&*])\");\n }\n\n word(): Base {\n return this.add(\"\\\\w\");\n }\n\n whitespace(): Base {\n return this.add(\"\\\\s\");\n }\n\n nonWhitespace(): Base {\n return this.add(\"\\\\S\");\n }\n\n literal(text: string): this {\n return this.add(escapeLiteral(text));\n }\n\n or(): AfterAnchor {\n return this.add(\"|\");\n }\n\n range(name: RangeKeys): Base {\n const range = Ranges[name];\n if (!range) throw new Error(`Unknown range: ${name}`);\n return this.add(`[${range}]`);\n }\n\n notRange(chars: string): Base {\n return this.add(`[^${chars}]`);\n }\n\n lazy(): Base {\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 letter(): Base {\n return this.add(\"[a-zA-Z]\");\n }\n\n anyCharacter(): Base {\n return this.add(\".\");\n }\n\n negativeLookahead(pattern: string): Base {\n return this.add(`(?!${pattern})`);\n }\n\n positiveLookahead(pattern: string): Base {\n return this.add(`(?=${pattern})`);\n }\n\n positiveLookbehind(pattern: string): Base {\n return this.add(`(?<=${pattern})`);\n }\n\n negativeLookbehind(pattern: string): Base {\n return this.add(`(?<!${pattern})`);\n }\n\n hasSpecialCharacter(): Base {\n return this.add(\"(?=.*[!@#$%^&*])\");\n }\n\n hasDigit(): Base {\n return this.add(\"(?=.*\\\\d)\");\n }\n\n hasLetter(): Base {\n return this.add(\"(?=.*[a-zA-Z])\");\n }\n\n optional(): SimpleQuantifier {\n return this.add(Quantifiers.optional);\n }\n\n exactly(n: number): SimpleQuantifier {\n return this.add(`{${n}}`);\n }\n\n atLeast(n: number): LazyQuantifier {\n return this.add(`{${n},}`);\n }\n\n atMost(n: number): LazyQuantifier {\n return this.add(`{0,${n}}`);\n }\n\n between(min: number, max: number): LazyQuantifier {\n return this.add(`{${min},${max}}`);\n }\n\n oneOrMore(): LazyQuantifier {\n return this.add(Quantifiers.oneOrMore);\n }\n\n zeroOrMore(): LazyQuantifier {\n return this.add(Quantifiers.zeroOrMore);\n }\n\n startNamedGroup(name: string): AfterAnchor {\n return this.add(`(?<${name}>`);\n }\n\n startGroup(): AfterAnchor {\n return this.add(\"(?:\");\n }\n\n startCaptureGroup(): AfterAnchor {\n return this.add(\"(\");\n }\n\n wordBoundary(): Base {\n return this.add(\"\\\\b\");\n }\n\n nonWordBoundary(): Base {\n return this.add(\"\\\\B\");\n }\n\n endGroup(): Base {\n return this.add(\")\");\n }\n\n startAnchor(): AfterAnchor {\n return this.add(\"^\");\n }\n\n endAnchor(): AfterAnchor {\n return this.add(\"$\");\n }\n\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 multiline(): this {\n this.flags.add(Flags.MULTILINE);\n return this;\n }\n\n dotAll(): this {\n this.flags.add(Flags.DOT_ALL);\n return this;\n }\n\n sticky(): this {\n this.flags.add(Flags.STICKY);\n return this;\n }\n\n unicodeChar(variant?: \"u\" | \"l\" | \"t\" | \"m\" | \"o\"): Base {\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 unicodeDigit(): Base {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{N}\");\n }\n\n unicodePunctuation(): Base {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{P}\");\n }\n\n unicodeSymbol(): Base {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{S}\");\n }\n\n repeat(count: number): Base {\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 ipv4Octet(): Base {\n return this.add(\"(25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]\\\\d|\\\\d)\");\n }\n\n protocol(): Base {\n return this.add(\"https?://\");\n }\n\n www(): Base {\n return this.add(\"(www\\\\.)?\");\n }\n\n tld(): Base {\n return this.add(\"(com|org|net)\");\n }\n\n path(): Base {\n return this.add(\"(/\\\\w+)*\");\n }\n\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 return new RegExp(this.toString(), [...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)!;\n}\n\nconst createRegex = (): AtStart => new HumanRegex();\n\nconst Patterns = (() => {\n const createCachedPattern = (builder: () => PartialBut<HumanRegex, \"toRegExp\">) => {\n const regex = builder().toRegExp();\n return () => new RegExp(regex.source, regex.flags);\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 .www()\n .word()\n .oneOrMore()\n .literal(\".\")\n .tld()\n .path()\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\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","negativeLookahead","pattern","positiveLookahead","positiveLookbehind","negativeLookbehind","hasSpecialCharacter","hasDigit","hasLetter","exactly","n","atLeast","atMost","between","min","max","startNamedGroup","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":"aAEA,MAAMA,EAAc,IAAIC,IAElBC,EAAQ,CACZC,OAAQ,IACRC,cAAe,IACfC,UAAW,IACXC,QAAS,IACTC,QAAS,IACTC,OAAQ,KAGJC,EAASC,OAAOC,OAAO,CAC3BC,MAAO,MACPC,gBAAiB,MACjBC,gBAAiB,MACjBC,OAAQ,SACRC,aAAc,YACdC,aAAc,MAKVC,EAAcR,OAAOC,OAAO,CAChCQ,WAAY,IACZC,UAAW,IACXC,SAAU,MAqBZ,MAAMC,EAIJ,WAAAC,GACEC,KAAKC,MAAQ,GACbD,KAAKE,MAAQ,IAAIC,IAGnB,KAAAf,GACE,OAAOY,KAAKI,IAAI,OAGlB,OAAAC,GACE,OAAOL,KAAKI,IAAI,oBAGlB,IAAAE,GACE,OAAON,KAAKI,IAAI,OAGlB,UAAAG,GACE,OAAOP,KAAKI,IAAI,OAGlB,aAAAI,GACE,OAAOR,KAAKI,IAAI,OAGlB,OAAAK,CAAQC,GACN,OAAOV,KAAKI,IAsNhB,SAAuBM,GAChBlC,EAAYmC,IAAID,IACnBlC,EAAYoC,IAAIF,EAAMA,EAAKG,QAAQ,sBAAuB,SAE5D,OAAOrC,EAAYsC,IAAIJ,EACzB,CA3NoBK,CAAcL,IAGhC,EAAAM,GACE,OAAOhB,KAAKI,IAAI,KAGlB,KAAAa,CAAMC,GACJ,MAAMD,EAAQhC,EAAOiC,GACrB,IAAKD,EAAO,MAAM,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E,KAAKI,IAAI,4CAGlB,QAAA6D,GACE,OAAOjE,KAAKI,IAAI,aAGlB,GAAA8D,GACE,OAAOlE,KAAKI,IAAI,aAGlB,GAAA+D,GACE,OAAOnE,KAAKI,IAAI,iBAGlB,IAAAgE,GACE,OAAOpE,KAAKI,IAAI,YAGV,GAAAA,CAAIiE,GAEV,OADArE,KAAKC,MAAM8D,KAAKM,GACTrE,KAGT,QAAAsE,GACE,OAAOtE,KAAKC,MAAMsE,KAAK,IAGzB,QAAAC,GACE,OAAO,IAAIC,OAAOzE,KAAKsE,WAAY,IAAItE,KAAKE,OAAOqE,KAAK,MAWtD,MAAAG,EAAc,IAAe,IAAI5E,EAEjC6E,EAAW,MACf,MAAMC,EAAuBC,IAC3B,MAAMC,EAAQD,IAAUL,WACxB,MAAO,IAAM,IAAIC,OAAOK,EAAMC,OAAQD,EAAM5E,MAAM,EAGpD,MAAO,CACL8E,MAAOJ,GAAoB,IACzBF,IACG5B,cACAxC,OACAV,YACAa,QAAQ,KACRH,OACAV,YACA6C,aACAhC,QAAQ,KACRH,OACAV,YACAiD,WACAlD,aACAc,QAAQ,KACRlB,SACA4C,QAAQ,GACRY,cAELkC,IAAKL,GAAoB,IACvBF,IACG5B,cACAmB,WACAC,MACA5D,OACAV,YACAa,QAAQ,KACR0D,MACAC,OACArB,cAELmC,mBAAoBN,GAAoB,IACtCF,IACG5B,cACArC,QAAQ,KACRrB,QACAiD,QAAQ,EAAG,GACX5B,QAAQ,KACRrB,QACAiD,QAAQ,EAAG,IACXU,cAGR,EAlDgB"} |
@@ -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:"."}),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}; | ||
| 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}?`)}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])")}optional(){return this.add(a.optional)}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)}zeroOrMore(){return this.add(a.zeroOrMore)}startNamedGroup(t){return this.add(`(?<${t}>`)}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":["const escapeCache = new Map<string, string>();\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\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\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 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 nonWhitespace(): 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 notRange(chars: string): this {\n return this.add(`[^${chars}]`);\n }\n\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 startNamedGroup(name: string): this {\n return this.add(`(?<${name}>`);\n }\n\n letter(): this {\n return this.add(\"[a-zA-Z]\");\n }\n\n anyCharacter(): this {\n return this.add(\".\");\n }\n\n negativeLookahead(pattern: string): this {\n return this.add(`(?!${pattern})`);\n }\n\n positiveLookahead(pattern: string): this {\n return this.add(`(?=${pattern})`);\n }\n\n positiveLookbehind(pattern: string): this {\n return this.add(`(?<=${pattern})`);\n }\n\n negativeLookbehind(pattern: string): this {\n return this.add(`(?<!${pattern})`);\n }\n\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 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 startGroup(): this {\n return this.add(\"(?:\");\n }\n\n startCaptureGroup(): this {\n return this.add(\"(\");\n }\n\n wordBoundary(): this {\n return this.add(\"\\\\b\");\n }\n\n nonWordBoundary(): this {\n return this.add(\"\\\\B\");\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 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 multiline(): this {\n this.flags.add(Flags.MULTILINE);\n return this;\n }\n\n dotAll(): this {\n this.flags.add(Flags.DOT_ALL);\n return this;\n }\n\n sticky(): this {\n this.flags.add(Flags.STICKY);\n return this;\n }\n\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 unicodeDigit(): this {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{N}\");\n }\n\n unicodePunctuation(): this {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{P}\");\n }\n\n unicodeSymbol(): this {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{S}\");\n }\n\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 ipv4Octet(): this {\n return this.add(\"(25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]\\\\d|\\\\d)\");\n }\n\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 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 return new RegExp(this.toString(), [...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)!;\n}\n\nconst createRegex = (): HumanRegex => new HumanRegex();\n\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: 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 .www()\n .word()\n .oneOrMore()\n .literal(\".\")\n .tld()\n .path()\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\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":"AAAA,MAAMA,EAAc,IAAIC,IAElBC,EAAQ,CACZC,OAAQ,IACRC,cAAe,IACfC,UAAW,IACXC,QAAS,IACTC,QAAS,IACTC,OAAQ,KAGJC,EAASC,OAAOC,OAAO,CAC3BC,MAAO,MACPC,gBAAiB,MACjBC,gBAAiB,MACjBC,OAAQ,SACRC,aAAc,YACdC,aAAc,MAKVC,EAAcR,OAAOC,OAAO,CAChCQ,WAAY,IACZC,UAAW,IACXC,SAAU,MAGZ,MAAMC,EAIJ,WAAAC,GACEC,KAAKC,MAAQ,GACbD,KAAKE,MAAQ,IAAIC,IAGnB,KAAAf,GACE,OAAOY,KAAKI,IAAI,OAGlB,OAAAC,GACE,OAAOL,KAAKI,IAAI,oBAGlB,IAAAE,GACE,OAAON,KAAKI,IAAI,OAGlB,UAAAG,GACE,OAAOP,KAAKI,IAAI,OAGlB,aAAAI,GACE,OAAOR,KAAKI,IAAI,OAGlB,OAAAK,CAAQC,GACN,OAAOV,KAAKI,IAsNhB,SAAuBM,GAChBlC,EAAYmC,IAAID,IACnBlC,EAAYoC,IAAIF,EAAMA,EAAKG,QAAQ,sBAAuB,SAE5D,OAAOrC,EAAYsC,IAAIJ,EACzB,CA3NoBK,CAAcL,IAGhC,EAAAM,GACE,OAAOhB,KAAKI,IAAI,KAGlB,KAAAa,C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,KAAK,IAAIxC,MAAasC,MAC1B7D,KAGT,SAAAgE,GACE,OAAOhE,KAAKI,IAAI,4CAGlB,QAAA6D,GACE,OAAOjE,KAAKI,IAAI,aAGlB,GAAA8D,GACE,OAAOlE,KAAKI,IAAI,aAGlB,GAAA+D,GACE,OAAOnE,KAAKI,IAAI,iBAGlB,IAAAgE,GACE,OAAOpE,KAAKI,IAAI,YAGV,GAAAA,CAAIiE,GAEV,OADArE,KAAKC,MAAM8D,KAAKM,GACTrE,KAGT,QAAAsE,GACE,OAAOtE,KAAKC,MAAMsE,KAAK,IAGzB,QAAAC,GACE,OAAO,IAAIC,OAAOzE,KAAKsE,WAAY,IAAItE,KAAKE,OAAOqE,KAAK,MAWtD,MAAAG,EAAc,IAAkB,IAAI5E,EAEpC6E,EAAW,MACf,MAAMC,EAAuBC,IAC3B,MAAMC,EAAQD,IAAUL,WACxB,MAAO,IAAM,IAAIC,OAAOK,EAAMC,OAAQD,EAAM5E,MAAM,EAGpD,MAAO,CACL8E,MAAOJ,GAAoB,IACzBF,IACG5B,cACAxC,OACAV,YACAa,QAAQ,KACRH,OACAV,YACA6C,aACAhC,QAAQ,KACRH,OACAV,YACAiD,WACAlD,aACAc,QAAQ,KACRlB,SACA6C,QAAQ,GACRW,cAELkC,IAAKL,GAAoB,IACvBF,IACG5B,cACAmB,WACAC,MACA5D,OACAV,YACAa,QAAQ,KACR0D,MACAC,OACArB,cAELmC,mBAAoBN,GAAoB,IACtCF,IACG5B,cACArC,QAAQ,KACRrB,QACAkD,QAAQ,EAAG,GACX7B,QAAQ,KACRrB,QACAkD,QAAQ,EAAG,IACXS,cAGR,EAlDgB"} | ||
| {"version":3,"file":"human-regex.esm.js","sources":["../src/human-regex.ts"],"sourcesContent":["type PartialBut<T, K extends keyof T> = Partial<T> & Pick<T, K>;\n\nconst escapeCache = new Map<string, string>();\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\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\nconst Quantifiers = Object.freeze({\n zeroOrMore: \"*\",\n oneOrMore: \"+\",\n optional: \"?\",\n});\n\ntype Quantifiers =\n | \"exactly\"\n | \"atLeast\"\n | \"atMost\"\n | \"between\"\n | \"oneOrMore\"\n | \"zeroOrMore\"\n | \"repeat\";\ntype QuantifierMethods = Quantifiers | \"optional\" | \"lazy\";\n\ntype WithLazy = HumanRegex;\ntype Base = Omit<HumanRegex, \"lazy\">;\n\ntype AtStart = Omit<Base, QuantifierMethods | \"endGroup\">;\ntype AfterAnchor = Omit<Base, QuantifierMethods | \"or\">;\ntype SimpleQuantifier = Omit<Base, Quantifiers>;\ntype LazyQuantifier = Omit<WithLazy, Quantifiers>;\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 digit(): Base {\n return this.add(\"\\\\d\");\n }\n\n special(): Base {\n return this.add(\"(?=.*[!@#$%^&*])\");\n }\n\n word(): Base {\n return this.add(\"\\\\w\");\n }\n\n whitespace(): Base {\n return this.add(\"\\\\s\");\n }\n\n nonWhitespace(): Base {\n return this.add(\"\\\\S\");\n }\n\n literal(text: string): this {\n return this.add(escapeLiteral(text));\n }\n\n or(): AfterAnchor {\n return this.add(\"|\");\n }\n\n range(name: RangeKeys): Base {\n const range = Ranges[name];\n if (!range) throw new Error(`Unknown range: ${name}`);\n return this.add(`[${range}]`);\n }\n\n notRange(chars: string): Base {\n return this.add(`[^${chars}]`);\n }\n\n lazy(): Base {\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 letter(): Base {\n return this.add(\"[a-zA-Z]\");\n }\n\n anyCharacter(): Base {\n return this.add(\".\");\n }\n\n negativeLookahead(pattern: string): Base {\n return this.add(`(?!${pattern})`);\n }\n\n positiveLookahead(pattern: string): Base {\n return this.add(`(?=${pattern})`);\n }\n\n positiveLookbehind(pattern: string): Base {\n return this.add(`(?<=${pattern})`);\n }\n\n negativeLookbehind(pattern: string): Base {\n return this.add(`(?<!${pattern})`);\n }\n\n hasSpecialCharacter(): Base {\n return this.add(\"(?=.*[!@#$%^&*])\");\n }\n\n hasDigit(): Base {\n return this.add(\"(?=.*\\\\d)\");\n }\n\n hasLetter(): Base {\n return this.add(\"(?=.*[a-zA-Z])\");\n }\n\n optional(): SimpleQuantifier {\n return this.add(Quantifiers.optional);\n }\n\n exactly(n: number): SimpleQuantifier {\n return this.add(`{${n}}`);\n }\n\n atLeast(n: number): LazyQuantifier {\n return this.add(`{${n},}`);\n }\n\n atMost(n: number): LazyQuantifier {\n return this.add(`{0,${n}}`);\n }\n\n between(min: number, max: number): LazyQuantifier {\n return this.add(`{${min},${max}}`);\n }\n\n oneOrMore(): LazyQuantifier {\n return this.add(Quantifiers.oneOrMore);\n }\n\n zeroOrMore(): LazyQuantifier {\n return this.add(Quantifiers.zeroOrMore);\n }\n\n startNamedGroup(name: string): AfterAnchor {\n return this.add(`(?<${name}>`);\n }\n\n startGroup(): AfterAnchor {\n return this.add(\"(?:\");\n }\n\n startCaptureGroup(): AfterAnchor {\n return this.add(\"(\");\n }\n\n wordBoundary(): Base {\n return this.add(\"\\\\b\");\n }\n\n nonWordBoundary(): Base {\n return this.add(\"\\\\B\");\n }\n\n endGroup(): Base {\n return this.add(\")\");\n }\n\n startAnchor(): AfterAnchor {\n return this.add(\"^\");\n }\n\n endAnchor(): AfterAnchor {\n return this.add(\"$\");\n }\n\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 multiline(): this {\n this.flags.add(Flags.MULTILINE);\n return this;\n }\n\n dotAll(): this {\n this.flags.add(Flags.DOT_ALL);\n return this;\n }\n\n sticky(): this {\n this.flags.add(Flags.STICKY);\n return this;\n }\n\n unicodeChar(variant?: \"u\" | \"l\" | \"t\" | \"m\" | \"o\"): Base {\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 unicodeDigit(): Base {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{N}\");\n }\n\n unicodePunctuation(): Base {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{P}\");\n }\n\n unicodeSymbol(): Base {\n this.flags.add(Flags.UNICODE);\n return this.add(\"\\\\p{S}\");\n }\n\n repeat(count: number): Base {\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 ipv4Octet(): Base {\n return this.add(\"(25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]\\\\d|\\\\d)\");\n }\n\n protocol(): Base {\n return this.add(\"https?://\");\n }\n\n www(): Base {\n return this.add(\"(www\\\\.)?\");\n }\n\n tld(): Base {\n return this.add(\"(com|org|net)\");\n }\n\n path(): Base {\n return this.add(\"(/\\\\w+)*\");\n }\n\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 return new RegExp(this.toString(), [...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)!;\n}\n\nconst createRegex = (): AtStart => new HumanRegex();\n\nconst Patterns = (() => {\n const createCachedPattern = (builder: () => PartialBut<HumanRegex, \"toRegExp\">) => {\n const regex = builder().toRegExp();\n return () => new RegExp(regex.source, regex.flags);\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 .www()\n .word()\n .oneOrMore()\n .literal(\".\")\n .tld()\n .path()\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\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","negativeLookahead","pattern","positiveLookahead","positiveLookbehind","negativeLookbehind","hasSpecialCharacter","hasDigit","hasLetter","exactly","n","atLeast","atMost","between","min","max","startNamedGroup","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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AIC,OAAOzE,KAAKsE,WAAY,IAAItE,KAAKE,OAAOqE,KAAK,MAWtD,MAAAG,EAAc,IAAe,IAAI5E,EAEjC6E,EAAW,MACf,MAAMC,EAAuBC,IAC3B,MAAMC,EAAQD,IAAUL,WACxB,MAAO,IAAM,IAAIC,OAAOK,EAAMC,OAAQD,EAAM5E,MAAM,EAGpD,MAAO,CACL8E,MAAOJ,GAAoB,IACzBF,IACG5B,cACAxC,OACAV,YACAa,QAAQ,KACRH,OACAV,YACA6C,aACAhC,QAAQ,KACRH,OACAV,YACAiD,WACAlD,aACAc,QAAQ,KACRlB,SACA4C,QAAQ,GACRY,cAELkC,IAAKL,GAAoB,IACvBF,IACG5B,cACAmB,WACAC,MACA5D,OACAV,YACAa,QAAQ,KACR0D,MACAC,OACArB,cAELmC,mBAAoBN,GAAoB,IACtCF,IACG5B,cACArC,QAAQ,KACRrB,QACAiD,QAAQ,EAAG,GACX5B,QAAQ,KACRrB,QACAiD,QAAQ,EAAG,IACXU,cAGR,EAlDgB"} |
+1
-1
| { | ||
| "name": "human-regex", | ||
| "version": "2.1.2", | ||
| "version": "2.1.3", | ||
| "description": "Human-friendly regex builder with English-like syntax", | ||
@@ -5,0 +5,0 @@ "main": "dist/human-regex.cjs.js", |
45631
3.24%