As we know the TextEncoder will drop support utf-16 and other legacy encoding types from Mozilla. Google also confirmed it and Chrome will only remain support utf-16 encoding.
Here is a polyfill implementation for convert utf-8 string to utf-16le
- function utf8ToUtf16(s) {
- var a = new Uint8Array(s.length * 2),
- view = new DataView(a.buffer);
- s.split('').forEach(function (c, i) {
- //the third parameter equal to true indicate little ending
- view.setUint16(i * 2, c.charCodeAt(0), true);
- });
- return a;
- }
Reference:
https://bugzilla.mozilla.org/show_bug.cgi?id=1257877
https://www.chromestatus.com/feature/5630760492990464