Util.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.toCodePoints = function (str) {
  4. var codePoints = [];
  5. var i = 0;
  6. var length = str.length;
  7. while (i < length) {
  8. var value = str.charCodeAt(i++);
  9. if (value >= 0xd800 && value <= 0xdbff && i < length) {
  10. var extra = str.charCodeAt(i++);
  11. if ((extra & 0xfc00) === 0xdc00) {
  12. codePoints.push(((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000);
  13. }
  14. else {
  15. codePoints.push(value);
  16. i--;
  17. }
  18. }
  19. else {
  20. codePoints.push(value);
  21. }
  22. }
  23. return codePoints;
  24. };
  25. exports.fromCodePoint = function () {
  26. var codePoints = [];
  27. for (var _i = 0; _i < arguments.length; _i++) {
  28. codePoints[_i] = arguments[_i];
  29. }
  30. if (String.fromCodePoint) {
  31. return String.fromCodePoint.apply(String, codePoints);
  32. }
  33. var length = codePoints.length;
  34. if (!length) {
  35. return '';
  36. }
  37. var codeUnits = [];
  38. var index = -1;
  39. var result = '';
  40. while (++index < length) {
  41. var codePoint = codePoints[index];
  42. if (codePoint <= 0xffff) {
  43. codeUnits.push(codePoint);
  44. }
  45. else {
  46. codePoint -= 0x10000;
  47. codeUnits.push((codePoint >> 10) + 0xd800, codePoint % 0x400 + 0xdc00);
  48. }
  49. if (index + 1 === length || codeUnits.length > 0x4000) {
  50. result += String.fromCharCode.apply(String, codeUnits);
  51. codeUnits.length = 0;
  52. }
  53. }
  54. return result;
  55. };
  56. var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  57. // Use a lookup table to find the index.
  58. var lookup = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
  59. for (var i = 0; i < chars.length; i++) {
  60. lookup[chars.charCodeAt(i)] = i;
  61. }
  62. exports.decode = function (base64) {
  63. var bufferLength = base64.length * 0.75, len = base64.length, i, p = 0, encoded1, encoded2, encoded3, encoded4;
  64. if (base64[base64.length - 1] === '=') {
  65. bufferLength--;
  66. if (base64[base64.length - 2] === '=') {
  67. bufferLength--;
  68. }
  69. }
  70. var buffer = typeof ArrayBuffer !== 'undefined' &&
  71. typeof Uint8Array !== 'undefined' &&
  72. typeof Uint8Array.prototype.slice !== 'undefined'
  73. ? new ArrayBuffer(bufferLength)
  74. : new Array(bufferLength);
  75. var bytes = Array.isArray(buffer) ? buffer : new Uint8Array(buffer);
  76. for (i = 0; i < len; i += 4) {
  77. encoded1 = lookup[base64.charCodeAt(i)];
  78. encoded2 = lookup[base64.charCodeAt(i + 1)];
  79. encoded3 = lookup[base64.charCodeAt(i + 2)];
  80. encoded4 = lookup[base64.charCodeAt(i + 3)];
  81. bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
  82. bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
  83. bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
  84. }
  85. return buffer;
  86. };
  87. exports.polyUint16Array = function (buffer) {
  88. var length = buffer.length;
  89. var bytes = [];
  90. for (var i = 0; i < length; i += 2) {
  91. bytes.push((buffer[i + 1] << 8) | buffer[i]);
  92. }
  93. return bytes;
  94. };
  95. exports.polyUint32Array = function (buffer) {
  96. var length = buffer.length;
  97. var bytes = [];
  98. for (var i = 0; i < length; i += 4) {
  99. bytes.push((buffer[i + 3] << 24) | (buffer[i + 2] << 16) | (buffer[i + 1] << 8) | buffer[i]);
  100. }
  101. return bytes;
  102. };
  103. //# sourceMappingURL=Util.js.map