Skip to content

Commit

Permalink
Cache patterns. Performance boost.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Apr 8, 2019
1 parent 5ece3fd commit 13343b0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import sep from 'path';

type AnymatchPattern = string|RegExp|{(string:string): boolean};
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;
Expand Down
69 changes: 39 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const normalizePath = require('normalize-path');
const {sep} = require('path'); // required for tests.

/**
* @typedef {(string:String) => boolean} AnymatchStrBoolFn
* @typedef {string|RegExp|AnymatchStrBoolFn} AnymatchPattern
* @typedef {(string:String) => boolean} AnymatchFn
* @typedef {string|RegExp|AnymatchFn} AnymatchPattern
* @typedef {AnymatchPattern|Array<AnymatchPattern>} AnymatchMatcher
*/

Expand All @@ -16,7 +16,7 @@ const arrify = (item) => Array.isArray(item) ? item : [item];
/**
*
* @param {AnymatchPattern} matcher
* @returns {AnymatchStrBoolFn}
* @returns {AnymatchFn}
*/
const createPattern = (matcher) => {
const isString = typeof matcher === 'string';
Expand All @@ -36,6 +36,32 @@ const createPattern = (matcher) => {
}
};

/**
* @param {Array<AnymatchFn>} patterns
* @param {Array<AnymatchFn>} negatedGlobs
* @param {String} path
* @param {Boolean} returnIndex
*/
const matchPatterns = (patterns, negatedGlobs, path, returnIndex) => {
const upath = normalizePath(path);
if (negatedGlobs.length > 0) {
for (let index = 0; index < negatedGlobs.length; index++) {
const nglob = negatedGlobs[index];
if (nglob(upath)) {
return returnIndex ? -1 : false;
}
}
}
for (let index = 0; index < patterns.length; index++) {
const pattern = patterns[index];
if (pattern(upath)) {
return returnIndex ? index : true;
}
}

return returnIndex ? -1 : false;
};

/**
* @param {AnymatchMatcher} matchers
* @param {String} testString
Expand All @@ -46,43 +72,26 @@ 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));
const patterns = mtchers.map(createPattern);

if (testString == null) {
return (testString, ri = false) => {
const returnIndex = typeof ri === 'boolean' ? ri : false;
return anymatch(matchers, testString, returnIndex);
return matchPatterns(patterns, negatedGlobs, testString, returnIndex);
}
}
if (typeof testString !== 'string') {
throw new TypeError('anymatch: second argument must be a string: got ' +
Object.prototype.toString.call(testString))
}

const unixified = normalizePath(testString);
const arrified = arrify(matchers);
const negatedGlobs = arrified
.filter(item => typeof item === 'string' && item.charAt(0) === BANG)
.map(item => item.slice(1));

// console.log('anymatch', {matchers, testString, containsNegatedGlob, negatedGlobs});

if (negatedGlobs.length > 0) {
for (var i = 0; i < negatedGlobs.length; i++) {
const nglob = negatedGlobs[i];
if (picomatch(nglob)(unixified)) {
return returnIndex ? -1 : false;
}
}
}

const patterns = arrified.map(createPattern);
for (let index = 0; index < patterns.length; index++) {
const pattern = patterns[index];
if (pattern(unixified)) {
return returnIndex ? index : true;
}
}

return returnIndex ? -1 : false;
return matchPatterns(patterns, negatedGlobs, testString, returnIndex);
};

module.exports = anymatch;

0 comments on commit 13343b0

Please sign in to comment.