Skip to content

Fix constant case losing idempotency around digits - #372

Closed
afonsojanu wants to merge 1 commit into
blakeembrey:mainfrom
afonsojanu:fix/constant-case-digit-idempotency
Closed

afonsojanu wants to merge 1 commit into
blakeembrey:mainfrom
afonsojanu:fix/constant-case-digit-idempotency

Conversation

@afonsojanu

Copy link
Copy Markdown

constantCase (and any of the other helpers that route through the same word
splitter, e.g. capitalCase, snakeCase, dotCase...) can lose its own
output the second time it runs.

constantCase("foo123bar");                    // "FOO123BAR"
constantCase(constantCase("foo123bar"));       // "FOO123_BAR"  <- extra "_"

constantCase("a1b2c3");                        // "A1B2C3"
constantCase(constantCase("a1b2c3"));          // "A1_B2_C3"    <- extra "_"s

The splitter's SPLIT_LOWER_UPPER_RE treats any digit directly followed by
an upper case letter as a word break, regardless of what's before the digit.
That's fine the first time (there's no upper case letter in foo123bar to
begin with), but once a word has been shouted into all caps, the same digit
is now followed by an upper case letter purely as an artifact of that
shouting, not because a new word actually started there, so re-splitting
inserts a bogus boundary.

I confirmed this against the reported repro and then fuzzed constantCase
against 200k random ASCII strings comparing f(x) to f(f(x)); the
unmodified splitter fails on roughly 4% of them, all following this same
digit pattern.

The fix keeps the "digit ends a word" rule, but only when the following
upper case letter is itself the start of a real word, i.e. followed by a
lower case letter (like the T in V1Test). An already-shouted run such as
FOO123BAR no longer matches, since the letter after the digit isn't
followed by a lower case letter anywhere nearby, it's followed by more
capitals.

With that change the same 200k-iteration fuzz run comes back with zero
constantCase mismatches, the full existing suite still passes (141 tests
across the monorepo, including the V1Test, TestV2 and Foo12019Bar
cases that specifically exercise digit/case boundaries), and I added a
regression test covering the two examples above.

constantCase() (and the other all-caps helpers built on the same
splitter) can produce output that splits differently the second time
it's run. A digit sitting between a lower case letter and an upper
case one was always treated as a word break, even when the "upper
case" side was really just the digit's own word being shouted by a
previous pass.

For example constantCase("foo123bar") correctly returns "FOO123BAR"
as one word, but running constantCase on that result again yields
"FOO123_BAR" because the regex now sees a digit directly followed by
an upper case letter and assumes a new word started there. Same story
for "a1b2c3" -> "A1B2C3" -> "A1_B2_C3".

The fix only treats a digit->upper transition as a genuine word
boundary when the upper case letter is itself followed by a lower
case letter, which is what happens for a real new word like the "T"
in "V1Test" but not for an already-shouted run like "FOO123BAR".
Verified against the existing suite (28/28 in the change-case package,
141/141 across the monorepo) and a 200k-iteration fuzz pass that found
zero remaining idempotency violations for constantCase.

Added a regression test covering both examples from the bug report.
@afonsojanu

Copy link
Copy Markdown
Author

Closing this for now as part of a clean slate — stepping back from everything I had open across these repos.

@afonsojanu afonsojanu closed this Oct 5, 2026
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