glob-to-regexp
Advanced tools
Comparing version 0.2.0 to 0.3.0
28
index.js
@@ -30,4 +30,6 @@ module.exports = function (glob, opts) { | ||
var inGroup = false; | ||
// RegExp flags (eg "i" ) to pass in to RegExp constructor. | ||
var flags = opts && typeof( opts.flags ) === "string" ? opts.flags : ""; | ||
var c; | ||
@@ -88,14 +90,30 @@ for (var i = 0, len = str.length; i < len; i++) { | ||
case "*": | ||
// Move over all consecutive "*"'s. | ||
// Also store the previous and next characters | ||
var prevChar = str[i - 1]; | ||
var starCount = 1; | ||
while(str[i + 1] === "*") { | ||
starCount++; | ||
i++; | ||
} | ||
var nextChar = str[i + 1]; | ||
if (!globstar) { | ||
// globstar is disabled, so treat any number of "*" as one | ||
reStr += ".*"; | ||
} else { | ||
if (str[i + 1] === "*") { | ||
reStr += ".*"; | ||
i++; // move over second '*' | ||
// globstar is enabled, so determine if this is a globstar segment | ||
var isGlobstar = starCount > 1 // multiple "*"'s | ||
&& (prevChar === "/" || prevChar === undefined) // from the start of the segment | ||
&& (nextChar === "/" || nextChar === undefined) // to the end of the segment | ||
if (isGlobstar) { | ||
// it's a globstar, so match zero or more path segments | ||
reStr += "(?:[^/]*(?:\/|$))*"; | ||
i++; // move over the "/" | ||
} else { | ||
// it's not a globstar, so only match one path segment | ||
reStr += "[^/]*"; | ||
} | ||
} | ||
// move over repeated *'s | ||
while(str[i + 1] === "*") i++; | ||
break; | ||
@@ -102,0 +120,0 @@ |
{ | ||
"name": "glob-to-regexp", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Convert globs to regular expressions", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
14
test.js
@@ -175,3 +175,11 @@ var globToRegexp = require("./index.js"); | ||
assertMatch("/foo/**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true }); | ||
assertMatch("/foo/**/bar.txt", "/foo/bar.txt", {globstar: true }); | ||
assertMatch("/foo/**/**/bar.txt", "/foo/bar.txt", {globstar: true }); | ||
assertMatch("/foo/**/*/baz.txt", "/foo/bar/baz.txt", {globstar: true }); | ||
assertMatch("/foo/**/*.txt", "/foo/bar.txt", {globstar: true }); | ||
assertMatch("/foo/**/**/*.txt", "/foo/bar.txt", {globstar: true }); | ||
assertMatch("/foo/**/*/*.txt", "/foo/bar/baz.txt", {globstar: true }); | ||
assertMatch("**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true }); | ||
assertMatch("**/foo.txt", "foo.txt", {globstar: true }); | ||
assertMatch("**/*.txt", "foo.txt", {globstar: true }); | ||
@@ -181,4 +189,10 @@ assertNotMatch("/foo/*", "/foo/bar/baz.txt", {globstar: true }); | ||
assertNotMatch("/foo/*/*.txt", "/foo/bar/baz/qux.txt", {globstar: true }); | ||
assertNotMatch("/foo/*/bar.txt", "/foo/bar.txt", {globstar: true }); | ||
assertNotMatch("/foo/*/*/baz.txt", "/foo/bar/baz.txt", {globstar: true }); | ||
assertNotMatch("/foo/**.txt", "/foo/bar/baz/qux.txt", {globstar: true }); | ||
assertNotMatch("/foo/bar**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true }); | ||
assertNotMatch("/foo/bar**", "/foo/bar/baz.txt", {globstar: true }); | ||
assertNotMatch("**/.txt", "/foo/bar/baz/qux.txt", {globstar: true }); | ||
assertNotMatch("*/*.txt", "/foo/bar/baz/qux.txt", {globstar: true }); | ||
assertNotMatch("*/*.txt", "foo.txt", {globstar: true }); | ||
@@ -185,0 +199,0 @@ assertNotMatch("http://foo.com/*", |
17597
305