-
Notifications
You must be signed in to change notification settings - Fork 3
Description
Description
In the latest version of stringr, the capitalize() extension behaves unexpectedly when the input string is already in full uppercase.
Example:
"HELLO".capitalize()Current output:
HELLO
Expected output:
Hello
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() // HelloWhy 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 👍