Skip to content

Commit

Permalink
Fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Apr 10, 2019
1 parent e18de05 commit bd27242
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 71 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions examples.js
Original file line number Diff line number Diff line change
@@ -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']
6 changes: 2 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import sep from 'path';
type AnymatchFn = (string:string) => boolean;
type AnymatchPattern = string|RegExp|AnymatchFn;
type AnymatchMatcher = AnymatchPattern|Array<AnymatchPattern>
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;
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnymatchPattern>} AnymatchMatcher
*/
Expand All @@ -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') {
Expand All @@ -31,8 +33,8 @@ const createPattern = (matcher) => {
};

/**
* @param {Array<AnymatchFn>} patterns
* @param {Array<AnymatchFn>} negatedGlobs
* @param {Array<Function>} patterns
* @param {Array<Function>} negatedGlobs
* @param {String|Array} path
* @param {Boolean} returnIndex
*/
Expand Down
57 changes: 0 additions & 57 deletions readme-examples.js

This file was deleted.

0 comments on commit bd27242

Please sign in to comment.