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.

38 lines
1.0 KiB

7 years ago
  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 normalize (str, options) {
  7. // make sure protocol is followed by two slashes
  8. str = str.replace(/:\//g, '://');
  9. // remove consecutive slashes
  10. str = str.replace(/([^:\s])\/+/g, '$1/');
  11. // remove trailing slash before parameters or hash
  12. str = str.replace(/\/(\?|&|#[^!])/g, '$1');
  13. // replace ? in parameters with &
  14. str = str.replace(/(\?.+)\?/g, '$1&');
  15. return str;
  16. }
  17. return function () {
  18. var input = arguments;
  19. var options = {};
  20. if (typeof arguments[0] === 'object') {
  21. // new syntax with array and options
  22. input = arguments[0];
  23. options = arguments[1] || {};
  24. }
  25. var joined = [].slice.call(input, 0).join('/');
  26. return normalize(joined, options);
  27. };
  28. });