1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | var diff = require('arr-diff');
|
11 | var debug = require('debug')('micromatch');
|
12 | var typeOf = require('kind-of');
|
13 | var omit = require('object.omit');
|
14 | var cache = require('regex-cache');
|
15 | var isGlob = require('is-glob');
|
16 | var expand = require('./lib/expand');
|
17 | var utils = require('./lib/utils');
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function micromatch(files, patterns, opts) {
|
30 | if (!files || !patterns) return [];
|
31 | opts = opts || {};
|
32 |
|
33 | if (typeof opts.cache === 'undefined') {
|
34 | opts.cache = true;
|
35 | }
|
36 |
|
37 | if (!Array.isArray(patterns)) {
|
38 | return match(files, patterns, opts);
|
39 | }
|
40 |
|
41 | var len = patterns.length, i = 0;
|
42 | var omit = [], keep = [];
|
43 |
|
44 | while (len--) {
|
45 | var glob = patterns[i++];
|
46 | if (glob.charCodeAt(0) === 33 ) {
|
47 | omit.push.apply(omit, match(files, glob.slice(1), opts));
|
48 | } else {
|
49 | keep.push.apply(keep, match(files, glob, opts));
|
50 | }
|
51 | }
|
52 | return diff(keep, omit);
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function match(files, pattern, opts) {
|
69 | if (typeOf(files) !== 'string' && !Array.isArray(files)) {
|
70 | throw new Error(msg('match', 'files', 'a string or array'));
|
71 | }
|
72 |
|
73 | files = utils.arrayify(files);
|
74 | opts = opts || {};
|
75 |
|
76 | var negate = opts.negate || false;
|
77 | var orig = pattern;
|
78 |
|
79 | if (typeof pattern === 'string' && opts.nonegate !== true) {
|
80 | negate = pattern.charAt(0) === '!';
|
81 | if (negate) {
|
82 | pattern = pattern.slice(1);
|
83 | }
|
84 | }
|
85 |
|
86 | var _isMatch = matcher(pattern, opts);
|
87 | var len = files.length, i = 0;
|
88 | var res = [];
|
89 |
|
90 | while (i < len) {
|
91 | var file = files[i++];
|
92 | var fp = utils.unixify(file, opts);
|
93 |
|
94 | if (!_isMatch(fp)) { continue; }
|
95 | res.push(fp);
|
96 | }
|
97 |
|
98 | if (res.length === 0) {
|
99 | if (opts.failglob === true) {
|
100 | throw new Error('micromatch.match() found no matches for: "' + orig + '".');
|
101 | }
|
102 |
|
103 | if (opts.nonull || opts.nullglob) {
|
104 | res.push(utils.unescapeGlob(orig));
|
105 | }
|
106 | }
|
107 |
|
108 |
|
109 | if (negate) { res = diff(files, res); }
|
110 |
|
111 |
|
112 | if (opts.ignore && opts.ignore.length) {
|
113 | pattern = opts.ignore;
|
114 | opts = omit(opts, ['ignore']);
|
115 | return diff(res, micromatch(res, pattern, opts));
|
116 | }
|
117 | return res;
|
118 | }
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | function filter(patterns, opts) {
|
137 | if (!Array.isArray(patterns) && typeof patterns !== 'string') {
|
138 | throw new TypeError(msg('filter', 'patterns', 'a string or array'));
|
139 | }
|
140 |
|
141 | patterns = utils.arrayify(patterns);
|
142 | return function (fp) {
|
143 | if (fp == null) return [];
|
144 | var len = patterns.length, i = 0;
|
145 | var res = true;
|
146 |
|
147 | fp = utils.unixify(fp, opts);
|
148 | while (i < len) {
|
149 | var fn = matcher(patterns[i++], opts);
|
150 | if (!fn(fp)) {
|
151 | res = false;
|
152 | break;
|
153 | }
|
154 | }
|
155 | return res;
|
156 | };
|
157 | }
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | function isMatch(fp, pattern, opts) {
|
178 | if (typeof fp !== 'string') {
|
179 | throw new TypeError(msg('isMatch', 'filepath', 'a string'));
|
180 | }
|
181 |
|
182 | fp = utils.unixify(fp, opts);
|
183 | if (typeOf(pattern) === 'object') {
|
184 | return matcher(fp, pattern);
|
185 | }
|
186 | return matcher(pattern, opts)(fp);
|
187 | }
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | function contains(fp, pattern, opts) {
|
195 | if (typeof fp !== 'string') {
|
196 | throw new TypeError(msg('contains', 'pattern', 'a string'));
|
197 | }
|
198 |
|
199 | opts = opts || {};
|
200 | opts.contains = (pattern !== '');
|
201 | fp = utils.unixify(fp, opts);
|
202 |
|
203 | if (opts.contains && !isGlob(pattern)) {
|
204 | return fp.indexOf(pattern) !== -1;
|
205 | }
|
206 | return matcher(pattern, opts)(fp);
|
207 | }
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | function any(fp, patterns, opts) {
|
220 | if (!Array.isArray(patterns) && typeof patterns !== 'string') {
|
221 | throw new TypeError(msg('any', 'patterns', 'a string or array'));
|
222 | }
|
223 |
|
224 | patterns = utils.arrayify(patterns);
|
225 | var len = patterns.length;
|
226 |
|
227 | fp = utils.unixify(fp, opts);
|
228 | while (len--) {
|
229 | var isMatch = matcher(patterns[len], opts);
|
230 | if (isMatch(fp)) {
|
231 | return true;
|
232 | }
|
233 | }
|
234 | return false;
|
235 | }
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | function matchKeys(obj, glob, options) {
|
247 | if (typeOf(obj) !== 'object') {
|
248 | throw new TypeError(msg('matchKeys', 'first argument', 'an object'));
|
249 | }
|
250 |
|
251 | var fn = matcher(glob, options);
|
252 | var res = {};
|
253 |
|
254 | for (var key in obj) {
|
255 | if (obj.hasOwnProperty(key) && fn(key)) {
|
256 | res[key] = obj[key];
|
257 | }
|
258 | }
|
259 | return res;
|
260 | }
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 | function matcher(pattern, opts) {
|
272 |
|
273 | if (typeof pattern === 'function') {
|
274 | return pattern;
|
275 | }
|
276 |
|
277 | if (pattern instanceof RegExp) {
|
278 | return function(fp) {
|
279 | return pattern.test(fp);
|
280 | };
|
281 | }
|
282 |
|
283 |
|
284 | pattern = utils.unixify(pattern, opts);
|
285 |
|
286 |
|
287 | if (!isGlob(pattern)) {
|
288 | return utils.matchPath(pattern, opts);
|
289 | }
|
290 |
|
291 | var re = makeRe(pattern, opts);
|
292 |
|
293 |
|
294 | if (opts && opts.matchBase) {
|
295 | return utils.hasFilename(re, opts);
|
296 | }
|
297 |
|
298 | return function(fp) {
|
299 | fp = utils.unixify(fp, opts);
|
300 | return re.test(fp);
|
301 | };
|
302 | }
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 | function toRegex(glob, options) {
|
317 | if (typeOf(glob) !== 'string') {
|
318 | throw new Error(msg('toRegex', 'glob', 'a string'));
|
319 | }
|
320 |
|
321 |
|
322 | var opts = Object.create(options || {});
|
323 | var flags = opts.flags || '';
|
324 | if (opts.nocase && flags.indexOf('i') === -1) {
|
325 | flags += 'i';
|
326 | }
|
327 |
|
328 | var parsed = expand(glob, opts);
|
329 |
|
330 |
|
331 | opts.negated = opts.negated || parsed.negated;
|
332 | opts.negate = opts.negated;
|
333 | glob = wrapGlob(parsed.pattern, opts);
|
334 | var re;
|
335 |
|
336 | try {
|
337 | re = new RegExp(glob, flags);
|
338 | return re;
|
339 | } catch (err) {
|
340 | var msg = 'micromatch invalid regex: (' + re + ')';
|
341 | if (opts.strict) throw new SyntaxError(msg + err);
|
342 | }
|
343 | return /$^/;
|
344 | }
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 | function wrapGlob(glob, opts) {
|
355 | var prefix = (opts && !opts.contains) ? '^' : '';
|
356 | var after = (opts && !opts.contains) ? '$' : '';
|
357 | glob = ('(?:' + glob + ')' + after);
|
358 | if (opts && opts.negate) {
|
359 | return prefix + ('(?!^' + glob + ').*$');
|
360 | }
|
361 | return prefix + glob;
|
362 | }
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 | function makeRe(glob, opts) {
|
370 | return cache(toRegex, glob, opts);
|
371 | }
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 | function msg(method, what, type) {
|
389 | return 'micromatch.' + method + '(): ' + what + ' should be ' + type + '.';
|
390 | }
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 | micromatch.any = any;
|
397 | micromatch.braces = micromatch.braceExpand = require('braces');
|
398 | micromatch.contains = contains;
|
399 | micromatch.expand = expand;
|
400 | micromatch.filter = filter;
|
401 | micromatch.isMatch = isMatch;
|
402 | micromatch.makeRe = makeRe;
|
403 | micromatch.match = match;
|
404 | micromatch.matcher = matcher;
|
405 | micromatch.matchKeys = matchKeys;
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 | module.exports = micromatch;
|