utf8-utils.ts 720 B

123456789101112131415161718
  1. // breaking up those two types in order to clarify what is happening in the decoding path.
  2. type DecodedFrame<T> = { key: string; data: T; info?: any };
  3. export type Frame = DecodedFrame<ArrayBuffer | string>;
  4. // http://stackoverflow.com/questions/8936984/uint8array-to-string-in-javascript/22373197
  5. // http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt
  6. /* utf.js - UTF-8 <=> UTF-16 convertion
  7. *
  8. * Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
  9. * Version: 1.0
  10. * LastModified: Dec 25 1999
  11. * This library is free. You can redistribute it and/or modify it.
  12. */
  13. export function strToUtf8array(str: string) {
  14. return Uint8Array.from(unescape(encodeURIComponent(str)), (c) =>
  15. c.charCodeAt(0),
  16. );
  17. }