outmatch
Version:
An extremely fast and lightweight glob-matching library with advanced features
3 lines (2 loc) • 6.75 kB
JavaScript
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e="undefined"!=typeof globalThis?globalThis:e||self).outmatch=r()}(this,(function(){"use strict";function e(e){if(e.length<3)return"{"+e+"}";for(var r=-1,n=2;n<e.length;n++)if("."===e[n]&&"."===e[n-1]&&(n<2||"\\"!==e[n-2])){if(r>-1)return"{"+e+"}";r=n-1}if(r>-1){var t=e.substr(0,r),a=e.substr(r+2);if(t.length>0&&a.length>0)return"["+e.substr(0,r)+"-"+e.substr(r+2)+"]"}return"{"+e+"}"}function r(n){if("string"!=typeof n)throw new TypeError("A pattern must be a string, but "+typeof n+" given");for(var t,a=!1,s=0,o=0,i=-1,c=[""],g=[],l=0;l<n.length;l++){var p=n[l];if("\\"!==p){if("{"===p)if(a)s++;else if(l>i&&!s){t=n.substring(i+1,l);for(var u=0;u<c.length;u++)c[u]+=t;g=[],i=l,a=!0,s++}else s--;else if("}"===p)if(a)o++;else if(1===o){if(t=n.substring(i+1,l),g.length>0){var h=[];g.push(r(t));for(u=0;u<c.length;u++)for(var f=0;f<g.length;f++)for(var d=0;d<g[f].length;d++)h.push(c[u]+g[f][d]);c=h}else{t=e(t);for(u=0;u<c.length;u++)c[u]+=t}i=l,o--}else o--;else a||","!==p||o-s!=1||(t=n.substring(i+1,l),g.push(r(t)),i=l);!a||o!==s&&l!==n.length-1||(a=!1,l=i-1)}else l++}if(-1===i)return[n];var b="{"===n[i]?i:i+1;if(b<n.length){t=n.substr(b);for(u=0;u<c.length;u++)c[u]+=t}return c}function n(e,r){var n,t=!1!==r["!"],a=!1!==r["()"],s=!1;if(t){for(n=0;n<e.length&&"!"===e[n];n++){if(a&&"("===e[n+1]){n--;break}s=!s}n>0&&(e=e.substr(n))}return{pattern:e,isNegated:s}}function t(e){return"-"===e||"^"===e||"$"===e||"+"===e||"."===e||"("===e||")"===e||"|"===e||"["===e||"]"===e||"{"===e||"}"===e||"*"===e||"?"===e||"\\"===e?"\\"+e:e}function a(e,r,n){var a=void 0===r.separator||r.separator,s="",o="",i=".";!0===a?(s="/",o="[/\\\\]",i="[^/\\\\]"):i=a?(o=function(e){for(var r="",n=0;n<e.length;n++)r+=t(e[n]);return r}(s=a)).length>1?"((?!"+(o="(?:"+o+")")+").)":"[^"+o+"]":".";var c=a?o+"+?":"",g=a?o+"*?":"",l=a?e.split(s):[e];return{source:e,segments:l,options:r,separator:a,separatorSplitter:s,separatorMatcher:o,optionalSeparator:g,requiredSeparator:c,wildcard:i,support:{qMark:!1!==r["?"],star:!1!==r["*"],globstar:a&&!1!==r["**"],brackets:!1!==r["[]"],extglobs:!1!==r["()"],excludeDot:n&&!1!==r.excludeDot}}}function s(e,r,n,t){return{source:e,isFirst:n,isLast:t,end:e.length-1}}var o="(?!\\.)";function i(e,r,n){return e.addToUnmatch&&(e.result.unmatch+=r),e.addToMatch&&(n&&!e.dotHandled&&(r=o+r),e.dotHandled=!0,e.result.match+=r),e.result}function c(e,r,n){var a=e.support,s=function(e,r,n){return{pattern:e,segment:r,result:n,openingBracket:r.end+1,closingBracket:-1,openingParens:0,closingParens:0,parensHandledUntil:-1,extglobModifiers:[],scanningForParens:!1,escapeChar:!1,addToMatch:!0,addToUnmatch:e.support.extglobs,dotHandled:!1,i:-1,char:"",nextChar:""}}(e,r,n),c=r.isLast?e.optionalSeparator:e.requiredSeparator;if(a.excludeDot||(s.dotHandled=!0),-1===r.end)return r.isLast&&!r.isFirst?n:i(s,c);if(a.globstar&&"**"===r.source)return i(s,"(?:"+((s.dotHandled?"":o)+e.wildcard+"*?"+c)+")*?");for(;++s.i<=r.end;){if(s.char=s.segment.source[s.i],s.nextChar=s.i<r.end?r.source[s.i+1]:"","\\"===s.char){if(s.i<s.segment.end){s.escapeChar=!0;continue}s.char=""}e=s.pattern,r=s.segment;var g=s.char,l=s.i;if(e.support.brackets&&!s.scanningForParens){if(l>s.openingBracket&&l<=s.closingBracket){s.escapeChar?i(s,t(g)):l===s.closingBracket?(i(s,"]"),s.openingBracket=r.source.length):"-"===g&&l===s.closingBracket-1?i(s,"\\-"):"!"===g&&l===s.openingBracket+1?i(s,"^"):i(s,"]"===g?"\\]":g),s.escapeChar=!1;continue}if(l>s.openingBracket){"]"===g&&!s.escapeChar&&l>s.openingBracket+1&&l>s.closingBracket?(s.closingBracket=l,s.i=s.openingBracket,e.separator?i(s,"(?!"+e.separatorMatcher+")[",!0):i(s,"[",!0)):l===r.end&&(i(s,"\\["),s.i=s.openingBracket,s.openingBracket=r.source.length,s.closingBracket=r.source.length),s.escapeChar=!1;continue}if("["===g&&!s.escapeChar&&l>s.closingBracket&&l<r.end){s.openingBracket=l,s.escapeChar=!1;continue}}if(s.pattern.support.extglobs){var p=s.extglobModifiers,u=(g=s.char,s.nextChar);l=s.i;if("("!==u||s.escapeChar||"@"!==g&&"?"!==g&&"*"!==g&&"+"!==g&&"!"!==g){if(")"!==g||s.escapeChar){if("|"===g&&s.closingParens&&!s.scanningForParens&&!s.escapeChar){i(s,"|");continue}}else if(s.scanningForParens)s.closingParens++;else if(p.length){var h=p.pop();if("!"===h&&-1!==p.indexOf("!"))throw new Error("Nested negated extglobs aren't supported");i(s,")"+(h="!"===h||"@"===h?"":h)),s.addToMatch=!0,s.addToUnmatch=!0,s.closingParens--;continue}}else if(s.scanningForParens)s.openingParens++;else if(l>s.parensHandledUntil&&!s.closingParens)s.parensHandledUntil=l,s.scanningForParens=!0,s.openingParens++;else{if(s.closingParens>=s.openingParens){"!"===g&&(s.addToMatch=!0,s.addToUnmatch=!1,i(s,s.pattern.wildcard+"*?",!0),s.addToMatch=!1,s.addToUnmatch=!0,s.result.useUnmatch=!0),p.push(g),i(s,"(?:",!0),s.openingParens--,s.i++;continue}s.openingParens--}if(s.scanningForParens){s.closingParens!==s.openingParens&&l!==s.segment.end||(s.scanningForParens=!1,s.i=s.parensHandledUntil-1),s.escapeChar=!1;continue}}a=(e=s.pattern).support;!s.escapeChar&&a.star&&"*"===s.char?s.i!==s.segment.end&&"*"===s.nextChar||i(s,e.wildcard+"*?",!0):!s.escapeChar&&a.qMark&&"?"===s.char?i(s,e.wildcard,!0):i(s,t(s.char)),s.escapeChar=!1}return i(s,c)}function g(e,r,n){for(var t=a(e,r,n),o={match:"",unmatch:"",useUnmatch:!1},i=t.segments,g=0;g<i.length;g++){c(t,s(i[g],0,0===g,g===i.length-1),o)}return o.useUnmatch?"(?!^"+o.unmatch+"$)"+o.match:o.match}function l(e,r){if("string"!=typeof r)throw new TypeError("Sample must be a string, but "+typeof r+" given");return e.test(r)}return function(e,t){if("string"!=typeof e&&!Array.isArray(e))throw new TypeError("The first argument must be a single pattern string or an array of patterns, but "+typeof e+" given");if("string"!=typeof t&&"boolean"!=typeof t||(t={separator:t}),2===arguments.length&&void 0!==t&&("object"!=typeof t||null===t||Array.isArray(t)))throw new TypeError("The second argument must be an options object or a string/boolean separator, but "+typeof t+" given");if("\\"===(t=t||{}).separator)throw new Error("\\ is not a valid separator");var a=function(e,t){e=Array.isArray(e)?e:[e],!1!==t["{}"]&&(e=function(e,r){for(var n=[],t=0;t<e.length;t++)for(var a=r(e[t]),s=0;s<a.length;s++)n.push(a[s]);return n}(e,r));for(var a=[],s=[],o="",i=0;i<e.length;i++){var c=n(e[i],t),l=g(c.pattern,t,!c.isNegated);c.isNegated?s.push(l):a.push(l)}return s.length&&(o="(?!(?:"+s.join("|")+")$)"),a.length>1?o+="(?:"+a.join("|")+")":1===a.length?o+=a[0]:o.length&&(o+=g("**",t,!0)),"^"+o+"$"}(e,t),s=new RegExp(a,t.flags),o=l.bind(null,s);return o.options=t,o.pattern=e,o.regexp=s,o}}));
//# sourceMappingURL=index.umd.js.map