Function to encode JS string as UTF-8 Uint8Arrays

This commit is contained in:
Kovid Goyal 2015-11-13 13:44:09 +05:30
parent 85844fb0b9
commit fa90586f67

View File

@ -20,3 +20,16 @@ def debounce(func, wait, immediate=False):
timeout = window.setTimeout(later, wait)
if call_now:
func.apply(context, args)
def to_utf8(string):
if type(TextEncoder) == 'function':
return TextEncoder('utf-8').encode(string)
escstr = encodeURIComponent(string)
binstr = escstr.replace(/%([0-9A-F]{2})/g, def(match, p1):
return String.fromCharCode('0x' + p1)
)
ua = Uint8Array(binstr.length)
Array.prototype.forEach.call(binstr, def(ch, i):
ua[i] = ch.charCodeAt(0)
)
return ua