2017-05-28 00:38:50 +02:00
|
|
|
'use strict';
|
2017-08-14 05:01:11 +02:00
|
|
|
const escapeStringRegexp = require('escape-string-regexp');
|
|
|
|
|
|
|
|
const reCache = new Map();
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
function makeRe(pattern, options) {
|
|
|
|
const opts = Object.assign({
|
|
|
|
caseSensitive: false
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
const cacheKey = pattern + JSON.stringify(opts);
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
if (reCache.has(cacheKey)) {
|
|
|
|
return reCache.get(cacheKey);
|
2017-05-28 00:38:50 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
const negated = pattern[0] === '!';
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
if (negated) {
|
2017-05-28 00:38:50 +02:00
|
|
|
pattern = pattern.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern = escapeStringRegexp(pattern).replace(/\\\*/g, '.*');
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
const re = new RegExp(`^${pattern}$`, opts.caseSensitive ? '' : 'i');
|
2017-05-28 00:38:50 +02:00
|
|
|
re.negated = negated;
|
2017-08-14 05:01:11 +02:00
|
|
|
reCache.set(cacheKey, re);
|
2017-05-28 00:38:50 +02:00
|
|
|
|
|
|
|
return re;
|
|
|
|
}
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
module.exports = (inputs, patterns, options) => {
|
2017-05-28 00:38:50 +02:00
|
|
|
if (!(Array.isArray(inputs) && Array.isArray(patterns))) {
|
2017-08-14 05:01:11 +02:00
|
|
|
throw new TypeError(`Expected two arrays, got ${typeof inputs} ${typeof patterns}`);
|
2017-05-28 00:38:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (patterns.length === 0) {
|
|
|
|
return inputs;
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
const firstNegated = patterns[0][0] === '!';
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
patterns = patterns.map(x => makeRe(x, options));
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
const ret = [];
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
for (const input of inputs) {
|
|
|
|
// If first pattern is negated we include everything to match user expectation
|
|
|
|
let matches = firstNegated;
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
for (const pattern of patterns) {
|
|
|
|
if (pattern.test(input)) {
|
|
|
|
matches = !pattern.negated;
|
2017-05-28 00:38:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matches) {
|
2017-08-14 05:01:11 +02:00
|
|
|
ret.push(input);
|
2017-05-28 00:38:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
module.exports.isMatch = (input, pattern, options) => {
|
|
|
|
const re = makeRe(pattern, options);
|
|
|
|
const matches = re.test(input);
|
|
|
|
return re.negated ? !matches : matches;
|
|
|
|
};
|