forked from Mikhus/domurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.min.js
1 lines (1 loc) · 3.72 KB
/
url.min.js
1
!function(t){"use strict";var y=/^[a-z]+:/,d=/[-a-z0-9]+(\.[-a-z0-9])*:\d+/i,v=/\/\/(.*?)(?::(.*?))?@/,r=/^win/i,g=/:$/,m=/^\?/,q=/^#/,w=/(.*\/)/,A=/^\/{2,}/,o=/'/g,e=/%([ef][0-9a-f])%([89ab][0-9a-f])%([89ab][0-9a-f])/gi,n=/%([cd][0-9a-f])%([89ab][0-9a-f])/gi,i=/%([0-7][0-9a-f])/gi,p=/\+/g,s=/^\w:$/,C=/[^/#?]/;var a,I="undefined"==typeof window&&"undefined"!=typeof global&&"function"==typeof require,S=I?t.require:null,b={protocol:"protocol",host:"hostname",port:"port",path:"pathname",query:"search",hash:"hash"},j={ftp:21,gopher:70,http:80,https:443,ws:80,wss:443};function x(){return I?a=a||"file://"+(process.platform.match(r)?"/":"")+S("fs").realpathSync("."):document.location.href}function h(t,r,o){var e,n,i;r=r||x(),I?e=S("url").parse(r):(e=document.createElement("a")).href=r;var s,p,a=(p={path:!0,query:!0,hash:!0},(s=r)&&y.test(s)&&(p.protocol=!0,p.host=!0,d.test(s)&&(p.port=!0),v.test(s)&&(p.user=!0,p.pass=!0)),p);for(n in i=r.match(v)||[],b)a[n]?t[n]=e[b[n]]||"":t[n]="";if(t.protocol=t.protocol.replace(g,""),t.query=t.query.replace(m,""),t.hash=z(t.hash.replace(q,"")),t.user=z(i[1]||""),t.pass=z(i[2]||""),t.port=j[t.protocol]==t.port||0==t.port?"":t.port,!a.protocol&&C.test(r.charAt(0))&&(t.path=r.split("?")[0].split("#")[0]),!a.protocol&&o){var h=new U(x().match(w)[0]),u=h.path.split("/"),c=t.path.split("/"),f=["protocol","user","pass","host","port"],l=f.length;for(u.pop(),n=0;n<l;n++)t[f[n]]=h[f[n]];for(;".."===c[0];)u.pop(),c.shift();t.path=("/"!==r.charAt(0)?u.join("/"):"")+"/"+c.join("/")}t.path=t.path.replace(A,"/"),t.paths(t.paths()),t.query=new F(t.query)}function u(t){return encodeURIComponent(t).replace(o,"%27")}function z(t){return(t=(t=(t=t.replace(p," ")).replace(e,function(t,r,o,e){var n=parseInt(r,16)-224,i=parseInt(o,16)-128;if(0==n&&i<32)return t;var s=(n<<12)+(i<<6)+(parseInt(e,16)-128);return 65535<s?t:String.fromCharCode(s)})).replace(n,function(t,r,o){var e=parseInt(r,16)-192;if(e<2)return t;var n=parseInt(o,16)-128;return String.fromCharCode((e<<6)+n)})).replace(i,function(t,r){return String.fromCharCode(parseInt(r,16))})}function F(t){for(var r=t.split("&"),o=0,e=r.length;o<e;o++){var n=r[o].split("="),i=decodeURIComponent(n[0].replace(p," "));if(i){var s=void 0!==n[1]?z(n[1]):null;void 0===this[i]?this[i]=s:(this[i]instanceof Array||(this[i]=[this[i]]),this[i].push(s))}}}function U(t,r){h(this,t,!r)}F.prototype.toString=function(){var t,r,o="",e=u;for(t in this){var n=this[t];if(!(n instanceof Function||void 0===n))if(n instanceof Array){var i=n.length;if(!i){o+=(o?"&":"")+e(t)+"=";continue}for(r=0;r<i;r++){var s=n[r];void 0!==s&&(o+=o?"&":"",o+=e(t)+(null===s?"":"="+e(s)))}}else o+=o?"&":"",o+=e(t)+(null===n?"":"="+e(n))}return o},U.prototype.clearQuery=function(){for(var t in this.query)this.query[t]instanceof Function||delete this.query[t];return this},U.prototype.queryLength=function(){var t=0;for(var r in this.query)this.query[r]instanceof Function||t++;return t},U.prototype.isEmptyQuery=function(){return 0===this.queryLength()},U.prototype.paths=function(t){var r,o="",e=0;if(t&&t.length&&t+""!==t){for(this.isAbsolute()&&(o="/"),r=t.length;e<r;e++)t[e]=!e&&s.test(t[e])?t[e]:u(t[e]);this.path=o+t.join("/")}for(e=0,r=(t=("/"===this.path.charAt(0)?this.path.slice(1):this.path).split("/")).length;e<r;e++)t[e]=z(t[e]);return t},U.prototype.encode=u,U.prototype.decode=z,U.prototype.isAbsolute=function(){return this.protocol||"/"===this.path.charAt(0)},U.prototype.toString=function(){return(this.protocol&&this.protocol+"://")+(this.user&&u(this.user)+(this.pass&&":"+u(this.pass))+"@")+(this.host&&this.host)+(this.port&&":"+this.port)+(this.path&&this.path)+(this.query.toString()&&"?"+this.query)+(this.hash&&"#"+u(this.hash))},t[t.exports?"exports":"Url"]=U}("undefined"!=typeof module&&module.exports?module:window);