Skip to content

Commit 1c5fe3a

Browse files
artakvgsfast
andauthored
updated packages 2025, tests 100% coverage (#22)
* updated packages 2025, tests 100% coverage * cursor files * more updates, mem leak test, becnhmark test --------- Co-authored-by: sfast <fast@sfasts-MacBook-Pro.local>
1 parent 28023af commit 1c5fe3a

24 files changed

+7232
-5874
lines changed

.cursorignore

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Dependencies
2+
node_modules/
3+
4+
# Build output
5+
dist/
6+
7+
# Documentation
8+
docs/
9+
10+
# Test coverage
11+
coverage/
12+
*.lcov
13+
.nyc_output/
14+
15+
# Logs
16+
*.log
17+
npm-debug.log*
18+
yarn-debug.log*
19+
yarn-error.log*
20+
21+
# Yarn Berry
22+
.yarn/*
23+
!.yarn/patches
24+
!.yarn/plugins
25+
!.yarn/releases
26+
!.yarn/sdks
27+
!.yarn/versions
28+
.pnp.*
29+
yarn.lock
30+
31+
# Lock files
32+
package-lock.json
33+
34+
# OS
35+
*.DS_Store
36+
Thumbs.db
37+
38+
# Temporary files
39+
tmp/
40+
temp/
41+
*.tmp
42+
43+
# IDE
44+
.idea/
45+
.vscode/
46+
.history/
47+
48+
# Large generated files
49+
*.map
50+

.cursorrules

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
# Cursor Rules for @sfast/pattern-emitter-ts
2+
3+
## Project Overview
4+
This is a TypeScript library that extends Node.js EventEmitter with RegExp pattern matching support.
5+
- **Language**: TypeScript 5.7.2
6+
- **Test Framework**: Jest 29.7.0
7+
- **Code Style**: Google TypeScript Style (gts)
8+
- **Target**: Production library with 100% test coverage
9+
10+
## Code Style & Standards
11+
12+
### TypeScript
13+
- Use strict TypeScript mode (already configured in tsconfig.json)
14+
- Prefer explicit types over `any` (use `any` only when necessary for flexibility)
15+
- Use ES2020 features (target is ES2020)
16+
- Follow Google TypeScript Style guidelines
17+
- Use JSDoc comments for all public APIs
18+
19+
### Naming Conventions
20+
- Classes: PascalCase (e.g., `PatternEmitter`)
21+
- Interfaces: PascalCase with `I` prefix (e.g., `IPatternEmitter`)
22+
- Types: PascalCase (e.g., `EventPattern`, `PatternListener`)
23+
- Private fields: camelCase with `_` prefix (e.g., `_listeners`)
24+
- Public methods: camelCase (e.g., `addListener`)
25+
26+
### File Organization
27+
```
28+
src/
29+
index.ts # Main export
30+
patternEmitter.ts # Core implementation
31+
interface.ts # Interface definitions
32+
types.ts # Type definitions
33+
utils/ # Utility functions
34+
test/
35+
patternEmitter.test.ts # All tests in one file
36+
```
37+
38+
## Development Guidelines
39+
40+
### Testing Requirements
41+
- **CRITICAL**: Maintain 100% code coverage
42+
- Write tests for ALL new features before implementation
43+
- Test both string and RegExp event patterns
44+
- Test edge cases (empty listeners, removeAll, once, etc.)
45+
- Run `npm test` before committing
46+
47+
### Code Quality Checks
48+
1. `npm run build` - Must compile without errors
49+
2. `npm test` - All tests must pass with 100% coverage
50+
3. `npm run lint` - No linting errors
51+
4. `npm run fix` - Auto-fix formatting issues
52+
53+
### When Making Changes
54+
55+
#### Adding New Features
56+
1. Update `src/interface.ts` if adding public API
57+
2. Update `src/types.ts` if adding new types
58+
3. Add JSDoc comments with `@param`, `@return`, `@example`
59+
4. Write tests first (TDD approach)
60+
5. Update README.md with usage examples
61+
6. Run `npm run docs` to regenerate documentation
62+
63+
#### Modifying Core Logic
64+
- The `PatternEmitter` class wraps listeners with `wrapListener()` to maintain order
65+
- Listener order is preserved using a global index (`_globalListenerIndex`)
66+
- String events use native EventEmitter, RegExp events use custom `_listeners` Map
67+
- Never break backward compatibility with EventEmitter API
68+
69+
#### Bug Fixes
70+
1. Write a failing test that reproduces the bug
71+
2. Fix the bug
72+
3. Verify test passes
73+
4. Check no other tests broke
74+
75+
### Architecture Principles
76+
77+
1. **Backward Compatibility**: Must remain 100% compatible with Node.js EventEmitter
78+
2. **Type Safety**: Leverage TypeScript for compile-time safety
79+
3. **Performance**: Optimize for event emission (hot path)
80+
4. **Simplicity**: Keep API surface small and intuitive
81+
82+
### Key Implementation Details
83+
84+
- `_actualListeners`: Maps wrapped listeners to original listeners
85+
- `_listeners`: Stores RegExp pattern listeners
86+
- `_regexMap`: Stores RegExp patterns by string representation
87+
- `wrapListener()`: Wraps each listener with an index for ordering
88+
- `getMatchingListeners()`: Collects all listeners (string + pattern) and sorts by index
89+
90+
### Common Tasks
91+
92+
**Add a new public method:**
93+
```typescript
94+
// 1. Add to interface
95+
interface IPatternEmitter {
96+
myNewMethod(arg: string): void;
97+
}
98+
99+
// 2. Implement in PatternEmitter
100+
/**
101+
* Description of what it does
102+
* @param {string} arg - Description of parameter
103+
* @return {void}
104+
*/
105+
public myNewMethod(arg: string): void {
106+
// implementation
107+
}
108+
109+
// 3. Write tests
110+
it('myNewMethod does X', () => {
111+
emitter.myNewMethod('test');
112+
expect(...).to.equal(...);
113+
});
114+
```
115+
116+
**Add a new type:**
117+
```typescript
118+
// In src/types.ts
119+
export type MyNewType = string | number;
120+
```
121+
122+
### Testing Patterns
123+
124+
```typescript
125+
// Use Chai assertions
126+
expect(value).to.equal(expected);
127+
expect(value).to.be.instanceOf(Class);
128+
expect(array).to.eql([...]);
129+
130+
// Test listeners are called
131+
let called = false;
132+
emitter.on('event', () => { called = true; });
133+
emitter.emit('event');
134+
expect(called).to.equal(true);
135+
136+
// Test RegExp patterns
137+
emitter.on(/^prefix/, handler);
138+
emitter.emit('prefix:something'); // Should match
139+
```
140+
141+
### Documentation
142+
143+
- Use JSDoc with `@param`, `@return`, `@example` tags
144+
- Keep README.md examples up to date
145+
- Generate docs with `npm run docs` before releasing
146+
147+
### Publishing Checklist
148+
149+
1. Run all tests: `npm test`
150+
2. Run linter: `npm run lint`
151+
3. Update version in `package.json`
152+
4. Update `CHANGELOG.md`
153+
5. Build: `npm run build`
154+
6. Verify package contents: `npm pack --dry-run`
155+
7. Generate docs: `npm run docs`
156+
8. Commit and tag: `git tag v0.x.x`
157+
158+
## Don't Do
159+
160+
❌ Don't use `any` unless absolutely necessary
161+
❌ Don't break EventEmitter compatibility
162+
❌ Don't reduce test coverage below 100%
163+
❌ Don't commit without running tests
164+
❌ Don't modify `dist/` directly (it's generated)
165+
❌ Don't add dependencies (keep it zero-dependency)
166+
❌ Don't use console.log (use proper logging if needed)
167+
❌ Don't modify `.pnp.*` or `.yarn/` files
168+
169+
## Do
170+
171+
✅ Write tests first (TDD)
172+
✅ Use meaningful variable names
173+
✅ Add JSDoc comments for public APIs
174+
✅ Keep backward compatibility
175+
✅ Run `npm run fix` before committing
176+
✅ Update README for new features
177+
✅ Maintain 100% test coverage
178+
✅ Follow Google TypeScript Style
179+
180+
## Useful Commands
181+
182+
```bash
183+
npm run build # Compile TypeScript
184+
npm test # Run tests with coverage
185+
npm run lint # Check code style
186+
npm run fix # Auto-fix style issues
187+
npm run check # Run gts check
188+
npm run docs # Generate TypeDoc
189+
npm pack --dry-run # Preview npm package contents
190+
```
191+
192+
## Questions to Ask Before Changes
193+
194+
1. Does this maintain EventEmitter compatibility?
195+
2. Are there tests for this change?
196+
3. Is test coverage still 100%?
197+
4. Does this follow the existing patterns in the codebase?
198+
5. Is the public API documented with JSDoc?
199+
6. Does this introduce any breaking changes?
200+
201+
## When in Doubt
202+
203+
- Follow existing code patterns in `src/patternEmitter.ts`
204+
- Check how EventEmitter does it
205+
- Prioritize simplicity over cleverness
206+
- Ask for clarification rather than assume
207+

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
insert_final_newline = true

.eslintignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
build/
2+
dist/
3+
node_modules/
4+
coverage/
5+
docs/
6+
test/
7+
*.js
8+
!jest.config.js
9+
!prettier.config.js
10+
!.eslintrc.js

.eslintrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./node_modules/gts/",
3+
"rules": {
4+
"@typescript-eslint/no-explicit-any": "warn",
5+
"@typescript-eslint/no-unused-vars": "warn"
6+
}
7+
}

.gitignore

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,51 @@
1-
node_modules
1+
# Dependencies
2+
node_modules/
23

4+
# Build output
35
dist/
46

5-
.idea/
6-
7-
.history/
8-
7+
# Documentation
98
docs/
109

10+
# Test coverage
1111
coverage/
12+
*.lcov
13+
.nyc_output/
14+
15+
# IDE / Editor
16+
.idea/
17+
.vscode/
18+
.history/
19+
*.swp
20+
*.swo
21+
*~
1222

23+
# OS
1324
*.DS_Store
25+
.DS_Store
26+
Thumbs.db
27+
28+
# Logs
29+
*.log
30+
npm-debug.log*
31+
yarn-debug.log*
32+
yarn-error.log*
33+
lerna-debug.log*
1434

35+
# Temporary files
1536
tmp/
37+
temp/
38+
*.tmp
39+
40+
# Yarn Berry (if using)
41+
.yarn/*
42+
!.yarn/patches
43+
!.yarn/plugins
44+
!.yarn/releases
45+
!.yarn/sdks
46+
!.yarn/versions
47+
.pnp.*
48+
yarn.lock
49+
50+
# Optional: Uncomment if you want to commit yarn.lock
51+
# !yarn.lock

0 commit comments

Comments
 (0)