anymatch
Advanced tools
Comparing version 3.0.3 to 3.1.0
48
index.js
@@ -14,2 +14,3 @@ 'use strict'; | ||
const BANG = '!'; | ||
const DEFAULT_OPTIONS = {returnIndex: false}; | ||
const arrify = (item) => Array.isArray(item) ? item : [item]; | ||
@@ -19,5 +20,6 @@ | ||
* @param {AnymatchPattern} matcher | ||
* @param {object} options | ||
* @returns {AnymatchFn} | ||
*/ | ||
const createPattern = (matcher) => { | ||
const createPattern = (matcher, options) => { | ||
if (typeof matcher === 'function') { | ||
@@ -27,3 +29,3 @@ return matcher; | ||
if (typeof matcher === 'string') { | ||
const glob = picomatch(matcher); | ||
const glob = picomatch(matcher, options); | ||
return (string) => matcher === string || glob(string); | ||
@@ -39,20 +41,27 @@ } | ||
* @param {Array<Function>} patterns | ||
* @param {Array<Function>} negatedGlobs | ||
* @param {String|Array} path | ||
* @param {Array<Function>} negPatterns | ||
* @param {String|Array} args | ||
* @param {Boolean} returnIndex | ||
* @returns {boolean|number} | ||
*/ | ||
const matchPatterns = (patterns, negatedGlobs, path, returnIndex) => { | ||
const additionalArgs = Array.isArray(path); | ||
const upath = normalizePath(additionalArgs ? path[0] : path); | ||
for (let index = 0; index < negatedGlobs.length; index++) { | ||
const nglob = negatedGlobs[index]; | ||
if (nglob(upath)) { | ||
const matchPatterns = (patterns, negPatterns, args, returnIndex) => { | ||
const isList = Array.isArray(args); | ||
const _path = isList ? args[0] : args; | ||
if (!isList && typeof _path !== 'string') { | ||
throw new TypeError('anymatch: second argument must be a string: got ' + | ||
Object.prototype.toString.call(_path)) | ||
} | ||
const path = normalizePath(_path); | ||
for (let index = 0; index < negPatterns.length; index++) { | ||
const nglob = negPatterns[index]; | ||
if (nglob(path)) { | ||
return returnIndex ? -1 : false; | ||
} | ||
} | ||
const args = additionalArgs && [upath].concat(path.slice(1)); | ||
const applied = isList && [path].concat(args.slice(1)); | ||
for (let index = 0; index < patterns.length; index++) { | ||
const pattern = patterns[index]; | ||
if (additionalArgs ? pattern(...args) : pattern(upath)) { | ||
if (isList ? pattern(...applied) : pattern(path)) { | ||
return returnIndex ? index : true; | ||
@@ -68,9 +77,12 @@ } | ||
* @param {Array|string} testString | ||
* @param {boolean=} returnIndex | ||
* @param {object} options | ||
* @returns {boolean|number|Function} | ||
*/ | ||
const anymatch = (matchers, testString, returnIndex = false) => { | ||
const anymatch = (matchers, testString, options = DEFAULT_OPTIONS) => { | ||
if (matchers == null) { | ||
throw new TypeError('anymatch: specify first argument'); | ||
} | ||
const opts = typeof options === 'boolean' ? {returnIndex: options} : options; | ||
const returnIndex = opts.returnIndex || false; | ||
// Early cache for matchers. | ||
@@ -81,4 +93,4 @@ const mtchers = arrify(matchers); | ||
.map(item => item.slice(1)) | ||
.map(item => picomatch(item)); | ||
const patterns = mtchers.map(createPattern); | ||
.map(item => picomatch(item, opts)); | ||
const patterns = mtchers.map(matcher => createPattern(matcher, opts)); | ||
@@ -91,6 +103,2 @@ if (testString == null) { | ||
} | ||
if (!Array.isArray(testString) && typeof testString !== 'string') { | ||
throw new TypeError('anymatch: second argument must be a string: got ' + | ||
Object.prototype.toString.call(testString)) | ||
} | ||
@@ -97,0 +105,0 @@ return matchPatterns(patterns, negatedGlobs, testString, returnIndex); |
{ | ||
"name": "anymatch", | ||
"version": "3.0.3", | ||
"version": "3.1.0", | ||
"description": "Matches strings against configurable strings, globs, regular expressions, and/or functions", | ||
@@ -9,5 +9,9 @@ "files": [ | ||
], | ||
"dependencies": { | ||
"normalize-path": "^3.0.0", | ||
"picomatch": "^2.0.4" | ||
}, | ||
"author": { | ||
"name": "Elan Shanker", | ||
"url": "http://github.com/es128" | ||
"url": "https://github.com/es128" | ||
}, | ||
@@ -20,5 +24,2 @@ "license": "ISC", | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/micromatch/anymatch/issues" | ||
}, | ||
"keywords": [ | ||
@@ -42,10 +43,9 @@ "match", | ||
}, | ||
"dependencies": { | ||
"normalize-path": "^3.0.0", | ||
"picomatch": "^2.0.4" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^6.1.3", | ||
"nyc": "^14.0.0" | ||
}, | ||
"engines": { | ||
"node": ">= 8" | ||
} | ||
} |
@@ -17,3 +17,3 @@ anymatch [![Build Status](https://travis-ci.org/micromatch/anymatch.svg?branch=master)](https://travis-ci.org/micromatch/anymatch) [![Coverage Status](https://img.shields.io/coveralls/micromatch/anymatch.svg?branch=master)](https://coveralls.io/r/micromatch/anymatch?branch=master) | ||
#### anymatch(matchers, testString, [returnIndex]) | ||
#### anymatch(matchers, testString, [returnIndex], [options]) | ||
* __matchers__: (_Array|String|RegExp|Function_) | ||
@@ -27,3 +27,4 @@ String to be directly matched, string with glob patterns, regular expression | ||
as the arguments for function matchers. | ||
* __returnIndex__: (_Boolean [optional]_) If true, return the array index of | ||
* __options__: (_Object_ [optional]_) Any of the [picomatch](https://github.com/micromatch/picomatch#options) options. | ||
* __returnIndex__: (_Boolean [optional]_) If true, return the array index of | ||
the first matcher that that testString matched, or -1 if no match, instead of a | ||
@@ -44,5 +45,7 @@ boolean result. | ||
// returnIndex = true | ||
anymatch(matchers, 'foo.js', true); // 2 | ||
anymatch(matchers, 'path/anyjs/foo.js', true); // 1 | ||
anymatch(matchers, 'foo.js', {returnIndex: true}); // 2 | ||
anymatch(matchers, 'path/anyjs/foo.js', {returnIndex: true}); // 1 | ||
// any picomatc | ||
// using globs to match directories and their children | ||
@@ -75,7 +78,7 @@ anymatch('node_modules', 'node_modules'); // true | ||
Change Log | ||
Changelog | ||
---------- | ||
[See release notes page on GitHub](https://github.com/micromatch/anymatch/releases) | ||
- **v3.0:**: Removed `startIndex` and `endIndex` arguments. | ||
- **v3.0:** Removed `startIndex` and `endIndex` arguments. Node 8.x-only. | ||
- **v2.0:** [micromatch](https://github.com/jonschlinkert/micromatch) moves away from minimatch-parity and inline with Bash. This includes handling backslashes differently (see https://github.com/micromatch/micromatch#backslashes for more information). | ||
@@ -82,0 +85,0 @@ - **v1.2:** anymatch uses [micromatch](https://github.com/jonschlinkert/micromatch) |
Sorry, the diff of this file is not supported yet
9376
104
88