blob: 33e4e15323f09199f54a76a348793b8ca9d5f0c4 (
plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
'use strict';
const escapeStringRegexp = require('escape-string-regexp');
const reCache = new Map();
function makeRe(pattern, options) {
const opts = Object.assign({
caseSensitive: false
}, options);
const cacheKey = pattern + JSON.stringify(opts);
if (reCache.has(cacheKey)) {
return reCache.get(cacheKey);
}
const negated = pattern[0] === '!';
if (negated) {
pattern = pattern.slice(1);
}
pattern = escapeStringRegexp(pattern).replace(/\\\*/g, '.*');
const re = new RegExp(`^${pattern}$`, opts.caseSensitive ? '' : 'i');
re.negated = negated;
reCache.set(cacheKey, re);
return re;
}
module.exports = (inputs, patterns, options) => {
if (!(Array.isArray(inputs) && Array.isArray(patterns))) {
throw new TypeError(`Expected two arrays, got ${typeof inputs} ${typeof patterns}`);
}
if (patterns.length === 0) {
return inputs;
}
const firstNegated = patterns[0][0] === '!';
patterns = patterns.map(x => makeRe(x, options));
const ret = [];
for (const input of inputs) {
// If first pattern is negated we include everything to match user expectation
let matches = firstNegated;
for (const pattern of patterns) {
if (pattern.test(input)) {
matches = !pattern.negated;
}
}
if (matches) {
ret.push(input);
}
}
return ret;
};
module.exports.isMatch = (input, pattern, options) => {
const re = makeRe(pattern, options);
const matches = re.test(input);
return re.negated ? !matches : matches;
};
|