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 939B

12345678910111213141516171819202122232425262728293031323334353637
  1. // @flow
  2. import { parseStandardURIString } from '../base/util';
  3. /**
  4. * Normalizes a URL entered by the user.
  5. * FIXME: Consider adding this to base/util/uri.
  6. *
  7. * @param {string} url - The URL to validate.
  8. * @returns {string|null} - The normalized URL, or null if the URL is invalid.
  9. */
  10. export function normalizeUserInputURL(url: string) {
  11. /* eslint-disable no-param-reassign */
  12. if (url) {
  13. url = url.replace(/\s/g, '').toLowerCase();
  14. const urlRegExp = new RegExp('^(\\w+://)?(.+)$');
  15. const urlComponents = urlRegExp.exec(url);
  16. if (!urlComponents[1] || !urlComponents[1].startsWith('http')) {
  17. url = `https://${urlComponents[2]}`;
  18. }
  19. const parsedURI
  20. = parseStandardURIString(url);
  21. if (!parsedURI.host) {
  22. return null;
  23. }
  24. return parsedURI.toString();
  25. }
  26. return url;
  27. /* eslint-enable no-param-reassign */
  28. }