UNPKG

8.22 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Module dependencies
5 */
6
7const unique = require('array-unique');
8const toRegex = require('to-regex');
9
10/**
11 * Local dependencies
12 */
13
14const compilers = require('./lib/compilers');
15const parsers = require('./lib/parsers');
16const Extglob = require('./lib/extglob');
17const utils = require('./lib/utils');
18const MAX_LENGTH = 1024 * 64;
19
20/**
21 * Convert the given `extglob` pattern into a regex-compatible string. Returns
22 * an object with the compiled result and the parsed AST.
23 *
24 * ```js
25 * const extglob = require('extglob');
26 * console.log(extglob('*.!(*a)'));
27 * //=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
28 * ```
29 * @param {String} `pattern`
30 * @param {Object} `options`
31 * @return {String}
32 * @api public
33 */
34
35function extglob(pattern, options) {
36 return extglob.create(pattern, options).output;
37}
38
39/**
40 * Takes an array of strings and an extglob pattern and returns a new
41 * array that contains only the strings that match the pattern.
42 *
43 * ```js
44 * const extglob = require('extglob');
45 * console.log(extglob.match(['a.a', 'a.b', 'a.c'], '*.!(*a)'));
46 * //=> ['a.b', 'a.c']
47 * ```
48 * @param {Array} `list` Array of strings to match
49 * @param {String} `pattern` Extglob pattern
50 * @param {Object} `options`
51 * @return {Array} Returns an array of matches
52 * @api public
53 */
54
55extglob.match = function(list, pattern, options) {
56 if (typeof pattern !== 'string') {
57 throw new TypeError('expected pattern to be a string');
58 }
59
60 list = utils.arrayify(list);
61 const isMatch = extglob.matcher(pattern, options);
62 const len = list.length;
63 let idx = -1;
64 const matches = [];
65
66 while (++idx < len) {
67 const ele = list[idx];
68
69 if (isMatch(ele)) {
70 matches.push(ele);
71 }
72 }
73
74 // if no options were passed, uniquify results and return
75 if (typeof options === 'undefined') {
76 return unique(matches);
77 }
78
79 if (matches.length === 0) {
80 if (options.failglob === true) {
81 throw new Error('no matches found for "' + pattern + '"');
82 }
83 if (options.nonull === true || options.nullglob === true) {
84 return [pattern.split('\\').join('')];
85 }
86 }
87
88 return options.nodupes !== false ? unique(matches) : matches;
89};
90
91/**
92 * Returns true if the specified `string` matches the given
93 * extglob `pattern`.
94 *
95 * ```js
96 * const extglob = require('extglob');
97 *
98 * console.log(extglob.isMatch('a.a', '*.!(*a)'));
99 * //=> false
100 * console.log(extglob.isMatch('a.b', '*.!(*a)'));
101 * //=> true
102 * ```
103 * @param {String} `string` String to match
104 * @param {String} `pattern` Extglob pattern
105 * @param {String} `options`
106 * @return {Boolean}
107 * @api public
108 */
109
110extglob.isMatch = function(str, pattern, options) {
111 if (typeof pattern !== 'string') {
112 throw new TypeError('expected pattern to be a string');
113 }
114
115 if (typeof str !== 'string') {
116 throw new TypeError('expected a string');
117 }
118
119 if (pattern === str) {
120 return true;
121 }
122
123 if (pattern === '' || pattern === ' ' || pattern === '.') {
124 return pattern === str;
125 }
126
127 const isMatch = utils.memoize('isMatch', pattern, options, extglob.matcher);
128 return isMatch(str);
129};
130
131/**
132 * Returns true if the given `string` contains the given pattern. Similar to `.isMatch` but
133 * the pattern can match any part of the string.
134 *
135 * ```js
136 * const extglob = require('extglob');
137 * console.log(extglob.contains('aa/bb/cc', '*b'));
138 * //=> true
139 * console.log(extglob.contains('aa/bb/cc', '*d'));
140 * //=> false
141 * ```
142 * @param {String} `str` The string to match.
143 * @param {String} `pattern` Glob pattern to use for matching.
144 * @param {Object} `options`
145 * @return {Boolean} Returns true if the patter matches any part of `str`.
146 * @api public
147 */
148
149extglob.contains = function(str, pattern, options) {
150 if (typeof str !== 'string') {
151 throw new TypeError('expected a string');
152 }
153
154 if (pattern === '' || pattern === ' ' || pattern === '.') {
155 return pattern === str;
156 }
157
158 const opts = Object.assign({}, options, {contains: true});
159 opts.strictClose = false;
160 opts.strictOpen = false;
161 return extglob.isMatch(str, pattern, opts);
162};
163
164/**
165 * Takes an extglob pattern and returns a matcher function. The returned
166 * function takes the string to match as its only argument.
167 *
168 * ```js
169 * const extglob = require('extglob');
170 * const isMatch = extglob.matcher('*.!(*a)');
171 *
172 * console.log(isMatch('a.a'));
173 * //=> false
174 * console.log(isMatch('a.b'));
175 * //=> true
176 * ```
177 * @param {String} `pattern` Extglob pattern
178 * @param {String} `options`
179 * @return {Boolean}
180 * @api public
181 */
182
183extglob.matcher = function(pattern, options) {
184 if (typeof pattern !== 'string') {
185 throw new TypeError('expected pattern to be a string');
186 }
187
188 function matcher() {
189 const re = extglob.makeRe(pattern, options);
190 return function(str) {
191 return re.test(str);
192 };
193 }
194
195 return utils.memoize('matcher', pattern, options, matcher);
196};
197
198/**
199 * Convert the given `extglob` pattern into a regex-compatible string. Returns
200 * an object with the compiled result and the parsed AST.
201 *
202 * ```js
203 * const extglob = require('extglob');
204 * console.log(extglob.create('*.!(*a)').output);
205 * //=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
206 * ```
207 * @param {String} `str`
208 * @param {Object} `options`
209 * @return {String}
210 * @api public
211 */
212
213extglob.create = function(pattern, options) {
214 if (typeof pattern !== 'string') {
215 throw new TypeError('expected pattern to be a string');
216 }
217
218 function create() {
219 const ext = new Extglob(options);
220 const ast = ext.parse(pattern, options);
221 return ext.compile(ast, options);
222 }
223
224 return utils.memoize('create', pattern, options, create);
225};
226
227/**
228 * Returns an array of matches captured by `pattern` in `string`, or `null`
229 * if the pattern did not match.
230 *
231 * ```js
232 * const extglob = require('extglob');
233 * extglob.capture(pattern, string[, options]);
234 *
235 * console.log(extglob.capture('test/*.js', 'test/foo.js'));
236 * //=> ['foo']
237 * console.log(extglob.capture('test/*.js', 'foo/bar.css'));
238 * //=> null
239 * ```
240 * @param {String} `pattern` Glob pattern to use for matching.
241 * @param {String} `string` String to match
242 * @param {Object} `options` See available [options](#options) for changing how matches are performed
243 * @return {Boolean} Returns an array of captures if the string matches the glob pattern, otherwise `null`.
244 * @api public
245 */
246
247extglob.capture = function(pattern, str, options) {
248 const re = extglob.makeRe(pattern, Object.assign({capture: true}, options));
249
250 function match() {
251 return function(string) {
252 const match = re.exec(string);
253 if (!match) {
254 return null;
255 }
256
257 return match.slice(1);
258 };
259 }
260
261 const capture = utils.memoize('capture', pattern, options, match);
262 return capture(str);
263};
264
265/**
266 * Create a regular expression from the given `pattern` and `options`.
267 *
268 * ```js
269 * const extglob = require('extglob');
270 * const re = extglob.makeRe('*.!(*a)');
271 * console.log(re);
272 * //=> /^[^\/]*?\.(?![^\/]*?a)[^\/]*?$/
273 * ```
274 * @param {String} `pattern` The pattern to convert to regex.
275 * @param {Object} `options`
276 * @return {RegExp}
277 * @api public
278 */
279
280extglob.makeRe = function(pattern, options) {
281 if (pattern instanceof RegExp) {
282 return pattern;
283 }
284
285 if (typeof pattern !== 'string') {
286 throw new TypeError('expected pattern to be a string');
287 }
288
289 if (pattern.length > MAX_LENGTH) {
290 throw new Error('expected pattern to be less than ' + MAX_LENGTH + ' characters');
291 }
292
293 function makeRe() {
294 const opts = Object.assign({strictErrors: false}, options);
295 if (opts.strictErrors === true) opts.strict = true;
296 const res = extglob.create(pattern, opts);
297 return toRegex(res.output, opts);
298 }
299
300 const regex = utils.memoize('makeRe', pattern, options, makeRe);
301 if (regex.source.length > MAX_LENGTH) {
302 throw new SyntaxError('potentially malicious regex detected');
303 }
304
305 return regex;
306};
307
308/**
309 * Cache
310 */
311
312extglob.cache = utils.cache;
313extglob.clearCache = function() {
314 extglob.cache.__data__ = {};
315};
316
317/**
318 * Expose `Extglob` constructor, parsers and compilers
319 */
320
321extglob.Extglob = Extglob;
322extglob.compilers = compilers;
323extglob.parsers = parsers;
324
325/**
326 * Expose `extglob`
327 * @type {Function}
328 */
329
330module.exports = extglob;