1 ;(function (require, exports, module, platform) {
3 if (module) module.exports = minimatch
4 else exports.minimatch = minimatch
7 require = function (id) {
9 case "sigmund": return function sigmund (obj) {
10 return JSON.stringify(obj)
12 case "path": return { basename: function (f) {
18 case "lru-cache": return function LRUCache () {
19 // not quite an LRU, but still space-limited.
22 this.set = function (k, v) {
24 if (cnt >= 100) cache = {}
27 this.get = function (k) { return cache[k] }
33 minimatch.Minimatch = Minimatch
35 var LRU = require("lru-cache")
36 , cache = minimatch.cache = new LRU({max: 100})
37 , GLOBSTAR = minimatch.GLOBSTAR = Minimatch.GLOBSTAR = {}
38 , sigmund = require("sigmund")
40 var path = require("path")
41 // any single thing other than /
42 // don't need to escape / when using new RegExp()
45 // * => any number of characters
48 // ** when dots are allowed. Anything goes, except .. and .
49 // not (^ or / followed by one or two dots followed by $ or /),
50 // followed by anything, any number of times.
51 , twoStarDot = "(?:(?!(?:\\\/|^)(?:\\.{1,2})($|\\\/)).)*?"
53 // not a ^ or / followed by a dot,
54 // followed by anything, any number of times.
55 , twoStarNoDot = "(?:(?!(?:\\\/|^)\\.).)*?"
57 // characters that need to be escaped in RegExp.
58 , reSpecials = charSet("().*{}+?[]^$\\!")
60 // "abc" -> { a:true, b:true, c:true }
61 function charSet (s) {
62 return s.split("").reduce(function (set, c) {
68 // normalizes slashes.
69 var slashSplit = /\/+/
71 minimatch.filter = filter
72 function filter (pattern, options) {
73 options = options || {}
74 return function (p, i, list) {
75 return minimatch(p, pattern, options)
83 Object.keys(b).forEach(function (k) {
86 Object.keys(a).forEach(function (k) {
92 minimatch.defaults = function (def) {
93 if (!def || !Object.keys(def).length) return minimatch
97 var m = function minimatch (p, pattern, options) {
98 return orig.minimatch(p, pattern, ext(def, options))
101 m.Minimatch = function Minimatch (pattern, options) {
102 return new orig.Minimatch(pattern, ext(def, options))
108 Minimatch.defaults = function (def) {
109 if (!def || !Object.keys(def).length) return Minimatch
110 return minimatch.defaults(def).Minimatch
114 function minimatch (p, pattern, options) {
115 if (typeof pattern !== "string") {
116 throw new TypeError("glob pattern string required")
119 if (!options) options = {}
121 // shortcut: comments match nothing.
122 if (!options.nocomment && pattern.charAt(0) === "#") {
126 // "" only matches ""
127 if (pattern.trim() === "") return p === ""
129 return new Minimatch(pattern, options).match(p)
132 function Minimatch (pattern, options) {
133 if (!(this instanceof Minimatch)) {
134 return new Minimatch(pattern, options, cache)
137 if (typeof pattern !== "string") {
138 throw new TypeError("glob pattern string required")
141 if (!options) options = {}
142 pattern = pattern.trim()
144 // windows: need to use /, not \
145 // On other platforms, \ is a valid (albeit bad) filename char.
146 if (platform === "win32") {
147 pattern = pattern.split("\\").join("/")
151 // these things aren't particularly big, but walking down the string
152 // and turning it into a regexp can get pretty costly.
153 var cacheKey = pattern + "\n" + sigmund(options)
154 var cached = minimatch.cache.get(cacheKey)
155 if (cached) return cached
156 minimatch.cache.set(cacheKey, this)
158 this.options = options
160 this.pattern = pattern
166 // make the set of regexps etc.
170 Minimatch.prototype.debug = function() {}
172 Minimatch.prototype.make = make
174 // don't do it more than once.
175 if (this._made) return
177 var pattern = this.pattern
178 var options = this.options
180 // empty patterns and comments match nothing.
181 if (!options.nocomment && pattern.charAt(0) === "#") {
190 // step 1: figure out negation, etc.
193 // step 2: expand braces
194 var set = this.globSet = this.braceExpand()
196 if (options.debug) this.debug = console.error
198 this.debug(this.pattern, set)
200 // step 3: now we have a set, so turn each one into a series of path-portion
201 // matching patterns.
202 // These will be regexps, except in the case of "**", which is
203 // set to the GLOBSTAR object for globstar behavior,
204 // and will not contain any / characters
205 set = this.globParts = set.map(function (s) {
206 return s.split(slashSplit)
209 this.debug(this.pattern, set)
212 set = set.map(function (s, si, set) {
213 return s.map(this.parse, this)
216 this.debug(this.pattern, set)
218 // filter out everything that didn't compile properly.
219 set = set.filter(function (s) {
220 return -1 === s.indexOf(false)
223 this.debug(this.pattern, set)
228 Minimatch.prototype.parseNegate = parseNegate
229 function parseNegate () {
230 var pattern = this.pattern
232 , options = this.options
235 if (options.nonegate) return
237 for ( var i = 0, l = pattern.length
238 ; i < l && pattern.charAt(i) === "!"
244 if (negateOffset) this.pattern = pattern.substr(negateOffset)
249 // a{b,c}d -> abd acd
251 // a{0..3}d -> a0d a1d a2d a3d
252 // a{b,c{d,e}f}g -> abg acdfg acefg
253 // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
255 // Invalid sets are not expanded.
256 // a{2..}b -> a{2..}b
258 minimatch.braceExpand = function (pattern, options) {
259 return new Minimatch(pattern, options).braceExpand()
262 Minimatch.prototype.braceExpand = braceExpand
263 function braceExpand (pattern, options) {
264 options = options || this.options
265 pattern = typeof pattern === "undefined"
266 ? this.pattern : pattern
268 if (typeof pattern === "undefined") {
269 throw new Error("undefined pattern")
272 if (options.nobrace ||
273 !pattern.match(/\{.*\}/)) {
274 // shortcut. no need to expand.
280 // examples and comments refer to this crazy pattern:
281 // a{b,c{d,e},{f,g}h}x{y,z}
294 // everything before the first \{ is just a prefix.
295 // So, we pluck that off, and work with the rest,
296 // and then prepend it to everything we find.
297 if (pattern.charAt(0) !== "{") {
300 for (var i = 0, l = pattern.length; i < l; i ++) {
301 var c = pattern.charAt(i)
305 } else if (c === "{" && !escaping) {
306 prefix = pattern.substr(0, i)
311 // actually no sets, all { were escaped.
312 if (prefix === null) {
313 this.debug("no sets")
317 var tail = braceExpand.call(this, pattern.substr(i), options)
318 return tail.map(function (t) {
323 // now we have something like:
324 // {b,c{d,e},{f,g}h}x{y,z}
325 // walk through the set, expanding each part, until
326 // the set ends. then, we'll expand the suffix.
327 // If the set only has a single member, then'll put the {} back
329 // first, handle numeric sets, since they're easier
330 var numset = pattern.match(/^\{(-?[0-9]+)\.\.(-?[0-9]+)\}/)
332 this.debug("numset", numset[1], numset[2])
333 var suf = braceExpand.call(this, pattern.substr(numset[0].length), options)
336 , inc = start > end ? -1 : 1
338 for (var i = start; i != (end + inc); i += inc) {
339 // append all the suffixes
340 for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
341 set.push(i + suf[ii])
347 // ok, walk through the set
348 // We hope, somewhat optimistically, that there
349 // will be a } at the end.
350 // If the closing brace isn't found, then the pattern is
351 // interpreted as braceExpand("\\" + pattern) so that
352 // the leading \{ will be interpreted literally.
353 var i = 1 // skip the \{
360 function addMember () {
365 this.debug("Entering for")
366 FOR: for (i = 1, l = pattern.length; i < l; i ++) {
367 var c = pattern.charAt(i)
386 // if this closes the actual set, then we're done
389 // pluck off the close-brace
412 // now we've either finished the set, and the suffix is
413 // pattern.substr(i), or we have *not* closed the set,
414 // and need to escape the leading brace
416 this.debug("didn't close", pattern)
417 return braceExpand.call(this, "\\" + pattern, options)
420 // x{y,z} -> ["xy", "xz"]
421 this.debug("set", set)
422 this.debug("suffix", pattern.substr(i))
423 var suf = braceExpand.call(this, pattern.substr(i), options)
424 // ["b", "c{d,e}","{f,g}h"] ->
425 // [["b"], ["cd", "ce"], ["fh", "gh"]]
426 var addBraces = set.length === 1
427 this.debug("set pre-expanded", set)
428 set = set.map(function (p) {
429 return braceExpand.call(this, p, options)
431 this.debug("set expanded", set)
434 // [["b"], ["cd", "ce"], ["fh", "gh"]] ->
435 // ["b", "cd", "ce", "fh", "gh"]
436 set = set.reduce(function (l, r) {
441 set = set.map(function (s) {
446 // now attach the suffixes.
448 for (var i = 0, l = set.length; i < l; i ++) {
449 for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
450 ret.push(set[i] + suf[ii])
456 // parse a component of the expanded set.
457 // At this point, no pattern may contain "/" in it
458 // so we're going to return a 2d array, where each entry is the full
459 // pattern, split on '/', and then turned into a regular expression.
460 // A regexp is made at the end which joins each array with an
461 // escaped /, and another full one which joins each regexp with |.
463 // Following the lead of Bash 4.1, note that "**" only has special meaning
464 // when it is the *only* thing in a path portion. Otherwise, any series
465 // of * is equivalent to a single *. Globstar behavior is enabled by
466 // default, and can be disabled by setting options.noglobstar.
467 Minimatch.prototype.parse = parse
469 function parse (pattern, isSub) {
470 var options = this.options
473 if (!options.noglobstar && pattern === "**") return GLOBSTAR
474 if (pattern === "") return ""
477 , hasMagic = !!options.nocase
479 // ? => one single character
480 , patternListStack = []
486 // . and .. never match anything that doesn't start with .,
487 // even when options.dot is set.
488 , patternStart = pattern.charAt(0) === "." ? "" // anything
489 // not (start or / followed by . or .. followed by / or end)
490 : options.dot ? "(?!(?:^|\\\/)\\.{1,2}(?:$|\\\/))"
494 function clearStateChar () {
496 // we had some state-tracking character
497 // that wasn't consumed by this pass.
511 self.debug('clearStateChar %j %j', stateChar, re)
516 for ( var i = 0, len = pattern.length, c
517 ; (i < len) && (c = pattern.charAt(i))
520 this.debug("%s\t%s %s %j", pattern, i, re, c)
522 // skip over any that are escaped.
523 if (escaping && reSpecials[c]) {
531 // completely not allowed, even escaped.
532 // Should already be path-split by now.
540 // the various stateChar values
541 // for the "extglob" stuff.
547 this.debug("%s\t%s %s %j <-- stateChar", pattern, i, re, c)
549 // all of those are literals inside a class, except that
550 // the glob [!a] means [^a] in regexp
552 this.debug(' in class')
553 if (c === "!" && i === classStart + 1) c = "^"
558 // if we already have a stateChar, then it means
559 // that there was something like ** or +? in there.
560 // Handle the stateChar, then proceed with this one.
561 self.debug('call clearStateChar %j', stateChar)
564 // if extglob is disabled, then +(asdf|foo) isn't a thing.
565 // just clear the statechar *now*, rather than even diving into
566 // the patternList stuff.
567 if (options.noext) clearStateChar()
582 patternListStack.push({ type: plType
584 , reStart: re.length })
585 // negation is (?:(?!js)[^/]*)
586 re += stateChar === "!" ? "(?:(?!" : "(?:"
587 this.debug('plType %j %j', stateChar, re)
592 if (inClass || !patternListStack.length) {
600 plType = patternListStack.pop().type
601 // negation is (?:(?!js)[^/]*)
602 // The others are (?:<pattern>)<type>
609 case "*": re += plType
610 case "@": break // the default anyway
615 if (inClass || !patternListStack.length || escaping) {
625 // these are mostly the same in regexp and glob
627 // swallow any state-tracking char before the [
637 reClassStart = re.length
642 // a right bracket shall lose its special
643 // meaning and represent itself in
644 // a bracket expression if it occurs
645 // first in the list. -- POSIX.2 2.8.3.2
646 if (i === classStart + 1 || !inClass) {
652 // finish up the class.
659 // swallow any state char that wasn't consumed
665 } else if (reSpecials[c]
666 && !(c === "^" && inClass)) {
676 // handle the case where we left a class open.
677 // "[abc" is valid, equivalent to "\[abc"
679 // split where the last [ was, and escape it
680 // this is a huge pita. We now have to re-walk
681 // the contents of the would-be class to re-translate
682 // any characters that were passed through as-is
683 var cs = pattern.substr(classStart + 1)
684 , sp = this.parse(cs, SUBPARSE)
685 re = re.substr(0, reClassStart) + "\\[" + sp[0]
686 hasMagic = hasMagic || sp[1]
689 // handle the case where we had a +( thing at the *end*
691 // each pattern list stack adds 3 chars, and we need to go through
692 // and escape any | chars that were passed through as-is for the regexp.
693 // Go through and escape them, taking care not to double-escape any
694 // | chars that were already escaped.
696 while (pl = patternListStack.pop()) {
697 var tail = re.slice(pl.reStart + 3)
698 // maybe some even number of \, then maybe 1 \, followed by a |
699 tail = tail.replace(/((?:\\{2})*)(\\?)\|/g, function (_, $1, $2) {
701 // the | isn't already escaped, so escape it.
705 // need to escape all those slashes *again*, without escaping the
706 // one that we need for escaping the | character. As it works out,
707 // escaping an even number of slashes can be done by simply repeating
708 // it exactly after itself. That's why this trick works.
710 // I am sorry that you have to see this.
711 return $1 + $1 + $2 + "|"
714 this.debug("tail=%j\n %s", tail, tail)
715 var t = pl.type === "*" ? star
716 : pl.type === "?" ? qmark
720 re = re.slice(0, pl.reStart)
725 // handle trailing things that only matter at the very end.
732 // only need to apply the nodot start if the re starts with
733 // something that could conceivably capture a dot
734 var addPatternStart = false
735 switch (re.charAt(0)) {
738 case "(": addPatternStart = true
741 // if the re is not "" at this point, then we need to make sure
742 // it doesn't match against an empty path part.
743 // Otherwise a/* will match a/, which it should not.
744 if (re !== "" && hasMagic) re = "(?=.)" + re
746 if (addPatternStart) re = patternStart + re
748 // parsing just a piece of a larger pattern.
749 if (isSub === SUBPARSE) {
750 return [ re, hasMagic ]
753 // skip the regexp for non-magical patterns
754 // unescape anything in it, though, so that it'll be
755 // an exact match against a file etc.
757 return globUnescape(pattern)
760 var flags = options.nocase ? "i" : ""
761 , regExp = new RegExp("^" + re + "$", flags)
763 regExp._glob = pattern
769 minimatch.makeRe = function (pattern, options) {
770 return new Minimatch(pattern, options || {}).makeRe()
773 Minimatch.prototype.makeRe = makeRe
775 if (this.regexp || this.regexp === false) return this.regexp
777 // at this point, this.set is a 2d array of partial
778 // pattern strings, or "**".
780 // It's better to use .match(). This function shouldn't
781 // be used, really, but it's pretty convenient sometimes,
782 // when you just want to work with a regex.
785 if (!set.length) return this.regexp = false
786 var options = this.options
788 var twoStar = options.noglobstar ? star
789 : options.dot ? twoStarDot
791 , flags = options.nocase ? "i" : ""
793 var re = set.map(function (pattern) {
794 return pattern.map(function (p) {
795 return (p === GLOBSTAR) ? twoStar
796 : (typeof p === "string") ? regExpEscape(p)
801 // must match entire pattern
802 // ending in a * or ** will make it less strict.
803 re = "^(?:" + re + ")$"
805 // can match anything, as long as it's not this.
806 if (this.negate) re = "^(?!" + re + ").*$"
809 return this.regexp = new RegExp(re, flags)
811 return this.regexp = false
815 minimatch.match = function (list, pattern, options) {
816 var mm = new Minimatch(pattern, options)
817 list = list.filter(function (f) {
820 if (options.nonull && !list.length) {
826 Minimatch.prototype.match = match
827 function match (f, partial) {
828 this.debug("match", f, this.pattern)
829 // short-circuit in the case of busted things.
831 if (this.comment) return false
832 if (this.empty) return f === ""
834 if (f === "/" && partial) return true
836 var options = this.options
838 // windows: need to use /, not \
839 // On other platforms, \ is a valid (albeit bad) filename char.
840 if (platform === "win32") {
841 f = f.split("\\").join("/")
844 // treat the test path as a set of pathparts.
845 f = f.split(slashSplit)
846 this.debug(this.pattern, "split", f)
848 // just ONE of the pattern sets in this.set needs to match
849 // in order for it to be valid. If negating, then just one
850 // match means that we have failed.
851 // Either way, return on the first hit.
854 this.debug(this.pattern, "set", set)
856 var splitFile = path.basename(f.join("/")).split("/")
858 for (var i = 0, l = set.length; i < l; i ++) {
859 var pattern = set[i], file = f
860 if (options.matchBase && pattern.length === 1) {
863 var hit = this.matchOne(file, pattern, partial)
865 if (options.flipNegate) return true
870 // didn't get any hits. this is success if it's a negative
871 // pattern, failure otherwise.
872 if (options.flipNegate) return false
876 // set partial to true to test if, for example,
877 // "/a/b" matches the start of "/*/b/*/d"
878 // Partial means, if you run out of file before you run
879 // out of pattern, then that's fine, as long as all
881 Minimatch.prototype.matchOne = function (file, pattern, partial) {
882 var options = this.options
884 this.debug("matchOne",
887 , pattern: pattern })
889 this.debug("matchOne", file.length, pattern.length)
894 , pl = pattern.length
895 ; (fi < fl) && (pi < pl)
898 this.debug("matchOne loop")
902 this.debug(pattern, p, f)
904 // should be impossible.
905 // some invalid regexp stuff in the set.
906 if (p === false) return false
908 if (p === GLOBSTAR) {
909 this.debug('GLOBSTAR', [pattern, p, f])
912 // a/**/b/**/c would match the following:
917 // To do this, take the rest of the pattern after
918 // the **, and see if it would match the file remainder.
919 // If so, return success.
920 // If not, the ** "swallows" a segment, and try again.
921 // This is recursively awful.
923 // a/**/b/**/c matching a/b/x/y/z/c
926 // - matchOne(b/x/y/z/c, b/**/c)
929 // - matchOne(x/y/z/c, c) -> no
930 // - matchOne(y/z/c, c) -> no
931 // - matchOne(z/c, c) -> no
932 // - matchOne(c, c) yes, hit
936 this.debug('** at the end')
937 // a ** at the end will just swallow the rest.
938 // We have found a match.
939 // however, it will not swallow /.x, unless
940 // options.dot is set.
941 // . and .. are *never* matched by **, for explosively
942 // exponential reasons.
943 for ( ; fi < fl; fi ++) {
944 if (file[fi] === "." || file[fi] === ".." ||
945 (!options.dot && file[fi].charAt(0) === ".")) return false
950 // ok, let's see if we can swallow whatever we can.
951 WHILE: while (fr < fl) {
952 var swallowee = file[fr]
954 this.debug('\nglobstar while',
955 file, fr, pattern, pr, swallowee)
957 // XXX remove this slice. Just pass the start index.
958 if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
959 this.debug('globstar found match!', fr, fl, swallowee)
963 // can't swallow "." or ".." ever.
964 // can only swallow ".foo" when explicitly asked.
965 if (swallowee === "." || swallowee === ".." ||
966 (!options.dot && swallowee.charAt(0) === ".")) {
967 this.debug("dot detected!", file, fr, pattern, pr)
971 // ** swallows a segment, and continue.
972 this.debug('globstar swallow a segment, and continue')
976 // no match was found.
977 // However, in partial mode, we can't say this is necessarily over.
978 // If there's more *pattern* left, then
981 this.debug("\n>>> no match, partial?", file, fr, pattern, pr)
982 if (fr === fl) return true
987 // something other than **
988 // non-magic patterns just have to match exactly
989 // patterns with magic have been turned into regexps.
991 if (typeof p === "string") {
992 if (options.nocase) {
993 hit = f.toLowerCase() === p.toLowerCase()
997 this.debug("string match", p, f, hit)
1000 this.debug("pattern match", p, f, hit)
1003 if (!hit) return false
1006 // Note: ending in / means that we'll get a final ""
1007 // at the end of the pattern. This can only match a
1008 // corresponding "" at the end of the file.
1009 // If the file ends in /, then it can only match a
1010 // a pattern that ends in /, unless the pattern just
1011 // doesn't have any more for it. But, a/b/ should *not*
1012 // match "a/b/*", even though "" matches against the
1013 // [^/]*? pattern, except in partial mode, where it might
1014 // simply not be reached yet.
1015 // However, a/b/ should still satisfy a/*
1017 // now either we fell off the end of the pattern, or we're done.
1018 if (fi === fl && pi === pl) {
1019 // ran out of pattern and filename at the same time.
1022 } else if (fi === fl) {
1023 // ran out of file, but still had pattern left.
1024 // this is ok if we're doing the match as part of
1025 // a glob fs traversal.
1027 } else if (pi === pl) {
1028 // ran out of pattern, still have file left.
1029 // this is only acceptable if we're on the very last
1030 // empty segment of a file with a trailing slash.
1031 // a/* should match a/b/
1032 var emptyFileEnd = (fi === fl - 1) && (file[fi] === "")
1036 // should be unreachable.
1037 throw new Error("wtf?")
1041 // replace stuff like \* with *
1042 function globUnescape (s) {
1043 return s.replace(/\\(.)/g, "$1")
1047 function regExpEscape (s) {
1048 return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
1051 })( typeof require === "function" ? require : null,
1053 typeof module === "object" ? module : null,
1054 typeof process === "object" ? process.platform : "win32"