regulas
Version:
Regulas is a zero-dependency, lightweight regex builder for JavaScript/TypeScript.
2 lines (1 loc) • 3.84 kB
JavaScript
"use strict";var p=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var x=(e,t)=>{for(var r in t)p(e,r,{get:t[r],enumerable:!0})},A=(e,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of d(t))!w.call(e,n)&&n!==r&&p(e,n,{get:()=>t[n],enumerable:!(i=$(t,n))||i.enumerable});return e};var S=e=>A(p({},"__esModule",{value:!0}),e);var G={};x(G,{Regulas:()=>P,allOf:()=>v,escape:()=>N,generateRange:()=>b,next:()=>k,oneOf:()=>j,prev:()=>C,range:()=>q});module.exports=S(G);var M={".":"\\.","*":"\\*","+":"\\+","?":"\\?","^":"\\^",$:"\\$","{":"\\{","}":"\\}","(":"\\(",")":"\\)","|":"\\|","[":"\\[","]":"\\]","\\":"\\\\"},c=new Map,m=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(">")?m.get(r.slice(1,-1)):N(r);if(r instanceof a||r instanceof g||r instanceof h){let i=r.toString();return r.variableName&&m.set(r.variableName,i),i}return""}).join(t)}var f=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(c.has(e))return c.get(e);let t="";for(let r=0;r<e.length;r++){let i=e[r];t+=M[i]||i}return c.set(e,t),t}function q(...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(e.toString(),n.toString())),e=n+1}return r.reverse().join("|")}function E(e,t){let r=[],i="",n=1;for(let s=0;s<t.length;s++){let o=e[s],R=t[s];if(o===R)r.push(o);else{let l=`[${o}-${R}]`;i===l?n++:(r.push(l),i=l)}}return n>1&&r.push(`{${n}}`),r.join("")}function P(...e){return new f(...e)}function j(...e){return new g(()=>u(e,"|"))}function v(...e){return new g(()=>u(e))}function k(...e){function t(r=!1){return r?`(?!(?:${u(e)}))`:`(?=(?:${u(e)}))`}return new h(t)}function C(...e){function t(r=!1){return r?`(?<!(?:${u(e)}))`:`(?<=(?:${u(e)}))`}return new h(t)}0&&(module.exports={Regulas,allOf,escape,generateRange,next,oneOf,prev,range});