encoding.js 632 B

1234567891011121314151617181920212223242526
  1. "use strict";
  2. let { TextEncoder, TextDecoder } = require("util");
  3. // Handle browserify's lack of support (https://github.com/browserify/node-util/issues/46), which
  4. // is important for the live viewer:
  5. if (!TextEncoder) {
  6. TextEncoder = global.TextEncoder;
  7. }
  8. if (!TextDecoder) {
  9. TextDecoder = global.TextDecoder;
  10. }
  11. const utf8Encoder = new TextEncoder();
  12. const utf8Decoder = new TextDecoder("utf-8", { ignoreBOM: true });
  13. function utf8Encode(string) {
  14. return utf8Encoder.encode(string);
  15. }
  16. function utf8DecodeWithoutBOM(bytes) {
  17. return utf8Decoder.decode(bytes);
  18. }
  19. module.exports = {
  20. utf8Encode,
  21. utf8DecodeWithoutBOM
  22. };