compose-regexp
Advanced tools
Comparing version 0.6.30 to 0.6.31
# Change Log | ||
## v0.6.31 | ||
*2023-03-19* | ||
- Avoid creating quantifiers by accident. Now `sequence(/a{/, /1}/)` returns `/a{(?:)1}/`. Fixes [#10](https://github.com/compose-regexp/compose-regexp.js/issues/10) | ||
## v0.6.30 | ||
@@ -4,0 +10,0 @@ |
@@ -106,3 +106,3 @@ (function (global, factory) { | ||
const suffixMatcher = /^(?:[+*?]|\{(?=((\d+)))\1,?(\d*)\})\??$/; | ||
const suffixMatcher = /^(?:[+*?]|\{(\d+)(?:,(\d*))?\})\??$/; | ||
@@ -291,2 +291,20 @@ | ||
const finalQuantifierMatcher = /\\\\|\\\{|(\{\d+(?:,\d*)?\}$)/g; | ||
const badQuantifierMatcher = /^\d*(?:,\d*)?\}/; | ||
function combinesAsQuantfier(x1, x2) { | ||
// first look for a bad start in x2 since this is fast | ||
const badStart = x2.source.match(badQuantifierMatcher); | ||
if (badStart == null) return false | ||
// Now scan the combination looking for a newly formed quantifier | ||
const haystack = x1.source + badStart[0]; | ||
let result; | ||
finalQuantifierMatcher.lastIndex = 0; | ||
while (result = finalQuantifierMatcher.exec(haystack)) { | ||
if (result[1] != null) return true | ||
} | ||
return false | ||
} | ||
// When composing expressions into a sequence, regexps that have a top-level | ||
@@ -522,6 +540,6 @@ // choice operator must be wrapped in a non-capturing group. This function | ||
&& (/^\d/.test(x2.source)) | ||
|| combinesAsQuantfier(x1, x2) | ||
? '(?:)' | ||
: '' | ||
) + x2.source; | ||
return x2 | ||
@@ -727,3 +745,3 @@ } | ||
const match = quantifier.match(suffixMatcher); | ||
if (!match || match[3] && Number(match[3]) < Number(match[2])) throw new SyntaxError("Invalid suffix '" + quantifier+ "'.") | ||
if (!match || match[2] && Number(match[2]) < Number(match[1])) throw new SyntaxError("Invalid suffix '" + quantifier+ "'.") | ||
return args.length === 0 | ||
@@ -730,0 +748,0 @@ ? _suffix.bind(null, quantifier) |
@@ -1,1 +0,1 @@ | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define("compose-regexp",["exports"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).composeRegexp={})}(this,(function(n){"use strict";const e=[];"dgimsuy".split("").forEach((function(n){try{new RegExp("",n),e.push(n)}catch(n){}}));const t=-1!==e.indexOf("u"),r=(()=>{try{return new RegExp("(?<=)"),!0}catch(n){return!1}})(),u={}.hasOwnProperty;function o(n){return n}const i=[].map,c=RegExp;function s(n){return-1===n.indexOf("\\")?n:JSON.parse('"'+n.replace(/$|"|\\x([\dA-Fa-f]{2})|\\u\{([\dA-Fa-f]{1,6})\}/g,(function(n,e,t){return""===n?'"':'"'===n?'\\"':String.fromCodePoint(parseInt(e||t,16))})))}const f={value:void 0,writable:!1,enumerable:!1,configurable:!1};function l(){return"_"+Math.floor(Math.random()*Math.pow(2,32)).toString(36)}const a="undefined"!=typeof WeakMap?new WeakMap:{key:"__$$__compose_regexp__$$__"+l()+l()+l()+l(),set:function(n,e){const t=typeof n;if(null==n||"object"!==t&&"function"!==t)throw new TypeError("Bad WeakMap key");const r=n[a.key];if(!r)return f.value={keys:[this],values:[e]},Object.defineProperty(n,a.key,f),this;const u=r.keys.indexOf(this);return 0===u?(r.keys.push(this),r.values.push(e),this):(r.values[u]=e,this)},get:function(n){const e=n[a.key];if(e){const n=e.keys.indexOf(this);if(-1!==n)return e.values[n]}}},d=/\\[^]|\(\?(?::|<?[=!])|[\[\](]/g,p=/\\.|\.|\(\?:\^\|\(\?<=\[\\n\\r\\u2028\\u2029\]\)\)|\(\?:\$\|\(\?=\[\\n\\r\\u2028\\u2029\]\)\)|\[|\]|\^|\$/g,g=t&&new RegExp("^[_$\\p{ID_Start}][$\\p{ID_Continue}]*$","u"),h=/\\.|\{\d+,?\d*\}|(\[|\]|\{|\})/g,y=/(?:$|(?=[\n\r\u2028\u2029]))/,x=/(?:^|(?<=[\n\r\u2028\u2029]))/,w=/\\[^1-9]|[\[\]]|\\(\d{1,2})|\$d:(\d+),n:(\d+)\^/g,k=/^(?:\\.|\[(?:[^\]\\]|\\.)*\])$/,m=/^\\p\{[A-Za-z][A-Za-z=]*\}$/,$=/[.?*+^$[\]\\(){}|]/g,E=/^(?:[+*?]|\{(?=((\d+)))\1,?(\d*)\})\??$/,R=/(\\.)|[-()|\[\]]((?=\?<?[=!]))?/g,b=/\\u[0-9A-Fa-f]{4}|\\x[0-9A-Fa-f]{2}|\\c[A-Za-z]|\\([^.?*+^$[\]\\(){}|\/DSWdswfnrtv-])|(\\[DSWdsw]-[^\]]|.-\\[DSWdsw])|\\.|\]/g,S=/\\u[0-9A-Fa-f]{4}|\\x[0-9A-Fa-f]{2}|\\c[A-Za-z]|\\k<(.*?)>|\\([^.?*+^$[\]\\(){}|\/DSWdswBbfnrtv])|\\.|\.|\[\^\]|\[|(\((?:\?[^])?)|(\)(?:[+?*]|\{\d+,?\d*\})?)/g;function A(n){if(n instanceof c){let e=a.get(n);return null==e&&a.set(n,e={captureCount:null,direction:0,hasRefs:null,hasFinalRef:null,isDisjunction:null,isOneGroupOrAssertion:null,source:null,unicode:null}),e}return n||{captureCount:null,direction:0,hasRefs:null,hasFinalRef:null,isDisjunction:null,isOneGroupOrAssertion:null,source:null,unicode:null}}const O=function(n,e,t){const r=A(n);return"object"==typeof e?Object.assign(r,e):r[e]=t},I=function(n,e){return A(n)[e]};function j(n,e){return Object.defineProperty((function(t){const r=I(t.key,n);return null!=r?r:O(t.key,n,e(t))}),"name",{value:n})}const F=[];const v=j("captureCount",(function(n){let e,t=0;for(d.lastIndex=0;e=d.exec(n.source);)"("===e[0]&&(t+=1);return t})),B=j("hasRefs",(function(n){let e,t=!1,r=!1,u=!1;for(w.lastIndex=0;e=w.exec(n.source);)u||null==e[1]&&null==e[2]?"["===e[0]?u=!0:"]"===e[0]&&(u=!1):(t=!0,w.lastIndex===n.source.length&&(r=!0));return O(n.key,"hasFinalRef",r),t})),C=j("isDisjunction",(function(n){if(-1===n.source.indexOf("|"))return!1;let e,t=0,r=!1;for(R.lastIndex=0;e=R.exec(n.source);)if(null==e[1]&&(r||"("!==e[0]||t++,r||")"!==e[0]||t--,r||"["!==e[0]&&"[-"!==e[0]||(r=!0),r&&"]"===e[0]&&(r=!1),0===t&&!r&&"|"===e[0]))return!0;return!1})),_=j("isOneGroupOrAssertion",(function(n){const e=n.source;if("("!==e.charAt(0)||")"!==e.charAt(e.length-1))return!1;let t,r=0,u=!1;for(R.lastIndex=0;t=R.exec(e);)if(null==t[1]){if(u||"("!==t[0]||r++,!u&&")"===t[0]&&(r--,0===r&&t.index!==e.length-1))return!1;u||"["!==t[0]&&"[-"!==t||(u=!0),u&&"]"===t[0]&&(u=!1)}else if(t.index===e.length-2)return!1;return!0}));function M(n){const e=n.source;if(null==e||"^"===e||"$"===e||"\\b"===e||"\\B"===e)throw new SyntaxError(`Nothing to repeat: / ${e||"(?:)"}/`);if(1===e.length||k.test(e)||W.U&&m.test(e))return!1;if(!_(n))return!0;if(/^\(\?<?[!=]/.test(e))throw new SyntaxError(`Nothing to repeat: /${e}/`);return!1}function D(n){const e=n.source;if(W.U&&("regexp"===n.kind&&!n.key.unicode||"result"===n.kind&&!I(n.key,"unicode"))){if(function(n){let e,t=S;function r(n){n.lastIndex=t.lastIndex,t=n}for(F.length=0,S.lastIndex=0;e=t.exec(n);)if(t===S)if("["===e[0])r(b);else{if("."===e[0]||"[^]"===e[0]||null!=e[1]&&!g.test(s(e[1]))||null!=e[2])return!0;if(null!=e[3])F.unshift(e[3]);else if(null!=e[4]){if(("(?="===F[0]||"(?!"===F[0])&&")"!==e[4])return!0;F.shift()}}else if("]"===e[0])r(S);else if(null!=e[1]||null!=e[2])return!0;return!1}(e))throw new SyntaxError(`Can't upgrade the RegExp to Unicode /${e}/${"regexp"===n.kind?n.key.flags:""}`);n.source=function(n){let e=!1;return n.replace(h,(function(n,t){if("]"===n){if(!e)return"\\]";e=!1}else if(!e&&null!=t){if("["!==t)return"\\"+t;e=!0}return n}))}(e)}let t=!1;return"regexp"===n.kind&&(n.key.dotAll||n.key.multiline)&&(n.source=e.replace(p,(function(e){return t?("]"===e&&(t=!1),e):("["===e&&(t=!0),n.key.dotAll&&"."===e?"[^]":n.key.multiline&&"^"===e&&r?x.source:n.key.multiline&&"$"===e&&r?y.source:e)}))),n}const U=new RegExp(`^(?:([${e.join("")}])(?!.*\\1))*$`),N=new RegExp(`[${e.join()}]`,"g");const T={"-1":"backward",1:"forward"};let W,z,P={current:1};function Z(){z={hasRefs:!1,hasFinalRef:!1,captureCount:0},W={U:!1,I:null,M:null}}function G(n,e){return e.condition&&!e.condition(n)||(n.source=e.open+(n.source||"")+")"),e.suffix&&(n.source+=e.suffix),n}function q(n){return G(n,{condition:function(n){return("regexp"===n.kind||"result"===n.kind)&&C(n)},open:"(?:"})}function J(n,e,t,u){return t=t||n.length>1,i.call(n,(function n(u){return(!e&&t?q:o)(Array.isArray(u)?J(u,!1,t,0):"function"==typeof u?n(function(n){const e=z,t=W;try{return n()}finally{z=e,W=t}}(u)):u instanceof c?function(n){const e=I(n.key,"direction");if(e*P.current==-1)throw new TypeError(`Illegal ${T[e]} RegExp argument while building a ${T[P.current]} one: /${n.source}/`);return 0!==e&&(z.hasRefs=!0),n}(function(n){const e=n.key.flags;if(!U.test(e))throw new TypeError(`Unkown flag(s): ${e.replace(N,"")}`);const t=!!n.key.unicode,u=n.key.ignoreCase,o=n.key.multiline;if(null!=W.I&&u!==W.I)throw new SyntaxError("Can't combine i and non-i regexps: "+n.key);if(!r&&null!=W.M&&o!==W.M)throw new SyntaxError("Can't combine m and non-m regexps: "+n.key);return W.I=u,W.M=o,W.U=W.U||t,n}({key:u,kind:"regexp",source:(i=u,I(i,"source")||O(i,"source",i.source))})):function(n){if("number"==typeof n||"string"==typeof n)return{key:null,kind:"string",source:String(n).replace($,"\\$&")};throw new TypeError(`Can't compose type ${typeof n} as RegExp`)}(u));var i})).map(D).map(function(n){let e=n;return function(t){if("regexp"===t.kind||"result"===t.kind){if(B(t)){z.hasRefs=!0;let r=!1;t.source=t.source.replace(w,(function(t,u,o,i){if(!r){if(null!=u){const n=Number(u)+e;if(n>99)throw new RangeError("Back reference index larger than 99");return"\\"+String(n)}if(null!=o){if("0"===o){const e=Number(i)+n;if(e>99)throw new RangeError("Back reference index larger than 99");return"\\"+String(e)}return`$d:${Number(o)-1},n:${i}^`}}return"["===t?r=!0:"]"===t&&(r=!1),t}))}z.hasFinalRef=!!I(t.key,"hasFinalRef"),e+=v(t)}return z.captureCount=e,t}}(u)).reduceRight(e?function(n,e){return n.source=null==n.source?e.source:`${e.source}|${n.source}`,n}:function(n,e){return n.source=null==n.source?e.source:e.source+("string"!==e.kind&&B(e)&&I(e.key,"hasFinalRef")&&/^\d/.test(n.source)?"(?:)":"")+n.source,n},{key:O(null,Object.assign({direction:z.hasRefs?P.current:0,isDisjunction:e&&(n.length>1||1===n.length&&n[0]instanceof c&&I(n[0],"isDisjunction")),unicode:W.U},z)),kind:"result",source:null})}function H(){return(W.I?"i":"")+(r?"":W.M?"m":"")+(W.U?"u":"")}function K(n,e){e=e||{};const t=u.call(e,"flags")?e.flagsOp(H(),e.flags):H(),r=e.either,o=new RegExp(r?n.source||"[]":n.source||"",t);return O(o,O(n.key,{})),O(o,"source",n.source),u.call(e,"direction")&&O(o,"direction",e.direction),o}const L=/(?:)/,Q=/[]/;function V(n){if(!r)throw new Error("no support for /(?<=...)/ which is required by "+n+"()")}function X(...n){return 0===n.length?Q:(Z(),K(J(n,!0,!1,0),{either:!0}))}function Y(...n){return J(n,!1,!1,0)}function nn(...n){return 0===n.length?L:(Z(),K(Y(...n)))}function en(n,e,t,r,u){return function(...o){if(null!=r&&r(u),!o.length)return t;const i=P.current;P.current=e;try{Z();return K(G(Y(...o),{open:n}),{direction:0})}finally{P.current=i}}}const tn=en("(?=",1,L),rn=en("(?!",1,Q),un=en("(?<=",-1,L,V,"lookBehind"),on=en("(?<!",-1,Q,V,"notBehind");function cn(n,...e){if(Z(),0===e.length)throw new SyntaxError("Suffix to an empty prefix");return K(G(Y(...e),{condition:M,open:"(?:",suffix:n}))}function sn(n,...e){"string"!=typeof n&&(n="{"+String(n)+"}");const t=n.match(E);if(!t||t[3]&&Number(t[3])<Number(t[2]))throw new SyntaxError("Invalid suffix '"+n+"'.");return 0===e.length?cn.bind(null,n):cn(n,...e)}cn.call;const fn=sn("?");function ln(n){return!t||g.test(s(n))}function an(n,e){if(!function(n){const e=typeof n;return"string"===e&&ln(n)||"number"===e&&0<n&&Math.round(n)===n}(n))throw new SyntaxError("Bad ref: "+n);if(null!=e&&("number"!=typeof e||e<1||e!=e|0))throw new RangeError("Bad depth: "+e);if("string"==typeof n)return new RegExp("\\k<"+n+">");const t=new RegExp("$d:"+(e||"0")+",n:"+n+"^");return O(t,{direction:P.current,hasFinalRef:!0,hasRefs:!0}),t}function dn(...n){Z();return K(G(J(n,!1,!1,1),{open:"("}))}function pn(n,...e){if("string"!=typeof n)throw new TypeError("String expected, got "+typeof n);ln(n),Z();return K(G(J(e,!1,!1,1),{open:"(?<"+n+">"}))}function gn(n,e){return n=n.split(""),e.split("").forEach((function(e){-1===n.indexOf(e)&&n.push(e)})),n.sort().join("")}function hn(n,...e){Z(),-1!==n.indexOf("u")&&(W.U=!0);return K(Y(...e),{flagsOp:gn,flags:n})}const yn={add:function(n,...e){if("string"!=typeof n)throw TypeError("String expected as first argument, got "+typeof n);if(!U.test(n))throw new SyntaxError("Invalid flags: "+n);return 0===e.length?hn.bind(null,n):hn(n,...e)}};const xn=t&&new RegExp("[^]","u");function wn(n,e){return nn(rn(e),n)}const kn={difference:wn,intersection:function(n,e){return nn(tn(e),n)},complement:function(n){return wn(t&&n.unicode?xn:/[^]/,n)},union:X};n.atomic=function(...n){return 1===P.current?nn(tn(dn(...n)),an(1)):nn(an(1),un(dn(...n)))},n.bound=function(n){return V("bound"),X([on(n),tn(n)],[un(n),rn(n)])},n.capture=dn,n.charSet=kn,n.either=X,n.flags=yn,n.lookAhead=tn,n.lookBehind=un,n.maybe=fn,n.namedCapture=function(n,...e){return 0===e.length?pn.bind(null,n):pn(n,...e)},n.noBound=function(n){return V("noBound"),X([on(n),rn(n)],[un(n),tn(n)])},n.notAhead=rn,n.notBehind=on,n.ref=an,n.sequence=nn,n.suffix=sn})); | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define("compose-regexp",["exports"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).composeRegexp={})}(this,(function(n){"use strict";const e=[];"dgimsuy".split("").forEach((function(n){try{new RegExp("",n),e.push(n)}catch(n){}}));const t=-1!==e.indexOf("u"),r=(()=>{try{return new RegExp("(?<=)"),!0}catch(n){return!1}})(),u={}.hasOwnProperty;function o(n){return n}const i=[].map,c=RegExp;function s(n){return-1===n.indexOf("\\")?n:JSON.parse('"'+n.replace(/$|"|\\x([\dA-Fa-f]{2})|\\u\{([\dA-Fa-f]{1,6})\}/g,(function(n,e,t){return""===n?'"':'"'===n?'\\"':String.fromCodePoint(parseInt(e||t,16))})))}const f={value:void 0,writable:!1,enumerable:!1,configurable:!1};function l(){return"_"+Math.floor(Math.random()*Math.pow(2,32)).toString(36)}const a="undefined"!=typeof WeakMap?new WeakMap:{key:"__$$__compose_regexp__$$__"+l()+l()+l()+l(),set:function(n,e){const t=typeof n;if(null==n||"object"!==t&&"function"!==t)throw new TypeError("Bad WeakMap key");const r=n[a.key];if(!r)return f.value={keys:[this],values:[e]},Object.defineProperty(n,a.key,f),this;const u=r.keys.indexOf(this);return 0===u?(r.keys.push(this),r.values.push(e),this):(r.values[u]=e,this)},get:function(n){const e=n[a.key];if(e){const n=e.keys.indexOf(this);if(-1!==n)return e.values[n]}}},d=/\\[^]|\(\?(?::|<?[=!])|[\[\](]/g,p=/\\.|\.|\(\?:\^\|\(\?<=\[\\n\\r\\u2028\\u2029\]\)\)|\(\?:\$\|\(\?=\[\\n\\r\\u2028\\u2029\]\)\)|\[|\]|\^|\$/g,g=t&&new RegExp("^[_$\\p{ID_Start}][$\\p{ID_Continue}]*$","u"),h=/\\.|\{\d+,?\d*\}|(\[|\]|\{|\})/g,y=/(?:$|(?=[\n\r\u2028\u2029]))/,x=/(?:^|(?<=[\n\r\u2028\u2029]))/,w=/\\[^1-9]|[\[\]]|\\(\d{1,2})|\$d:(\d+),n:(\d+)\^/g,k=/^(?:\\.|\[(?:[^\]\\]|\\.)*\])$/,m=/^\\p\{[A-Za-z][A-Za-z=]*\}$/,$=/[.?*+^$[\]\\(){}|]/g,E=/^(?:[+*?]|\{(\d+)(?:,(\d*))?\})\??$/,R=/(\\.)|[-()|\[\]]((?=\?<?[=!]))?/g,b=/\\u[0-9A-Fa-f]{4}|\\x[0-9A-Fa-f]{2}|\\c[A-Za-z]|\\([^.?*+^$[\]\\(){}|\/DSWdswfnrtv-])|(\\[DSWdsw]-[^\]]|.-\\[DSWdsw])|\\.|\]/g,S=/\\u[0-9A-Fa-f]{4}|\\x[0-9A-Fa-f]{2}|\\c[A-Za-z]|\\k<(.*?)>|\\([^.?*+^$[\]\\(){}|\/DSWdswBbfnrtv])|\\.|\.|\[\^\]|\[|(\((?:\?[^])?)|(\)(?:[+?*]|\{\d+,?\d*\})?)/g;function A(n){if(n instanceof c){let e=a.get(n);return null==e&&a.set(n,e={captureCount:null,direction:0,hasRefs:null,hasFinalRef:null,isDisjunction:null,isOneGroupOrAssertion:null,source:null,unicode:null}),e}return n||{captureCount:null,direction:0,hasRefs:null,hasFinalRef:null,isDisjunction:null,isOneGroupOrAssertion:null,source:null,unicode:null}}const O=function(n,e,t){const r=A(n);return"object"==typeof e?Object.assign(r,e):r[e]=t},I=function(n,e){return A(n)[e]};function j(n,e){return Object.defineProperty((function(t){const r=I(t.key,n);return null!=r?r:O(t.key,n,e(t))}),"name",{value:n})}const F=[];const v=j("captureCount",(function(n){let e,t=0;for(d.lastIndex=0;e=d.exec(n.source);)"("===e[0]&&(t+=1);return t})),B=j("hasRefs",(function(n){let e,t=!1,r=!1,u=!1;for(w.lastIndex=0;e=w.exec(n.source);)u||null==e[1]&&null==e[2]?"["===e[0]?u=!0:"]"===e[0]&&(u=!1):(t=!0,w.lastIndex===n.source.length&&(r=!0));return O(n.key,"hasFinalRef",r),t})),C=/\\\\|\\\{|(\{\d+(?:,\d*)?\}$)/g,_=/^\d*(?:,\d*)?\}/;const M=j("isDisjunction",(function(n){if(-1===n.source.indexOf("|"))return!1;let e,t=0,r=!1;for(R.lastIndex=0;e=R.exec(n.source);)if(null==e[1]&&(r||"("!==e[0]||t++,r||")"!==e[0]||t--,r||"["!==e[0]&&"[-"!==e[0]||(r=!0),r&&"]"===e[0]&&(r=!1),0===t&&!r&&"|"===e[0]))return!0;return!1})),D=j("isOneGroupOrAssertion",(function(n){const e=n.source;if("("!==e.charAt(0)||")"!==e.charAt(e.length-1))return!1;let t,r=0,u=!1;for(R.lastIndex=0;t=R.exec(e);)if(null==t[1]){if(u||"("!==t[0]||r++,!u&&")"===t[0]&&(r--,0===r&&t.index!==e.length-1))return!1;u||"["!==t[0]&&"[-"!==t||(u=!0),u&&"]"===t[0]&&(u=!1)}else if(t.index===e.length-2)return!1;return!0}));function U(n){const e=n.source;if(null==e||"^"===e||"$"===e||"\\b"===e||"\\B"===e)throw new SyntaxError(`Nothing to repeat: / ${e||"(?:)"}/`);if(1===e.length||k.test(e)||Z.U&&m.test(e))return!1;if(!D(n))return!0;if(/^\(\?<?[!=]/.test(e))throw new SyntaxError(`Nothing to repeat: /${e}/`);return!1}function N(n){const e=n.source;if(Z.U&&("regexp"===n.kind&&!n.key.unicode||"result"===n.kind&&!I(n.key,"unicode"))){if(function(n){let e,t=S;function r(n){n.lastIndex=t.lastIndex,t=n}for(F.length=0,S.lastIndex=0;e=t.exec(n);)if(t===S)if("["===e[0])r(b);else{if("."===e[0]||"[^]"===e[0]||null!=e[1]&&!g.test(s(e[1]))||null!=e[2])return!0;if(null!=e[3])F.unshift(e[3]);else if(null!=e[4]){if(("(?="===F[0]||"(?!"===F[0])&&")"!==e[4])return!0;F.shift()}}else if("]"===e[0])r(S);else if(null!=e[1]||null!=e[2])return!0;return!1}(e))throw new SyntaxError(`Can't upgrade the RegExp to Unicode /${e}/${"regexp"===n.kind?n.key.flags:""}`);n.source=function(n){let e=!1;return n.replace(h,(function(n,t){if("]"===n){if(!e)return"\\]";e=!1}else if(!e&&null!=t){if("["!==t)return"\\"+t;e=!0}return n}))}(e)}let t=!1;return"regexp"===n.kind&&(n.key.dotAll||n.key.multiline)&&(n.source=e.replace(p,(function(e){return t?("]"===e&&(t=!1),e):("["===e&&(t=!0),n.key.dotAll&&"."===e?"[^]":n.key.multiline&&"^"===e&&r?x.source:n.key.multiline&&"$"===e&&r?y.source:e)}))),n}const T=new RegExp(`^(?:([${e.join("")}])(?!.*\\1))*$`),W=new RegExp(`[${e.join()}]`,"g");const z={"-1":"backward",1:"forward"};function P(n){return n?function(n,e){return n.source=null==n.source?e.source:`${e.source}|${n.source}`,n}:function(n,e){return n.source=null==n.source?e.source:e.source+("string"!==e.kind&&B(e)&&I(e.key,"hasFinalRef")&&/^\d/.test(n.source)||function(n,e){const t=e.source.match(_);if(null==t)return!1;const r=n.source+t[0];let u;for(C.lastIndex=0;u=C.exec(r);)if(null!=u[1])return!0;return!1}(e,n)?"(?:)":"")+n.source,n}}let Z,G,q={current:1};function J(){G={hasRefs:!1,hasFinalRef:!1,captureCount:0},Z={U:!1,I:null,M:null}}function H(n,e){return e.condition&&!e.condition(n)||(n.source=e.open+(n.source||"")+")"),e.suffix&&(n.source+=e.suffix),n}function K(n){return H(n,{condition:function(n){return("regexp"===n.kind||"result"===n.kind)&&M(n)},open:"(?:"})}function L(n,e,t,u){return t=t||n.length>1,i.call(n,(function n(u){return(!e&&t?K:o)(Array.isArray(u)?L(u,!1,t,0):"function"==typeof u?n(function(n){const e=G,t=Z;try{return n()}finally{G=e,Z=t}}(u)):u instanceof c?function(n){const e=I(n.key,"direction");if(e*q.current==-1)throw new TypeError(`Illegal ${z[e]} RegExp argument while building a ${z[q.current]} one: /${n.source}/`);return 0!==e&&(G.hasRefs=!0),n}(function(n){const e=n.key.flags;if(!T.test(e))throw new TypeError(`Unkown flag(s): ${e.replace(W,"")}`);const t=!!n.key.unicode,u=n.key.ignoreCase,o=n.key.multiline;if(null!=Z.I&&u!==Z.I)throw new SyntaxError("Can't combine i and non-i regexps: "+n.key);if(!r&&null!=Z.M&&o!==Z.M)throw new SyntaxError("Can't combine m and non-m regexps: "+n.key);return Z.I=u,Z.M=o,Z.U=Z.U||t,n}({key:u,kind:"regexp",source:(i=u,I(i,"source")||O(i,"source",i.source))})):function(n){if("number"==typeof n||"string"==typeof n)return{key:null,kind:"string",source:String(n).replace($,"\\$&")};throw new TypeError(`Can't compose type ${typeof n} as RegExp`)}(u));var i})).map(N).map(function(n){let e=n;return function(t){if("regexp"===t.kind||"result"===t.kind){if(B(t)){G.hasRefs=!0;let r=!1;t.source=t.source.replace(w,(function(t,u,o,i){if(!r){if(null!=u){const n=Number(u)+e;if(n>99)throw new RangeError("Back reference index larger than 99");return"\\"+String(n)}if(null!=o){if("0"===o){const e=Number(i)+n;if(e>99)throw new RangeError("Back reference index larger than 99");return"\\"+String(e)}return`$d:${Number(o)-1},n:${i}^`}}return"["===t?r=!0:"]"===t&&(r=!1),t}))}G.hasFinalRef=!!I(t.key,"hasFinalRef"),e+=v(t)}return G.captureCount=e,t}}(u)).reduceRight(P(e),{key:O(null,Object.assign({direction:G.hasRefs?q.current:0,isDisjunction:e&&(n.length>1||1===n.length&&n[0]instanceof c&&I(n[0],"isDisjunction")),unicode:Z.U},G)),kind:"result",source:null})}function Q(){return(Z.I?"i":"")+(r?"":Z.M?"m":"")+(Z.U?"u":"")}function V(n,e){e=e||{};const t=u.call(e,"flags")?e.flagsOp(Q(),e.flags):Q(),r=e.either,o=new RegExp(r?n.source||"[]":n.source||"",t);return O(o,O(n.key,{})),O(o,"source",n.source),u.call(e,"direction")&&O(o,"direction",e.direction),o}const X=/(?:)/,Y=/[]/;function nn(n){if(!r)throw new Error("no support for /(?<=...)/ which is required by "+n+"()")}function en(...n){return 0===n.length?Y:(J(),V(L(n,!0,!1,0),{either:!0}))}function tn(...n){return L(n,!1,!1,0)}function rn(...n){return 0===n.length?X:(J(),V(tn(...n)))}function un(n,e,t,r,u){return function(...o){if(null!=r&&r(u),!o.length)return t;const i=q.current;q.current=e;try{J();return V(H(tn(...o),{open:n}),{direction:0})}finally{q.current=i}}}const on=un("(?=",1,X),cn=un("(?!",1,Y),sn=un("(?<=",-1,X,nn,"lookBehind"),fn=un("(?<!",-1,Y,nn,"notBehind");function ln(n,...e){if(J(),0===e.length)throw new SyntaxError("Suffix to an empty prefix");return V(H(tn(...e),{condition:U,open:"(?:",suffix:n}))}function an(n,...e){"string"!=typeof n&&(n="{"+String(n)+"}");const t=n.match(E);if(!t||t[2]&&Number(t[2])<Number(t[1]))throw new SyntaxError("Invalid suffix '"+n+"'.");return 0===e.length?ln.bind(null,n):ln(n,...e)}ln.call;const dn=an("?");function pn(n){return!t||g.test(s(n))}function gn(n,e){if(!function(n){const e=typeof n;return"string"===e&&pn(n)||"number"===e&&0<n&&Math.round(n)===n}(n))throw new SyntaxError("Bad ref: "+n);if(null!=e&&("number"!=typeof e||e<1||e!=e|0))throw new RangeError("Bad depth: "+e);if("string"==typeof n)return new RegExp("\\k<"+n+">");const t=new RegExp("$d:"+(e||"0")+",n:"+n+"^");return O(t,{direction:q.current,hasFinalRef:!0,hasRefs:!0}),t}function hn(...n){J();return V(H(L(n,!1,!1,1),{open:"("}))}function yn(n,...e){if("string"!=typeof n)throw new TypeError("String expected, got "+typeof n);pn(n),J();return V(H(L(e,!1,!1,1),{open:"(?<"+n+">"}))}function xn(n,e){return n=n.split(""),e.split("").forEach((function(e){-1===n.indexOf(e)&&n.push(e)})),n.sort().join("")}function wn(n,...e){J(),-1!==n.indexOf("u")&&(Z.U=!0);return V(tn(...e),{flagsOp:xn,flags:n})}const kn={add:function(n,...e){if("string"!=typeof n)throw TypeError("String expected as first argument, got "+typeof n);if(!T.test(n))throw new SyntaxError("Invalid flags: "+n);return 0===e.length?wn.bind(null,n):wn(n,...e)}};const mn=t&&new RegExp("[^]","u");function $n(n,e){return rn(cn(e),n)}const En={difference:$n,intersection:function(n,e){return rn(on(e),n)},complement:function(n){return $n(t&&n.unicode?mn:/[^]/,n)},union:en};n.atomic=function(...n){return 1===q.current?rn(on(hn(...n)),gn(1)):rn(gn(1),sn(hn(...n)))},n.bound=function(n){return nn("bound"),en([fn(n),on(n)],[sn(n),cn(n)])},n.capture=hn,n.charSet=En,n.either=en,n.flags=kn,n.lookAhead=on,n.lookBehind=sn,n.maybe=dn,n.namedCapture=function(n,...e){return 0===e.length?yn.bind(null,n):yn(n,...e)},n.noBound=function(n){return nn("noBound"),en([fn(n),cn(n)],[sn(n),on(n)])},n.notAhead=cn,n.notBehind=fn,n.ref=gn,n.sequence=rn,n.suffix=an})); |
{ | ||
"name": "compose-regexp", | ||
"version": "0.6.30", | ||
"version": "0.6.31", | ||
"description": "A set of functions to build and compose complex regular expressions", | ||
@@ -53,3 +53,3 @@ "type": "module", | ||
"rollup": "^3.11.0", | ||
"stable-version": "npm:compose-regexp@0.6.29", | ||
"stable-version": "npm:compose-regexp@0.6.30", | ||
"terser": "^5.16.1" | ||
@@ -56,0 +56,0 @@ }, |
@@ -74,3 +74,3 @@ import {atomic, capture, either, flags, lookAhead, maybe, sequence, suffix} from 'stable-version' | ||
/[+*?]/, | ||
['{', atomic(/(\d+)/),/,?(\d*)/, '}'] | ||
['{', /(\d+)/, maybe(/,(\d*)/), '}'] | ||
), | ||
@@ -77,0 +77,0 @@ maybe('?') |
@@ -78,3 +78,3 @@ import {slice, supportsLookBehind, supportsU, unescape} from './utils.js' | ||
const match = quantifier.match(suffixMatcher) | ||
if (!match || match[3] && Number(match[3]) < Number(match[2])) throw new SyntaxError("Invalid suffix '" + quantifier+ "'.") | ||
if (!match || match[2] && Number(match[2]) < Number(match[1])) throw new SyntaxError("Invalid suffix '" + quantifier+ "'.") | ||
return args.length === 0 | ||
@@ -81,0 +81,0 @@ ? _suffix.bind(null, quantifier) |
@@ -184,2 +184,20 @@ | ||
const finalQuantifierMatcher = /\\\\|\\\{|(\{\d+(?:,\d*)?\}$)/g | ||
const badQuantifierMatcher = /^\d*(?:,\d*)?\}/ | ||
function combinesAsQuantfier(x1, x2) { | ||
// first look for a bad start in x2 since this is fast | ||
const badStart = x2.source.match(badQuantifierMatcher) | ||
if (badStart == null) return false | ||
// Now scan the combination looking for a newly formed quantifier | ||
const haystack = x1.source + badStart[0] | ||
let result | ||
finalQuantifierMatcher.lastIndex = 0 | ||
while (result = finalQuantifierMatcher.exec(haystack)) { | ||
if (result[1] != null) return true | ||
} | ||
return false | ||
} | ||
// When composing expressions into a sequence, regexps that have a top-level | ||
@@ -415,6 +433,6 @@ // choice operator must be wrapped in a non-capturing group. This function | ||
&& (/^\d/.test(x2.source)) | ||
|| combinesAsQuantfier(x1, x2) | ||
? '(?:)' | ||
: '' | ||
) + x2.source | ||
return x2 | ||
@@ -421,0 +439,0 @@ } |
@@ -50,3 +50,3 @@ // /!\ DO NOT EDIT MANUALLY /!\ | ||
const suffixMatcher = /^(?:[+*?]|\{(?=((\d+)))\1,?(\d*)\})\??$/ | ||
const suffixMatcher = /^(?:[+*?]|\{(\d+)(?:,(\d*))?\})\??$/ | ||
@@ -53,0 +53,0 @@ |
import o from 'ospec' | ||
// This must happen before importing the lib | ||
import {nullProto, r, m} from '../test-utils/utils.js' | ||
import {r, m} from '../test-utils/utils.js' | ||
@@ -797,2 +797,54 @@ import { | ||
o("marformed range quantifiers are handled properly", function() { | ||
o(sequence(/a{1/, /}/)).satisfies(r(/a{1(?:)}/)) | ||
o(sequence(/a{/, /1}/)).satisfies(r(/a{(?:)1}/)) | ||
o(sequence(/a{/, /1,}/)).satisfies(r(/a{(?:)1,}/)) | ||
o(sequence(/a{/, /1,12}/)).satisfies(r(/a{(?:)1,12}/)) | ||
o(sequence(/{1/, /}/)).satisfies(r(/{1(?:)}/)) | ||
o(sequence(/{/, /1}/)).satisfies(r(/{(?:)1}/)) | ||
o(sequence(/{/, /1,}/)).satisfies(r(/{(?:)1,}/)) | ||
o(sequence(/{/, /1,12}/)).satisfies(r(/{(?:)1,12}/)) | ||
// also works when the quantifier is spread in more that two pieces | ||
o(sequence(/a{/, "1", /}/)).satisfies(r(/a{(?:)1}/)) | ||
o(sequence(/a{/, "1", /,/, /}/)).satisfies(r(/a{(?:)1,}/)) | ||
o(sequence(/a{/, "1", /,/, "2", /}/)).satisfies(r(/a{(?:)1,2}/)) | ||
// once again with the partial quantifier in non-terminal position | ||
o(sequence(/a{1/, /}a/)).satisfies(r(/a{1(?:)}a/)) | ||
o(sequence(/a{/, /1}a/)).satisfies(r(/a{(?:)1}a/)) | ||
o(sequence(/a{/, /1,}a/)).satisfies(r(/a{(?:)1,}a/)) | ||
o(sequence(/a{/, /1,12}a/)).satisfies(r(/a{(?:)1,12}a/)) | ||
o(sequence(/{1/, /}a/)).satisfies(r(/{1(?:)}a/)) | ||
o(sequence(/{/, /1}a/)).satisfies(r(/{(?:)1}a/)) | ||
o(sequence(/{/, /1,}a/)).satisfies(r(/{(?:)1,}a/)) | ||
o(sequence(/{/, /1,12}a/)).satisfies(r(/{(?:)1,12}a/)) | ||
o(sequence(/a{/, "1", /}a/)).satisfies(r(/a{(?:)1}a/)) | ||
o(sequence(/a{/, "1", /,/, /}a/)).satisfies(r(/a{(?:)1,}a/)) | ||
o(sequence(/a{/, "1", /,/, "2", /}a/)).satisfies(r(/a{(?:)1,2}a/)) | ||
// no false positives: | ||
o(sequence(/a{1/, 'x')).satisfies(r(/a{1x/)) | ||
o(sequence(/a{1,/, 'x')).satisfies(r(/a{1,x/)) | ||
o(sequence(/a{1,2/, 'x')).satisfies(r(/a{1,2x/)) | ||
o(sequence(/a{/,'x')).satisfies(r(/a{x/)) | ||
o(sequence(/a{1/,'x')).satisfies(r(/a{1x/)) | ||
o(sequence(/a{1,/,'x')).satisfies(r(/a{1,x/)) | ||
o(sequence(/a{1,2/,'x')).satisfies(r(/a{1,2x/)) | ||
o(sequence("x", /}/)).satisfies(r(/x}/)) | ||
o(sequence("x", /1}/)).satisfies(r(/x1}/)) | ||
o(sequence("x", /1,}/)).satisfies(r(/x1,}/)) | ||
o(sequence("x",/1,12}/)).satisfies(r(/x1,12}/)) | ||
o(sequence("{", /}/)).satisfies(r(/\{}/)) | ||
o(sequence(/{/, /}/)).satisfies(r(/{}/)) | ||
o(sequence(/a\{/,'1', /}/)).satisfies(r(/a\{1}/)) | ||
}) | ||
o("refs in groups", function() { | ||
@@ -799,0 +851,0 @@ o(sequence( |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
178424
3063