Description
In the latest version of stringr, the capitalize() extension behaves unexpectedly when the input string is already in full uppercase.
Example:
Current output:
Expected output:
The method currently uppercases the first character but leaves the rest of the string unchanged. This causes fully-uppercase inputs to remain fully uppercase, which defeats the purpose of a typical capitalize operation (first letter uppercase, remaining letters lowercase).
Root cause
In case.dart, the current implementation is:
false => isEmpty ? '' : this[0].toUpperCase() + substring(1),
Here, substring(1) preserves the original casing of the remainder of the string.
Proposed fix
Lowercase the remainder explicitly:
false => isEmpty
? ''
: this[0].toUpperCase() + substring(1).toLowerCase(),
This produces consistent behavior:
"HELLO".capitalize() // Hello
"hello".capitalize() // Hello
"HeLLo".capitalize() // Hello
Why this matters
Most capitalize implementations across ecosystems normalize the string to:
Uppercase first letter + lowercase remainder
Current behavior is surprising and inconsistent, especially when working with API responses or legacy data that may arrive in all caps.
Happy to submit a PR if this approach is acceptable 👍
Description
In the latest version of
stringr, thecapitalize()extension behaves unexpectedly when the input string is already in full uppercase.Example:
Current output:
Expected output:
The method currently uppercases the first character but leaves the rest of the string unchanged. This causes fully-uppercase inputs to remain fully uppercase, which defeats the purpose of a typical
capitalizeoperation (first letter uppercase, remaining letters lowercase).Root cause
In
case.dart, the current implementation is:Here,
substring(1)preserves the original casing of the remainder of the string.Proposed fix
Lowercase the remainder explicitly:
This produces consistent behavior:
Why this matters
Most
capitalizeimplementations across ecosystems normalize the string to:Current behavior is surprising and inconsistent, especially when working with API responses or legacy data that may arrive in all caps.
Happy to submit a PR if this approach is acceptable 👍