String.prototype.normalize for safer string comparison (#tilPost)

Today I came a across the String.prototype.normalize function which I haven’t used before. It exists to make string comparisons more reliable.
Let’s me show you a quick example:
// pick a random word with a German Umlaut
const word …


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Today I came a across the String.prototype.normalize function which I haven't used before. It exists to make string comparisons more reliable.

Let's me show you a quick example:

// pick a random word with a German Umlaut
const word = 'über';         // displayed as 'über'
console.log( word.length );  // 4

const alikeWord = 'u\u0308ber';  // displayed as 'über'
console.log( alikeWord.length ); // 5

console.log( word === alikeWord ); // false

As you see strings that look completely identical to us doesn't have to be the same internally. The string alikeWord makes use of a Combining Diacritical Mark to generate the German Umlaut ü – to be specific it uses COMBINING DIAERESIS. The thing is that ü also has its own codepoint in Unicode. Now we have two ways to display this glyph which makes comparison a bit tricky.

To solve this issue we can use normalize to well... yeah normalize strings. ;)

const word = 'über';         // displayed as 'über'
console.log( word.length );  // 4

const alikeWord = 'u\u0308ber'.normalize(); // displayed as 'über'
console.log( alikeWord.length );            // 4

console.log( word === alikeWord ); // true

Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate
APA
Stefan Judis | Sciencx (2022-12-01T20:30:35+00:00) » String.prototype.normalize for safer string comparison (#tilPost). Retrieved from https://www.scien.cx/2017/04/16/string-prototype-normalize-for-safer-string-comparison-tilpost/.
MLA
" » String.prototype.normalize for safer string comparison (#tilPost)." Stefan Judis | Sciencx - Sunday April 16, 2017, https://www.scien.cx/2017/04/16/string-prototype-normalize-for-safer-string-comparison-tilpost/
HARVARD
Stefan Judis | Sciencx Sunday April 16, 2017 » String.prototype.normalize for safer string comparison (#tilPost)., viewed 2022-12-01T20:30:35+00:00,<https://www.scien.cx/2017/04/16/string-prototype-normalize-for-safer-string-comparison-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » String.prototype.normalize for safer string comparison (#tilPost). [Internet]. [Accessed 2022-12-01T20:30:35+00:00]. Available from: https://www.scien.cx/2017/04/16/string-prototype-normalize-for-safer-string-comparison-tilpost/
CHICAGO
" » String.prototype.normalize for safer string comparison (#tilPost)." Stefan Judis | Sciencx - Accessed 2022-12-01T20:30:35+00:00. https://www.scien.cx/2017/04/16/string-prototype-normalize-for-safer-string-comparison-tilpost/
IEEE
" » String.prototype.normalize for safer string comparison (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2017/04/16/string-prototype-normalize-for-safer-string-comparison-tilpost/. [Accessed: 2022-12-01T20:30:35+00:00]
rf:citation
» String.prototype.normalize for safer string comparison (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2017/04/16/string-prototype-normalize-for-safer-string-comparison-tilpost/ | 2022-12-01T20:30:35+00:00
https://github.com/addpipe/simple-recorderjs-demo