|
| 1 | +const inspect = require('util').inspect; |
| 2 | +const i = function (val) {return inspect(val, {colors: true})}; |
| 3 | + |
| 4 | +const origAnymatch = require('./'); |
| 5 | +console.log("const anymatch = require('anymatch');\n"); |
| 6 | + |
| 7 | +const matchers = [ |
| 8 | + 'path/to/file.js', |
| 9 | + 'path/anyjs/**/*.js', |
| 10 | + /foo.js$/, |
| 11 | + (string => string.includes('bar') && string.length > 10) |
| 12 | +]; |
| 13 | + |
| 14 | +console.log('const matchers =', |
| 15 | + i(matchers).replace('[Function]', matchers[3].toString() + ''), ';\n'); |
| 16 | + |
| 17 | +const anymatch = (...args) => { |
| 18 | + let arg1 = args[0] === matchers ? `matchers` : i(args[0]); |
| 19 | + let str = `anymatch(${arg1}, ${i(args[1])}`; |
| 20 | + if (args[2]) str += `, ${i(args[2])}`; |
| 21 | + str += `);` |
| 22 | + console.log(`${str} // ${i(origAnymatch(...args))}`) |
| 23 | +}; |
| 24 | + |
| 25 | +anymatch(matchers, 'path/to/file.js'); // true |
| 26 | +anymatch(matchers, 'path/anyjs/baz.js'); // true |
| 27 | +anymatch(matchers, 'path/to/foo.js'); // true |
| 28 | +anymatch(matchers, 'path/to/bar.js'); // true |
| 29 | +anymatch(matchers, 'bar.js'); // false |
| 30 | + |
| 31 | +// returnIndex = true |
| 32 | +anymatch(matchers, 'foo.js', true); // 2 |
| 33 | +anymatch(matchers, 'path/anyjs/foo.js', true); // 1 |
| 34 | + |
| 35 | +// using globs to match directories and their children |
| 36 | +anymatch('node_modules', 'node_modules'); // true |
| 37 | +anymatch('node_modules', 'node_modules/somelib/index.js'); // false |
| 38 | +anymatch('node_modules/**', 'node_modules/somelib/index.js'); // true |
| 39 | +anymatch('node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // false |
| 40 | +anymatch('**/node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // true |
| 41 | + |
| 42 | +const matcher = origAnymatch(matchers); |
| 43 | +matcher('path/to/file.js'); // true |
| 44 | +matcher('path/anyjs/baz.js', true); // 1 |
| 45 | + |
| 46 | +// console.log(i(['foo.js', 'bar.js'].filter(matcher))); // ['foo.js'] |
| 47 | +console.log( '\nconst matcher = anymatch(matchers);' ); |
| 48 | +console.log("['foo.js', 'bar.js'].filter(matcher);", |
| 49 | + " //", i(['foo.js', 'bar.js'].filter(matcher) )); // ['foo.js'] |
0 commit comments