From bd272425c4115c93473f4f39215e97dec1346bfc Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Wed, 10 Apr 2019 23:37:05 +0200 Subject: [PATCH] Fixes. --- README.md | 12 +++++----- examples.js | 49 +++++++++++++++++++++++++++++++++++++++ index.d.ts | 6 ++--- index.js | 10 ++++---- readme-examples.js | 57 ---------------------------------------------- 5 files changed, 63 insertions(+), 71 deletions(-) create mode 100644 examples.js delete mode 100644 readme-examples.js diff --git a/README.md b/README.md index 9e5e634..5da624b 100644 --- a/README.md +++ b/README.md @@ -30,12 +30,7 @@ boolean result. ```js const anymatch = require('anymatch'); -const matchers = [ - 'path/to/file.js', - 'path/anyjs/**/*.js', - /foo\.js$/, - (string) => string.includes('bar') && string.length > 10 -]; +const matchers = [ 'path/to/file.js', 'path/anyjs/**/*.js', /foo.js$/, string => string.includes('bar') && string.length > 10 ] ; anymatch(matchers, 'path/to/file.js'); // true anymatch(matchers, 'path/anyjs/baz.js'); // true @@ -53,6 +48,11 @@ anymatch('node_modules', 'node_modules/somelib/index.js'); // false anymatch('node_modules/**', 'node_modules/somelib/index.js'); // true anymatch('node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // false anymatch('**/node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // true + +const matcher = anymatch(matchers); +['foo.js', 'bar.js'].filter(matcher); // [ 'foo.js' ] +anymatch master* ❯ + ``` #### anymatch(matchers) diff --git a/examples.js b/examples.js new file mode 100644 index 0000000..a1c091d --- /dev/null +++ b/examples.js @@ -0,0 +1,49 @@ +const inspect = require('util').inspect; +const i = function (val) {return inspect(val, {colors: true})}; + +const origAnymatch = require('./'); +console.log("const anymatch = require('anymatch');\n"); + +const matchers = [ + 'path/to/file.js', + 'path/anyjs/**/*.js', + /foo.js$/, + (string => string.includes('bar') && string.length > 10) +]; + +console.log('const matchers =', + i(matchers).replace('[Function]', matchers[3].toString() + ''), ';\n'); + +const anymatch = (...args) => { + let arg1 = args[0] === matchers ? `matchers` : i(args[0]); + let str = `anymatch(${arg1}, ${i(args[1])}`; + if (args[2]) str += `, ${i(args[2])}`; + str += `);` + console.log(`${str} // ${i(origAnymatch(...args))}`) +}; + +anymatch(matchers, 'path/to/file.js'); // true +anymatch(matchers, 'path/anyjs/baz.js'); // true +anymatch(matchers, 'path/to/foo.js'); // true +anymatch(matchers, 'path/to/bar.js'); // true +anymatch(matchers, 'bar.js'); // false + +// returnIndex = true +anymatch(matchers, 'foo.js', true); // 2 +anymatch(matchers, 'path/anyjs/foo.js', true); // 1 + +// using globs to match directories and their children +anymatch('node_modules', 'node_modules'); // true +anymatch('node_modules', 'node_modules/somelib/index.js'); // false +anymatch('node_modules/**', 'node_modules/somelib/index.js'); // true +anymatch('node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // false +anymatch('**/node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // true + +const matcher = origAnymatch(matchers); +matcher('path/to/file.js'); // true +matcher('path/anyjs/baz.js', true); // 1 + +// console.log(i(['foo.js', 'bar.js'].filter(matcher))); // ['foo.js'] +console.log( '\nconst matcher = anymatch(matchers);' ); +console.log("['foo.js', 'bar.js'].filter(matcher);", + " //", i(['foo.js', 'bar.js'].filter(matcher) )); // ['foo.js'] diff --git a/index.d.ts b/index.d.ts index 8634d9f..56491fa 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,8 +5,6 @@ import sep from 'path'; type AnymatchFn = (string:string) => boolean; type AnymatchPattern = string|RegExp|AnymatchFn; type AnymatchMatcher = AnymatchPattern|Array -declare function anymatch(matchers: AnymatchMatcher, testString: string): boolean; -declare function anymatch(matchers: AnymatchMatcher, testString: string, returnIndex: boolean): number; -declare function anymatch(matchers: AnymatchMatcher): (testString: string) => boolean; -declare function anymatch(matchers: AnymatchMatcher): (testString: string, returnIndex: boolean) => number; +declare function anymatch(matchers: AnymatchMatcher, testString: string, returnIndex?: boolean): boolean; +declare function anymatch(matchers: AnymatchMatcher): (testString: string, returnIndex?: boolean) => number; export = anymatch; \ No newline at end of file diff --git a/index.js b/index.js index bb60597..204f42a 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,9 @@ const picomatch = require('picomatch'); const normalizePath = require('normalize-path'); /** - * @typedef {(string:String) => boolean} AnymatchFn + * @typedef {(...args) => boolean} BooleanFn + * @typedef {(string) => boolean} StrBoolFn + * @typedef {BooleanFn|StrBoolFn} AnymatchFn * @typedef {string|RegExp|AnymatchFn} AnymatchPattern * @typedef {AnymatchPattern|Array} AnymatchMatcher */ @@ -14,7 +16,7 @@ const arrify = (item) => Array.isArray(item) ? item : [item]; /** * @param {AnymatchPattern} matcher - * @returns {Function} + * @returns {AnymatchFn} */ const createPattern = (matcher) => { if (typeof matcher === 'function') { @@ -31,8 +33,8 @@ const createPattern = (matcher) => { }; /** - * @param {Array} patterns - * @param {Array} negatedGlobs + * @param {Array} patterns + * @param {Array} negatedGlobs * @param {String|Array} path * @param {Boolean} returnIndex */ diff --git a/readme-examples.js b/readme-examples.js deleted file mode 100644 index 763220e..0000000 --- a/readme-examples.js +++ /dev/null @@ -1,57 +0,0 @@ -var inspect = require('util').inspect; -var i = function (val) {return inspect(val, {colors: true})}; - - -var anymatch = require('./'); -console.log("var anymatch = require('anymatch');\n"); - -var matchers = [ - 'path/to/file.js', - 'path/anyjs/**/*.js', - /foo.js$/, - function (string) { - return string.indexOf('bar') !== -1 && string.length > 10; - } -]; - -console.log('var matchers =', - i(matchers).replace('[Function]', matchers[3].toString() + '\n'), ';\n'); - -console.log("anymatch(matchers, 'path/to/file.js');", - " =>", i(anymatch(matchers, 'path/to/file.js') )); // true -console.log("anymatch(matchers, 'path/anyjs/baz.js');", - " =>", i(anymatch(matchers, 'path/anyjs/baz.js') )); // true -console.log("anymatch(matchers, 'path/to/foo.js');", - " =>", i(anymatch(matchers, 'path/to/foo.js') )); // true -console.log("anymatch(matchers, 'path/to/bar.js');", - " =>", i(anymatch(matchers, 'path/to/bar.js') )); // true -console.log("anymatch(matchers, 'bar.js');", - " =>", i(anymatch(matchers, 'bar.js') )); // false - -// returnIndex = true -console.log( '\n// returnIndex = true' ); -console.log("anymatch(matchers, 'foo.js', true);", - " =>", i(anymatch(matchers, 'foo.js', true) )); // 2 -console.log("anymatch(matchers, 'path/anyjs/foo.js', true);", - " =>", i(anymatch(matchers, 'path/anyjs/foo.js', true) )); // 1 - -// skip matchers -console.log( '\n// skip matchers' ); -console.log("anymatch(matchers, 'path/to/file.js', false, 1);", - " =>", i(anymatch(matchers, 'path/to/file.js', false, 1) )); // false -console.log("anymatch(matchers, 'path/anyjs/foo.js', true, 2, 3);", - " =>", i(anymatch(matchers, 'path/anyjs/foo.js', true, 2, 3) )); // 2 -console.log("anymatch(matchers, 'path/to/bar.js', true, 0, 3);", - " =>", i(anymatch(matchers, 'path/to/bar.js', true, 0, 3) )); // -1 - - -var matcher = anymatch(matchers); -console.log( '\nvar matcher = anymatch(matchers);' ); -console.log("matcher('path/to/file.js');", - " =>", i(matcher('path/to/file.js') )); // true -console.log("matcher('path/anyjs/baz.js', true);", - " =>", i(matcher('path/anyjs/baz.js', true) )); // 1 -console.log("matcher('path/anyjs/baz.js', true, 2);", - " =>", i(matcher('path/anyjs/baz.js', true, 2) )); // -1 -console.log("['foo.js', 'bar.js'].filter(matcher);", - " =>", i(['foo.js', 'bar.js'].filter(matcher) )); // ['foo.js']