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 | while (i < len) {
|
148 | var fn = matcher(patterns[i++], opts);
|
149 | if (!fn(fp)) {
|
150 | res = false;
|
151 | break;
|
152 | }
|
153 | }
|
154 | return res;
|
155 | };
|
156 | }
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | function isMatch(fp, pattern, opts) {
|
177 | if (typeof fp !== 'string') {
|
178 | throw new TypeError(msg('isMatch', 'filepath', 'a string'));
|
179 | }
|
180 |
|
181 | if (typeOf(pattern) === 'object') {
|
182 | return matcher(fp, pattern);
|
183 | }
|
184 | return matcher(pattern, opts)(fp);
|
185 | }
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | function contains(fp, pattern, opts) {
|
193 | opts = opts || {};
|
194 | opts.contains = (pattern !== '');
|
195 | if (opts.contains && !isGlob(pattern)) {
|
196 | return fp.indexOf(pattern) !== -1;
|
197 | }
|
198 | return matcher(pattern, opts)(fp);
|
199 | }
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | function any(fp, patterns, opts) {
|
212 | if (!Array.isArray(patterns) && typeof patterns !== 'string') {
|
213 | throw new TypeError(msg('any', 'patterns', 'a string or array'));
|
214 | }
|
215 |
|
216 | patterns = utils.arrayify(patterns);
|
217 | var len = patterns.length;
|
218 |
|
219 | while (len--) {
|
220 | var isMatch = matcher(patterns[len], opts);
|
221 | if (isMatch(fp)) {
|
222 | return true;
|
223 | }
|
224 | }
|
225 | return false;
|
226 | }
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 | function matchKeys(obj, pattern, options) {
|
236 | if (typeOf(obj) !== 'object') {
|
237 | throw new TypeError(msg('matchKeys', 'first argument', 'an object'));
|
238 | }
|
239 |
|
240 | var fn = matcher(pattern, options);
|
241 | var res = {};
|
242 |
|
243 | for (var key in obj) {
|
244 | if (obj.hasOwnProperty(key) && fn(key)) {
|
245 | res[key] = obj[key];
|
246 | }
|
247 | }
|
248 | return res;
|
249 | }
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | function matcher(pattern, opts) {
|
261 |
|
262 | if (typeof pattern === 'function') {
|
263 | return pattern;
|
264 | }
|
265 |
|
266 | if (pattern instanceof RegExp) {
|
267 | return function(fp) {
|
268 | return pattern.test(fp);
|
269 | };
|
270 | }
|
271 |
|
272 | if (!isGlob(pattern)) {
|
273 | return utils.matchPath(pattern, opts);
|
274 | }
|
275 |
|
276 | var re = makeRe(pattern, opts);
|
277 |
|
278 |
|
279 | if (opts && opts.matchBase) {
|
280 | return utils.hasFilename(re, opts);
|
281 | }
|
282 |
|
283 | return function(fp) {
|
284 | return re.test(fp);
|
285 | };
|
286 | }
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 | function toRegex(glob, options) {
|
301 | if (typeOf(glob) !== 'string') {
|
302 | throw new Error(msg('toRegex', 'glob', 'a string'));
|
303 | }
|
304 |
|
305 |
|
306 | var opts = Object.create(options || {});
|
307 | var flags = opts.flags || '';
|
308 | if (opts.nocase && flags.indexOf('i') === -1) {
|
309 | flags += 'i';
|
310 | }
|
311 |
|
312 | var parsed = expand(glob, opts);
|
313 |
|
314 |
|
315 | opts.negated = opts.negated || parsed.negated;
|
316 | opts.negate = opts.negated;
|
317 | glob = wrapGlob(parsed.pattern, opts);
|
318 |
|
319 | try {
|
320 | return new RegExp(glob, flags);
|
321 | } catch (err) {
|
322 | debug('toRegex', err);
|
323 | }
|
324 | return /$^/;
|
325 | }
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 | function makeRe(glob, opts) {
|
333 | return cache(toRegex, glob, opts);
|
334 | }
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 | function wrapGlob(glob, opts) {
|
346 | var prefix = (opts && !opts.contains) ? '^' : '';
|
347 | var after = (opts && !opts.contains) ? '$' : '';
|
348 | glob = ('(?:' + glob + ')' + after);
|
349 | if (opts && opts.negate) {
|
350 | return prefix + ('(?!^' + glob + ').*$');
|
351 | }
|
352 | return prefix + glob;
|
353 | }
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 | function msg(method, what, type) {
|
371 | return 'micromatch.' + method + '(): ' + what + ' should be ' + type + '.';
|
372 | }
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 | micromatch.any = any;
|
379 | micromatch.braces = micromatch.braceExpand = require('braces');
|
380 | micromatch.contains = contains;
|
381 | micromatch.expand = expand;
|
382 | micromatch.filter = filter;
|
383 | micromatch.isMatch = isMatch;
|
384 | micromatch.makeRe = makeRe;
|
385 | micromatch.match = match;
|
386 | micromatch.matcher = matcher;
|
387 | micromatch.matchKeys = matchKeys;
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 | module.exports = micromatch;
|