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

helpers.js 2.6KB

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