decoder.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.decode = decode;
  6. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  7. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  8. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  9. function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); }
  10. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  11. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  12. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  13. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
  14. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  15. function con(b) {
  16. if ((b & 0xc0) === 0x80) {
  17. return b & 0x3f;
  18. } else {
  19. throw new Error("invalid UTF-8 encoding");
  20. }
  21. }
  22. function code(min, n) {
  23. if (n < min || 0xd800 <= n && n < 0xe000 || n >= 0x10000) {
  24. throw new Error("invalid UTF-8 encoding");
  25. } else {
  26. return n;
  27. }
  28. }
  29. function decode(bytes) {
  30. return _decode(bytes).map(function (x) {
  31. return String.fromCharCode(x);
  32. }).join("");
  33. }
  34. function _decode(bytes) {
  35. if (bytes.length === 0) {
  36. return [];
  37. }
  38. /**
  39. * 1 byte
  40. */
  41. {
  42. var _bytes = _toArray(bytes),
  43. b1 = _bytes[0],
  44. bs = _bytes.slice(1);
  45. if (b1 < 0x80) {
  46. return [code(0x0, b1)].concat(_toConsumableArray(_decode(bs)));
  47. }
  48. if (b1 < 0xc0) {
  49. throw new Error("invalid UTF-8 encoding");
  50. }
  51. }
  52. /**
  53. * 2 bytes
  54. */
  55. {
  56. var _bytes2 = _toArray(bytes),
  57. _b = _bytes2[0],
  58. b2 = _bytes2[1],
  59. _bs = _bytes2.slice(2);
  60. if (_b < 0xe0) {
  61. return [code(0x80, ((_b & 0x1f) << 6) + con(b2))].concat(_toConsumableArray(_decode(_bs)));
  62. }
  63. }
  64. /**
  65. * 3 bytes
  66. */
  67. {
  68. var _bytes3 = _toArray(bytes),
  69. _b2 = _bytes3[0],
  70. _b3 = _bytes3[1],
  71. b3 = _bytes3[2],
  72. _bs2 = _bytes3.slice(3);
  73. if (_b2 < 0xf0) {
  74. return [code(0x800, ((_b2 & 0x0f) << 12) + (con(_b3) << 6) + con(b3))].concat(_toConsumableArray(_decode(_bs2)));
  75. }
  76. }
  77. /**
  78. * 4 bytes
  79. */
  80. {
  81. var _bytes4 = _toArray(bytes),
  82. _b4 = _bytes4[0],
  83. _b5 = _bytes4[1],
  84. _b6 = _bytes4[2],
  85. b4 = _bytes4[3],
  86. _bs3 = _bytes4.slice(4);
  87. if (_b4 < 0xf8) {
  88. return [code(0x10000, (((_b4 & 0x07) << 18) + con(_b5) << 12) + (con(_b6) << 6) + con(b4))].concat(_toConsumableArray(_decode(_bs3)));
  89. }
  90. }
  91. throw new Error("invalid UTF-8 encoding");
  92. }