encoding.js 760 B

12345678910111213141516171819202122232425262728
  1. // A small collection of encodings for convenience of use in the browser.
  2. const decoder = new TextDecoder();
  3. const encoders = {
  4. // certainly not the fastest, but hashes are pretty small
  5. base64: data => btoa(String.fromCharCode(...data)),
  6. hex: data => {
  7. let out = '';
  8. for (const byte of data) {
  9. if (byte < 0x10) {
  10. out += '0';
  11. }
  12. out += byte.toString(16);
  13. }
  14. return out;
  15. },
  16. utf8: data => decoder.decode(data),
  17. };
  18. /**
  19. * @hidden
  20. */
  21. export const mustGetEncoder = (encoding) => {
  22. const encoder = encoders[encoding];
  23. if (!encoder) {
  24. throw new Error(`Unknown encoding ${encoding}`);
  25. }
  26. return encoder;
  27. };
  28. //# sourceMappingURL=encoding.js.map