aboutsummaryrefslogtreecommitdiff
path: root/node_modules/matcher/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/matcher/index.js')
-rw-r--r--node_modules/matcher/index.js36
1 files changed, 19 insertions, 17 deletions
diff --git a/node_modules/matcher/index.js b/node_modules/matcher/index.js
index 947d8cf4f..33e4e1532 100644
--- a/node_modules/matcher/index.js
+++ b/node_modules/matcher/index.js
@@ -3,34 +3,33 @@ const escapeStringRegexp = require('escape-string-regexp');
const reCache = new Map();
-function makeRe(pattern, shouldNegate) {
- const cacheKey = pattern + shouldNegate;
+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);
}
- let negated = false;
+ const negated = pattern[0] === '!';
- if (pattern[0] === '!') {
- negated = true;
+ if (negated) {
pattern = pattern.slice(1);
}
pattern = escapeStringRegexp(pattern).replace(/\\\*/g, '.*');
- if (negated && shouldNegate) {
- pattern = `(?!${pattern})`;
- }
-
- const re = new RegExp(`^${pattern}$`, 'i');
+ const re = new RegExp(`^${pattern}$`, opts.caseSensitive ? '' : 'i');
re.negated = negated;
reCache.set(cacheKey, re);
return re;
}
-module.exports = (inputs, patterns) => {
+module.exports = (inputs, patterns, options) => {
if (!(Array.isArray(inputs) && Array.isArray(patterns))) {
throw new TypeError(`Expected two arrays, got ${typeof inputs} ${typeof patterns}`);
}
@@ -41,7 +40,7 @@ module.exports = (inputs, patterns) => {
const firstNegated = patterns[0][0] === '!';
- patterns = patterns.map(x => makeRe(x, false));
+ patterns = patterns.map(x => makeRe(x, options));
const ret = [];
@@ -49,10 +48,9 @@ module.exports = (inputs, patterns) => {
// If first pattern is negated we include everything to match user expectation
let matches = firstNegated;
- // TODO: Figure out why tests fail when I use a for-of loop here
- for (let j = 0; j < patterns.length; j++) {
- if (patterns[j].test(input)) {
- matches = !patterns[j].negated;
+ for (const pattern of patterns) {
+ if (pattern.test(input)) {
+ matches = !pattern.negated;
}
}
@@ -64,4 +62,8 @@ module.exports = (inputs, patterns) => {
return ret;
};
-module.exports.isMatch = (input, pattern) => makeRe(pattern, true).test(input);
+module.exports.isMatch = (input, pattern, options) => {
+ const re = makeRe(pattern, options);
+ const matches = re.test(input);
+ return re.negated ? !matches : matches;
+};