diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-08-14 05:01:11 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-08-14 05:02:09 +0200 |
commit | 363723fc84f7b8477592e0105aeb331ec9a017af (patch) | |
tree | 29f92724f34131bac64d6a318dd7e30612e631c7 /node_modules/matcher/index.js | |
parent | 5634e77ad96bfe1818f6b6ee70b7379652e5487f (diff) |
node_modules
Diffstat (limited to 'node_modules/matcher/index.js')
-rw-r--r-- | node_modules/matcher/index.js | 51 |
1 files changed, 23 insertions, 28 deletions
diff --git a/node_modules/matcher/index.js b/node_modules/matcher/index.js index 796e7c8d0..947d8cf4f 100644 --- a/node_modules/matcher/index.js +++ b/node_modules/matcher/index.js @@ -1,15 +1,16 @@ 'use strict'; -var escapeStringRegexp = require('escape-string-regexp'); -var reCache = {}; +const escapeStringRegexp = require('escape-string-regexp'); + +const reCache = new Map(); function makeRe(pattern, shouldNegate) { - var cacheKey = pattern + shouldNegate; + const cacheKey = pattern + shouldNegate; - if (reCache[cacheKey]) { - return reCache[cacheKey]; + if (reCache.has(cacheKey)) { + return reCache.get(cacheKey); } - var negated = false; + let negated = false; if (pattern[0] === '!') { negated = true; @@ -19,54 +20,48 @@ function makeRe(pattern, shouldNegate) { pattern = escapeStringRegexp(pattern).replace(/\\\*/g, '.*'); if (negated && shouldNegate) { - pattern = '(?!' + pattern + ')'; + pattern = `(?!${pattern})`; } - var re = new RegExp('^' + pattern + '$', 'i'); - + const re = new RegExp(`^${pattern}$`, 'i'); re.negated = negated; - - reCache[cacheKey] = re; + reCache.set(cacheKey, re); return re; } -module.exports = function (inputs, patterns) { +module.exports = (inputs, patterns) => { if (!(Array.isArray(inputs) && Array.isArray(patterns))) { - throw new TypeError('Expected two arrays, got ' + typeof inputs + ' ' + typeof patterns); + throw new TypeError(`Expected two arrays, got ${typeof inputs} ${typeof patterns}`); } if (patterns.length === 0) { return inputs; } - var firstNegated = patterns[0][0] === '!'; + const firstNegated = patterns[0][0] === '!'; - patterns = patterns.map(function (x) { - return makeRe(x, false); - }); + patterns = patterns.map(x => makeRe(x, false)); - var ret = []; + const ret = []; - for (var i = 0; i < inputs.length; i++) { - // if first pattern is negated we include - // everything to match user expectation - var matches = firstNegated; + for (const input of inputs) { + // If first pattern is negated we include everything to match user expectation + let matches = firstNegated; - for (var j = 0; j < patterns.length; j++) { - if (patterns[j].test(inputs[i])) { + // 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; } } if (matches) { - ret.push(inputs[i]); + ret.push(input); } } return ret; }; -module.exports.isMatch = function (input, pattern) { - return makeRe(pattern, true).test(input); -}; +module.exports.isMatch = (input, pattern) => makeRe(pattern, true).test(input); |