UNPKG

oj

Version:

A unified templating language for the people. Thirsty people.

1 lines 7.91 kB
function Url(){this.protocol=null,this.slashes=null,this.auth=null,this.host=null,this.port=null,this.hostname=null,this.hash=null,this.search=null,this.query=null,this.pathname=null,this.path=null,this.href=null}function urlParse(e,t,n){if(e&&util.isObject(e)&&e instanceof Url)return e;var r=new Url;return r.parse(e,t,n),r}function urlFormat(e){return util.isString(e)&&(e=urlParse(e)),e instanceof Url?e.format():Url.prototype.format.call(e)}function urlResolve(e,t){return urlParse(e,!1,!0).resolve(t)}function urlResolveObject(e,t){return e?urlParse(e,!1,!0).resolveObject(t):t}var punycode=require("punycode"),util=require("util");exports.parse=urlParse,exports.resolve=urlResolve,exports.resolveObject=urlResolveObject,exports.format=urlFormat,exports.Url=Url;var protocolPattern=/^([a-z0-9.+-]+:)/i,portPattern=/:[0-9]*$/,delims=["<",">",'"',"`"," ","\r","\n"," "],unwise=["{","}","|","\\","^","`"].concat(delims),autoEscape=["'"].concat(unwise),nonHostChars=["%","/","?",";","#"].concat(autoEscape),hostEndingChars=["/","?","#"],hostnameMaxLen=255,hostnamePartPattern=/^[a-z0-9A-Z_-]{0,63}$/,hostnamePartStart=/^([a-z0-9A-Z_-]{0,63})(.*)$/,unsafeProtocol={javascript:!0,"javascript:":!0},hostlessProtocol={javascript:!0,"javascript:":!0},slashedProtocol={http:!0,https:!0,ftp:!0,gopher:!0,file:!0,"http:":!0,"https:":!0,"ftp:":!0,"gopher:":!0,"file:":!0},querystring=require("querystring");Url.prototype.parse=function(e,t,n){if(!util.isString(e))throw new TypeError("Parameter 'url' must be a string, not "+typeof e);var r=e;r=r.trim();var i=protocolPattern.exec(r);if(i){i=i[0];var s=i.toLowerCase();this.protocol=s,r=r.substr(i.length)}if(n||i||r.match(/^\/\/[^@\/]+@[^@\/]+/)){var o=r.substr(0,2)==="//";o&&(!i||!hostlessProtocol[i])&&(r=r.substr(2),this.slashes=!0)}if(!hostlessProtocol[i]&&(o||i&&!slashedProtocol[i])){var u=-1;for(var a=0;a<hostEndingChars.length;a++){var f=r.indexOf(hostEndingChars[a]);f!==-1&&(u===-1||f<u)&&(u=f)}var l,c;u===-1?c=r.lastIndexOf("@"):c=r.lastIndexOf("@",u),c!==-1&&(l=r.slice(0,c),r=r.slice(c+1),this.auth=decodeURIComponent(l)),u=-1;for(var a=0;a<nonHostChars.length;a++){var f=r.indexOf(nonHostChars[a]);f!==-1&&(u===-1||f<u)&&(u=f)}u===-1&&(u=r.length),this.host=r.slice(0,u),r=r.slice(u),this.parseHost(),this.hostname=this.hostname||"";var h=this.hostname[0]==="["&&this.hostname[this.hostname.length-1]==="]";if(!h){var p=this.hostname.split(/\./);for(var a=0,d=p.length;a<d;a++){var v=p[a];if(!v)continue;if(!v.match(hostnamePartPattern)){var m="";for(var g=0,y=v.length;g<y;g++)v.charCodeAt(g)>127?m+="x":m+=v[g];if(!m.match(hostnamePartPattern)){var b=p.slice(0,a),w=p.slice(a+1),E=v.match(hostnamePartStart);E&&(b.push(E[1]),w.unshift(E[2])),w.length&&(r="/"+w.join(".")+r),this.hostname=b.join(".");break}}}}this.hostname.length>hostnameMaxLen?this.hostname="":this.hostname=this.hostname.toLowerCase();if(!h){var S=this.hostname.split("."),x=[];for(var a=0;a<S.length;++a){var T=S[a];x.push(T.match(/[^A-Za-z0-9_-]/)?"xn--"+punycode.encode(T):T)}this.hostname=x.join(".")}var N=this.port?":"+this.port:"",C=this.hostname||"";this.host=C+N,this.href+=this.host,h&&(this.hostname=this.hostname.substr(1,this.hostname.length-2),r[0]!=="/"&&(r="/"+r))}if(!unsafeProtocol[s])for(var a=0,d=autoEscape.length;a<d;a++){var k=autoEscape[a],L=encodeURIComponent(k);L===k&&(L=escape(k)),r=r.split(k).join(L)}var A=r.indexOf("#");A!==-1&&(this.hash=r.substr(A),r=r.slice(0,A));var O=r.indexOf("?");O!==-1?(this.search=r.substr(O),this.query=r.substr(O+1),t&&(this.query=querystring.parse(this.query)),r=r.slice(0,O)):t&&(this.search="",this.query={}),r&&(this.pathname=r),slashedProtocol[s]&&this.hostname&&!this.pathname&&(this.pathname="/");if(this.pathname||this.search){var N=this.pathname||"",T=this.search||"";this.path=N+T}return this.href=this.format(),this},Url.prototype.format=function(){var e=this.auth||"";e&&(e=encodeURIComponent(e),e=e.replace(/%3A/i,":"),e+="@");var t=this.protocol||"",n=this.pathname||"",r=this.hash||"",i=!1,s="";this.host?i=e+this.host:this.hostname&&(i=e+(this.hostname.indexOf(":")===-1?this.hostname:"["+this.hostname+"]"),this.port&&(i+=":"+this.port)),this.query&&util.isObject(this.query)&&Object.keys(this.query).length&&(s=querystring.stringify(this.query));var o=this.search||s&&"?"+s||"";return t&&t.substr(-1)!==":"&&(t+=":"),this.slashes||(!t||slashedProtocol[t])&&i!==!1?(i="//"+(i||""),n&&n.charAt(0)!=="/"&&(n="/"+n)):i||(i=""),r&&r.charAt(0)!=="#"&&(r="#"+r),o&&o.charAt(0)!=="?"&&(o="?"+o),n=n.replace(/[?#]/g,function(e){return encodeURIComponent(e)}),o=o.replace("#","%23"),t+i+n+o+r},Url.prototype.resolve=function(e){return this.resolveObject(urlParse(e,!1,!0)).format()},Url.prototype.resolveObject=function(e){if(util.isString(e)){var t=new Url;t.parse(e,!1,!0),e=t}var n=new Url;Object.keys(this).forEach(function(e){n[e]=this[e]},this),n.hash=e.hash;if(e.href==="")return n.href=n.format(),n;if(e.slashes&&!e.protocol)return Object.keys(e).forEach(function(t){t!=="protocol"&&(n[t]=e[t])}),slashedProtocol[n.protocol]&&n.hostname&&!n.pathname&&(n.path=n.pathname="/"),n.href=n.format(),n;if(e.protocol&&e.protocol!==n.protocol){if(!slashedProtocol[e.protocol])return Object.keys(e).forEach(function(t){n[t]=e[t]}),n.href=n.format(),n;n.protocol=e.protocol;if(!e.host&&!hostlessProtocol[e.protocol]){var r=(e.pathname||"").split("/");while(r.length&&!(e.host=r.shift()));e.host||(e.host=""),e.hostname||(e.hostname=""),r[0]!==""&&r.unshift(""),r.length<2&&r.unshift(""),n.pathname=r.join("/")}else n.pathname=e.pathname;n.search=e.search,n.query=e.query,n.host=e.host||"",n.auth=e.auth,n.hostname=e.hostname||e.host,n.port=e.port;if(n.pathname||n.search){var i=n.pathname||"",s=n.search||"";n.path=i+s}return n.slashes=n.slashes||e.slashes,n.href=n.format(),n}var o=n.pathname&&n.pathname.charAt(0)==="/",u=e.host||e.pathname&&e.pathname.charAt(0)==="/",a=u||o||n.host&&e.pathname,f=a,l=n.pathname&&n.pathname.split("/")||[],r=e.pathname&&e.pathname.split("/")||[],c=n.protocol&&!slashedProtocol[n.protocol];c&&(n.hostname="",n.port=null,n.host&&(l[0]===""?l[0]=n.host:l.unshift(n.host)),n.host="",e.protocol&&(e.hostname=null,e.port=null,e.host&&(r[0]===""?r[0]=e.host:r.unshift(e.host)),e.host=null),a=a&&(r[0]===""||l[0]===""));if(u)n.host=e.host||e.host===""?e.host:n.host,n.hostname=e.hostname||e.hostname===""?e.hostname:n.hostname,n.search=e.search,n.query=e.query,l=r;else if(r.length)l||(l=[]),l.pop(),l=l.concat(r),n.search=e.search,n.query=e.query;else if(!util.isNullOrUndefined(e.search)){if(c){n.hostname=n.host=l.shift();var h=n.host&&n.host.indexOf("@")>0?n.host.split("@"):!1;h&&(n.auth=h.shift(),n.host=n.hostname=h.shift())}n.search=e.search,n.query=e.query;if(!util.isNull(n.pathname)||!util.isNull(n.search))n.path=(n.pathname?n.pathname:"")+(n.search?n.search:"");return n.href=n.format(),n}if(!l.length)return n.pathname=null,n.search?n.path="/"+n.search:n.path=null,n.href=n.format(),n;var p=l.slice(-1)[0],d=(n.host||e.host)&&(p==="."||p==="..")||p==="",v=0;for(var m=l.length;m>=0;m--)p=l[m],p=="."?l.splice(m,1):p===".."?(l.splice(m,1),v++):v&&(l.splice(m,1),v--);if(!a&&!f)for(;v--;v)l.unshift("..");a&&l[0]!==""&&(!l[0]||l[0].charAt(0)!=="/")&&l.unshift(""),d&&l.join("/").substr(-1)!=="/"&&l.push("");var g=l[0]===""||l[0]&&l[0].charAt(0)==="/";if(c){n.hostname=n.host=g?"":l.length?l.shift():"";var h=n.host&&n.host.indexOf("@")>0?n.host.split("@"):!1;h&&(n.auth=h.shift(),n.host=n.hostname=h.shift())}a=a||n.host&&l.length,a&&!g&&l.unshift(""),l.length?n.pathname=l.join("/"):(n.pathname=null,n.path=null);if(!util.isNull(n.pathname)||!util.isNull(n.search))n.path=(n.pathname?n.pathname:"")+(n.search?n.search:"");return n.auth=e.auth||n.auth,n.slashes=n.slashes||e.slashes,n.href=n.format(),n},Url.prototype.parseHost=function(){var e=this.host,t=portPattern.exec(e);t&&(t=t[0],t!==":"&&(this.port=t.substr(1)),e=e.substr(0,e.length-t.length)),e&&(this.hostname=e)}