path.js 611 B

1234567891011121314151617181920212223242526272829
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. /*
  6. * Join all the arguments together and normalize the resulting URI.
  7. * The initial path must be an full URI with a protocol (i.e. http://).
  8. */
  9. exports.joinURI = (initialPath, ...paths) => {
  10. let url;
  11. try {
  12. url = new URL(initialPath);
  13. }
  14. catch (e) {
  15. return;
  16. }
  17. for (let path of paths) {
  18. if (path) {
  19. url = new URL(path, url);
  20. }
  21. }
  22. return url.href;
  23. };