Skip to content

Commit bd27242

Browse files
committed
Fixes.
1 parent e18de05 commit bd27242

File tree

5 files changed

+63
-71
lines changed

5 files changed

+63
-71
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ boolean result.
3030
```js
3131
const anymatch = require('anymatch');
3232

33-
const matchers = [
34-
'path/to/file.js',
35-
'path/anyjs/**/*.js',
36-
/foo\.js$/,
37-
(string) => string.includes('bar') && string.length > 10
38-
];
33+
const matchers = [ 'path/to/file.js', 'path/anyjs/**/*.js', /foo.js$/, string => string.includes('bar') && string.length > 10 ] ;
3934

4035
anymatch(matchers, 'path/to/file.js'); // true
4136
anymatch(matchers, 'path/anyjs/baz.js'); // true
@@ -53,6 +48,11 @@ anymatch('node_modules', 'node_modules/somelib/index.js'); // false
5348
anymatch('node_modules/**', 'node_modules/somelib/index.js'); // true
5449
anymatch('node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // false
5550
anymatch('**/node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // true
51+
52+
const matcher = anymatch(matchers);
53+
['foo.js', 'bar.js'].filter(matcher); // [ 'foo.js' ]
54+
anymatch master* ❯
55+
5656
```
5757

5858
#### anymatch(matchers)

examples.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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']

index.d.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import sep from 'path';
55
type AnymatchFn = (string:string) => boolean;
66
type AnymatchPattern = string|RegExp|AnymatchFn;
77
type AnymatchMatcher = AnymatchPattern|Array<AnymatchPattern>
8-
declare function anymatch(matchers: AnymatchMatcher, testString: string): boolean;
9-
declare function anymatch(matchers: AnymatchMatcher, testString: string, returnIndex: boolean): number;
10-
declare function anymatch(matchers: AnymatchMatcher): (testString: string) => boolean;
11-
declare function anymatch(matchers: AnymatchMatcher): (testString: string, returnIndex: boolean) => number;
8+
declare function anymatch(matchers: AnymatchMatcher, testString: string, returnIndex?: boolean): boolean;
9+
declare function anymatch(matchers: AnymatchMatcher): (testString: string, returnIndex?: boolean) => number;
1210
export = anymatch;

index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const picomatch = require('picomatch');
44
const normalizePath = require('normalize-path');
55

66
/**
7-
* @typedef {(string:String) => boolean} AnymatchFn
7+
* @typedef {(...args) => boolean} BooleanFn
8+
* @typedef {(string) => boolean} StrBoolFn
9+
* @typedef {BooleanFn|StrBoolFn} AnymatchFn
810
* @typedef {string|RegExp|AnymatchFn} AnymatchPattern
911
* @typedef {AnymatchPattern|Array<AnymatchPattern>} AnymatchMatcher
1012
*/
@@ -14,7 +16,7 @@ const arrify = (item) => Array.isArray(item) ? item : [item];
1416

1517
/**
1618
* @param {AnymatchPattern} matcher
17-
* @returns {Function}
19+
* @returns {AnymatchFn}
1820
*/
1921
const createPattern = (matcher) => {
2022
if (typeof matcher === 'function') {
@@ -31,8 +33,8 @@ const createPattern = (matcher) => {
3133
};
3234

3335
/**
34-
* @param {Array<AnymatchFn>} patterns
35-
* @param {Array<AnymatchFn>} negatedGlobs
36+
* @param {Array<Function>} patterns
37+
* @param {Array<Function>} negatedGlobs
3638
* @param {String|Array} path
3739
* @param {Boolean} returnIndex
3840
*/

readme-examples.js

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)