|
1 giorno fa | |
---|---|---|
.. | ||
lib | 1 giorno fa | |
README.md | 1 giorno fa | |
package.json | 1 giorno fa |
Transform GLOB patterns to JavaScript regular expressions for fast file path matching.
This tiny library converts familiar shell-style glob patterns like **/*.ts
or src/{a,b}/**/*.js
into JavaScript RegExp
objects and provides a convenient matcher utility.
yarn add glob-to-regex.js
# or
npm i glob-to-regex.js
import {toRegex, toMatcher} from 'glob-to-regex.js';
// Build a RegExp from a glob
const re = toRegex('src/**/test.ts');
re.test('src/a/b/test.ts'); // true
re.test('src/test.ts'); // true
re.test('src/test.tsx'); // false
// Build a predicate function from a pattern or an array of patterns
const match = toMatcher(['**/*.ts', '!**/*.d.ts']); // negative patterns are not special; use a RegExp if needed
match('index.ts'); // true
match('types.d.ts'); // true (negation is not parsed specially)
toRegex(pattern: string): RegExp
^...$
).toMatcher(pattern: string | RegExp | Array): (path: string) => boolean
/
and ending with /flags?
are treated as regular expressions (e.g. "/\\.test\\.ts$/"
)./
separates path segments*
matches zero or more characters within a single segment (does not cross /
)?
matches exactly one character within a single segment**
matches across path segments, including none{a,b,c}
alternation groups (no nesting). Each item inside can itself contain glob syntax[abc]
, [a-z]
, [!a-z]
, [!abc]
Notes:
^...$
).{
is found, it is treated literally.toRegex('a/b/c.txt').test('a/b/c.txt'); // true
toRegex('a/*.txt').test('a/file.txt'); // true
toRegex('a/*.txt').test('a/x/y.txt'); // false
toRegex('file?.js').test('file1.js'); // true
toRegex('src/**/test.ts').test('src/a/b/test.ts'); // true
toRegex('assets/**').test('assets/a/b.png'); // true
toRegex('*.{html,txt}').test('page.html'); // true
toRegex('src/{a,b}/**/*.ts').test('src/b/x/y.ts'); // true
toRegex('file[0-9].txt').test('file5.txt'); // true
toRegex('file[!0-9].txt').test('filea.txt'); // true
toRegex('**/*.[jt]s{,x}').test('dir/a/b.jsx'); // true
Types are bundled. The library targets modern Node.js and browsers.
toRegex
performs a single pass over the pattern and creates a native RegExp. Matching is then performed by V8's highly optimized engine.
!**/*.d.ts
are not parsed specially. If you need exclusion, combine multiple matchers or filter results separately.Apache-2.0 © streamich