FWIW, Checking code or reading API docs is pretty useful :P
firebug works pretty well for testing out code snippets.
The internet is full of enough incorrect information regarding javascript as it is, and if you think about it, % has to be encoded, otherwise anything containing a % would be decoded wrongly ;)
This script is unnecessary, as JavaScript already has functions which can facillitate encoding and decoding of UTF-8. Encoding UTF-8 to a bytestring is unescape(encodeURIComponent(string)) and decoding UTF-8 is decodeURIComponent(escape(string)).
That is done in Buffer.toString(), this stream filter just makes sure that no string conversion is attempted on a multibyte character that is still incomplete (valid or not).
I didn't stick around after reading the title when the youtube page loaded, however I'm pretty sure it was Rick Astley singing Never Gonna Give You Up.
14 comments
[ 2.2 ms ] story [ 39.2 ms ] threadvar utf8_length = encodeURIComponent(data).replace(/%../g, 'x').length;
% signs are handled fine - they get encoded into %25 which then gets shortened to a single 'x' character - thus counted as a single byte.
Similarly, for example € gets encoded to "%E2%82%AC" which then gets correctly counted as 3 bytes.
I'm not quite sure what value of 'broken' you're thinking of? Did you try this and find it didn't work in some particular circumstance?
firebug works pretty well for testing out code snippets.
The internet is full of enough incorrect information regarding javascript as it is, and if you think about it, % has to be encoded, otherwise anything containing a % would be decoded wrongly ;)