UNPKG

regulas

Version:

Regulas is a zero-dependency, lightweight regex builder for JavaScript/TypeScript.

2 lines (1 loc) 3.3 kB
var m={".":"\\.","*":"\\*","+":"\\+","?":"\\?","^":"\\^",$:"\\$","{":"\\{","}":"\\}","(":"\\(",")":"\\)","|":"\\|","[":"\\[","]":"\\]","\\":"\\\\"},p=new Map,R=new Map,a=class{constructor(t){this.pattern=t,this.groupName="?:",this.quantifier="",this.orSegment="",this.negate=!1,this.variableName=""}get or(){return this.orSegment="|",this}get oneOrMore(){return this.quantifier="+",this}get zeroOrMore(){return this.quantifier="*",this}get lazy(){return this.quantifier+="?",this}get optional(){return this.quantifier="?",this}group(t){return this.groupName=`?<${t}>`,this}save(t){return this.variableName=t,this}repeat(...t){return t.length!==0?this.quantifier=`{${t.join(",")}}`:this.quantifier="+",this}toString(){typeof this.pattern=="function"&&(this.pattern=this.pattern());let t=`(?:${this.pattern})${this.quantifier}${this.orSegment}`;return this.groupName!=="?:"&&(t=`(${this.groupName}${t})`),t}},g=class extends a{constructor(t){super(t)}},h=class{constructor(t){this.groupName="";this.negate=!1;this.pattern=t,this.negate=!1,this.variableName=""}group(t){return this.groupName=`?<${t}>`,this}get not(){return this.negate=!0,this}toString(){return typeof this.pattern=="function"&&(this.pattern=this.pattern(this.negate)),this.groupName?`(${this.groupName}${this.pattern})`:this.pattern}};function u(e,t=""){return e.map(r=>{if(typeof r=="string")return r.startsWith("<")&&r.endsWith(">")?R.get(r.slice(1,-1)):N(r);if(r instanceof a||r instanceof g||r instanceof h){let i=r.toString();return r.variableName&&R.set(r.variableName,i),i}return""}).join(t)}var c=class{constructor(...t){this.compiledPattern="";this.compiledPattern=u(t)}toRegex(t=""){return new RegExp(this.compiledPattern,t)}get fullMatch(){return this.compiledPattern=`^${this.compiledPattern}$`,this}};function N(e){if(p.has(e))return p.get(e);let t="";for(let r=0;r<e.length;r++){let i=e[r];t+=m[i]||i}return p.set(e,t),t}function d(...e){function t(){let r=[],i=[];return e.forEach(n=>{if(n.includes("-")){let[s,o]=n.split("-");isNaN(Number(s))&&isNaN(Number(o))?i.push(n):r.push(b(Number(s),Number(o)))}else isNaN(Number(n))?i.push(n):r.push(n)}),r.length===0?`[${i.join("")}]`:i.length===0?`(?<![\\d-])(?:${r.reverse().join("|")})(?!\\d)`:`(?<!\\d)(?:${r.reverse().join("|")})(?!\\d)|[${i.join("")}]`}return new a(t)}function b(e,t){let r=[];if(e===0&&t===9)return"[0-9]";if(e===t)return`${e}`;for(e>t&&([e,t]=[t,e]),e===0&&(r.push("0"),e++);e<=t;){let i=e.toString().length,n=Math.pow(10,i)-1;if(n>t&&(n=(Math.floor(e/Math.pow(10,i-1))+1)*Math.pow(10,i-1)-1),n>t){let s=e;for(;s<t;){let o=s+(9-s%10);if(o<t)n=o;else break;s=o+1}}n>t&&(n=t),r.push($(e.toString(),n.toString())),e=n+1}return r.reverse().join("|")}function $(e,t){let r=[],i="",n=1;for(let s=0;s<t.length;s++){let o=e[s],f=t[s];if(o===f)r.push(o);else{let l=`[${o}-${f}]`;i===l?n++:(r.push(l),i=l)}}return n>1&&r.push(`{${n}}`),r.join("")}function w(...e){return new c(...e)}function x(...e){return new g(()=>u(e,"|"))}function A(...e){return new g(()=>u(e))}function S(...e){function t(r=!1){return r?`(?!(?:${u(e)}))`:`(?=(?:${u(e)}))`}return new h(t)}function M(...e){function t(r=!1){return r?`(?<!(?:${u(e)}))`:`(?<=(?:${u(e)}))`}return new h(t)}export{w as Regulas,A as allOf,N as escape,b as generateRange,S as next,x as oneOf,M as prev,d as range};