[Pkg-javascript-commits] [node-minimatch] 03/05: Imported Upstream version 1.0.0

Jérémy Lal kapouer at moszumanska.debian.org
Tue Jul 29 00:17:40 UTC 2014


This is an automated email from the git hooks/post-receive script.

kapouer pushed a commit to branch master
in repository node-minimatch.

commit c38a995a958e780c0ce2257e5c6d338520554b2c
Author: Jérémy Lal <kapouer at melix.org>
Date:   Tue Jul 29 02:15:19 2014 +0200

    Imported Upstream version 1.0.0
---
 .gitignore                             |   1 +
 .travis.yml                            |   4 +
 README.md                              |  80 +++++++--------
 minimatch.js                           | 172 ++++++++++++++++-----------------
 package.json                           |   4 +-
 test/brace-expand.js                   |   7 ++
 test/defaults.js                       |   2 +-
 test/extglob-ending-with-state-char.js |   8 ++
 8 files changed, 146 insertions(+), 132 deletions(-)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..fca8ef0
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+  - 0.10
+  - 0.11
diff --git a/README.md b/README.md
index 6fd07d2..5b3967e 100644
--- a/README.md
+++ b/README.md
@@ -19,6 +19,7 @@ var minimatch = require("minimatch")
 
 minimatch("bar.foo", "*.foo") // true!
 minimatch("bar.foo", "*.bar") // false!
+minimatch("bar.foo", "*.+(bar|foo)", { debug: true }) // true, and noisy!
 ```
 
 ## Features
@@ -36,44 +37,6 @@ See:
 * `man 3 fnmatch`
 * `man 5 gitignore`
 
-### Comparisons to other fnmatch/glob implementations
-
-While strict compliance with the existing standards is a worthwhile
-goal, some discrepancies exist between minimatch and other
-implementations, and are intentional.
-
-If the pattern starts with a `!` character, then it is negated.  Set the
-`nonegate` flag to suppress this behavior, and treat leading `!`
-characters normally.  This is perhaps relevant if you wish to start the
-pattern with a negative extglob pattern like `!(a|B)`.  Multiple `!`
-characters at the start of a pattern will negate the pattern multiple
-times.
-
-If a pattern starts with `#`, then it is treated as a comment, and
-will not match anything.  Use `\#` to match a literal `#` at the
-start of a line, or set the `nocomment` flag to suppress this behavior.
-
-The double-star character `**` is supported by default, unless the
-`noglobstar` flag is set.  This is supported in the manner of bsdglob
-and bash 4.1, where `**` only has special significance if it is the only
-thing in a path part.  That is, `a/**/b` will match `a/x/y/b`, but
-`a/**b` will not.  **Note that this is different from the way that `**` is
-handled by ruby's `Dir` class.**
-
-If an escaped pattern has no matches, and the `nonull` flag is set,
-then minimatch.match returns the pattern as-provided, rather than
-interpreting the character escapes.  For example,
-`minimatch.match([], "\\*a\\?")` will return `"\\*a\\?"` rather than
-`"*a?"`.  This is akin to setting the `nullglob` option in bash, except
-that it does not resolve escaped pattern characters.
-
-If brace expansion is not disabled, then it is performed before any
-other interpretation of the glob pattern.  Thus, a pattern like
-`+(a|{b),c)}`, which would not be valid in bash or zsh, is expanded
-**first** into the set of `+(a|b)` and `+(a|c)`, and those patterns are
-checked for validity.  Since those two are valid, matching proceeds.
-
-
 ## Minimatch Class
 
 Create a minimatch object by instanting the `minimatch.Minimatch` class.
@@ -194,8 +157,8 @@ Perform a case-insensitive match.
 ### nonull
 
 When a match is not found by `minimatch.match`, return a list containing
-the pattern itself.  When set, an empty list is returned if there are
-no matches.
+the pattern itself if this option is set.  When not set, an empty list
+is returned if there are no matches.
 
 ### matchBase
 
@@ -216,3 +179,40 @@ Suppress the behavior of treating a leading `!` character as negation.
 
 Returns from negate expressions the same as if they were not negated.
 (Ie, true on a hit, false on a miss.)
+
+
+## Comparisons to other fnmatch/glob implementations
+
+While strict compliance with the existing standards is a worthwhile
+goal, some discrepancies exist between minimatch and other
+implementations, and are intentional.
+
+If the pattern starts with a `!` character, then it is negated.  Set the
+`nonegate` flag to suppress this behavior, and treat leading `!`
+characters normally.  This is perhaps relevant if you wish to start the
+pattern with a negative extglob pattern like `!(a|B)`.  Multiple `!`
+characters at the start of a pattern will negate the pattern multiple
+times.
+
+If a pattern starts with `#`, then it is treated as a comment, and
+will not match anything.  Use `\#` to match a literal `#` at the
+start of a line, or set the `nocomment` flag to suppress this behavior.
+
+The double-star character `**` is supported by default, unless the
+`noglobstar` flag is set.  This is supported in the manner of bsdglob
+and bash 4.1, where `**` only has special significance if it is the only
+thing in a path part.  That is, `a/**/b` will match `a/x/y/b`, but
+`a/**b` will not.
+
+If an escaped pattern has no matches, and the `nonull` flag is set,
+then minimatch.match returns the pattern as-provided, rather than
+interpreting the character escapes.  For example,
+`minimatch.match([], "\\*a\\?")` will return `"\\*a\\?"` rather than
+`"*a?"`.  This is akin to setting the `nullglob` option in bash, except
+that it does not resolve escaped pattern characters.
+
+If brace expansion is not disabled, then it is performed before any
+other interpretation of the glob pattern.  Thus, a pattern like
+`+(a|{b),c)}`, which would not be valid in bash or zsh, is expanded
+**first** into the set of `+(a|b)` and `+(a|c)`, and those patterns are
+checked for validity.  Since those two are valid, matching proceeds.
diff --git a/minimatch.js b/minimatch.js
index 405746b..4761786 100644
--- a/minimatch.js
+++ b/minimatch.js
@@ -68,17 +68,6 @@ function charSet (s) {
 // normalizes slashes.
 var slashSplit = /\/+/
 
-minimatch.monkeyPatch = monkeyPatch
-function monkeyPatch () {
-  var desc = Object.getOwnPropertyDescriptor(String.prototype, "match")
-  var orig = desc.value
-  desc.value = function (p) {
-    if (p instanceof Minimatch) return p.match(this)
-    return orig.call(this, p)
-  }
-  Object.defineProperty(String.prototype, desc)
-}
-
 minimatch.filter = filter
 function filter (pattern, options) {
   options = options || {}
@@ -178,6 +167,8 @@ function Minimatch (pattern, options) {
   this.make()
 }
 
+Minimatch.prototype.debug = function() {}
+
 Minimatch.prototype.make = make
 function make () {
   // don't do it more than once.
@@ -202,7 +193,9 @@ function make () {
   // step 2: expand braces
   var set = this.globSet = this.braceExpand()
 
-  if (options.debug) console.error(this.pattern, set)
+  if (options.debug) this.debug = console.error
+
+  this.debug(this.pattern, set)
 
   // step 3: now we have a set, so turn each one into a series of path-portion
   // matching patterns.
@@ -213,21 +206,21 @@ function make () {
     return s.split(slashSplit)
   })
 
-  if (options.debug) console.error(this.pattern, set)
+  this.debug(this.pattern, set)
 
   // glob --> regexps
   set = set.map(function (s, si, set) {
     return s.map(this.parse, this)
   }, this)
 
-  if (options.debug) console.error(this.pattern, set)
+  this.debug(this.pattern, set)
 
   // filter out everything that didn't compile properly.
   set = set.filter(function (s) {
     return -1 === s.indexOf(false)
   })
 
-  if (options.debug) console.error(this.pattern, set)
+  this.debug(this.pattern, set)
 
   this.set = set
 }
@@ -267,6 +260,13 @@ minimatch.braceExpand = function (pattern, options) {
 }
 
 Minimatch.prototype.braceExpand = braceExpand
+
+function pad(n, width, z) {
+  z = z || '0';
+  n = n + '';
+  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
+}
+
 function braceExpand (pattern, options) {
   options = options || this.options
   pattern = typeof pattern === "undefined"
@@ -302,11 +302,11 @@ function braceExpand (pattern, options) {
   // So, we pluck that off, and work with the rest,
   // and then prepend it to everything we find.
   if (pattern.charAt(0) !== "{") {
-    // console.error(pattern)
+    this.debug(pattern)
     var prefix = null
     for (var i = 0, l = pattern.length; i < l; i ++) {
       var c = pattern.charAt(i)
-      // console.error(i, c)
+      this.debug(i, c)
       if (c === "\\") {
         escaping = !escaping
       } else if (c === "{" && !escaping) {
@@ -317,11 +317,11 @@ function braceExpand (pattern, options) {
 
     // actually no sets, all { were escaped.
     if (prefix === null) {
-      // console.error("no sets")
+      this.debug("no sets")
       return [pattern]
     }
 
-    var tail = braceExpand(pattern.substr(i), options)
+   var tail = braceExpand.call(this, pattern.substr(i), options)
     return tail.map(function (t) {
       return prefix + t
     })
@@ -336,16 +336,21 @@ function braceExpand (pattern, options) {
   // first, handle numeric sets, since they're easier
   var numset = pattern.match(/^\{(-?[0-9]+)\.\.(-?[0-9]+)\}/)
   if (numset) {
-    // console.error("numset", numset[1], numset[2])
-    var suf = braceExpand(pattern.substr(numset[0].length), options)
+    this.debug("numset", numset[1], numset[2])
+    var suf = braceExpand.call(this, pattern.substr(numset[0].length), options)
       , start = +numset[1]
+      , needPadding = numset[1][0] === '0'
+      , startWidth = numset[1].length
+      , padded
       , end = +numset[2]
       , inc = start > end ? -1 : 1
       , set = []
+
     for (var i = start; i != (end + inc); i += inc) {
+      padded = needPadding ? pad(i, startWidth) : i + ''
       // append all the suffixes
       for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
-        set.push(i + suf[ii])
+        set.push(padded + suf[ii])
       }
     }
     return set
@@ -369,10 +374,10 @@ function braceExpand (pattern, options) {
     member = ""
   }
 
-  // console.error("Entering for")
+  this.debug("Entering for")
   FOR: for (i = 1, l = pattern.length; i < l; i ++) {
     var c = pattern.charAt(i)
-    // console.error("", i, c)
+    this.debug("", i, c)
 
     if (escaping) {
       escaping = false
@@ -420,22 +425,22 @@ function braceExpand (pattern, options) {
   // pattern.substr(i), or we have *not* closed the set,
   // and need to escape the leading brace
   if (depth !== 0) {
-    // console.error("didn't close", pattern)
-    return braceExpand("\\" + pattern, options)
+    this.debug("didn't close", pattern)
+    return braceExpand.call(this, "\\" + pattern, options)
   }
 
   // x{y,z} -> ["xy", "xz"]
-  // console.error("set", set)
-  // console.error("suffix", pattern.substr(i))
-  var suf = braceExpand(pattern.substr(i), options)
+  this.debug("set", set)
+  this.debug("suffix", pattern.substr(i))
+  var suf = braceExpand.call(this, pattern.substr(i), options)
   // ["b", "c{d,e}","{f,g}h"] ->
   //   [["b"], ["cd", "ce"], ["fh", "gh"]]
   var addBraces = set.length === 1
-  // console.error("set pre-expanded", set)
+  this.debug("set pre-expanded", set)
   set = set.map(function (p) {
-    return braceExpand(p, options)
-  })
-  // console.error("set expanded", set)
+    return braceExpand.call(this, p, options)
+  }, this)
+  this.debug("set expanded", set)
 
 
   // [["b"], ["cd", "ce"], ["fh", "gh"]] ->
@@ -496,6 +501,7 @@ function parse (pattern, isSub) {
       // not (start or / followed by . or .. followed by / or end)
       : options.dot ? "(?!(?:^|\\\/)\\.{1,2}(?:$|\\\/))"
       : "(?!\\.)"
+    , self = this
 
   function clearStateChar () {
     if (stateChar) {
@@ -514,6 +520,7 @@ function parse (pattern, isSub) {
           re += "\\"+stateChar
           break
       }
+      self.debug('clearStateChar %j %j', stateChar, re)
       stateChar = false
     }
   }
@@ -522,9 +529,7 @@ function parse (pattern, isSub) {
       ; (i < len) && (c = pattern.charAt(i))
       ; i ++ ) {
 
-    if (options.debug) {
-      console.error("%s\t%s %s %j", pattern, i, re, c)
-    }
+    this.debug("%s\t%s %s %j", pattern, i, re, c)
 
     // skip over any that are escaped.
     if (escaping && reSpecials[c]) {
@@ -551,13 +556,12 @@ function parse (pattern, isSub) {
       case "+":
       case "@":
       case "!":
-        if (options.debug) {
-          console.error("%s\t%s %s %j <-- stateChar", pattern, i, re, c)
-        }
+        this.debug("%s\t%s %s %j <-- stateChar", pattern, i, re, c)
 
         // all of those are literals inside a class, except that
         // the glob [!a] means [^a] in regexp
         if (inClass) {
+          this.debug('  in class')
           if (c === "!" && i === classStart + 1) c = "^"
           re += c
           continue
@@ -566,6 +570,7 @@ function parse (pattern, isSub) {
         // if we already have a stateChar, then it means
         // that there was something like ** or +? in there.
         // Handle the stateChar, then proceed with this one.
+        self.debug('call clearStateChar %j', stateChar)
         clearStateChar()
         stateChar = c
         // if extglob is disabled, then +(asdf|foo) isn't a thing.
@@ -591,6 +596,7 @@ function parse (pattern, isSub) {
                               , reStart: re.length })
         // negation is (?:(?!js)[^/]*)
         re += stateChar === "!" ? "(?:(?!" : "(?:"
+        this.debug('plType %j %j', stateChar, re)
         stateChar = false
         continue
 
@@ -600,6 +606,7 @@ function parse (pattern, isSub) {
           continue
         }
 
+        clearStateChar()
         hasMagic = true
         re += ")"
         plType = patternListStack.pop().type
@@ -623,6 +630,7 @@ function parse (pattern, isSub) {
           continue
         }
 
+        clearStateChar()
         re += "|"
         continue
 
@@ -715,7 +723,7 @@ function parse (pattern, isSub) {
       return $1 + $1 + $2 + "|"
     })
 
-    // console.error("tail=%j\n   %s", tail, tail)
+    this.debug("tail=%j\n   %s", tail, tail)
     var t = pl.type === "*" ? star
           : pl.type === "?" ? qmark
           : "\\" + pl.type
@@ -817,11 +825,12 @@ function makeRe () {
 }
 
 minimatch.match = function (list, pattern, options) {
+  options = options || {}
   var mm = new Minimatch(pattern, options)
   list = list.filter(function (f) {
     return mm.match(f)
   })
-  if (options.nonull && !list.length) {
+  if (mm.options.nonull && !list.length) {
     list.push(pattern)
   }
   return list
@@ -829,7 +838,7 @@ minimatch.match = function (list, pattern, options) {
 
 Minimatch.prototype.match = match
 function match (f, partial) {
-  // console.error("match", f, this.pattern)
+  this.debug("match", f, this.pattern)
   // short-circuit in the case of busted things.
   // comments, etc.
   if (this.comment) return false
@@ -847,9 +856,7 @@ function match (f, partial) {
 
   // treat the test path as a set of pathparts.
   f = f.split(slashSplit)
-  if (options.debug) {
-    console.error(this.pattern, "split", f)
-  }
+  this.debug(this.pattern, "split", f)
 
   // just ONE of the pattern sets in this.set needs to match
   // in order for it to be valid.  If negating, then just one
@@ -857,11 +864,21 @@ function match (f, partial) {
   // Either way, return on the first hit.
 
   var set = this.set
-  // console.error(this.pattern, "set", set)
+  this.debug(this.pattern, "set", set)
+
+  // Find the basename of the path by looking for the last non-empty segment
+  var filename;
+  for (var i = f.length - 1; i >= 0; i--) {
+    filename = f[i]
+    if (filename) break
+  }
 
   for (var i = 0, l = set.length; i < l; i ++) {
-    var pattern = set[i]
-    var hit = this.matchOne(f, pattern, partial)
+    var pattern = set[i], file = f
+    if (options.matchBase && pattern.length === 1) {
+      file = [filename]
+    }
+    var hit = this.matchOne(file, pattern, partial)
     if (hit) {
       if (options.flipNegate) return true
       return !this.negate
@@ -882,20 +899,12 @@ function match (f, partial) {
 Minimatch.prototype.matchOne = function (file, pattern, partial) {
   var options = this.options
 
-  if (options.debug) {
-    console.error("matchOne",
-                  { "this": this
-                  , file: file
-                  , pattern: pattern })
-  }
-
-  if (options.matchBase && pattern.length === 1) {
-    file = path.basename(file.join("/")).split("/")
-  }
+  this.debug("matchOne",
+              { "this": this
+              , file: file
+              , pattern: pattern })
 
-  if (options.debug) {
-    console.error("matchOne", file.length, pattern.length)
-  }
+  this.debug("matchOne", file.length, pattern.length)
 
   for ( var fi = 0
           , pi = 0
@@ -904,23 +913,18 @@ Minimatch.prototype.matchOne = function (file, pattern, partial) {
       ; (fi < fl) && (pi < pl)
       ; fi ++, pi ++ ) {
 
-    if (options.debug) {
-      console.error("matchOne loop")
-    }
+    this.debug("matchOne loop")
     var p = pattern[pi]
       , f = file[fi]
 
-    if (options.debug) {
-      console.error(pattern, p, f)
-    }
+    this.debug(pattern, p, f)
 
     // should be impossible.
     // some invalid regexp stuff in the set.
     if (p === false) return false
 
     if (p === GLOBSTAR) {
-      if (options.debug)
-        console.error('GLOBSTAR', [pattern, p, f])
+      this.debug('GLOBSTAR', [pattern, p, f])
 
       // "**"
       // a/**/b/**/c would match the following:
@@ -947,8 +951,7 @@ Minimatch.prototype.matchOne = function (file, pattern, partial) {
       var fr = fi
         , pr = pi + 1
       if (pr === pl) {
-        if (options.debug)
-          console.error('** at the end')
+        this.debug('** at the end')
         // a ** at the end will just swallow the rest.
         // We have found a match.
         // however, it will not swallow /.x, unless
@@ -966,15 +969,12 @@ Minimatch.prototype.matchOne = function (file, pattern, partial) {
       WHILE: while (fr < fl) {
         var swallowee = file[fr]
 
-        if (options.debug) {
-          console.error('\nglobstar while',
-                        file, fr, pattern, pr, swallowee)
-        }
+        this.debug('\nglobstar while',
+                    file, fr, pattern, pr, swallowee)
 
         // XXX remove this slice.  Just pass the start index.
         if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
-          if (options.debug)
-            console.error('globstar found match!', fr, fl, swallowee)
+          this.debug('globstar found match!', fr, fl, swallowee)
           // found a match.
           return true
         } else {
@@ -982,23 +982,21 @@ Minimatch.prototype.matchOne = function (file, pattern, partial) {
           // can only swallow ".foo" when explicitly asked.
           if (swallowee === "." || swallowee === ".." ||
               (!options.dot && swallowee.charAt(0) === ".")) {
-            if (options.debug)
-              console.error("dot detected!", file, fr, pattern, pr)
+            this.debug("dot detected!", file, fr, pattern, pr)
             break WHILE
           }
 
           // ** swallows a segment, and continue.
-          if (options.debug)
-            console.error('globstar swallow a segment, and continue')
+          this.debug('globstar swallow a segment, and continue')
           fr ++
         }
       }
       // no match was found.
       // However, in partial mode, we can't say this is necessarily over.
-      // If there's more *pattern* left, then 
+      // If there's more *pattern* left, then
       if (partial) {
         // ran out of file
-        // console.error("\n>>> no match, partial?", file, fr, pattern, pr)
+        this.debug("\n>>> no match, partial?", file, fr, pattern, pr)
         if (fr === fl) return true
       }
       return false
@@ -1014,14 +1012,10 @@ Minimatch.prototype.matchOne = function (file, pattern, partial) {
       } else {
         hit = f === p
       }
-      if (options.debug) {
-        console.error("string match", p, f, hit)
-      }
+      this.debug("string match", p, f, hit)
     } else {
       hit = f.match(p)
-      if (options.debug) {
-        console.error("pattern match", p, f, hit)
-      }
+      this.debug("pattern match", p, f, hit)
     }
 
     if (!hit) return false
diff --git a/package.json b/package.json
index 36510b8..3332329 100644
--- a/package.json
+++ b/package.json
@@ -2,14 +2,14 @@
   "author": "Isaac Z. Schlueter <i at izs.me> (http://blog.izs.me)",
   "name": "minimatch",
   "description": "a glob matcher in javascript",
-  "version": "0.2.12",
+  "version": "1.0.0",
   "repository": {
     "type": "git",
     "url": "git://github.com/isaacs/minimatch.git"
   },
   "main": "minimatch.js",
   "scripts": {
-    "test": "tap test"
+    "test": "tap test/*.js"
   },
   "engines": {
     "node": "*"
diff --git a/test/brace-expand.js b/test/brace-expand.js
index 7ee278a..e63d3f6 100644
--- a/test/brace-expand.js
+++ b/test/brace-expand.js
@@ -21,6 +21,13 @@ tap.test("brace expansion", function (t) {
         , "a4b"
         , "a5b" ] ]
     , [ "a{b}c", ["a{b}c"] ]
+    , [ "a{00..05}b"
+      , ["a00b"
+        ,"a01b"
+        ,"a02b"
+        ,"a03b"
+        ,"a04b"
+        ,"a05b" ] ]
   ].forEach(function (tc) {
     var p = tc[0]
       , expect = tc[1]
diff --git a/test/defaults.js b/test/defaults.js
index 25f1f60..75e0571 100644
--- a/test/defaults.js
+++ b/test/defaults.js
@@ -237,7 +237,7 @@ tap.test("basic tests", function (t) {
 
       var pattern = c[0]
         , expect = c[1].sort(alpha)
-        , options = c[2] || {}
+        , options = c[2]
         , f = c[3] || files
         , tapOpts = c[4] || {}
 
diff --git a/test/extglob-ending-with-state-char.js b/test/extglob-ending-with-state-char.js
new file mode 100644
index 0000000..6676e26
--- /dev/null
+++ b/test/extglob-ending-with-state-char.js
@@ -0,0 +1,8 @@
+var test = require('tap').test
+var minimatch = require('../')
+
+test('extglob ending with statechar', function(t) {
+  t.notOk(minimatch('ax', 'a?(b*)'))
+  t.ok(minimatch('ax', '?(a*|b)'))
+  t.end()
+})

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-minimatch.git



More information about the Pkg-javascript-commits mailing list