Browse files

Add support for bash-like globs through 'extended' flag

  • Loading branch information...
1 parent 561ddde commit 84428dca45a09e1dfd163ebe3ea6179b261da910 Johan Warlander committed Nov 26, 2013
Showing with 67 additions and 4 deletions.
  1. +9 −0 README.md
  2. +30 −4 index.js
  3. +28 −0 test.js
View
9 README.md
@@ -5,6 +5,10 @@
Turn a *-wildcard style glob (`"*.min.js"`) into a regular expression
(`/^.*\.min\.js$/`)!
+To match bash-like globs, eg. ? for any single-character match, [a-z]
+for character ranges, and {*.html, *.js} for multiple alternatives,
+call with extended = true.
+
## Install
npm install glob-to-regexp
@@ -24,6 +28,11 @@ Turn a *-wildcard style glob (`"*.min.js"`) into a regular expression
re.test("http://example.com/www/app.js"); // true
re.test("http://example.com/www/lib/factory-proxy-model-observer.js"); // true
+ // Extended globs
+ re = globToRegExp("*/www/{*.js,*.html}", true);
+ re.test("http://example.com/www/app.js"); // true
+ re.test("http://example.com/www/index.html"); // true
+
## License
Copyright (c) 2013, Nick Fitzgerald
View
34 index.js
@@ -1,9 +1,11 @@
-module.exports = function (glob) {
+module.exports = function (glob, extended) {
if (glob === null || typeof glob === "undefined") {
return null;
}
+ if (typeof extended === "undefined") extended = false;
var str = String(glob),
reStr = "",
+ inMulti = false,
c;
for (var i = 0, len = str.length; i < len; i++) {
@@ -14,18 +16,42 @@ module.exports = function (glob) {
case "$":
case "^":
case "+":
- case "?":
case ".":
case "(":
case ")":
case "=":
case "!":
case "|":
+ reStr += "\\" + c;
+ break;
+ case "?":
+ if (extended) {
+ reStr += ".";
+ break;
+ }
+ case "[":
+ case "]":
+ if (extended) {
+ reStr += c;
+ break;
+ }
case "{":
+ if (extended) {
+ inMulti = true;
+ reStr += "(";
+ break;
+ }
case "}":
+ if (extended) {
+ inMulti = false;
+ reStr += ")";
+ break;
+ }
case ",":
- case "[":
- case "]":
+ if (inMulti) {
+ reStr += "|";
+ break;
+ }
reStr += "\\" + c;
break;
case "*":
View
28 test.js
@@ -23,4 +23,32 @@ assert.ok(globToRegexp("*/js/*.js").test("http://example.com/js/jquery.min.js"))
var testStr = "\\/$^+?.()=!|{},[].*"
assert.ok(globToRegexp(testStr).test(testStr));
+
+// EXTENDED MODE
+// - support ?, [], {some, *, alternativ?s}
+
+// ?: Match one character, no more and no less
+assert.ok(globToRegexp("f?o", true).test("foo"));
+assert.equal(false, globToRegexp("f?o", true).test("fooo"));
+assert.equal(false, globToRegexp("f?oo", true).test("foo"));
+
+// []: Match a character range
+assert.ok(globToRegexp("fo[oz]", true).test("foo"));
+assert.ok(globToRegexp("fo[oz]", true).test("foz"));
+
+// {}: Match a choice of different substrings
+assert.ok(globToRegexp("foo{bar,baaz}", true).test("foobaaz"));
+assert.ok(globToRegexp("foo{bar,b*z}", true).test("foobuzz"));
+
+// More complex extended matches
+assert.ok(globToRegexp("http://?o[oz].b*z.com/{*.js,*.html}", true).test("http://foo.baaz.com/jquery.min.js"));
+assert.ok(globToRegexp("http://?o[oz].b*z.com/{*.js,*.html}", true).test("http://moz.buzz.com/index.html"));
+assert.equal(false, globToRegexp("http://?o[oz].b*z.com/{*.js,*.html}", true).test("http://moz.buzz.com/index.htm"));
+assert.equal(false, globToRegexp("http://?o[oz].b*z.com/{*.js,*.html}", true).test("http://moz.bar.com/index.html"));
+assert.equal(false, globToRegexp("http://?o[oz].b*z.com/{*.js,*.html}", true).test("http://flozz.buzz.com/index.html"));
+
+// Remaining special chars should still match themselves
+var testExtStr = "\\/$^+.()=!|,.*"
+assert.ok(globToRegexp(testExtStr).test(testExtStr));
+
console.log("Ok!");

0 comments on commit 84428dc

Please sign in to comment.