The two café titles now produce the same branch name, whichever way the Lisbon office typed the accent. One complaint from the team lead’s list is still open. The Git host rejects any branch name longer than 32 bytes of UTF-8, and the JavaScript helper checks that limit with the wrong count. So far, only luck has kept the Tokyo name under the limit.
The team lead’s Python version encoded the name first and measured the result. In this lesson, you’ll do the same in JavaScript.
Encode a String With TextEncoder
In Python, encoding is a method on the string. You call .encode() and get a bytes object back, which list() shows as plain integers:
>>> "Zoë".encode()
b'Zo\xc3\xab'
>>> list("Zoë".encode())
[90, 111, 195, 171]
JavaScript strings have no .encode() method. Encoding lives in a separate object, a TextEncoder, which you create with new, the same way you’d call a class in Python to get an instance. Its .encode() method takes the string and returns the bytes:
const encoder = new TextEncoder();
const bytes = encoder.encode("Zoë");
console.log(bytes);