pax_global_header00006660000000000000000000000064134504350350014514gustar00rootroot0000000000000052 comment=2abf65a834259c6504ed3b80e85f893f8cd99127 glob-to-regexp-0.4.1/000077500000000000000000000000001345043503500143515ustar00rootroot00000000000000glob-to-regexp-0.4.1/.travis.yml000066400000000000000000000000551345043503500164620ustar00rootroot00000000000000language: node_js node_js: - 0.8 - "0.10"glob-to-regexp-0.4.1/README.md000066400000000000000000000063311345043503500156330ustar00rootroot00000000000000# Glob To Regular Expression [![Build Status](https://travis-ci.org/fitzgen/glob-to-regexp.png?branch=master)](https://travis-ci.org/fitzgen/glob-to-regexp) 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 }`. To obey [globstars `**`](https://github.com/isaacs/node-glob#glob-primer) rules set option `{globstar: true}`. NOTE: This changes the behavior of `*` when `globstar` is `true` as shown below: When `{globstar: true}`: `/foo/**` will match any string that starts with `/foo/` like `/foo/index.htm`, `/foo/bar/baz.txt`, etc. Also, `/foo/**/*.txt` will match any string that starts with `/foo/` and ends with `.txt` like `/foo/bar.txt`, `/foo/bar/baz.txt`, etc. Whereas `/foo/*` (single `*`, not a globstar) will match strings that start with `/foo/` like `/foo/index.htm`, `/foo/baz.txt` but will not match strings that contain a `/` to the right like `/foo/bar/baz.txt`, `/foo/bar/baz/qux.dat`, etc. Set flags on the resulting `RegExp` object by adding the `flags` property to the option object, eg `{ flags: "i" }` for ignoring case. ## Install npm install glob-to-regexp ## Usage ```js var globToRegExp = require('glob-to-regexp'); var re = globToRegExp("p*uck"); re.test("pot luck"); // true re.test("pluck"); // true re.test("puck"); // true re = globToRegExp("*.min.js"); re.test("http://example.com/jquery.min.js"); // true re.test("http://example.com/jquery.min.js.map"); // false re = globToRegExp("*/www/*.js"); 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}", { extended: 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 All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. glob-to-regexp-0.4.1/index.js000066400000000000000000000065521345043503500160260ustar00rootroot00000000000000module.exports = function (glob, opts) { if (typeof glob !== 'string') { throw new TypeError('Expected a string'); } 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; // When globstar is _false_ (default), '/foo/*' is translated a regexp like // '^\/foo\/.*$' which will match any string beginning with '/foo/' // When globstar is _true_, '/foo/*' is translated to regexp like // '^\/foo\/[^/]*$' which will match any string beginning with '/foo/' BUT // which does not have a '/' to the right of it. // E.g. with '/foo/*' these will match: '/foo/bar', '/foo/bar.txt' but // these will not '/foo/bar/baz', '/foo/bar/baz.txt' // Lastely, when globstar is _true_, '/foo/**' is equivelant to '/foo/*' when // globstar is _false_ var globstar = opts ? !!opts.globstar : 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; // RegExp flags (eg "i" ) to pass in to RegExp constructor. var flags = opts && typeof( opts.flags ) === "string" ? opts.flags : ""; 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 "|": 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 "*": // 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 { // 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 += "([^/]*)"; } } break; default: reStr += c; } } // When regexp 'g' flag is specified don't // constrain the regular expression with ^ & $ if (!flags || !~flags.indexOf('g')) { reStr = "^" + reStr + "$"; } return new RegExp(reStr, flags); }; glob-to-regexp-0.4.1/package.json000066400000000000000000000007521345043503500166430ustar00rootroot00000000000000{ "name": "glob-to-regexp", "version": "0.4.1", "description": "Convert globs to regular expressions", "main": "index.js", "scripts": { "test": "node test.js" }, "repository": { "type": "git", "url": "https://github.com/fitzgen/glob-to-regexp.git" }, "keywords": [ "regexp", "glob", "regexps", "regular expressions", "regular expression", "wildcard" ], "author": "Nick Fitzgerald ", "license": "BSD-2-Clause" } glob-to-regexp-0.4.1/test.js000066400000000000000000000251321345043503500156710ustar00rootroot00000000000000var globToRegexp = require("./index.js"); var assert = require("assert"); function assertMatch(glob, str, opts) { //console.log(glob, globToRegexp(glob, opts)); assert.ok(globToRegexp(glob, opts).test(str)); } function assertNotMatch(glob, str, opts) { //console.log(glob, globToRegexp(glob, opts)); assert.equal(false, globToRegexp(glob, opts).test(str)); } function test(globstar) { // Match everything assertMatch("*", "foo"); assertMatch("*", "foo", { flags: 'g' }); // Match the end assertMatch("f*", "foo"); assertMatch("f*", "foo", { flags: 'g' }); // Match the start assertMatch("*o", "foo"); assertMatch("*o", "foo", { flags: 'g' }); // Match the middle assertMatch("f*uck", "firetruck"); assertMatch("f*uck", "firetruck", { flags: 'g' }); // Don't match without Regexp 'g' assertNotMatch("uc", "firetruck"); // Match anywhere with RegExp 'g' assertMatch("uc", "firetruck", { flags: 'g' }); // Match zero characters assertMatch("f*uck", "fuck"); assertMatch("f*uck", "fuck", { flags: 'g' }); // More complex matches assertMatch("*.min.js", "http://example.com/jquery.min.js", {globstar: false}); assertMatch("*.min.*", "http://example.com/jquery.min.js", {globstar: false}); assertMatch("*/js/*.js", "http://example.com/js/jquery.min.js", {globstar: false}); // More complex matches with RegExp 'g' flag (complex regression) assertMatch("*.min.*", "http://example.com/jquery.min.js", { flags: 'g' }); assertMatch("*.min.js", "http://example.com/jquery.min.js", { flags: 'g' }); assertMatch("*/js/*.js", "http://example.com/js/jquery.min.js", { flags: 'g' }); // Test string "\\\\/$^+?.()=!|{},[].*" represents \\/$^+?.()=!|{},[].* // The equivalent regex is: /^\\\/\$\^\+\?\.\(\)\=\!\|\{\}\,\[\]\..*$/ // Both glob and regex match: \/$^+?.()=!|{},[].* var testStr = "\\\\/$^+?.()=!|{},[].*"; var targetStr = "\\/$^+?.()=!|{},[].*"; assertMatch(testStr, targetStr); assertMatch(testStr, targetStr, { flags: 'g' }); // Equivalent matches without/with using RegExp 'g' assertNotMatch(".min.", "http://example.com/jquery.min.js"); assertMatch("*.min.*", "http://example.com/jquery.min.js"); assertMatch(".min.", "http://example.com/jquery.min.js", { flags: 'g' }); assertNotMatch("http:", "http://example.com/jquery.min.js"); assertMatch("http:*", "http://example.com/jquery.min.js"); assertMatch("http:", "http://example.com/jquery.min.js", { flags: 'g' }); assertNotMatch("min.js", "http://example.com/jquery.min.js"); assertMatch("*.min.js", "http://example.com/jquery.min.js"); assertMatch("min.js", "http://example.com/jquery.min.js", { flags: 'g' }); // Match anywhere (globally) using RegExp 'g' assertMatch("min", "http://example.com/jquery.min.js", { flags: 'g' }); assertMatch("/js/", "http://example.com/js/jquery.min.js", { flags: 'g' }); assertNotMatch("/js*jq*.js", "http://example.com/js/jquery.min.js"); assertMatch("/js*jq*.js", "http://example.com/js/jquery.min.js", { flags: 'g' }); // 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 one character with RegExp 'g' assertMatch("f?o", "foo", { extended: true, globstar: globstar, flags: 'g' }); assertMatch("f?o", "fooo", { extended: true, globstar: globstar, flags: 'g' }); assertMatch("f?o?", "fooo", { extended: true, globstar: globstar, flags: 'g' }); assertNotMatch("?fo", "fooo", { extended: true, globstar: globstar, flags: 'g' }); assertNotMatch("f?oo", "foo", { extended: true, globstar: globstar, flags: 'g' }); assertNotMatch("foo?", "foo", { extended: true, globstar: globstar, flags: 'g' }); // []: Match a character range assertMatch("fo[oz]", "foo", { extended: true }); assertMatch("fo[oz]", "foz", { extended: true }); assertNotMatch("fo[oz]", "fog", { extended: true }); // []: Match a character range and RegExp 'g' (regresion) assertMatch("fo[oz]", "foo", { extended: true, globstar: globstar, flags: 'g' }); assertMatch("fo[oz]", "foz", { extended: true, globstar: globstar, flags: 'g' }); assertNotMatch("fo[oz]", "fog", { extended: true, globstar: globstar, flags: 'g' }); // {}: 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 }); // {}: Match a choice of different substrings and RegExp 'g' (regression) assertMatch("foo{bar,baaz}", "foobaaz", { extended: true, globstar: globstar, flags: 'g' }); assertMatch("foo{bar,baaz}", "foobar", { extended: true, globstar: globstar, flags: 'g' }); assertNotMatch("foo{bar,baaz}", "foobuzz", { extended: true, globstar: globstar, flags: 'g' }); assertMatch("foo{bar,b*z}", "foobuzz", { extended: true, globstar: globstar, flags: 'g' }); // 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 }); // More complex extended matches and RegExp 'g' (regresion) assertMatch("http://?o[oz].b*z.com/{*.js,*.html}", "http://foo.baaz.com/jquery.min.js", { extended: true, globstar: globstar, flags: 'g' }); assertMatch("http://?o[oz].b*z.com/{*.js,*.html}", "http://moz.buzz.com/index.html", { extended: true, globstar: globstar, flags: 'g' }); assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}", "http://moz.buzz.com/index.htm", { extended: true, globstar: globstar, flags: 'g' }); assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}", "http://moz.bar.com/index.html", { extended: true, globstar: globstar, flags: 'g' }); assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}", "http://flozz.buzz.com/index.html", { extended: true, globstar: globstar, flags: 'g' }); // globstar assertMatch("http://foo.com/**/{*.js,*.html}", "http://foo.com/bar/jquery.min.js", { extended: true, globstar: globstar, flags: 'g' }); assertMatch("http://foo.com/**/{*.js,*.html}", "http://foo.com/bar/baz/jquery.min.js", { extended: true, globstar: globstar, flags: 'g' }); assertMatch("http://foo.com/**", "http://foo.com/bar/baz/jquery.min.js", { extended: true, globstar: globstar, flags: 'g' }); // Remaining special chars should still match themselves // Test string "\\\\/$^+.()=!|,.*" represents \\/$^+.()=!|,.* // The equivalent regex is: /^\\\/\$\^\+\.\(\)\=\!\|\,\..*$/ // Both glob and regex match: \/$^+.()=!|,.* var testExtStr = "\\\\/$^+.()=!|,.*"; var targetExtStr = "\\/$^+.()=!|,.*"; assertMatch(testExtStr, targetExtStr, { extended: true }); assertMatch(testExtStr, targetExtStr, { extended: true, globstar: globstar, flags: 'g' }); } // regression // globstar false test(false) // globstar true test(true); // globstar specific tests assertMatch("/foo/*", "/foo/bar.txt", {globstar: true }); assertMatch("/foo/**", "/foo/baz.txt", {globstar: true }); assertMatch("/foo/**", "/foo/bar/baz.txt", {globstar: true }); assertMatch("/foo/*/*.txt", "/foo/bar/baz.txt", {globstar: true }); assertMatch("/foo/**/*.txt", "/foo/bar/baz.txt", {globstar: true }); 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 }); assertNotMatch("/foo/*", "/foo/bar/baz.txt", {globstar: true }); assertNotMatch("/foo/*.txt", "/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 }); assertNotMatch("http://foo.com/*", "http://foo.com/bar/baz/jquery.min.js", { extended: true, globstar: true }); assertNotMatch("http://foo.com/*", "http://foo.com/bar/baz/jquery.min.js", { globstar: true }); assertMatch("http://foo.com/*", "http://foo.com/bar/baz/jquery.min.js", { globstar: false }); assertMatch("http://foo.com/**", "http://foo.com/bar/baz/jquery.min.js", { globstar: true }); assertMatch("http://foo.com/*/*/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: true }); assertMatch("http://foo.com/**/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: true }); assertMatch("http://foo.com/*/*/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: false }); assertMatch("http://foo.com/*/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: false }); assertNotMatch("http://foo.com/*/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: true }); console.log("Ok!");