您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

helpers.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // @flow
  2. /**
  3. * Creates a deferred object.
  4. *
  5. * @returns {{promise, resolve, reject}}
  6. */
  7. export function createDeferred(): Object {
  8. const deferred = {};
  9. deferred.promise = new Promise((resolve, reject) => {
  10. deferred.resolve = resolve;
  11. deferred.reject = reject;
  12. });
  13. return deferred;
  14. }
  15. const MATCH_OPERATOR_REGEXP = /[|\\{}()[\]^$+*?.-]/g;
  16. /**
  17. * Escape RegExp special characters.
  18. *
  19. * Based on https://github.com/sindresorhus/escape-string-regexp.
  20. *
  21. * @param {string} s - The regexp string to escape.
  22. * @returns {string}
  23. */
  24. export function escapeRegexp(s: string) {
  25. if (typeof s !== 'string') {
  26. throw new TypeError('Expected a string');
  27. }
  28. return s.replace(MATCH_OPERATOR_REGEXP, '\\$&');
  29. }
  30. /**
  31. * Returns the base URL of the app.
  32. *
  33. * @param {Object} w - Window object to use instead of the built in one.
  34. * @returns {string}
  35. */
  36. export function getBaseUrl(w: Object = window) {
  37. const doc = w.document;
  38. const base = doc.querySelector('base');
  39. if (base && base.href) {
  40. return base.href;
  41. }
  42. const { protocol, host } = w.location;
  43. return `${protocol}//${host}`;
  44. }
  45. /**
  46. * Returns the namespace for all global variables, functions, etc that we need.
  47. *
  48. * @returns {Object} The namespace.
  49. *
  50. * NOTE: After React-ifying everything this should be the only global.
  51. */
  52. export function getJitsiMeetGlobalNS() {
  53. if (!window.JitsiMeetJS) {
  54. window.JitsiMeetJS = {};
  55. }
  56. if (!window.JitsiMeetJS.app) {
  57. window.JitsiMeetJS.app = {};
  58. }
  59. return window.JitsiMeetJS.app;
  60. }
  61. /**
  62. * A helper function that behaves similar to Object.assign, but only reassigns a
  63. * property in target if it's defined in source.
  64. *
  65. * @param {Object} target - The target object to assign the values into.
  66. * @param {Object} source - The source object.
  67. * @returns {Object}
  68. */
  69. export function assignIfDefined(target: Object, source: Object) {
  70. const to = Object(target);
  71. for (const nextKey in source) {
  72. if (source.hasOwnProperty(nextKey)) {
  73. const value = source[nextKey];
  74. if (typeof value !== 'undefined') {
  75. to[nextKey] = value;
  76. }
  77. }
  78. }
  79. return to;
  80. }
  81. /**
  82. * Prints the error and reports it to the global error handler.
  83. *
  84. * @param {Error} e - The error object.
  85. * @param {string} msg - A custom message to print in addition to the error.
  86. * @returns {void}
  87. */
  88. export function reportError(e: Object, msg: string = '') {
  89. console.error(msg, e);
  90. window.onerror && window.onerror(msg, null, null, null, e);
  91. }