kurl
Version:
It's a powerfull URL parser written in JavaScript and compatible with NodeJS with a lot of features that it will help you to modify/parse easily a URL without any problem.
2 lines • 9.83 kB
JavaScript
/*! JavaScript/NodeJS URL v1.2.7 | Copyright 2014 yeikos - MIT license | https://github.com/yeikos/js.url */
;(function(e){function r(e){return typeof e==="string"||typeof e==="number"?""+e:""}function i(e,n,r){var i=arguments.length-1,s=t.unparam(this.attr(e).substr(1));if(!i){return s}else if(i===1){if(n&&typeof n==="object"){this.attr(e,n)}else{return s[n]}}else if(i===2){if(r===null){delete s[n]}else{s[n]=r}this.attr(e,s)}return this}function s(e,n){var i=t.unbuild(this._attributes,this.location instanceof t?this.location._attributes:false),s={},o=false,u=0,a=e.length,f;n=r(n).toLowerCase();if(n==="hostname"){delete i.host;delete i.port}for(u;u<a;++u){f=e[u];if(!o){if(f in i)s[f]=i[f];if(f===n)o=true}}return t.build(s)}function o(e,n){var i=e==="hash"?"#":"?";if(n&&typeof n==="object"){n=t.param(n)}else{n=(n=r(n))&&n.substr(0,1)===i?n.substr(1):n}try{n=encodeURI(decodeURI(n))}catch(s){n=""}return n?i+decodeURI(n):""}function u(e){var t,n=0,r=[];if(typeof e!=="object"||!e)return e;for(t in e){if(n++!==Number(t))return e;r.push(e[t])}return r}function a(e){return typeof HTMLElement==="function"?e instanceof HTMLElement:e&&typeof e==="object"&&e.nodeType===1&&typeof e.nodeName==="string"}var t=function(n,r){if(!(this instanceof t))return t.instance(arguments);this._attributes={};if(!this.isLocation)this.location=t.instance(arguments.length>1?[r]:[],function(){this.isLocation=true});if(arguments.length)this.href(n);return this},n="URL";t.prototype={constructor:t,_attributes:null,href:function(e){var n;if(!arguments.length){return t.build(this._attributes,this.location)}else if(e===null){this._attributes={}}else if((n=typeof e)==="string"||e&&n==="object"){this._attributes=t.unbuild(e,this.location)}return this},attr:function(e,n){var i=arguments.length;if(!i){var s={};for(i in this._attributes)s[i]=r(this._attributes[i]);return s}else if(i===1){if((i=typeof e)==="string"){e=e.toLowerCase();if(e==="query")e="search";return r(this._attributes[e.toLowerCase()])}else if(e&&(i=typeof e)==="object"){for(i in e)this.attr(i,e[(i||"").toLowerCase()])}}else if(i===2&&typeof e==="string"&&(i=e.toLowerCase())){if(function(n){while(n--)if(t.attributes[n]===e)return true}(t.attributes.length)){if(e==="host"||e==="hostname"||e==="port"){if(e==="port"){i={hostname:this._attributes.hostname,port:t.normalize.port(n)}}else{i=t.normalize.host(r(n)+(e==="hostname"&&this._attributes.port?":"+this._attributes.port:""))}this._attributes.host=i.hostname+(i.port?":"+i.port:"");this._attributes.hostname=i.hostname;this._attributes.port=i.port}else{if(e==="query")e="search";this._attributes[e]=t.normalize[e](n)}}}return this},search:function(){return i.apply(this,["search"].concat([].slice.apply(arguments)))},hash:function(){return i.apply(this,["hash"].concat([].slice.apply(arguments)))},isExternal:function(){var e=t.unbuild(this._attributes,this.location);if(!(this.location instanceof t))return e.protocol||e.host?true:false;return this.location.attr("protocol")!==e.protocol||this.location.attr("host")!==e.host},select:function(){return s.apply(this,[arguments])},unselect:function(){var e=t.attributes.slice(0),n=[].slice.apply(arguments),i={},o=n.length,u=e.length;while(o--)i[r(n[o]).toLowerCase()]=1;if(i.hostname||i.port){i.host=1}else if(i.host){i.hostname=1;i.port=1}while(u--)if(i[e[u]])e.splice(u,1);return s.apply(this,[e])},from:function(e){return s.apply(this,[t.attributes.slice(0).reverse(),e])},to:function(e){return s.apply(this,[t.attributes,e])}};t.prototype.query=t.prototype.search;t.version="1.2.7";t.attributes=["protocol","auth","host","hostname","port","pathname","search","query","hash"];t.normalize={regexp_split:/^(?:(?:(.+:)?\/\/)(?:(?:(.+)@)?([^\/:]+)(?::([^\/]+))?)?)?(?:(\/?[^?#]*)?(\?[^#]*)?(#.*)?)?/,regex_rfc3986_schema:/^[a-z][\w+\-.]*$/i,regex_rfc3986_userinfo:/^(?:[\w\-._~!$&'()*+,;=:]|%[0-9A-Fa-f]{2})*$/,regex_rfc3986_host:/^(?:\[(?:(?:(?:(?:[0-9A-Fa-f]{1,4}:){6}|::(?:[0-9A-Fa-f]{1,4}:){5}|(?:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,1}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}|(?:(?:[0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}|(?:(?:[0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}:|(?:(?:[0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})?::)(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(?:(?:[0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})?::)|[Vv][0-9A-Fa-f]+\.[\w\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(?:[\w\-._~!$&'()*+,;=]|%[0-9A-Fa-f]{2})*)$/,regex_rfc3986_port:/^\d+$/,protocol:function(e){var n=(e=r(e)).length;e=e.substr(n-1,1)===":"?e.substr(0,e.length-1):e;return t.normalize.regex_rfc3986_schema.test(e=e.toLowerCase())?e+":":""},auth:function(e){return t.normalize.regex_rfc3986_userinfo.test(e=r(e))?e:""},host:function(e){var n="",i;e=r(e).split(":");if(e.length>1){n=t.normalize.port(e.pop());if(!(i=t.normalize.hostname(e.join(":"))))n=""}else{i=t.normalize.hostname(e.shift())}return{hostname:i,port:n}},hostname:function(e){return t.normalize.regex_rfc3986_host.test(e=r(e).toLowerCase())?e:""},port:function(e){return t.normalize.regex_rfc3986_port.test(e=r(e))?e:""},pathname:function(e){try{e=encodeURI(decodeURI(r(e)))}catch(t){e=""}var n=e.split("/"),i=n.length,s=[],o=0;while(i--){if(n[i]===".."){o++}else if(n[i]!=="."){if(!o){s.unshift(n[i])}else{o--}}}return(s=s.join("/")).substr(0,1)!=="/"&&e.substr(0,1)==="/"?"/"+s:s},search:function(e){return o.apply(this,["search",e])},hash:function(e){return o.apply(this,["hash",e])}};t.instance=function(e,n){var r=function(t){if(typeof t==="function")t.call(this)};e=typeof e==="object"&&e?[].slice.call(e,0):e;return(r.prototype=t.prototype).constructor.apply(new r(n),e instanceof Array?e:[])};t.getElementURL=function(e){if(!a(e))return"";var t=typeof HTMLElement==="function"?(t=e instanceof HTMLFormElement?"action":e instanceof HTMLAnchorElement||e instanceof HTMLBaseElement||e instanceof HTMLLinkElement?"href":e instanceof HTMLImageElement||e instanceof HTMLScriptElement||e instanceof HTMLIFrameElement?"src":0)?t:"":(t=(t=e.nodeName.toLowerCase())==="form"?"action":t==="a"||t==="base"||t==="link"?"href":t==="img"||t==="script"||t==="iframe"?"src":0)?t:"";return(typeof e.getAttribute==="function"||typeof e.getAttribute==="object"&&e.getAttribute)&&(t=e.getAttribute(t))?t:""};t.build=function(e,t){var n=this.unbuild(e,t);return(n.protocol?n.protocol+"//":n.host?"//":"")+(n.auth&&n.host?n.auth+"@":"")+n.host+(!n.pathname&&(n.protocol||n.host)&&(n.search||n.hash)?"/":"")+(n.pathname+n.search+n.hash)};t.unbuild=function(e,n){var r=t.normalize,i,s,o;n=n instanceof t?n.attr():n&&((o=typeof n)==="string"||o==="object")?t.unbuild(n):false;if(a(e))e=t.getElementURL(e);if(typeof e==="string"){e=encodeURI(decodeURI(e));if(!(s=e.match(t.normalize.regexp_split)))s=[];i={protocol:r.protocol(s[1]),auth:r.auth(s[2]),hostname:r.hostname(s[3]),port:r.port(s[4]),pathname:r.pathname(s[5]),search:r.search(s[6]),hash:r.hash(s[7])}}else if(e instanceof t){i=e.attr()}else{if(!e||typeof e!=="object")e={};i={protocol:r.protocol(e.protocol),auth:r.auth(e.auth),pathname:r.pathname(e.pathname),search:r.search(e.search),hash:r.hash(e.hash)};if("host"in e){i.hostname=(o=r.host(e.host)).hostname;i.port=o.port}else if("hostname"in e){i.hostname=r.hostname(e.hostname);i.port=i.hostname?r.port(e.port):""}else{i.hostname="";i.port=""}}if(n){if("host"in n){i._hostname=(o=r.host(n.host)).hostname;i._port=o.port}else if("hostname"in n){i._hostname=r.hostname(n.hostname);i._port=i.hostname?r.port(n.port):""}if(!i.protocol){if("protocol"in n)i.protocol=r.protocol(n.protocol);if(!i.hostname){if("_hostname"in i)i.hostname=i._hostname;if(!i.port){if("_port"in i)i.port=i._port;if(!i.auth){if("auth"in n)i.auth=r.auth(n.auth);if(!i.pathname){if("pathname"in n)i.pathname=r.pathname(n.pathname);if(!i.search){if("search"in n)i.search=r.search(n.search);if(!i.hash&&"hash"in n)i.hash=r.search(n.hash)}}else if("pathname"in n&&i.pathname.substr(0,1)!=="/"){i.pathname=r.pathname(n.pathname).split("/").slice(0,-1).concat(i.pathname).join("/")}}}}}delete i._hostname;delete i._port}i.host=i.hostname+(i.port?":"+i.port:"");return i};t.param=function(e){var t=function(e,n){if(!e||typeof e!=="object")return"";var r=[],i=/%20/g,s=true,o,a,f,l;e=u(e);for(o in e){s=false;a=e instanceof Array&&!(typeof e[o]==="object"&&e[o])?"":encodeURIComponent(o);l=n?n+"["+a+"]":encodeURIComponent(o);r.push((f=e[o])&&typeof f==="object"?t(f,l):l.replace(i,"+")+((f=encodeURIComponent(typeof f==="string"||typeof f==="number"?f:"").replace(i,"+"))?"="+f:""))}if(s)r.push(n);return r.join("&")};return t(e)};t.unparam=function(e){if(typeof e!=="string")return{};var t,n,r,i,s,o,a,f,l,c,h,p=/^[0-9]d*/,d=function(e){return!isNaN(parseFloat(e))&&isFinite(e)&&/^(?!0\d)/.test(e)&&/^[\d\.]+$/.test(e)},v=/^[^\[]+/,m=/\[(.*?)\]/g,g=/\+/g,y={};for(t=0,n=(e=e.split(/&+/)).length;t<n;++t){if(!(r=e[t]))continue;r=r.replace(g," ");try{a=decodeURIComponent((c=r.split("=")).shift())}catch(b){continue}try{f=decodeURIComponent(c.join("=").replace(g," "))}catch(b){continue}if(!(c=a.match(v)))continue;o=a;a=c[0];if(o=o.match(m)){if(!y[a])y[a]={};l=y[a];for(i=0,s=o.length;i<s;++i){if(!(r=(r=o[i]).substr(1,r.length-2))){r=h=0;for(c in l){c=Number(c);if(p.test(c)&&c>=r){r=c;h=1}}if(h)++r}r=decodeURIComponent((r+"").replace(g," "));if(i===s-1){l[r]=d(f)?Number(f):f}else{if(!l[r])l[r]={};l=l[r]}}}else{y[a]=d(f)?Number(f):f}}c=function(e){for(var t in e)if(typeof e[t]==="object"&&e[t])c(e[t]=u(e[t]))};c(y);return y};return e?module.exports=t:window[n]=t})(typeof module==="object"&&module&&typeof module.exports==="object"&&module.exports);