123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- 'use strict';
- const util = require('util');
- const braces = require('braces');
- const picomatch = require('picomatch');
- const utils = require('picomatch/lib/utils');
- const isEmptyString = val => val === '' || val === './';
- const micromatch = (list, patterns, options) => {
- patterns = [].concat(patterns);
- list = [].concat(list);
- let omit = new Set();
- let keep = new Set();
- let items = new Set();
- let negatives = 0;
- let onResult = state => {
- items.add(state.output);
- if (options && options.onResult) {
- options.onResult(state);
- }
- };
- for (let i = 0; i < patterns.length; i++) {
- let isMatch = picomatch(String(patterns[i]), { ...options, onResult }, true);
- let negated = isMatch.state.negated || isMatch.state.negatedExtglob;
- if (negated) negatives++;
- for (let item of list) {
- let matched = isMatch(item, true);
- let match = negated ? !matched.isMatch : matched.isMatch;
- if (!match) continue;
- if (negated) {
- omit.add(matched.output);
- } else {
- omit.delete(matched.output);
- keep.add(matched.output);
- }
- }
- }
- let result = negatives === patterns.length ? [...items] : [...keep];
- let matches = result.filter(item => !omit.has(item));
- if (options && matches.length === 0) {
- if (options.failglob === true) {
- throw new Error(`No matches found for "${patterns.join(', ')}"`);
- }
- if (options.nonull === true || options.nullglob === true) {
- return options.unescape ? patterns.map(p => p.replace(/\\/g, '')) : patterns;
- }
- }
- return matches;
- };
- micromatch.match = micromatch;
- micromatch.matcher = (pattern, options) => picomatch(pattern, options);
- micromatch.isMatch = (str, patterns, options) => picomatch(patterns, options)(str);
- micromatch.any = micromatch.isMatch;
- micromatch.not = (list, patterns, options = {}) => {
- patterns = [].concat(patterns).map(String);
- let result = new Set();
- let items = [];
- let onResult = state => {
- if (options.onResult) options.onResult(state);
- items.push(state.output);
- };
- let matches = new Set(micromatch(list, patterns, { ...options, onResult }));
- for (let item of items) {
- if (!matches.has(item)) {
- result.add(item);
- }
- }
- return [...result];
- };
- micromatch.contains = (str, pattern, options) => {
- if (typeof str !== 'string') {
- throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
- }
- if (Array.isArray(pattern)) {
- return pattern.some(p => micromatch.contains(str, p, options));
- }
- if (typeof pattern === 'string') {
- if (isEmptyString(str) || isEmptyString(pattern)) {
- return false;
- }
- if (str.includes(pattern) || (str.startsWith('./') && str.slice(2).includes(pattern))) {
- return true;
- }
- }
- return micromatch.isMatch(str, pattern, { ...options, contains: true });
- };
- micromatch.matchKeys = (obj, patterns, options) => {
- if (!utils.isObject(obj)) {
- throw new TypeError('Expected the first argument to be an object');
- }
- let keys = micromatch(Object.keys(obj), patterns, options);
- let res = {};
- for (let key of keys) res[key] = obj[key];
- return res;
- };
- micromatch.some = (list, patterns, options) => {
- let items = [].concat(list);
- for (let pattern of [].concat(patterns)) {
- let isMatch = picomatch(String(pattern), options);
- if (items.some(item => isMatch(item))) {
- return true;
- }
- }
- return false;
- };
- micromatch.every = (list, patterns, options) => {
- let items = [].concat(list);
- for (let pattern of [].concat(patterns)) {
- let isMatch = picomatch(String(pattern), options);
- if (!items.every(item => isMatch(item))) {
- return false;
- }
- }
- return true;
- };
- micromatch.all = (str, patterns, options) => {
- if (typeof str !== 'string') {
- throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
- }
- return [].concat(patterns).every(p => picomatch(p, options)(str));
- };
|