SHARE
TWEET

Untitled

a guest Mar 17th, 2018 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var url = require("url"),
  2.   path = require("path");
  3.  
  4. exports.parse = function (str) {
  5.   var parsed = url.parse(str);
  6.   return {
  7.     get scheme : function () { return parsed.protocol ? parsed.protocol.substr(-1) : undefined },
  8.     get authorityRoot : function () { return parsed.slashes ? "//" : undefined },
  9.     get authority : function () { return parsed.host },
  10.     get userInfo : function () { return parsed.auth },
  11.     get user : function () { return parsed.auth ? parsed.auth.split(":").shift() : undefined },
  12.     get password : function () { return parsed.auth ? parsed.auth.split(":").slice(1).join(":") : undefined },
  13.     get domain : function () { return parsed.hostname },
  14.     get port : function () { return parsed.port },
  15.     get path : function () { return parsed.pathname },
  16.     get root : function () { return parsed.pathname && parsed.pathname.charAt(0) === "/" ? "/" : undefined },
  17.     get directory : function () { return parsed.pathname ?
  18.         this.root ? path.dirname(parsed.pathname.substr(1)) : path.dirname(parsed.pathname)
  19.         : undefined },
  20.     get file : function () { return parsed.pathname ? path.filename(parsed.pathname) : undefined },
  21.     get query : function () { return parsed.query },
  22.     get fragment : function () { return parsed.hash ? parsed.hash.substr(1) : undefined }
  23.     // setters left as an exercise for the reader.
  24.   };
  25. };
  26.  
  27. exports.resolveObject = function (source, dest) {
  28.   return exports.parse(exports.resolve(source, dest));
  29. };
  30. exports.resolve = function (source, dest) {
  31.   return url.resolve(source, dest);
  32. };
  33.  
  34. exports.format = function (obj) {
  35.   if (obj.href) obj = exports.parse(obj.href);
  36.   return (
  37.     obj.scheme ? obj.scheme + ":" : ""
  38.   ) + (
  39.     obj.authorityRoot || ""
  40.   ) + (
  41.     obj.authority || ""
  42.   ) + (
  43.     obj.path || ""
  44.   ) + (
  45.     obj.query ? "?" + obj.query : ""
  46.   ) + (
  47.     obj.fragement ? "#" + obj.fragment : ""
  48.   );
  49. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top