Skip to content
This repository has been archived by the owner on May 19, 2021. It is now read-only.

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

Merged
merged 1 commit into from
Nov 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
34 changes: 30 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -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++) {
Expand All @@ -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 "*":
Expand Down
28 changes: 28 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!");