$ show kaelzhang/node-ignore
node-ignore is a pure JavaScript library that implements the .gitignore specification (2.22.1) for filtering file paths according to ignore rules. It is used by major tools like ESLint and GitBook. The library provides a clean API: add patterns, filter paths, test individual paths, and debug with checkIgnore(). Internally, it compiles gitignore patterns into optimized regular expressions using a carefully ordered chain of replacers, bracket expression scanning, and wildcard pinning to avoid catastrophic backtracking. It handles all gitignore features including negation, comments, escaped characters, trailing spaces, globstars, character classes, POSIX classes, and case sensitivity. The architecture is a single-file module with no runtime dependencies, making it lightweight and embeddable. Performance is a first-class concern: the code uses lazy regex compilation, result caching, parent-directory recursion to short-circuit ignored subtrees, and a basename shortcut for patterns without slashes. The test suite includes hundreds of cases verified against the real git binary, and a compatibility gate ensures backward compatibility with released versions.
The LLM identified 3 files worth highlighting for reuse:
The core library implementing gitignore pattern matching. Notable for its highly optimized pattern-to-regex compilation chain (REPLACERS), which processes patterns through a series of string replacements with early-exit checks to avoid unnecessary work. Features include bracket expression scanning with POSIX class support, wildcard pinning to prevent catastrophic backtracking, lazy regex construction per rule, result caching per path, and parent-directory recursion to skip entire subtrees when a directory is ignored. The code demonstrates deep understanding of both the gitignore spec and JavaScript regex engine internals.
A robust benchmarking framework that compares multiple versions of the library. It isolates each version in a separate Node.js process to avoid V8 inline cache pollution, interleaves samples across versions to cancel out machine drift, and uses median absolute deviation (MAD) instead of standard deviation to resist outliers from garbage collection pauses. Samples are collected in multiple blocks with worker respawns to capture process-level variability. The framework is self-contained and could be extracted for benchmarking any JavaScript module.
A compatibility gate that verifies the current codebase against the test suites of previously released versions. It checks out each version into a git worktree, replaces its index.js with the working tree's, and runs the old test suites. This ensures that backward-incompatible changes are explicitly acknowledged and documented. The approach is generalizable to any library that maintains a test suite per release.