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
Stefan Judis | Sciencx (2017-04-16T22:00:00+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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.