You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // @flow
  2. import { parseStandardURIString } from '../base/util';
  3. declare var interfaceConfig: Object;
  4. /**
  5. * Used for web. Indicates if the setting section is enabled.
  6. *
  7. * @param {string} settingName - The name of the setting section as defined in
  8. * interface_config.js and SettingsMenu.js.
  9. * @returns {boolean} True to indicate that the given setting section
  10. * is enabled, false otherwise.
  11. */
  12. export function isSettingEnabled(settingName: string) {
  13. return interfaceConfig.SETTINGS_SECTIONS.includes(settingName);
  14. }
  15. /**
  16. * Normalizes a URL entered by the user.
  17. * FIXME: Consider adding this to base/util/uri.
  18. *
  19. * @param {string} url - The URL to validate.
  20. * @returns {string|null} - The normalized URL, or null if the URL is invalid.
  21. */
  22. export function normalizeUserInputURL(url: string) {
  23. /* eslint-disable no-param-reassign */
  24. if (url) {
  25. url = url.replace(/\s/g, '').toLowerCase();
  26. const urlRegExp = new RegExp('^(\\w+://)?(.+)$');
  27. const urlComponents = urlRegExp.exec(url);
  28. if (!urlComponents[1] || !urlComponents[1].startsWith('http')) {
  29. url = `https://${urlComponents[2]}`;
  30. }
  31. const parsedURI = parseStandardURIString(url);
  32. if (!parsedURI.host) {
  33. return null;
  34. }
  35. return parsedURI.toString();
  36. }
  37. return url;
  38. /* eslint-enable no-param-reassign */
  39. }
  40. /**
  41. * Used for web. Returns whether or not only Device Selection is configured to
  42. * display as a setting.
  43. *
  44. * @returns {boolean}
  45. */
  46. export function shouldShowOnlyDeviceSelection() {
  47. return interfaceConfig.SETTINGS_SECTIONS.length === 1
  48. && isSettingEnabled('devices');
  49. }