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.

50 lines
1.3KB

  1. (function (name, context, definition) {
  2. if (typeof module !== 'undefined' && module.exports) module.exports = definition();
  3. else if (typeof define === 'function' && define.amd) define(definition);
  4. else context[name] = definition();
  5. })('urljoin', this, function () {
  6. function startsWith(str, searchString) {
  7. return str.substr(0, searchString.length) === searchString;
  8. }
  9. function normalize (str, options) {
  10. if (startsWith(str, 'file://')) {
  11. // make sure file protocol has max three slashes
  12. str = str.replace(/(\/{0,3})\/*/g, '$1');
  13. } else {
  14. // make sure protocol is followed by two slashes
  15. str = str.replace(/:\//g, '://');
  16. // remove consecutive slashes
  17. str = str.replace(/([^:\s\%\3\A])\/+/g, '$1/');
  18. }
  19. // remove trailing slash before parameters or hash
  20. str = str.replace(/\/(\?|&|#[^!])/g, '$1');
  21. // replace ? in parameters with &
  22. str = str.replace(/(\?.+)\?/g, '$1&');
  23. return str;
  24. }
  25. return function () {
  26. var input = arguments;
  27. var options = {};
  28. if (typeof arguments[0] === 'object') {
  29. // new syntax with array and options
  30. input = arguments[0];
  31. options = arguments[1] || {};
  32. }
  33. var joined = [].slice.call(input, 0).join('/');
  34. return normalize(joined, options);
  35. };
  36. });