uniapp,h5

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.pick = pick;
  4. exports.installTimerFunctions = installTimerFunctions;
  5. exports.byteLength = byteLength;
  6. exports.randomString = randomString;
  7. const globals_node_js_1 = require("./globals.node.js");
  8. function pick(obj, ...attr) {
  9. return attr.reduce((acc, k) => {
  10. if (obj.hasOwnProperty(k)) {
  11. acc[k] = obj[k];
  12. }
  13. return acc;
  14. }, {});
  15. }
  16. // Keep a reference to the real timeout functions so they can be used when overridden
  17. const NATIVE_SET_TIMEOUT = globals_node_js_1.globalThisShim.setTimeout;
  18. const NATIVE_CLEAR_TIMEOUT = globals_node_js_1.globalThisShim.clearTimeout;
  19. function installTimerFunctions(obj, opts) {
  20. if (opts.useNativeTimers) {
  21. obj.setTimeoutFn = NATIVE_SET_TIMEOUT.bind(globals_node_js_1.globalThisShim);
  22. obj.clearTimeoutFn = NATIVE_CLEAR_TIMEOUT.bind(globals_node_js_1.globalThisShim);
  23. }
  24. else {
  25. obj.setTimeoutFn = globals_node_js_1.globalThisShim.setTimeout.bind(globals_node_js_1.globalThisShim);
  26. obj.clearTimeoutFn = globals_node_js_1.globalThisShim.clearTimeout.bind(globals_node_js_1.globalThisShim);
  27. }
  28. }
  29. // base64 encoded buffers are about 33% bigger (https://en.wikipedia.org/wiki/Base64)
  30. const BASE64_OVERHEAD = 1.33;
  31. // we could also have used `new Blob([obj]).size`, but it isn't supported in IE9
  32. function byteLength(obj) {
  33. if (typeof obj === "string") {
  34. return utf8Length(obj);
  35. }
  36. // arraybuffer or blob
  37. return Math.ceil((obj.byteLength || obj.size) * BASE64_OVERHEAD);
  38. }
  39. function utf8Length(str) {
  40. let c = 0, length = 0;
  41. for (let i = 0, l = str.length; i < l; i++) {
  42. c = str.charCodeAt(i);
  43. if (c < 0x80) {
  44. length += 1;
  45. }
  46. else if (c < 0x800) {
  47. length += 2;
  48. }
  49. else if (c < 0xd800 || c >= 0xe000) {
  50. length += 3;
  51. }
  52. else {
  53. i++;
  54. length += 4;
  55. }
  56. }
  57. return length;
  58. }
  59. /**
  60. * Generates a random 8-characters string.
  61. */
  62. function randomString() {
  63. return (Date.now().toString(36).substring(3) +
  64. Math.random().toString(36).substring(2, 5));
  65. }