Normalizing and Segmenting Text

Your truncation now keeps 😀 whole because it counts code points instead of UTF-16 code units. Two reports from the Lisbon office are still open. Ticket PAY-2402 was filed twice as “café receipts”, once through the tracker’s own form and once typed in another tool that spells accents differently, and the two copies produce different branch names. A second ticket shows a family emoji that the team’s title preview counts as five characters.

A string can hold the same visible text in more than one way, and a code point isn’t always what a person calls a character. In this lesson, you’ll make titles that look the same compare equal and count characters the way a reader sees them. Start with the two copies of PAY-2402.

Compare Titles That Look the Same

Ported without the Python script’s normalization step, the check is a strict comparison. The second string spells the typed copy’s accent with the \u0301 escape, which works the same way in JavaScript and Python:

Language: JavaScript
const stored = "café receipts";
const typed = "cafe\u0301 receipts";
console.log(stored, typed);
console.log(stored === typed, stored.length, typed.length);

Predict how the two titles print, what === says, and how long each one is. Then run the file:

Language: Program Output
café receipts café receipts
false 13 14

Both print as café receipts, yet === says they differ, and the typed one is a code unit longer. Logging code points shows why:

Language: JavaScript
console.log("é".codePointAt(0));
for (const char of "cafe\u0301") {
  console.log(char.codePointAt(0));
}

The stored é is one code point, and the typed word ends in two:

Language: Program Output
233
99
97
102
101
769

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.