-
Notifications
You must be signed in to change notification settings - Fork 0
/
manymatch.js
53 lines (47 loc) · 1.9 KB
/
manymatch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var Minimatch = require('minimatch').Minimatch,
util = require('util');
/**
* Creates a Manymatch instance that contains a series of regexes produced
* from the given patterns.
* @param {array} patterns An array of string patterns (minimatch patterns).
* Patterns will be processed in order. Patterns starting with
* `!` will only negate the results from the patterns before it,
* so for `[ '!/mydir/1', '/mydir/*']`, the first pattern will effectively
* be ignored and everything in mydir/ will be matched.
*/
var Manymatch = function Manymatch (patterns) {
if(typeof patterns === 'string')
patterns = [patterns];
else if(!util.isArray(patterns)){
throw new TypeError();
}
var i, pattern;
this.minimatches = [];
for(i=0; i<patterns.length; i++){
pattern = patterns[i];
if(pattern.charAt(0) === '!' && this.minimatches.length === 0) {
continue; //ignore pattern because first ones should not start with `!`
}
this.minimatches.push(new Minimatch(pattern));
}
};
/**
* Matches a string against a Manymatch (a series of `minimatch` patterns).
* The matching will happen in sequence. Patterns beginning with `!` are
* negations. Negations will only negate possibilities from previous patterns.
* @param {string} value the string to test against the minimatch patterns
*/
Manymatch.prototype.match = function(value) {
if(typeof value !== 'string')
throw new TypeError();
var isMatch = false;
this.minimatches.forEach(function(minimatch, index, array) {
if(minimatch.negate) {
if(!isMatch) return; // ignore this one because no match was found before this negation
if(!minimatch.match(value)) isMatch = false; // no match means the path is negated
} else
if(minimatch.match(value)) isMatch = true;
});
return isMatch;
};
module.exports = Manymatch;