glob-to-regexp
Advanced tools
Comparing version 0.0.0 to 0.0.1
107
index.js
@@ -1,25 +0,84 @@ | ||
module.exports = function (glob) { | ||
const reStr = glob | ||
// Escape existing regular expression syntax | ||
.replace(/\\/g, "\\\\") | ||
.replace(/\//g, "\\/") | ||
.replace(/\^/g, "\\^") | ||
.replace(/\$/g, "\\$") | ||
.replace(/\+/g, "\\+") | ||
.replace(/\?/g, "\\?") | ||
.replace(/\./g, "\\.") | ||
.replace(/\(/g, "\\(") | ||
.replace(/\)/g, "\\)") | ||
.replace(/\=/g, "\\=") | ||
.replace(/\!/g, "\\!") | ||
.replace(/\|/g, "\\|") | ||
.replace(/\{/g, "\\{") | ||
.replace(/\}/g, "\\}") | ||
.replace(/\,/g, "\\,") | ||
.replace(/\[/g, "\\[") | ||
.replace(/\]/g, "\\]") | ||
.replace(/\-/g, "\\-") | ||
// Turn * into the match everything wildcard | ||
.replace(/\*/g, ".*") | ||
module.exports = function (glob, opts) { | ||
if (glob == null) { | ||
return null; | ||
} | ||
var str = String(glob); | ||
// The regexp we are building, as a string. | ||
var reStr = ""; | ||
// Whether we are matching so called "extended" globs (like bash) and should | ||
// support single character matching, matching ranges of characters, group | ||
// matching, etc. | ||
var extended = opts ? !!opts.extended : false; | ||
// If we are doing extended matching, this boolean is true when we are inside | ||
// a group (eg {*.html,*.js}), and false otherwise. | ||
var inGroup = false; | ||
var c; | ||
for (var i = 0, len = str.length; i < len; i++) { | ||
c = str[i]; | ||
switch (c) { | ||
case "\\": | ||
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) { | ||
inGroup = true; | ||
reStr += "("; | ||
break; | ||
} | ||
case "}": | ||
if (extended) { | ||
inGroup = false; | ||
reStr += ")"; | ||
break; | ||
} | ||
case ",": | ||
if (inGroup) { | ||
reStr += "|"; | ||
break; | ||
} | ||
reStr += "\\" + c; | ||
break; | ||
case "*": | ||
reStr += ".*"; | ||
break; | ||
default: | ||
reStr += c; | ||
} | ||
} | ||
return new RegExp("^" + reStr + "$"); | ||
} | ||
}; |
{ | ||
"name": "glob-to-regexp", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "Convert globs to regular expressions", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,2 +8,6 @@ # Glob To Regular Expression | ||
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 | ||
@@ -15,3 +19,3 @@ | ||
var globToRegExp = require('blob-to-regexp'); | ||
var globToRegExp = require('glob-to-regexp'); | ||
var re = globToRegExp("f*uck"); | ||
@@ -29,2 +33,7 @@ re.test("firetruck"); // true | ||
// Extended globs | ||
re = globToRegExp("*/www/{*.js,*.html}", { extended: true }); | ||
re.test("http://example.com/www/app.js"); // true | ||
re.test("http://example.com/www/index.html"); // true | ||
## License | ||
@@ -31,0 +40,0 @@ |
66
test.js
var globToRegexp = require("./index.js"); | ||
var assert = require("assert"); | ||
function assertMatch(glob, str, opts) { | ||
assert.ok(globToRegexp(glob, opts).test(str)); | ||
} | ||
function assertNotMatch(glob, str, opts) { | ||
assert.equal(false, globToRegexp(glob, opts).test(str)); | ||
} | ||
// Match everything | ||
assert.ok(globToRegexp("*").test("foo")); | ||
assertMatch("*", "foo"); | ||
// Match the end | ||
assert.ok(globToRegexp("f*").test("foo")); | ||
assertMatch("f*", "foo"); | ||
// Match the start | ||
assert.ok(globToRegexp("*o").test("foo")); | ||
assertMatch("*o", "foo"); | ||
// Match the middle | ||
assert.ok(globToRegexp("f*uck").test("firetruck")); | ||
assertMatch("f*uck", "firetruck"); | ||
// Match zero characters | ||
assert.ok(globToRegexp("f*uck").test("fuck")); | ||
assertMatch("f*uck", "fuck"); | ||
// More complex matches | ||
assert.ok(globToRegexp("*.min.js").test("http://example.com/jquery.min.js")); | ||
assert.ok(globToRegexp("*.min.*").test("http://example.com/jquery.min.js")); | ||
assert.ok(globToRegexp("*/js/*.js").test("http://example.com/js/jquery.min.js")); | ||
assertMatch("*.min.js", "http://example.com/jquery.min.js"); | ||
assertMatch("*.min.*", "http://example.com/jquery.min.js") | ||
assertMatch("*/js/*.js", "http://example.com/js/jquery.min.js") | ||
var testStr = "\\/$^+?.()=!|{},[].*" | ||
assertMatch(testStr, testStr); | ||
// Extended mode | ||
// ?: Match one character, no more and no less | ||
assertMatch("f?o", "foo", { extended: true }); | ||
assertNotMatch("f?o", "fooo", { extended: true }); | ||
assertNotMatch("f?oo", "foo", { extended: true }); | ||
// []: Match a character range | ||
assertMatch("fo[oz]", "foo", { extended: true }); | ||
assertMatch("fo[oz]", "foz", { extended: true }); | ||
assertNotMatch("fo[oz]", "fog", { extended: true }); | ||
// {}: Match a choice of different substrings | ||
assertMatch("foo{bar,baaz}", "foobaaz", { extended: true }); | ||
assertMatch("foo{bar,baaz}", "foobar", { extended: true }); | ||
assertNotMatch("foo{bar,baaz}", "foobuzz", { extended: true }); | ||
assertMatch("foo{bar,b*z}", "foobuzz", { extended: true }); | ||
// More complex extended matches | ||
assertMatch("http://?o[oz].b*z.com/{*.js,*.html}", | ||
"http://foo.baaz.com/jquery.min.js", | ||
{ extended: true }); | ||
assertMatch("http://?o[oz].b*z.com/{*.js,*.html}", | ||
"http://moz.buzz.com/index.html", | ||
{ extended: true }); | ||
assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}", | ||
"http://moz.buzz.com/index.htm", | ||
{ extended: true }); | ||
assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}", | ||
"http://moz.bar.com/index.html", | ||
{ extended: true }); | ||
assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}", | ||
"http://flozz.buzz.com/index.html", | ||
{ extended: true }); | ||
// Remaining special chars should still match themselves | ||
var testExtStr = "\\/$^+.()=!|,.*" | ||
assertMatch(testExtStr, testExtStr, { extended: true }); | ||
console.log("Ok!"); |
6845
128
62