DEV Community

zhihu wu
zhihu wu

Posted on

Case Conversion Is Not a Round Trip: ß, İ, and Strings That Change Length

Sooner or later a user hands your case converter a name that breaks it. Not a weird name — a normal German or Turkish one. Here are five things I reproduced in Node 22 while building a case-conversion tool, all from one console session.

1. Upper-casing can change the length

'ß'.toUpperCase()        // 'SS'   (1 char -> 2)
'straße'.toUpperCase()   // 'STRASSE' (6 -> 7)
'fi'.toUpperCase()        // 'FI'
Enter fullscreen mode Exit fullscreen mode

Any pipeline that upper-cases before writing to a fixed-width column, a VARCHAR(6), or a slug budget can truncate silently. The mapped output is not guaranteed to fit the input's width.

2. Turkish dotted/dotless I defeats the default mapping

'I'.toLocaleLowerCase('tr')  // 'ı'  U+0131 (dotless)
'I'.toLowerCase()            // 'i'
'i'.toLocaleUpperCase('tr')  // 'İ'
Enter fullscreen mode Exit fullscreen mode

A login form that lower-cases a username with the default locale produces a different string than the user typed, and the lookup misses. Pass the locale explicitly — or don't case-fold identities at all.

3. Lower-casing İ produces two code points

[...'İ'.toLowerCase()].map(c => c.codePointAt(0).toString(16))
// ['69', '307']  -> 'i' + COMBINING DOT ABOVE
'İ'.toLowerCase() === 'i'   // false
'İSTANBUL'.toLowerCase().length  // 9, not 8
Enter fullscreen mode Exit fullscreen mode

Enough to overflow a column sized for eight characters, and enough to fail an exact comparison against the same city typed on another keyboard.

4. Greek sigma is contextual

'ΟΔΟΣ'.toLowerCase() returns 'οδος' ending in ς (U+03C2), not σ. Per-character mapping and whole-string mapping disagree, so a hand-rolled loop over characters gives different output than the engine's built-in.

5. It is a collision, not just an inequality

'straße'.toUpperCase() === 'STRASSE'.toUpperCase()  // true
'ß'.toUpperCase().toLowerCase()                     // 'ss'
Enter fullscreen mode Exit fullscreen mode

Two distinct names fold to one key. A case-insensitive unique index or a Map keyed on the upper-cased form merges two real users — and the second line shows you cannot reconstruct the original from the folded form. The mapping is many-to-one and lossy.

What to do instead

  • Store the original string, and derive a separate folded key for lookups (NFC + case fold, or toLocaleLowerCase with an explicit locale) instead of overwriting the field.
  • Compare with a collator — new Intl.Collator(undefined, { sensitivity: 'accent' }) — rather than string equality on a folded value.
  • Never size a column from the upper-cased form. Measure the mapped output, and remember .length counts UTF-16 units, not characters.

I keep a case converter pinned for exactly this: paste the string, look at all nine casings at once (UPPER, lower, Title, Sentence, camel, Pascal, snake, kebab, CONSTANT), and check whether the round trip survives before it turns into a migration. It runs entirely in the browser, so no string leaves the tab.

Case conversion looks like a one-liner. It is a Unicode mapping with exceptions — and the exceptions are people's names.

Top comments (0)