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.

utils.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* @flow */
  2. /**
  3. * Gets a {@link URL} without hash and query/search params from a specific
  4. * {@code URL}.
  5. *
  6. * @param {URL} url - The {@code URL} which may have hash and query/search
  7. * params.
  8. * @returns {URL}
  9. */
  10. export function getURLWithoutParams(url: URL): URL {
  11. const { hash, search } = url;
  12. if ((hash && hash.length > 1) || (search && search.length > 1)) {
  13. url = new URL(url.href); // eslint-disable-line no-param-reassign
  14. url.hash = '';
  15. url.search = '';
  16. // XXX The implementation of URL at least on React Native appends ? and
  17. // # at the end of the href which is not desired.
  18. let { href } = url;
  19. if (href) {
  20. href.endsWith('#') && (href = href.substring(0, href.length - 1));
  21. href.endsWith('?') && (href = href.substring(0, href.length - 1));
  22. // eslint-disable-next-line no-param-reassign
  23. url.href === href || (url = new URL(href));
  24. }
  25. }
  26. return url;
  27. }
  28. /**
  29. * Gets a URL string without hash and query/search params from a specific
  30. * {@code URL}.
  31. *
  32. * @param {URL} url - The {@code URL} which may have hash and query/search
  33. * params.
  34. * @returns {string}
  35. */
  36. export function getURLWithoutParamsNormalized(url: URL): string {
  37. const urlWithoutParams = getURLWithoutParams(url).href;
  38. if (urlWithoutParams) {
  39. return urlWithoutParams.toLowerCase();
  40. }
  41. return '';
  42. }