encoder.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.encode = encode;
  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(n) {
  16. return 0x80 | n & 0x3f;
  17. }
  18. function encode(str) {
  19. var arr = str.split("").map(function (x) {
  20. return x.charCodeAt(0);
  21. });
  22. return _encode(arr);
  23. }
  24. function _encode(arr) {
  25. if (arr.length === 0) {
  26. return [];
  27. }
  28. var _arr = _toArray(arr),
  29. n = _arr[0],
  30. ns = _arr.slice(1);
  31. if (n < 0) {
  32. throw new Error("utf8");
  33. }
  34. if (n < 0x80) {
  35. return [n].concat(_toConsumableArray(_encode(ns)));
  36. }
  37. if (n < 0x800) {
  38. return [0xc0 | n >>> 6, con(n)].concat(_toConsumableArray(_encode(ns)));
  39. }
  40. if (n < 0x10000) {
  41. return [0xe0 | n >>> 12, con(n >>> 6), con(n)].concat(_toConsumableArray(_encode(ns)));
  42. }
  43. if (n < 0x110000) {
  44. return [0xf0 | n >>> 18, con(n >>> 12), con(n >>> 6), con(n)].concat(_toConsumableArray(_encode(ns)));
  45. }
  46. throw new Error("utf8");
  47. }