SHARE
TWEET

parseURI, ffs

thethunderzone Feb 14th, 2020 426 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function parseURI(aUrl){
  2.     var x, y, wstr = (aUrl = aUrl.trim()), dirs=[], pathname=null, port=null, proto=null, host=null, path=null, file=null, fileext=null, hash=null, params=null;
  3.     // read direction RTL
  4.     if ((x = wstr.indexOf("#")) > -1) {
  5.         hash = wstr.substr(x + 1)
  6.         wstr = wstr.slice(0, x);
  7.     }
  8.     if ((x = wstr.indexOf("?")) > -1) {
  9.         params = wstr.substr(x + 1);
  10.         wstr = wstr.slice(0, x);
  11.     } // read direction LTR
  12.     if ((x = wstr.indexOf("://")) > -1) {
  13.         proto = wstr.substr(0, x);
  14.         wstr = wstr.slice(x + 3);
  15.     } // twisted code below
  16.     if ((x = wstr.indexOf(":")) > -1) {
  17.         y = wstr.indexOf("/");
  18.         if (y > -1) {
  19.             if (x < y) {
  20.                 port = wstr.slice(x + 1, y);
  21.                 host = wstr.slice(0, x);
  22.                 wstr = wstr.slice(x + 1 + 1 + port.length);
  23.             } else {
  24.                 host = wstr.slice(0, y);
  25.                 wstr = wstr.slice(y);
  26.             }
  27.         } else {
  28.             port = wstr.slice(x + 1);
  29.             host = wstr.slice(0, x);
  30.             wstr = "";
  31.         }
  32.     } else {
  33.         x = wstr.indexOf("/");
  34.         if (x > -1) {
  35.             host = wstr.slice(0, x);
  36.             wstr = wstr.slice(x + 1);
  37.         } else {
  38.             host = wstr;
  39.             wstr = "";
  40.         }
  41.     }
  42.     if ((x = wstr.indexOf(".")) > -1) {
  43.         fileext = wstr.slice(x + 1);
  44.         if ("/" === fileext.slice(-1)) fileext = null;
  45.     }
  46.     pathname = wstr;
  47.     if ((x = wstr.lastIndexOf("/")) > -1) {
  48.         file = wstr.slice(x + 1);
  49.     } else {
  50.         file = wstr;
  51.     }
  52.     if (file) dirs = pathname.slice(1).split("/").slice(0, -1);
  53.     else dirs = pathname.slice(1).split("/").slice(0, -(pathname.lastIndexOf("/")>-1));
  54.     return {
  55.         url: aUrl,
  56.         host: host, proto: proto, port: port>0&&port<0x10000?+port:null, file:file?file:null, fileext: fileext, dirs: dirs,
  57.         hash: hash, params: params, pathname: pathname?pathname:null
  58.     };
  59. }
  60.  
  61. console.log( parseURI("https://pastebin.com") )
  62. console.log( parseURI("https://pastebin.com/") )
  63. console.log( parseURI("https://pastebin.com/:43") )
  64. console.log( parseURI("https://pastebin.com/:43/a") )
  65. console.log( parseURI("https://pastebin.com/:43/a/") )
  66. console.log( parseURI("https://pastebin.com/:43/a/a") )
  67. console.log( parseURI("https://pastebin.com:54/:43") )
  68. console.log( parseURI("https://pastebin.com:54/:43?abc=1") )
  69. console.log( parseURI("https://pastebin.com:54/:43?abc=1#haha") )
  70. console.log( parseURI("https://pastebin.com/?#haha") )
  71. console.log( parseURI("https://pastebin.com/?abc=1#haha") )
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