EventEmitter with RegExp pattern matching - fully tested, production-ready event system for Node.js and TypeScript
β¨ Full EventEmitter Compatibility - Drop-in replacement for Node's EventEmitter
π― RegExp Pattern Matching - Listen to events matching regular expressions
π¦ TypeScript Native - Built with TypeScript 5.7, fully typed
π§ͺ 100% Test Coverage - 100 comprehensive tests (77 core + 21 edge cases + 12 memory tests + 4 benchmarks)
β‘ Zero Dependencies - Lightweight and production-ready
π Listener Ordering - Maintains registration order across all event types
π 6 Runnable Examples - Learn from real-world usage patterns
- Installation
- Quick Start
- Examples
- API Documentation
- Performance
- Testing
- Development
- Contributing
- License
npm install @sfast/pattern-emitter-tsor with yarn:
yarn add @sfast/pattern-emitter-tsReplace your EventEmitter imports:
// Before
import {EventEmitter} from 'events';
// After
import {PatternEmitter} from '@sfast/pattern-emitter-ts';That's it! PatternEmitter is fully compatible with EventEmitter's API.
import {PatternEmitter} from '@sfast/pattern-emitter-ts';
const emitter = new PatternEmitter();
// String events (exact match)
emitter.on('user:login', user => {
console.log(`User logged in: ${user.name}`);
});
// RegExp patterns (flexible matching)
emitter.on(/^user:/, data => {
console.log(`Any user event: ${data}`);
});
// Emit events
emitter.emit('user:login', {name: 'Alice'}); // Both listeners fire
emitter.emit('user:logout', {name: 'Alice'}); // Only pattern listener firesconst router = new PatternEmitter();
// Exact endpoint
router.on('request:users:list', () => {
console.log('GET /users');
});
// Pattern for all user endpoints
router.on(/^request:users:/, action => {
console.log(`User endpoint: ${action}`);
});
// Pattern for all create operations
router.on(/:create$/, resource => {
console.log(`Creating ${resource}`);
});
router.emit('request:users:create', 'users'); // All 3 fire!This package includes 6 comprehensive, runnable examples:
# 1. Basic Usage - String vs Regex patterns
npx ts-node example/01-basic.ts
# 2. API Routing - Request handling with patterns
npx ts-node example/02-api-routing.ts
# 3. E-Commerce App - Order processing system
npx ts-node example/03-real-world-app.ts
# 4. Advanced Features - once(), introspection, removal
npx ts-node example/04-advanced-features.ts
# 5. Namespace Events - Hierarchical event organization
npx ts-node example/05-namespace-events.ts
# 6. Performance Benchmarks - EventEmitter vs PatternEmitter
npx ts-node example/06-benchmarks.tsconst events = new PatternEmitter();
// Service A: Auth events
events.on(/^service:auth:/, event => {
console.log('Auth service:', event);
});
// Service B: Payment events
events.on(/^service:payment:/, event => {
console.log('Payment service:', event);
});
// Global monitoring
events.on(/^service:/, event => {
console.log('Monitor:', event);
});
events.emit('service:auth:login', {user: 'alice'});
events.emit('service:payment:success', {amount: 99});See example/README.md for detailed explanations of all examples.
All standard EventEmitter methods are supported:
| Method | Description |
|---|---|
on(event, listener) |
Add a listener (alias: addListener) |
once(event, listener) |
Add a one-time listener |
off(event, listener) |
Remove a listener (alias: removeListener) |
removeAllListeners([event]) |
Remove all listeners |
emit(event, ...args) |
Emit an event |
listenerCount(event) |
Get listener count |
listeners(event) |
Get listeners for an event (supports RegExp!) |
| Method | Description |
|---|---|
allListeners |
Getter returning Map of ALL listeners (strings + patterns) |
eventNames() |
Array of string/symbol event names (NOT patterns) |
eventPatterns() |
Array of RegExp pattern strings (NOT string events) |
listenersByEventType(event) |
Get all matching listeners (backward compat) |
type EventPattern = string | symbol | RegExp;
type PatternListener = (...args: any[]) => void;const emitter = new PatternEmitter();
// All match the same event - they all fire
emitter.on('api:user:create', () => {}); // Exact
emitter.on(/^api:/, () => {}); // Prefix
emitter.on(/create$/, () => {}); // Suffix
emitter.on(/.*/, () => {}); // Everything
emitter.emit('api:user:create'); // All 4 fire in registration order// Get all event names
console.log(emitter.eventNames()); // ['api:user:create']
// Get all patterns
console.log(emitter.eventPatterns()); // ['/^api:/', '/create$/', '/.*/']
// Get all listeners (unified view)
const all = emitter.allListeners; // Map<EventPattern, PatternListener[]>
// Get listeners for specific event/pattern
const stringListeners = emitter.listeners('api:user:create'); // Function[]
const patternListeners = emitter.listeners(/^api:/); // Function[]
// Count listeners
console.log(emitter.listenerCount('api:user:create')); // Includes matching patternsconst handler = data => console.log(data);
// Add
emitter.on('event', handler);
// Check if it fired
const hadListeners = emitter.emit('event', 'data'); // Returns boolean
// Remove
emitter.removeListener('event', handler);
// Remove all for event type
emitter.removeAllListeners('event');
// Remove all listeners
emitter.removeAllListeners();Generate complete TypeDoc documentation:
npm run docsThis creates detailed API documentation in the docs/ folder.
Run the benchmarks:
npx ts-node example/06-benchmarks.tsKey Findings:
| Metric | EventEmitter | PatternEmitter | Overhead |
|---|---|---|---|
| String events | 0.0003ms | 0.0005ms | ~50-60% |
| Pattern matching | β N/A | β Exclusive | - |
| 100 patterns | β N/A | 0.0004ms | Scales well |
Real-World Impact: Negligible (we're talking microseconds)
| Scenario | EventEmitter | PatternEmitter |
|---|---|---|
| Exact string matching only | β Faster | β Compatible |
| Pattern matching needed | β Can't do | β Built for this |
| Middleware/routing systems | β Natural | |
| Hierarchical namespaces | β Easy | |
| Millions of events/sec | β Optimal |
Recommendation: Use PatternEmitter by default. The flexibility is worth the tiny overhead. Only optimize to EventEmitter if profiling shows it as a bottleneck.
The project maintains 100% test coverage with comprehensive test suite:
# Run tests
npm test
# Run with coverage
npm test -- --coverage
# Watch mode
npm test -- --watch| Category | Tests | Coverage |
|---|---|---|
| Core Functionality | 77 tests | All APIs, emit, listeners, removal |
| Edge Cases | 21 tests | Overlapping patterns, special chars, boundaries |
| Memory Management | 12 tests | Leak prevention, cleanup, stress tests |
| Performance | 4 tests | Cache optimization, scaling |
| TOTAL | 100 tests | 100% coverage |
-------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
patternEmitter.ts | 100 | 100 | 100 | 100 |
-------------------|---------|----------|---------|---------|-------------------
- Node.js >= 14
- npm >= 6 or yarn >= 1.22
# Clone the repository
git clone https://github.com/sfast/pattern-emitter-ts.git
cd pattern-emitter-ts
# Install dependencies
npm install
# Build the project
npm run buildnpm run build # Compile TypeScript to JavaScript
npm run test # Run tests with coverage
npm run lint # Run ESLint
npm run fix # Auto-fix linting issues
npm run check # Run Google TypeScript Style checks
npm run docs # Generate TypeDoc documentationContributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project uses:
- Google TypeScript Style (gts) for code formatting
- ESLint for linting
- Jest for testing
Please ensure all tests pass and coverage remains at 100% before submitting.
See CHANGELOG.md for version history and release notes.
Made with β€οΈ by the steadfast.tech team
Quick Links:
- Examples - 6 runnable examples
- API Documentation - Full TypeDoc docs (run
npm run docs) - Changelog - Version history
- Issues - Report bugs
- NPM Package