vscode-anymatch
Advanced tools
Comparing version 1.3.3 to 3.0.3
134
index.js
'use strict'; | ||
var micromatch = require('micromatch'); | ||
var normalize = require('normalize-path'); | ||
var path = require('path'); // required for tests. | ||
var arrify = function(a) { return a == null ? [] : (Array.isArray(a) ? a : [a]); }; | ||
const picomatch = require('picomatch'); | ||
const normalizePath = require('normalize-path'); | ||
var anymatch = function(criteria, value, returnIndex, startIndex, endIndex) { | ||
criteria = arrify(criteria); | ||
value = arrify(value); | ||
if (arguments.length === 1) { | ||
return anymatch.bind(null, criteria.map(function(criterion) { | ||
return typeof criterion === 'string' && criterion[0] !== '!' ? | ||
micromatch.matcher(criterion, { dot: true }) : criterion; | ||
})); | ||
/** | ||
* @typedef {(testString: string) => boolean} AnymatchFn | ||
* @typedef {string|RegExp|AnymatchFn} AnymatchPattern | ||
* @typedef {AnymatchPattern|AnymatchPattern[]} AnymatchMatcher | ||
*/ | ||
const BANG = '!'; | ||
const arrify = (item) => Array.isArray(item) ? item : [item]; | ||
/** | ||
* @param {AnymatchPattern} matcher | ||
* @returns {AnymatchFn} | ||
*/ | ||
const createPattern = (matcher) => { | ||
if (typeof matcher === 'function') { | ||
return matcher; | ||
} | ||
startIndex = startIndex || 0; | ||
var string = value[0]; | ||
var altString, altValue; | ||
var matched = false; | ||
var matchIndex = -1; | ||
function testCriteria(criterion, index) { | ||
var result; | ||
switch (Object.prototype.toString.call(criterion)) { | ||
case '[object String]': | ||
result = string === criterion || altString && altString === criterion; | ||
result = result || micromatch.isMatch(string, criterion, { dot: true }); | ||
break; | ||
case '[object RegExp]': | ||
result = criterion.test(string) || altString && criterion.test(altString); | ||
break; | ||
case '[object Function]': | ||
result = criterion.apply(null, value); | ||
result = result || altValue && criterion.apply(null, altValue); | ||
break; | ||
default: | ||
result = false; | ||
if (typeof matcher === 'string') { | ||
const glob = picomatch(matcher, { dot: true }); | ||
return (string) => matcher === string || glob(string); | ||
} | ||
if (matcher instanceof RegExp) { | ||
return (string) => matcher.test(string); | ||
} | ||
return (string) => false; | ||
}; | ||
/** | ||
* @param {Array<Function>} patterns | ||
* @param {Array<Function>} negatedGlobs | ||
* @param {String|Array} path | ||
* @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)) { | ||
return returnIndex ? -1 : false; | ||
} | ||
if (result) { | ||
matchIndex = index + startIndex; | ||
} | ||
const args = additionalArgs && [upath].concat(path.slice(1)); | ||
for (let index = 0; index < patterns.length; index++) { | ||
const pattern = patterns[index]; | ||
if (additionalArgs ? pattern(...args) : pattern(upath)) { | ||
return returnIndex ? index : true; | ||
} | ||
return result; | ||
} | ||
var crit = criteria; | ||
var negGlobs = crit.reduce(function(arr, criterion, index) { | ||
if (typeof criterion === 'string' && criterion[0] === '!') { | ||
if (crit === criteria) { | ||
// make a copy before modifying | ||
crit = crit.slice(); | ||
} | ||
crit[index] = null; | ||
arr.push(criterion.substr(1)); | ||
return returnIndex ? -1 : false; | ||
}; | ||
/** | ||
* @param {AnymatchMatcher} matchers | ||
* @param {Array|string} testString | ||
* @param {boolean=} returnIndex | ||
* @returns {boolean|number|Function} | ||
*/ | ||
const anymatch = (matchers, testString, returnIndex = false) => { | ||
if (matchers == null) { | ||
throw new TypeError('anymatch: specify first argument'); | ||
} | ||
// Early cache for matchers. | ||
const mtchers = arrify(matchers); | ||
const negatedGlobs = mtchers | ||
.filter(item => typeof item === 'string' && item.charAt(0) === BANG) | ||
.map(item => item.slice(1)) | ||
.map(item => picomatch(item, { dot: true })); | ||
const patterns = mtchers.map(createPattern); | ||
if (testString == null) { | ||
return (testString, ri = false) => { | ||
const returnIndex = typeof ri === 'boolean' ? ri : false; | ||
return matchPatterns(patterns, negatedGlobs, testString, returnIndex); | ||
} | ||
return arr; | ||
}, []); | ||
if (!negGlobs.length || !micromatch.any(string, negGlobs, { dot: true })) { | ||
if (path.sep === '\\' && typeof string === 'string') { | ||
altString = normalize(string); | ||
altString = altString === string ? null : altString; | ||
if (altString) altValue = [altString].concat(value.slice(1)); | ||
} | ||
matched = crit.slice(startIndex, endIndex).some(testCriteria); | ||
} | ||
return returnIndex === true ? matchIndex : matched; | ||
if (!Array.isArray(testString) && typeof testString !== 'string') { | ||
throw new TypeError('anymatch: second argument must be a string: got ' + | ||
Object.prototype.toString.call(testString)) | ||
} | ||
return matchPatterns(patterns, negatedGlobs, testString, returnIndex); | ||
}; | ||
module.exports = anymatch; |
{ | ||
"name": "vscode-anymatch", | ||
"version": "1.3.3", | ||
"version": "3.0.3", | ||
"description": "Matches strings against configurable strings, globs, regular expressions, and/or functions", | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
@@ -13,9 +14,9 @@ "author": { | ||
"license": "ISC", | ||
"homepage": "https://github.com/es128/anymatch", | ||
"homepage": "https://github.com/micromatch/anymatch", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/es128/anymatch" | ||
"url": "https://github.com/micromatch/anymatch" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/es128/anymatch/issues" | ||
"url": "https://github.com/micromatch/anymatch/issues" | ||
}, | ||
@@ -37,13 +38,13 @@ "keywords": [ | ||
"scripts": { | ||
"test": "istanbul cover _mocha && cat ./coverage/lcov.info | coveralls" | ||
"test": "nyc mocha", | ||
"mocha": "mocha" | ||
}, | ||
"dependencies": { | ||
"micromatch": "^2.1.5", | ||
"normalize-path": "^2.0.0" | ||
"normalize-path": "^3.0.0", | ||
"picomatch": "^2.0.4" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "^2.11.2", | ||
"istanbul": "^0.3.13", | ||
"mocha": "^2.2.4" | ||
"mocha": "^6.1.3", | ||
"nyc": "^14.0.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
anymatch [![Build Status](https://travis-ci.org/es128/anymatch.svg?branch=master)](https://travis-ci.org/es128/anymatch) [![Coverage Status](https://img.shields.io/coveralls/es128/anymatch.svg?branch=master)](https://coveralls.io/r/es128/anymatch?branch=master) | ||
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) | ||
====== | ||
@@ -8,12 +8,12 @@ Javascript module to match a string against a regular expression, glob, string, | ||
[![NPM](https://nodei.co/npm/anymatch.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/anymatch/) | ||
[![NPM](https://nodei.co/npm-dl/anymatch.png?height=3&months=9)](https://nodei.co/npm-dl/anymatch/) | ||
__Note: This module has Bash-parity, please be aware that Windows-style backslashes are not supported as separators. See https://github.com/micromatch/micromatch#backslashes for more information.__ | ||
Usage | ||
----- | ||
```sh | ||
npm install anymatch --save | ||
npm install anymatch | ||
``` | ||
#### anymatch (matchers, testString, [returnIndex], [startIndex], [endIndex]) | ||
#### anymatch(matchers, testString, [returnIndex]) | ||
* __matchers__: (_Array|String|RegExp|Function_) | ||
@@ -30,19 +30,7 @@ String to be directly matched, string with glob patterns, regular expression | ||
boolean result. | ||
* __startIndex, endIndex__: (_Integer [optional]_) Can be used to define a | ||
subset out of the array of provided matchers to test against. Can be useful | ||
with bound matcher functions (see below). When used with `returnIndex = true` | ||
preserves original indexing. Behaves the same as `Array.prototype.slice` (i.e. | ||
includes array members up to, but not including endIndex). | ||
```js | ||
var anymatch = require('anymatch'); | ||
const anymatch = require('anymatch'); | ||
var matchers = [ | ||
'path/to/file.js', | ||
'path/anyjs/**/*.js', | ||
/foo\.js$/, | ||
function (string) { | ||
return string.indexOf('bar') !== -1 && string.length > 10 | ||
} | ||
]; | ||
const matchers = [ 'path/to/file.js', 'path/anyjs/**/*.js', /foo.js$/, string => string.includes('bar') && string.length > 10 ] ; | ||
@@ -59,7 +47,2 @@ anymatch(matchers, 'path/to/file.js'); // true | ||
// skip matchers | ||
anymatch(matchers, 'path/to/file.js', false, 1); // false | ||
anymatch(matchers, 'path/anyjs/foo.js', true, 2, 3); // 2 | ||
anymatch(matchers, 'path/to/bar.js', true, 0, 3); // -1 | ||
// using globs to match directories and their children | ||
@@ -71,8 +54,13 @@ anymatch('node_modules', 'node_modules'); // true | ||
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) | ||
#### anymatch(matchers) | ||
You can also pass in only your matcher(s) to get a curried function that has | ||
already been bound to the provided matching criteria. This can be used as an | ||
`Array.prototype.filter` callback. | ||
`Array#filter` callback. | ||
@@ -84,3 +72,2 @@ ```js | ||
matcher('path/anyjs/baz.js', true); // 1 | ||
matcher('path/anyjs/baz.js', true, 2); // -1 | ||
@@ -92,9 +79,8 @@ ['foo.js', 'bar.js'].filter(matcher); // ['foo.js'] | ||
---------- | ||
[See release notes page on GitHub](https://github.com/es128/anymatch/releases) | ||
[See release notes page on GitHub](https://github.com/micromatch/anymatch/releases) | ||
NOTE: As of v1.2.0, anymatch uses [micromatch](https://github.com/jonschlinkert/micromatch) | ||
for glob pattern matching. The glob matching behavior should be functionally | ||
equivalent to the commonly used [minimatch](https://github.com/isaacs/minimatch) | ||
library (aside from some fixed bugs and greater performance), so a major | ||
version bump wasn't merited. Issues with glob pattern matching should be | ||
- **v3.0:**: Removed `startIndex` and `endIndex` arguments. | ||
- **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). | ||
- **v1.2:** anymatch uses [micromatch](https://github.com/jonschlinkert/micromatch) | ||
for glob pattern matching. Issues with glob pattern matching should be | ||
reported directly to the [micromatch issue tracker](https://github.com/jonschlinkert/micromatch/issues). | ||
@@ -104,2 +90,2 @@ | ||
------- | ||
[ISC](https://raw.github.com/es128/anymatch/master/LICENSE) | ||
[ISC](https://raw.github.com/micromatch/anymatch/master/LICENSE) |
8946
2
5
99
85
+ Addedpicomatch@^2.0.4
+ Addednormalize-path@3.0.0(transitive)
+ Addedpicomatch@2.3.1(transitive)
- Removedmicromatch@^2.1.5
- Removedarr-diff@2.0.0(transitive)
- Removedarr-flatten@1.1.0(transitive)
- Removedarray-unique@0.2.1(transitive)
- Removedbraces@1.8.5(transitive)
- Removedexpand-brackets@0.1.5(transitive)
- Removedexpand-range@1.8.2(transitive)
- Removedextglob@0.3.2(transitive)
- Removedfilename-regex@2.0.1(transitive)
- Removedfill-range@2.2.4(transitive)
- Removedfor-in@1.0.2(transitive)
- Removedfor-own@0.1.5(transitive)
- Removedglob-base@0.3.0(transitive)
- Removedglob-parent@2.0.0(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-dotfile@1.0.3(transitive)
- Removedis-equal-shallow@0.1.3(transitive)
- Removedis-extendable@0.1.1(transitive)
- Removedis-extglob@1.0.0(transitive)
- Removedis-glob@2.0.1(transitive)
- Removedis-number@2.1.04.0.0(transitive)
- Removedis-posix-bracket@0.1.1(transitive)
- Removedis-primitive@2.0.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisobject@2.1.0(transitive)
- Removedkind-of@3.2.26.0.3(transitive)
- Removedmath-random@1.0.4(transitive)
- Removedmicromatch@2.3.11(transitive)
- Removednormalize-path@2.1.1(transitive)
- Removedobject.omit@2.0.1(transitive)
- Removedparse-glob@3.0.4(transitive)
- Removedpreserve@0.2.0(transitive)
- Removedrandomatic@3.1.1(transitive)
- Removedregex-cache@0.4.4(transitive)
- Removedremove-trailing-separator@1.1.0(transitive)
- Removedrepeat-element@1.1.4(transitive)
- Removedrepeat-string@1.6.1(transitive)
Updatednormalize-path@^3.0.0