| 30 | */ |
| 31 | |
| 32 | const micromatch = (list, patterns, options) => { |
| 33 | patterns = [].concat(patterns); |
| 34 | list = [].concat(list); |
| 35 | |
| 36 | let omit = new Set(); |
| 37 | let keep = new Set(); |
| 38 | let items = new Set(); |
| 39 | let negatives = 0; |
| 40 | |
| 41 | let onResult = state => { |
| 42 | items.add(state.output); |
| 43 | if (options && options.onResult) { |
| 44 | options.onResult(state); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | for (let i = 0; i < patterns.length; i++) { |
| 49 | let isMatch = picomatch(String(patterns[i]), { ...options, onResult }, true); |
| 50 | let negated = isMatch.state.negated || isMatch.state.negatedExtglob; |
| 51 | if (negated) negatives++; |
| 52 | |
| 53 | for (let item of list) { |
| 54 | let matched = isMatch(item, true); |
| 55 | |
| 56 | let match = negated ? !matched.isMatch : matched.isMatch; |
| 57 | if (!match) continue; |
| 58 | |
| 59 | if (negated) { |
| 60 | omit.add(matched.output); |
| 61 | } else { |
| 62 | omit.delete(matched.output); |
| 63 | keep.add(matched.output); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | let result = negatives === patterns.length ? [...items] : [...keep]; |
| 69 | let matches = result.filter(item => !omit.has(item)); |
| 70 | |
| 71 | if (options && matches.length === 0) { |
| 72 | if (options.failglob === true) { |
| 73 | throw new Error(`No matches found for "${patterns.join(', ')}"`); |
| 74 | } |
| 75 | |
| 76 | if (options.nonull === true || options.nullglob === true) { |
| 77 | return options.unescape ? patterns.map(p => p.replace(/\\/g, '')) : patterns; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return matches; |
| 82 | }; |
| 83 | |
| 84 | /** |
| 85 | * Backwards compatibility |