encoding.js 832 B

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