combineURLs.js 492 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. /**
  3. * Creates a new URL by combining the specified URLs
  4. *
  5. * @param {string} baseURL The base URL
  6. * @param {string} relativeURL The relative URL
  7. *
  8. * @returns {string} The combined URL
  9. */
  10. export default function combineURLs(baseURL, relativeURL) {
  11. if (!relativeURL) {
  12. return baseURL;
  13. }
  14. let end = baseURL.length;
  15. while (end > 0 && baseURL.charCodeAt(end - 1) === 47) {
  16. end--;
  17. }
  18. return baseURL.slice(0, end) + '/' + relativeURL.replace(/^\/+/, '');
  19. }