uri.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Adapted from the following:
  2. //
  3. // Original License
  4. // ----------------
  5. // parseUri 1.2.2
  6. // (c) Steven Levithan <stevenlevithan.com>
  7. // MIT License
  8. function parseUri (str) {
  9. var o = parseUri.options,
  10. m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
  11. uri = {},
  12. i = 14;
  13. while (i--) uri[o.key[i]] = m[i] || "";
  14. uri[o.q.name] = {};
  15. uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
  16. if ($1) uri[o.q.name][$1] = $2;
  17. });
  18. return uri;
  19. };
  20. parseUri.options = {
  21. strictMode: false,
  22. key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
  23. q: {
  24. name: "queryKey",
  25. parser: /(?:^|&)([^&=]*)=?([^&]*)/g
  26. },
  27. parser: {
  28. strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
  29. loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
  30. }
  31. };
  32. exports.parseUri = parseUri;