Skip to content

Commit

Permalink
feat: add minimatch wrapper to support caching (#23515)
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy committed Jul 24, 2023
1 parent b9c8c44 commit e24ca0e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/util/minimatch.spec.ts
@@ -0,0 +1,18 @@
import { minimatch } from './minimatch';

describe('util/minimatch', () => {
it('caches minimatch', () => {
expect(minimatch('foo')).toBe(minimatch('foo'));
expect(minimatch('foo', { dot: true })).toBe(
minimatch('foo', { dot: true })
);
});

it('does not cache minimatch', () => {
expect(minimatch('foo', undefined, false)).not.toBe(
minimatch('foo', undefined, false)
);
expect(minimatch('foo')).not.toBe(minimatch('foo', undefined, false));
expect(minimatch('foo', { dot: true })).not.toBe(minimatch('foo'));
});
});
24 changes: 24 additions & 0 deletions lib/util/minimatch.ts
@@ -0,0 +1,24 @@
import { Minimatch, MinimatchOptions } from 'minimatch';

const cache = new Map<string, Minimatch>();

export function minimatch(
pattern: string,
options?: MinimatchOptions,
useCache = true
): Minimatch {
const key = options ? `${pattern}:${JSON.stringify(options)}` : pattern;

if (useCache) {
const cachedResult = cache.get(key);
if (cachedResult) {
return cachedResult;
}
}

const instance = new Minimatch(pattern, options);
if (useCache) {
cache.set(key, instance);
}
return instance;
}

0 comments on commit e24ca0e

Please sign in to comment.