fix(titleCase): extra spaces when input already contains spaces#122
fix(titleCase): extra spaces when input already contains spaces#122guoyangzhen wants to merge 1 commit intounjs:mainfrom
Conversation
The splitByCase function did not recognize space as a separator,
causing 'Hello World' to be split as ['Hello ', 'World'] with the
space trailing the first token. When titleCase joined with spaces,
this produced double spaces ('Hello World').
Fix: add space to STR_SPLITTERS so splitByCase properly splits on
spaces. This also makes titleCase idempotent and fixes other case
functions (camelCase, kebabCase, etc.) to handle space-separated
input correctly.
Fixes unjs#96
📝 WalkthroughWalkthroughThis PR fixes a bug in the Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
test/scule.test.ts (1)
136-149: Add an explicit idempotency assertion fortitleCase.This suite now covers input variants well; adding one direct nested-call check would lock the issue objective more explicitly.
Suggested test addition
describe("titleCase", () => { test.each([ ["", ""], ["f", "F"], ["foo", "Foo"], ["foo-bar", "Foo Bar"], ["this-IS-aTitle", "This is a Title"], ["Hello World", "Hello World"], ["hello world", "Hello World"], ["Hello World", "Hello World"], ])("%s => %s", (input, expected) => { expect(titleCase(input)).toMatchObject(expected); }); + + test("is idempotent", () => { + expect(titleCase(titleCase("hello world"))).toBe("Hello World"); + }); });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/scule.test.ts` around lines 136 - 149, Add an idempotency assertion to the titleCase test: for each (input, expected) case in the describe("titleCase") table, after asserting expect(titleCase(input)).toMatchObject(expected), also assert that calling titleCase again returns the same value (e.g. expect(titleCase(titleCase(input))).toMatchObject(titleCase(input)) or toMatchObject(expected)). Update the test block that references titleCase to include this nested-call check so the function is verified to be idempotent.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@test/scule.test.ts`:
- Around line 136-149: Add an idempotency assertion to the titleCase test: for
each (input, expected) case in the describe("titleCase") table, after asserting
expect(titleCase(input)).toMatchObject(expected), also assert that calling
titleCase again returns the same value (e.g.
expect(titleCase(titleCase(input))).toMatchObject(titleCase(input)) or
toMatchObject(expected)). Update the test block that references titleCase to
include this nested-call check so the function is verified to be idempotent.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 0699c0e2-3c39-41f8-ab14-06093d85bef6
📒 Files selected for processing (2)
src/index.tstest/scule.test.ts
Fixes #96
Problem
titleCase('Hello World')produces'Hello World'(double space). CallingtitleCase(titleCase('hello'))also produces double spaces becausesplitByCasedoes not recognize space as a separator.Root cause:
splitByCaseusesSTR_SPLITTERS = ['-', '_', '/', '.']but does not include space. When processing'Hello World', the falling-edge detection at the space character (uppercase W → space) splits the string incorrectly, attaching the space to the preceding token:['Hello ', 'World'].Fix
Added space to
STR_SPLITTERS. NowsplitByCase('Hello World')correctly returns['Hello', 'World'], andtitleCasejoins them with a single space.This also improves
camelCase,kebabCase,snakeCase, etc. for space-separated input.Tests
Hello World,hello world,Hello WorldtitleCaseis now idempotent:titleCase(titleCase('hello')) === 'Hello'Summary by CodeRabbit
Bug Fixes
Tests