Skip to content
BoogeyCodesPublic

About

A tiny zero-dependency glob matcher for TypeScript: *, **, ?, character classes and brace expansion, compiled to RegExp

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

globlite

CI TypeScript Zero dependencies ESM + CJS License: MIT

A tiny glob matcher for TypeScript, with no dependencies. It compiles a glob pattern to a regular expression and matches paths against it — *, **, ?, character classes and brace expansion.

import { isMatch, filter } from 'globlite'

isMatch('src/app.ts', 'src/**/*.ts')        // true
isMatch('src/app.test.ts', 'src/**/*.test.ts') // true
filter(files, '**/*.{js,ts}')               // keep the JS and TS files
filter(files, ['**/*.ts', '!**/*.test.ts']) // TS files, excluding tests (! = negate)

Why

Matching file paths against a pattern is something half the tools you write end up needing, and the full glob libraries carry a lot of weight. globlite is the small version: the glob features you actually use, translated to a RegExp you can see, in a few hundred lines with no dependencies. It is not a file-system walker — it matches strings — so it slots in wherever you already have a list of paths.

Pattern syntax

Token Matches
* any run of characters within one path segment (not /)
** any number of whole path segments, including none (a/**/b matches a/b)
? a single character, except /
[abc], [a-z] one character from the set or range
[!abc] one character not in the set
{a,b,c} alternation; nested and multiple groups expand as a product
\x the literal character x

By default *, ? and ** do not match a leading dot in a segment, so * does not match .env; pass { dot: true } to change that. Pass { nocase: true } for case-insensitive matching.

API

import { isMatch, filter, compile } from 'globlite'

// test a single path (pattern may be a string or an array of strings)
isMatch('a.js', '*.js')                       // true
isMatch('a.css', ['*.js', '*.css'])           // true

// keep matching paths
filter(['a.ts', 'a.js', 'b.ts'], '*.ts')      // ['a.ts', 'b.ts']

// compile once, test many times
const matcher = compile('src/**/*.ts', { nocase: true })
matcher.test('src/App.TS')                    // true
matcher.regex                                 // the underlying RegExp

Options: { dot?: boolean; nocase?: boolean }.

How it works

A pattern is first brace-expanded (a.{js,ts} → a.js, a.ts), and each result is translated to a regular-expression source: * becomes [^/]*, ? becomes [^/], **/ becomes (?:[^/]*\/)* so it can span or skip segments, character classes map straight across, and every other character is escaped so it matches literally. The alternatives are joined into one anchored RegExp. Because the output is a plain regex, matching is fast and the behaviour is inspectable.

Testing

npm install
npm test            # 16 tests across brace expansion and matching
npm run build       # ESM + CJS + .d.ts via tsup

Tests cover single-segment vs cross-segment wildcards, ?, character classes and negation, brace alternation (including nested), dotfile handling, literal special characters, case-insensitivity, pattern arrays and filter. CI runs on Linux and Windows across Node 18/20/22.

Ideas for later

  • extglob patterns (+(a|b), !(a)).
  • A \\-separator mode for Windows paths.
  • Returning match captures.

License

MIT

About

A tiny zero-dependency glob matcher for TypeScript: *, **, ?, character classes and brace expansion, compiled to RegExp

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages