22 points · by felixge · 16y ago · 13 comments
var utf8_length = encodeURIComponent(data).replace(/%../g, 'x').length;
var len = Buffer.byteLength(a_string, 'utf8')% 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?