7 var extend = require('extend-shallow');
8 var unique = require('array-unique');
9 var toRegex = require('to-regex');
15 var compilers = require('./lib/compilers');
16 var parsers = require('./lib/parsers');
17 var Extglob = require('./lib/extglob');
18 var utils = require('./lib/utils');
19 var MAX_LENGTH = 1024 * 64;
22 * Convert the given `extglob` pattern into a regex-compatible string. Returns
23 * an object with the compiled result and the parsed AST.
26 * var extglob = require('extglob');
27 * console.log(extglob('*.!(*a)'));
28 * //=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
30 * @param {String} `pattern`
31 * @param {Object} `options`
36 function extglob(pattern, options) {
37 return extglob.create(pattern, options).output;
41 * Takes an array of strings and an extglob pattern and returns a new
42 * array that contains only the strings that match the pattern.
45 * var extglob = require('extglob');
46 * console.log(extglob.match(['a.a', 'a.b', 'a.c'], '*.!(*a)'));
49 * @param {Array} `list` Array of strings to match
50 * @param {String} `pattern` Extglob pattern
51 * @param {Object} `options`
52 * @return {Array} Returns an array of matches
56 extglob.match = function(list, pattern, options) {
57 if (typeof pattern !== 'string') {
58 throw new TypeError('expected pattern to be a string');
61 list = utils.arrayify(list);
62 var isMatch = extglob.matcher(pattern, options);
63 var len = list.length;
75 // if no options were passed, uniquify results and return
76 if (typeof options === 'undefined') {
77 return unique(matches);
80 if (matches.length === 0) {
81 if (options.failglob === true) {
82 throw new Error('no matches found for "' + pattern + '"');
84 if (options.nonull === true || options.nullglob === true) {
85 return [pattern.split('\\').join('')];
89 return options.nodupes !== false ? unique(matches) : matches;
93 * Returns true if the specified `string` matches the given
97 * var extglob = require('extglob');
99 * console.log(extglob.isMatch('a.a', '*.!(*a)'));
101 * console.log(extglob.isMatch('a.b', '*.!(*a)'));
104 * @param {String} `string` String to match
105 * @param {String} `pattern` Extglob pattern
106 * @param {String} `options`
111 extglob.isMatch = function(str, pattern, options) {
112 if (typeof pattern !== 'string') {
113 throw new TypeError('expected pattern to be a string');
116 if (typeof str !== 'string') {
117 throw new TypeError('expected a string');
120 if (pattern === str) {
124 if (pattern === '' || pattern === ' ' || pattern === '.') {
125 return pattern === str;
128 var isMatch = utils.memoize('isMatch', pattern, options, extglob.matcher);
133 * Returns true if the given `string` contains the given pattern. Similar to `.isMatch` but
134 * the pattern can match any part of the string.
137 * var extglob = require('extglob');
138 * console.log(extglob.contains('aa/bb/cc', '*b'));
140 * console.log(extglob.contains('aa/bb/cc', '*d'));
143 * @param {String} `str` The string to match.
144 * @param {String} `pattern` Glob pattern to use for matching.
145 * @param {Object} `options`
146 * @return {Boolean} Returns true if the patter matches any part of `str`.
150 extglob.contains = function(str, pattern, options) {
151 if (typeof str !== 'string') {
152 throw new TypeError('expected a string');
155 if (pattern === '' || pattern === ' ' || pattern === '.') {
156 return pattern === str;
159 var opts = extend({}, options, {contains: true});
160 opts.strictClose = false;
161 opts.strictOpen = false;
162 return extglob.isMatch(str, pattern, opts);
166 * Takes an extglob pattern and returns a matcher function. The returned
167 * function takes the string to match as its only argument.
170 * var extglob = require('extglob');
171 * var isMatch = extglob.matcher('*.!(*a)');
173 * console.log(isMatch('a.a'));
175 * console.log(isMatch('a.b'));
178 * @param {String} `pattern` Extglob pattern
179 * @param {String} `options`
184 extglob.matcher = function(pattern, options) {
185 if (typeof pattern !== 'string') {
186 throw new TypeError('expected pattern to be a string');
190 var re = extglob.makeRe(pattern, options);
191 return function(str) {
196 return utils.memoize('matcher', pattern, options, matcher);
200 * Convert the given `extglob` pattern into a regex-compatible string. Returns
201 * an object with the compiled result and the parsed AST.
204 * var extglob = require('extglob');
205 * console.log(extglob.create('*.!(*a)').output);
206 * //=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
208 * @param {String} `str`
209 * @param {Object} `options`
214 extglob.create = function(pattern, options) {
215 if (typeof pattern !== 'string') {
216 throw new TypeError('expected pattern to be a string');
220 var ext = new Extglob(options);
221 var ast = ext.parse(pattern, options);
222 return ext.compile(ast, options);
225 return utils.memoize('create', pattern, options, create);
229 * Returns an array of matches captured by `pattern` in `string`, or `null`
230 * if the pattern did not match.
233 * var extglob = require('extglob');
234 * extglob.capture(pattern, string[, options]);
236 * console.log(extglob.capture('test/*.js', 'test/foo.js'));
238 * console.log(extglob.capture('test/*.js', 'foo/bar.css'));
241 * @param {String} `pattern` Glob pattern to use for matching.
242 * @param {String} `string` String to match
243 * @param {Object} `options` See available [options](#options) for changing how matches are performed
244 * @return {Boolean} Returns an array of captures if the string matches the glob pattern, otherwise `null`.
248 extglob.capture = function(pattern, str, options) {
249 var re = extglob.makeRe(pattern, extend({capture: true}, options));
252 return function(string) {
253 var match = re.exec(string);
258 return match.slice(1);
262 var capture = utils.memoize('capture', pattern, options, match);
267 * Create a regular expression from the given `pattern` and `options`.
270 * var extglob = require('extglob');
271 * var re = extglob.makeRe('*.!(*a)');
273 * //=> /^[^\/]*?\.(?![^\/]*?a)[^\/]*?$/
275 * @param {String} `pattern` The pattern to convert to regex.
276 * @param {Object} `options`
281 extglob.makeRe = function(pattern, options) {
282 if (pattern instanceof RegExp) {
286 if (typeof pattern !== 'string') {
287 throw new TypeError('expected pattern to be a string');
290 if (pattern.length > MAX_LENGTH) {
291 throw new Error('expected pattern to be less than ' + MAX_LENGTH + ' characters');
295 var opts = extend({strictErrors: false}, options);
296 if (opts.strictErrors === true) opts.strict = true;
297 var res = extglob.create(pattern, opts);
298 return toRegex(res.output, opts);
301 var regex = utils.memoize('makeRe', pattern, options, makeRe);
302 if (regex.source.length > MAX_LENGTH) {
303 throw new SyntaxError('potentially malicious regex detected');
313 extglob.cache = utils.cache;
314 extglob.clearCache = function() {
315 extglob.cache.__data__ = {};
319 * Expose `Extglob` constructor, parsers and compilers
322 extglob.Extglob = Extglob;
323 extglob.compilers = compilers;
324 extglob.parsers = parsers;
331 module.exports = extglob;