Skip to content

refactor: extract patterns.ts and placeholder-replacer.ts from index.ts#282

Open
Wolfvin wants to merge 1 commit into
vinta:masterfrom
Wolfvin:refactor/extract-patterns-and-methods
Open

refactor: extract patterns.ts and placeholder-replacer.ts from index.ts#282
Wolfvin wants to merge 1 commit into
vinta:masterfrom
Wolfvin:refactor/extract-patterns-and-methods

Conversation

@Wolfvin

@Wolfvin Wolfvin commented Jun 13, 2026

Copy link
Copy Markdown

Summary

Refactors the monolithic src/shared/index.ts (498 lines) into three well-structured modules with clear separation of concerns, while preserving 100% identical behavior.

What Changed

Extracted Modules

  • src/shared/patterns.ts — All 60+ regex patterns and character class constants (CJK, AN, operators, brackets, quotes, file paths, etc.)
  • src/shared/placeholder-replacer.tsPlaceholderReplacer class for protecting content during regex processing

Decomposed spacingText() into Private Methods

Method Responsibility
applyPunctuationSpacing() Dots, periods, colons, tildes, commas, semicolons
applyQuoteSpacing() Double, single, curly quotes, possessives
applyHashSpacing() Hashtag/hash spacing with slash context awareness
applyOperatorSpacing() Arithmetic operators (+, -, *, =, &)
applyComparisonOperatorSpacing() Comparison operators (<, >)
applyFilePathSpacing() Unix/Windows file path spacing
applySlashSpacing() Context-aware slash handling (operator vs separator)
applyBracketSpacing() Bracket spacing around CJK text
fixBracketInnerSpacing() Inner bracket content cleanup

Regression Proof

This refactoring was validated using output-based regression testing (the Regrets skill) with a 3-layer verification:

Verification 1 — Regrets Fingerprints

All 8 behavioral clusters remained GREEN across 5-run drift tests.

Cluster Fingerprint Status
cjk-alphanum-spacing n30nmvg ✅ STABLE
cjk-symbol-spacing 4a14yk9 ✅ STABLE
cjk-punctuation-spacing 1fwk5dr ✅ STABLE
cjk-slash-path-spacing 2nj1rhd ✅ STABLE
cjk-quote-bracket-spacing 59cgutn ✅ STABLE
cjk-grade-compound-spacing 41cod4b ✅ STABLE
cjk-html-tag-spacing 1usarqr ✅ STABLE
has-proper-spacing 4r4g9yv ✅ STABLE

Verification 2 — Direct Output Match

All 57 test cases produce output identical to the pre-refactor ground truth (KEBENARAN 1).

Verification 3 — Fingerprint Cross-Check

All 8 regenerated fingerprints match the pre-refactor fingerprint contract (KEBENARAN 2).

Existing Tests

All existing vitest test suites (30 test files, 60+ test cases) continue to pass.

Why This Refactoring?

  1. Readability: The 498-line index.ts was a monolith mixing pattern definitions, utility classes, and spacing logic
  2. Maintainability: Each concern now has its own file with clear imports
  3. Discoverability: New contributors can find relevant patterns in patterns.ts without scrolling through 500 lines
  4. Testability: Individual spacing steps can be understood and reasoned about independently

Extract regex patterns and PlaceholderReplacer class into dedicated modules,
and decompose the monolithic spacingText method into well-named private methods.

**Extracted modules:**
- src/shared/patterns.ts — all 60+ regex patterns and character class constants
- src/shared/placeholder-replacer.ts — PlaceholderReplacer class for content protection

**Extracted private methods from Pangu.spacingText:**
- applyPunctuationSpacing() — dots, periods, colons, tildes
- applyQuoteSpacing() — double, single, curly quotes
- applyHashSpacing() — hashtag/hash spacing with slash context
- applyOperatorSpacing() — arithmetic and logical operators
- applyComparisonOperatorSpacing() — < > operators
- applyFilePathSpacing() — Unix/Windows path spacing
- applySlashSpacing() — context-aware slash handling
- applyBracketSpacing() — bracket spacing around CJK
- fixBracketInnerSpacing() — inner bracket content cleanup

**Verified by 3-layer regression proof:**
1. Regrets: All 8 clusters GREEN (5-run drift test stable)
2. Direct output: All 57 test cases match KEBENARAN 1 ground truth
3. Cross-check: All 8 fingerprints match KEBENARAN 2

Behavior is IDENTICAL — only structure changed, no logic modified.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant