aboutsummaryrefslogtreecommitdiff
path: root/node_modules/extglob/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/extglob/index.js')
-rw-r--r--node_modules/extglob/index.js389
1 files changed, 271 insertions, 118 deletions
diff --git a/node_modules/extglob/index.js b/node_modules/extglob/index.js
index 2e774d4aa..116e6d5cb 100644
--- a/node_modules/extglob/index.js
+++ b/node_modules/extglob/index.js
@@ -1,178 +1,331 @@
-/*!
- * extglob <https://github.com/jonschlinkert/extglob>
- *
- * Copyright (c) 2015, Jon Schlinkert.
- * Licensed under the MIT License.
- */
-
'use strict';
/**
* Module dependencies
*/
-var isExtglob = require('is-extglob');
-var re, cache = {};
+var extend = require('extend-shallow');
+var unique = require('array-unique');
+var toRegex = require('to-regex');
/**
- * Expose `extglob`
+ * Local dependencies
*/
-module.exports = extglob;
+var compilers = require('./lib/compilers');
+var parsers = require('./lib/parsers');
+var Extglob = require('./lib/extglob');
+var utils = require('./lib/utils');
+var MAX_LENGTH = 1024 * 64;
/**
- * Convert the given extglob `string` to a regex-compatible
- * string.
+ * Convert the given `extglob` pattern into a regex-compatible string. Returns
+ * an object with the compiled result and the parsed AST.
*
* ```js
* var extglob = require('extglob');
- * extglob('!(a?(b))');
- * //=> '(?!a(?:b)?)[^/]*?'
+ * console.log(extglob('*.!(*a)'));
+ * //=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
* ```
- *
- * @param {String} `str` The string to convert.
+ * @param {String} `pattern`
* @param {Object} `options`
- * @option {Boolean} [options] `esc` If `false` special characters will not be escaped. Defaults to `true`.
- * @option {Boolean} [options] `regex` If `true` a regular expression is returned instead of a string.
* @return {String}
* @api public
*/
+function extglob(pattern, options) {
+ return extglob.create(pattern, options).output;
+}
+
+/**
+ * Takes an array of strings and an extglob pattern and returns a new
+ * array that contains only the strings that match the pattern.
+ *
+ * ```js
+ * var extglob = require('extglob');
+ * console.log(extglob.match(['a.a', 'a.b', 'a.c'], '*.!(*a)'));
+ * //=> ['a.b', 'a.c']
+ * ```
+ * @param {Array} `list` Array of strings to match
+ * @param {String} `pattern` Extglob pattern
+ * @param {Object} `options`
+ * @return {Array} Returns an array of matches
+ * @api public
+ */
+
+extglob.match = function(list, pattern, options) {
+ if (typeof pattern !== 'string') {
+ throw new TypeError('expected pattern to be a string');
+ }
-function extglob(str, opts) {
- opts = opts || {};
- var o = {}, i = 0;
+ list = utils.arrayify(list);
+ var isMatch = extglob.matcher(pattern, options);
+ var len = list.length;
+ var idx = -1;
+ var matches = [];
- // fix common character reversals
- // '*!(.js)' => '*.!(js)'
- str = str.replace(/!\(([^\w*()])/g, '$1!(');
+ while (++idx < len) {
+ var ele = list[idx];
- // support file extension negation
- str = str.replace(/([*\/])\.!\([*]\)/g, function (m, ch) {
- if (ch === '/') {
- return escape('\\/[^.]+');
+ if (isMatch(ele)) {
+ matches.push(ele);
}
- return escape('[^.]+');
- });
-
- // create a unique key for caching by
- // combining the string and options
- var key = str
- + String(!!opts.regex)
- + String(!!opts.contains)
- + String(!!opts.escape);
+ }
- if (cache.hasOwnProperty(key)) {
- return cache[key];
+ // if no options were passed, uniquify results and return
+ if (typeof options === 'undefined') {
+ return unique(matches);
}
- if (!(re instanceof RegExp)) {
- re = regex();
+ if (matches.length === 0) {
+ if (options.failglob === true) {
+ throw new Error('no matches found for "' + pattern + '"');
+ }
+ if (options.nonull === true || options.nullglob === true) {
+ return [pattern.split('\\').join('')];
+ }
}
- opts.negate = false;
- var m;
+ return options.nodupes !== false ? unique(matches) : matches;
+};
- while (m = re.exec(str)) {
- var prefix = m[1];
- var inner = m[3];
- if (prefix === '!') {
- opts.negate = true;
- }
+/**
+ * Returns true if the specified `string` matches the given
+ * extglob `pattern`.
+ *
+ * ```js
+ * var extglob = require('extglob');
+ *
+ * console.log(extglob.isMatch('a.a', '*.!(*a)'));
+ * //=> false
+ * console.log(extglob.isMatch('a.b', '*.!(*a)'));
+ * //=> true
+ * ```
+ * @param {String} `string` String to match
+ * @param {String} `pattern` Extglob pattern
+ * @param {String} `options`
+ * @return {Boolean}
+ * @api public
+ */
- var id = '__EXTGLOB_' + (i++) + '__';
- // use the prefix of the _last_ (outtermost) pattern
- o[id] = wrap(inner, prefix, opts.escape);
- str = str.split(m[0]).join(id);
+extglob.isMatch = function(str, pattern, options) {
+ if (typeof pattern !== 'string') {
+ throw new TypeError('expected pattern to be a string');
}
- var keys = Object.keys(o);
- var len = keys.length;
+ if (typeof str !== 'string') {
+ throw new TypeError('expected a string');
+ }
- // we have to loop again to allow us to convert
- // patterns in reverse order (starting with the
- // innermost/last pattern first)
- while (len--) {
- var prop = keys[len];
- str = str.split(prop).join(o[prop]);
+ if (pattern === str) {
+ return true;
}
- var result = opts.regex
- ? toRegex(str, opts.contains, opts.negate)
- : str;
+ if (pattern === '' || pattern === ' ' || pattern === '.') {
+ return pattern === str;
+ }
- result = result.split('.').join('\\.');
+ var isMatch = utils.memoize('isMatch', pattern, options, extglob.matcher);
+ return isMatch(str);
+};
- // cache the result and return it
- return (cache[key] = result);
-}
+/**
+ * Returns true if the given `string` contains the given pattern. Similar to `.isMatch` but
+ * the pattern can match any part of the string.
+ *
+ * ```js
+ * var extglob = require('extglob');
+ * console.log(extglob.contains('aa/bb/cc', '*b'));
+ * //=> true
+ * console.log(extglob.contains('aa/bb/cc', '*d'));
+ * //=> false
+ * ```
+ * @param {String} `str` The string to match.
+ * @param {String} `pattern` Glob pattern to use for matching.
+ * @param {Object} `options`
+ * @return {Boolean} Returns true if the patter matches any part of `str`.
+ * @api public
+ */
+
+extglob.contains = function(str, pattern, options) {
+ if (typeof str !== 'string') {
+ throw new TypeError('expected a string');
+ }
+
+ if (pattern === '' || pattern === ' ' || pattern === '.') {
+ return pattern === str;
+ }
+
+ var opts = extend({}, options, {contains: true});
+ opts.strictClose = false;
+ opts.strictOpen = false;
+ return extglob.isMatch(str, pattern, opts);
+};
/**
- * Convert `string` to a regex string.
+ * Takes an extglob pattern and returns a matcher function. The returned
+ * function takes the string to match as its only argument.
*
- * @param {String} `str`
- * @param {String} `prefix` Character that determines how to wrap the string.
- * @param {Boolean} `esc` If `false` special characters will not be escaped. Defaults to `true`.
- * @return {String}
+ * ```js
+ * var extglob = require('extglob');
+ * var isMatch = extglob.matcher('*.!(*a)');
+ *
+ * console.log(isMatch('a.a'));
+ * //=> false
+ * console.log(isMatch('a.b'));
+ * //=> true
+ * ```
+ * @param {String} `pattern` Extglob pattern
+ * @param {String} `options`
+ * @return {Boolean}
+ * @api public
*/
-function wrap(inner, prefix, esc) {
- if (esc) inner = escape(inner);
-
- switch (prefix) {
- case '!':
- return '(?!' + inner + ')[^/]' + (esc ? '%%%~' : '*?');
- case '@':
- return '(?:' + inner + ')';
- case '+':
- return '(?:' + inner + ')+';
- case '*':
- return '(?:' + inner + ')' + (esc ? '%%' : '*')
- case '?':
- return '(?:' + inner + '|)';
- default:
- return inner;
+extglob.matcher = function(pattern, options) {
+ if (typeof pattern !== 'string') {
+ throw new TypeError('expected pattern to be a string');
}
-}
-function escape(str) {
- str = str.split('*').join('[^/]%%%~');
- str = str.split('.').join('\\.');
- return str;
-}
+ function matcher() {
+ var re = extglob.makeRe(pattern, options);
+ return function(str) {
+ return re.test(str);
+ };
+ }
+
+ return utils.memoize('matcher', pattern, options, matcher);
+};
/**
- * extglob regex.
+ * Convert the given `extglob` pattern into a regex-compatible string. Returns
+ * an object with the compiled result and the parsed AST.
+ *
+ * ```js
+ * var extglob = require('extglob');
+ * console.log(extglob.create('*.!(*a)').output);
+ * //=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
+ * ```
+ * @param {String} `str`
+ * @param {Object} `options`
+ * @return {String}
+ * @api public
*/
-function regex() {
- return /(\\?[@?!+*$]\\?)(\(([^()]*?)\))/;
-}
+extglob.create = function(pattern, options) {
+ if (typeof pattern !== 'string') {
+ throw new TypeError('expected pattern to be a string');
+ }
+
+ function create() {
+ var ext = new Extglob(options);
+ var ast = ext.parse(pattern, options);
+ return ext.compile(ast, options);
+ }
+
+ return utils.memoize('create', pattern, options, create);
+};
/**
- * Negation regex
+ * Returns an array of matches captured by `pattern` in `string`, or `null`
+ * if the pattern did not match.
+ *
+ * ```js
+ * var extglob = require('extglob');
+ * extglob.capture(pattern, string[, options]);
+ *
+ * console.log(extglob.capture('test/*.js', 'test/foo.js'));
+ * //=> ['foo']
+ * console.log(extglob.capture('test/*.js', 'foo/bar.css'));
+ * //=> null
+ * ```
+ * @param {String} `pattern` Glob pattern to use for matching.
+ * @param {String} `string` String to match
+ * @param {Object} `options` See available [options](#options) for changing how matches are performed
+ * @return {Boolean} Returns an array of captures if the string matches the glob pattern, otherwise `null`.
+ * @api public
*/
-function negate(str) {
- return '(?!^' + str + ').*$';
-}
+extglob.capture = function(pattern, str, options) {
+ var re = extglob.makeRe(pattern, extend({capture: true}, options));
+
+ function match() {
+ return function(string) {
+ var match = re.exec(string);
+ if (!match) {
+ return null;
+ }
+
+ return match.slice(1);
+ };
+ }
+
+ var capture = utils.memoize('capture', pattern, options, match);
+ return capture(str);
+};
/**
- * Create the regex to do the matching. If
- * the leading character in the `pattern` is `!`
- * a negation regex is returned.
+ * Create a regular expression from the given `pattern` and `options`.
*
- * @param {String} `pattern`
- * @param {Boolean} `contains` Allow loose matching.
- * @param {Boolean} `isNegated` True if the pattern is a negation pattern.
+ * ```js
+ * var extglob = require('extglob');
+ * var re = extglob.makeRe('*.!(*a)');
+ * console.log(re);
+ * //=> /^[^\/]*?\.(?![^\/]*?a)[^\/]*?$/
+ * ```
+ * @param {String} `pattern` The pattern to convert to regex.
+ * @param {Object} `options`
+ * @return {RegExp}
+ * @api public
*/
-function toRegex(pattern, contains, isNegated) {
- var prefix = contains ? '^' : '';
- var after = contains ? '$' : '';
- pattern = ('(?:' + pattern + ')' + after);
- if (isNegated) {
- pattern = prefix + negate(pattern);
+extglob.makeRe = function(pattern, options) {
+ if (pattern instanceof RegExp) {
+ return pattern;
}
- return new RegExp(prefix + pattern);
-}
+
+ if (typeof pattern !== 'string') {
+ throw new TypeError('expected pattern to be a string');
+ }
+
+ if (pattern.length > MAX_LENGTH) {
+ throw new Error('expected pattern to be less than ' + MAX_LENGTH + ' characters');
+ }
+
+ function makeRe() {
+ var opts = extend({strictErrors: false}, options);
+ if (opts.strictErrors === true) opts.strict = true;
+ var res = extglob.create(pattern, opts);
+ return toRegex(res.output, opts);
+ }
+
+ var regex = utils.memoize('makeRe', pattern, options, makeRe);
+ if (regex.source.length > MAX_LENGTH) {
+ throw new SyntaxError('potentially malicious regex detected');
+ }
+
+ return regex;
+};
+
+/**
+ * Cache
+ */
+
+extglob.cache = utils.cache;
+extglob.clearCache = function() {
+ extglob.cache.__data__ = {};
+};
+
+/**
+ * Expose `Extglob` constructor, parsers and compilers
+ */
+
+extglob.Extglob = Extglob;
+extglob.compilers = compilers;
+extglob.parsers = parsers;
+
+/**
+ * Expose `extglob`
+ * @type {Function}
+ */
+
+module.exports = extglob;