You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

encoder.js 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.encode = encode;
  6. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  7. function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); }
  8. function con(n) {
  9. return 0x80 | n & 0x3f;
  10. }
  11. function encode(str) {
  12. var arr = str.split("").map(function (x) {
  13. return x.charCodeAt(0);
  14. });
  15. return _encode(arr);
  16. }
  17. function _encode(arr) {
  18. if (arr.length === 0) {
  19. return [];
  20. }
  21. var _arr = _toArray(arr),
  22. n = _arr[0],
  23. ns = _arr.slice(1);
  24. if (n < 0) {
  25. throw new Error("utf8");
  26. }
  27. if (n < 0x80) {
  28. return [n].concat(_toConsumableArray(_encode(ns)));
  29. }
  30. if (n < 0x800) {
  31. return [0xc0 | n >>> 6, con(n)].concat(_toConsumableArray(_encode(ns)));
  32. }
  33. if (n < 0x10000) {
  34. return [0xe0 | n >>> 12, con(n >>> 6), con(n)].concat(_toConsumableArray(_encode(ns)));
  35. }
  36. if (n < 0x110000) {
  37. return [0xf0 | n >>> 18, con(n >>> 12), con(n >>> 6), con(n)].concat(_toConsumableArray(_encode(ns)));
  38. }
  39. throw new Error("utf8");
  40. }