|
| 1 | +var minimatch = require("minimatch"); |
| 2 | + |
| 3 | +// via https://github.com/sindresorhus/array-union (MIT license) |
| 4 | +function arrayUnion(...args) { |
| 5 | + return [...new Set(args.flat())]; |
| 6 | +} |
| 7 | + |
| 8 | +// via https://github.com/sindresorhus/array-differ/blob/main/index.js (MIT license) |
| 9 | +function arrayDiffer(array, ...values) { |
| 10 | + const rest = new Set(values.flat()); |
| 11 | + return array.filter(element => !rest.has(element)); |
| 12 | +} |
| 13 | + |
| 14 | +// via https://github.com/sindresorhus/arrify/blob/main/index.js (MIT license) |
| 15 | +function arrify(value) { |
| 16 | + if (value === null || value === undefined) { |
| 17 | + return []; |
| 18 | + } |
| 19 | + |
| 20 | + if (Array.isArray(value)) { |
| 21 | + return value; |
| 22 | + } |
| 23 | + |
| 24 | + if (typeof value === 'string') { |
| 25 | + return [value]; |
| 26 | + } |
| 27 | + |
| 28 | + if (typeof value[Symbol.iterator] === 'function') { |
| 29 | + return [...value]; |
| 30 | + } |
| 31 | + |
| 32 | + return [value]; |
| 33 | +} |
| 34 | + |
| 35 | +// via https://www.npmjs.com/package/maximatch (MIT license) |
| 36 | +module.exports = function (list, patterns, options) { |
| 37 | + list = arrify(list); |
| 38 | + |
| 39 | + patterns = arrify(patterns); |
| 40 | + |
| 41 | + if (list.length === 0 || patterns.length === 0) { |
| 42 | + return []; |
| 43 | + } |
| 44 | + |
| 45 | + options = options || {}; |
| 46 | + |
| 47 | + return patterns.reduce(function (ret, pattern) { |
| 48 | + if (typeof pattern === "function") { |
| 49 | + return arrayUnion(ret, list.filter(pattern)); |
| 50 | + } else if (pattern instanceof RegExp) { |
| 51 | + return arrayUnion( |
| 52 | + ret, |
| 53 | + list.filter(function (item) { |
| 54 | + return pattern.test(item); |
| 55 | + }), |
| 56 | + ); |
| 57 | + } else { |
| 58 | + var process = arrayUnion; |
| 59 | + |
| 60 | + if (pattern[0] === "!") { |
| 61 | + pattern = pattern.slice(1); |
| 62 | + |
| 63 | + process = arrayDiffer; |
| 64 | + } |
| 65 | + |
| 66 | + return process(ret, minimatch.match(list, pattern, options)); |
| 67 | + } |
| 68 | + }, []); |
| 69 | +}; |
0 commit comments