Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

helpers.js 2.5KB

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