...
 
Commits (12)
The ISC License
Copyright (c) 2014 Elan Shanker
Copyright (c) 2019 Elan Shanker, Paul Miller (https://paulmillr.com)
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
......
......@@ -14,7 +14,7 @@ Usage
npm install anymatch
```
#### anymatch(matchers, testString, [returnIndex])
#### anymatch(matchers, testString, [returnIndex], [options])
* __matchers__: (_Array|String|RegExp|Function_)
String to be directly matched, string with glob patterns, regular expression
test, function that takes the testString as an argument and returns a truthy
......@@ -23,7 +23,8 @@ value if it should be matched, or an array of any number and mix of these types.
passed as an array, the first element of the array will be used as the
`testString` for non-function matchers, while the entire array will be applied
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
boolean result.
......@@ -39,8 +40,10 @@ 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
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
anymatch('node_modules', 'node_modules'); // true
......@@ -69,11 +72,11 @@ matcher('path/anyjs/baz.js', true); // 1
['foo.js', 'bar.js'].filter(matcher); // ['foo.js']
```
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).
- **v1.2:** anymatch uses [micromatch](https://github.com/jonschlinkert/micromatch)
for glob pattern matching. Issues with glob pattern matching should be
......
node-anymatch (3.1.1+~2.0.7-1) unstable; urgency=medium
* Team upload
* Declare compliance with policy 4.4.1
* New upstream version 3.1.1+~2.0.7
* Use ${nodejs:Provides}
* Add "Rules-Requires-Root: no"
* Update debian/copyright
-- Xavier Guimard <yadd@debian.org> Mon, 14 Oct 2019 22:31:34 +0200
node-anymatch (3.1.0+~2.0.7-1) unstable; urgency=medium
* Team upload
* New upstream version 3.1.0+~2.0.7
* Use debian/nodejs/root_modules instead of custom install (picomatch)
-- Xavier Guimard <yadd@debian.org> Wed, 18 Sep 2019 23:04:57 +0200
node-anymatch (3.0.3+~2.0.7-3) unstable; urgency=medium
* Team upload
......
......@@ -5,26 +5,25 @@ Section: javascript
Testsuite: autopkgtest-pkg-nodejs
Priority: optional
Build-Depends:
debhelper (>= 12~),
dh-buildinfo,
nodejs,
node-micromatch,
node-normalize-path,
node-arrify,
mocha,
pkg-js-tools (>= 0.8.6)
Standards-Version: 4.4.0
debhelper-compat (= 12)
, node-micromatch <!nocheck>
, node-normalize-path <!nocheck>
, node-arrify <!nocheck>
, mocha <!nocheck>
, pkg-js-tools (>= 0.9.12~)
Standards-Version: 4.4.1
Vcs-Browser: https://salsa.debian.org/js-team/node-anymatch
Vcs-Git: https://salsa.debian.org/js-team/node-anymatch.git
Homepage: https://github.com/es128/anymatch
Rules-Requires-Root: no
Package: node-anymatch
Architecture: all
Depends:
${misc:Depends},
nodejs,
node-normalize-path
Provides: node-picomatch
${misc:Depends}
, nodejs
, node-normalize-path
Provides: ${nodejs:Provides}
Description: Matches strings against configurable strings
Javascript module to match a string against a regular expression, glob, string,
or function that takes the string as an argument and returns a truthy or falsy
......
......@@ -4,7 +4,7 @@ Upstream-Contact: https://github.com/es128/anymatch/issues
Source: https://github.com/es128/anymatch
Files: *
Copyright: 2014, Elan Shanker
Copyright: 2019, Elan Shanker
License: ISC
Files: picomatch/*
......@@ -13,6 +13,7 @@ License: Expat
Files: debian/*
Copyright: 2017, Aarti Kashyap <kaarti.sr@gmail.com>
2019, Xavier Guimard <yadd@debian.org>
License: ISC
License: Expat
......
package.json picomatch/
index.js picomatch/
lib picomatch/
......@@ -6,9 +6,11 @@ type AnymatchTester = {
(testString: string|any[]): boolean;
}
type PicomatchOptions = {dot: boolean};
declare const anymatch: {
(matchers: AnymatchMatcher): AnymatchTester;
(matchers: AnymatchMatcher, testString: string|any[], returnIndex: true): number;
(matchers: AnymatchMatcher, testString: string|any[], returnIndex: true | PicomatchOptions): number;
(matchers: AnymatchMatcher, testString: string|any[]): boolean;
}
......
......@@ -11,18 +11,20 @@ const normalizePath = require('normalize-path');
* @typedef {AnymatchPattern|AnymatchPattern[]} AnymatchMatcher
*/
const BANG = '!';
const DEFAULT_OPTIONS = {returnIndex: false};
const arrify = (item) => Array.isArray(item) ? item : [item];
/**
* @param {AnymatchPattern} matcher
* @param {object} options
* @returns {AnymatchFn}
*/
const createPattern = (matcher) => {
const createPattern = (matcher, options) => {
if (typeof matcher === 'function') {
return matcher;
}
if (typeof matcher === 'string') {
const glob = picomatch(matcher);
const glob = picomatch(matcher, options);
return (string) => matcher === string || glob(string);
}
if (matcher instanceof RegExp) {
......@@ -33,24 +35,31 @@ const createPattern = (matcher) => {
/**
* @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;
}
}
......@@ -61,20 +70,23 @@ const matchPatterns = (patterns, negatedGlobs, path, returnIndex) => {
/**
* @param {AnymatchMatcher} matchers
* @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.
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));
const patterns = mtchers.map(createPattern);
.map(item => picomatch(item, opts));
const patterns = mtchers.map(matcher => createPattern(matcher, opts));
if (testString == null) {
return (testString, ri = false) => {
......@@ -82,10 +94,6 @@ const anymatch = (matchers, testString, returnIndex = false) => {
return matchPatterns(patterns, negatedGlobs, testString, returnIndex);
}
}
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);
};
......
{
"name": "anymatch",
"version": "3.0.3",
"version": "3.1.1",
"description": "Matches strings against configurable strings, globs, regular expressions, and/or functions",
"files": [
"index.js",
"index.d.ts"
],
"dependencies": {
"normalize-path": "^3.0.0",
"picomatch": "^2.0.4"
},
"author": {
"name": "Elan Shanker",
"url": "http://github.com/es128"
"url": "https://github.com/es128"
},
"license": "ISC",
"homepage": "https://github.com/micromatch/anymatch",
......@@ -16,9 +20,6 @@
"type": "git",
"url": "https://github.com/micromatch/anymatch"
},
"bugs": {
"url": "https://github.com/micromatch/anymatch/issues"
},
"keywords": [
"match",
"any",
......@@ -37,12 +38,11 @@
"test": "nyc mocha",
"mocha": "mocha"
},
"dependencies": {
"normalize-path": "^3.0.0",
"picomatch": "^2.0.4"
},
"devDependencies": {
"mocha": "^6.1.3",
"nyc": "^14.0.0"
},
"engines": {
"node": ">= 8"
}
}
......@@ -97,7 +97,7 @@ describe('anymatch', () => {
assert.throws(() => anymatch());
})
it('should not allow bad testString', () => {
assert.throws(() => anymatch(matchers, {path: 'path/to/bar.js'}));
assert.throws(() => anymatch(matchers, { path: 'path/to/bar.js' }));
});
it('should allow string to be passed as first member of an array', () => {
assert.doesNotThrow(() => anymatch(matchers, ['path/to/bar.js']));
......@@ -183,4 +183,11 @@ describe('anymatch', () => {
assert(!anymatch(matchers)('path/no/no.js'));
});
});
describe('picomatch options', () => {
it('should support picomatch options', () => {
assert.equal(false, anymatch('path/to/?dotfile', 'path/to/.dotfile'));
assert.equal(true, anymatch('path/to/?dotfile', 'path/to/.dotfile', { dot: true }));
});
});
});