15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
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
70
71
72
73
74
75
76
77
'use strict';
const escapeStringRegexp = require('escape-string-regexp');
 
const regexpCache = new Map();
 
function makeRegexp(pattern, options) {
    options = {
        caseSensitive: false,
        ...options
    };
 
    const cacheKey = pattern + JSON.stringify(options);
 
    if (regexpCache.has(cacheKey)) {
        return regexpCache.get(cacheKey);
    }
 
    const negated = pattern[0] === '!';
 
    if (negated) {
        pattern = pattern.slice(1);
    }
 
    pattern = escapeStringRegexp(pattern).replace(/\\\*/g, '[\\s\\S]*');
 
    const regexp = new RegExp(`^${pattern}$`, options.caseSensitive ? '' : 'i');
    regexp.negated = negated;
    regexpCache.set(cacheKey, regexp);
 
    return regexp;
}
 
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 isFirstPatternNegated = patterns[0][0] === '!';
 
    patterns = patterns.map(pattern => makeRegexp(pattern, options));
 
    const result = [];
 
    for (const input of inputs) {
        // If first pattern is negated we include everything to match user expectation.
        let matches = isFirstPatternNegated;
 
        for (const pattern of patterns) {
            if (pattern.test(input)) {
                matches = !pattern.negated;
            }
        }
 
        if (matches) {
            result.push(input);
        }
    }
 
    return result;
};
 
module.exports.isMatch = (input, pattern, options) => {
    const inputArray = Array.isArray(input) ? input : [input];
    const patternArray = Array.isArray(pattern) ? pattern : [pattern];
 
    return inputArray.some(input => {
        return patternArray.every(pattern => {
            const regexp = makeRegexp(pattern, options);
            const matches = regexp.test(input);
            return regexp.negated ? !matches : matches;
        });
    });
};