JavaScript has no concept of character encoding for String, everything is in UTF-16. Most of time time the value of a char
in UTF-16 matches UTF-8, so you can forget it's any different.
There are more optimal ways to do this but
function s(x) {return x.charCodeAt(0);}
"test.message".split('').map(s);
// [116, 101, 115, 116, 46, 109, 101, 115, 115, 97, 103, 101]
So what is unescape(encodeURIComponent(str))
doing? Let's look at each individually,
encodeURIComponent
is converting every character instr
which is illegal or has a meaning in URI Syntax into a URI escaped version so that there is no problem using it as a key or value in the search component of a URI, for exampleencodeURIComponent('&='); // "%26%3D"
Notice how this is now a 6 character long String.unescape
is actually depreciated, but it does a similar job todecodeURI
ordecodeURIComponent
(the reverse ofencodeURIComponent
). If we look in the ES5 spec we can see11. Let c be the character whose code unit value is the integer represented by the four hexadecimal digits at positions k+2, k+3, k+4, and k+5 within Result(1).
So,4
digits is2
bytes is "UTF-8", however as I mentioned, all Strings are UTF-16, so it's really a UTF-16 string limiting itself to UTF-8.