parseuri.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. define(function() {
  2. // FROM: https://github.com/get/parseuri/blob/master/index.js
  3. /**
  4. * Parses an URI
  5. *
  6. * @author Steven Levithan <stevenlevithan.com> (MIT license)
  7. * @api private
  8. */
  9. // Added support for "file:" protocol
  10. var re = /^(?:(?![^:@]+:[^:@\/]*@)(http|https|ws|wss|file):\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
  11. var parts = [
  12. 'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'
  13. ];
  14. /**
  15. * the parseUri function.
  16. */
  17. return function(str) {
  18. str = str.toString();
  19. var src = str,
  20. b = str.indexOf('['),
  21. e = str.indexOf(']');
  22. if (b != -1 && e != -1) {
  23. str = str.substring(0, b) + str.substring(b, e).replace(/:/g, ';') + str.substring(e, str.length);
  24. }
  25. var m = re.exec(str || ''),
  26. uri = {},
  27. i = 14;
  28. while (i--) {
  29. uri[parts[i]] = m[i] || '';
  30. }
  31. if (b != -1 && e != -1) {
  32. uri.source = src;
  33. uri.host = uri.host.substring(1, uri.host.length - 1).replace(/;/g, ':');
  34. // Keep square brackets
  35. uri.authority = uri.authority.replace(/;/g, ':');
  36. uri.ipv6uri = true;
  37. }
  38. // Parse parameters
  39. var q = {
  40. name: "queryKey",
  41. parser: /(?:^|&)([^&=]*)=?([^&]*)/g
  42. };
  43. uri[q.name] = {};
  44. uri[parts[12]].replace(q.parser, function ($0, $1, $2) {
  45. if ($1) uri[q.name][$1] = $2;
  46. });
  47. return uri;
  48. };
  49. });