Skip to content

fix(types): TrainCase should insert joiner between tokens, not as prefix#117

Open
guoyangzhen wants to merge 1 commit intounjs:mainfrom
guoyangzhen:fix/train-case-joiner
Open

fix(types): TrainCase should insert joiner between tokens, not as prefix#117
guoyangzhen wants to merge 1 commit intounjs:mainfrom
guoyangzhen:fix/train-case-joiner

Conversation

@guoyangzhen
Copy link
Copy Markdown

@guoyangzhen guoyangzhen commented Mar 21, 2026

Fixes #110

Problem

The TrainCase type passes the Joiner parameter as the Accumulator to CapitalizedWords, causing the joiner to be prepended to the output instead of inserted between tokens.

// Current (broken)
TrainCase<"hello_world"> // => "-HelloWorld"

// Expected
TrainCase<"hello_world"> // => "Hello-World"

Root Cause

CapitalizedWords uses the second parameter as an initial accumulator value. TrainCase passes Joiner as the Accumulator, resulting in ${"-"}${Capitalize<"hello">} = "-Hello".

Fix

Added JoinCapitalizedWords type that properly inserts the joiner between tokens, following the same pattern as the existing JoinLowercaseWords. The first word has no prefix, subsequent words are preceded by the joiner.

Verification

The runtime trainCase function already works correctly (uses .join("-")). This fix aligns the type-level behavior with the runtime behavior.

Summary by CodeRabbit

  • Refactor
    • Internal type system improvements with no impact to user-facing functionality.

Fixes unjs#110

The TrainCase type was passing the joiner as the Accumulator parameter
to CapitalizedWords, causing it to be prepended to the output
(e.g. "-HelloWorld" instead of "Hello-World").

Added JoinCapitalizedWords type that properly inserts the joiner
between capitalized tokens, following the same pattern as
JoinLowercaseWords.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 21, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8dc110af-df4f-42ff-aa36-2859876e2eee

📥 Commits

Reviewing files that changed from the base of the PR and between 5bc6804 and 2cd15bb.

📒 Files selected for processing (1)
  • src/types.ts

📝 Walkthrough

Walkthrough

Updated the TrainCase type to correctly insert delimiters between capitalized tokens rather than using the delimiter as a prefix. Introduced a new internal JoinCapitalizedWords helper type to properly handle recursive joining of string segments with capitalization.

Changes

Cohort / File(s) Summary
Type System Enhancement
src/types.ts
Added JoinCapitalizedWords internal type for recursive token joining with proper delimiter placement. Updated TrainCase to use the new helper, fixing behavior where delimiters were incorrectly prefixed instead of interspersed between tokens.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Hops through the types with glee,
Delimiters now flow wild and free,
No more dashes leading the way,
Train-Case marches correctly today!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main fix: correcting TrainCase to insert the joiner between tokens rather than as a prefix.
Linked Issues check ✅ Passed The code changes directly address issue #110 by introducing JoinCapitalizedWords to insert joiners between tokens, aligning type-level behavior with runtime output.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the TrainCase type implementation in src/types.ts; no unrelated modifications are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can validate your CodeRabbit configuration file in your editor.

If your editor has YAML language server, you can enable auto-completion and validation by adding # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json at the top of your CodeRabbit configuration file.

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.

TrainCase type should insert joiners between capitalized tokens

1 participant