forked from ericf/urljs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url-min.js
7 lines (7 loc) · 6.44 KB
/
url-min.js
1
2
3
4
5
6
7
/*!
* URL.js
*
* Copyright 2011 Eric Ferraiuolo
* https://github.com/ericf/urljs
*/
var URL=function(){var u=this;if(!(u&&u.hasOwnProperty&&(u instanceof URL))){u=new URL()}return u._init.apply(u,arguments)};(function(){var ABSOLUTE="absolute",RELATIVE="relative",HTTP="http",HTTPS="https",COLON=":",SLASH_SLASH="//",AT="@",DOT=".",SLASH="/",DOT_DOT="..",DOT_DOT_SLASH="../",QUESTION="?",EQUALS="=",AMP="&",HASH="#",EMPTY_STRING="",TYPE="type",SCHEME="scheme",USER_INFO="userInfo",HOST="host",PORT="port",PATH="path",QUERY="query",FRAGMENT="fragment",URL_TYPE_REGEX=/^(?:(https?:\/\/|\/\/)|(\/|\?|#)|[^;:@=\.\s])/i,URL_ABSOLUTE_REGEX=/^(?:(https?):\/\/|\/\/)(?:([^:@\s]+:?[^:@\s]+?)@)?((?:[^;:@=\/\?\.\s]+\.)+[A-Za-z0-9\-]{2,})(?::(\d+))?(?=\/|\?|#|$)([^\?#]+)?(?:\?([^#]+))?(?:#(.+))?/i,URL_RELATIVE_REGEX=/^([^\?#]+)?(?:\?([^#]+))?(?:#(.+))?/i,OBJECT="object",STRING="string",TRIM_REGEX=/^\s+|\s+$/g,trim,isObject,isString;trim=String.prototype.trim?function(s){return(s&&s.trim?s.trim():s)}:function(s){try{return s.replace(TRIM_REGEX,EMPTY_STRING)}catch(e){return s}};isObject=function(o){return(o&&typeof o===OBJECT)};isString=function(o){return typeof o===STRING};URL.ABSOLUTE=ABSOLUTE;URL.RELATIVE=RELATIVE;URL.normalize=function(url){return new URL(url).toString()};URL.resolve=function(baseUrl,url){return new URL(baseUrl).resolve(url).toString()};URL.prototype={_init:function(url){this.constructor=URL;url=isString(url)?url:url instanceof URL?url.toString():null;this._original=url;this._url={};this._isValid=this._parse(url);return this},toString:function(){var url=this._url,urlParts=[],type=url[TYPE],scheme=url[SCHEME],path=url[PATH],query=url[QUERY],fragment=url[FRAGMENT];if(type===ABSOLUTE){urlParts.push(scheme?(scheme+COLON+SLASH_SLASH):SLASH_SLASH,this.authority());if(path&&path.indexOf(SLASH)!==0){path=SLASH+path}}urlParts.push(path,query?(QUESTION+this.queryString()):EMPTY_STRING,fragment?(HASH+fragment):EMPTY_STRING);return urlParts.join(EMPTY_STRING)},original:function(){return this._original},isValid:function(){return this._isValid},isAbsolute:function(){return this._url[TYPE]===ABSOLUTE},isRelative:function(){return this._url[TYPE]===RELATIVE},isHostRelative:function(){var path=this._url[PATH];return(this.isRelative()&&path&&path.indexOf(SLASH)===0)},type:function(){return this._url[TYPE]},scheme:function(scheme){return(arguments.length?this._set(SCHEME,scheme):this._url[SCHEME])},userInfo:function(userInfo){return(arguments.length?this._set(USER_INFO,userInfo):this._url[USER_INFO])},host:function(host){return(arguments.length?this._set(HOST,host):this._url[HOST])},domain:function(){var host=this._url[HOST];return(host?host.split(DOT).slice(-2).join(DOT):undefined)},port:function(port){return(arguments.length?this._set(PORT,port):this._url[PORT])},authority:function(){var url=this._url,userInfo=url[USER_INFO],host=url[HOST],port=url[PORT];return[userInfo?(userInfo+AT):EMPTY_STRING,host,port?(COLON+port):EMPTY_STRING,].join(EMPTY_STRING)},path:function(path){return(arguments.length?this._set(PATH,path):this._url[PATH])},query:function(query){return(arguments.length?this._set(QUERY,query):this._url[QUERY])},queryString:function(queryString){if(arguments.length){return this._set(QUERY,this._parseQuery(queryString))}queryString=EMPTY_STRING;var query=this._url[QUERY],i,len;if(query){for(i=0,len=query.length;i<len;i++){queryString+=query[i].join(EQUALS);if(i<len-1){queryString+=AMP}}}return queryString},fragment:function(fragment){return(arguments.length?this._set(FRAGMENT,fragment):this._url[FRAGMENT])},resolve:function(url){url=(url instanceof URL)?url:new URL(url);var resolved,path;if(!(this.isValid()&&url.isValid())){return this}if(url.isAbsolute()){return(this.isAbsolute()?url.scheme()?url:new URL(url).scheme(this.scheme()):url)}resolved=new URL(this.isAbsolute()?this:null);if(url.path()){if(url.isHostRelative()||!this.path()){path=url.path()}else{path=this.path().substring(0,this.path().lastIndexOf(SLASH)+1)+url.path()}resolved.path(this._normalizePath(path)).query(url.query()).fragment(url.fragment())}else{if(url.query()){resolved.query(url.query()).fragment(url.fragment())}else{if(url.fragment()){resolved.fragment(url.fragment())}}}return resolved},reduce:function(url){url=(url instanceof URL)?url:new URL(url);var reduced=this.resolve(url);if(this.isAbsolute()&&reduced.isAbsolute()){if(reduced.scheme()===this.scheme()&&reduced.authority()===this.authority()){reduced.scheme(null).userInfo(null).host(null).port(null)}}return reduced},_parse:function(url,type){url=trim(url);if(!(isString(url)&&url.length>0)){return false}var urlParts,parsed;if(!type){type=url.match(URL_TYPE_REGEX);type=type?type[1]?ABSOLUTE:type[2]?RELATIVE:null:null}switch(type){case ABSOLUTE:urlParts=url.match(URL_ABSOLUTE_REGEX);if(urlParts){parsed={};parsed[TYPE]=ABSOLUTE;parsed[SCHEME]=urlParts[1]?urlParts[1].toLowerCase():undefined;parsed[USER_INFO]=urlParts[2];parsed[HOST]=urlParts[3].toLowerCase();parsed[PORT]=urlParts[4]?parseInt(urlParts[4],10):undefined;parsed[PATH]=urlParts[5]||SLASH;parsed[QUERY]=this._parseQuery(urlParts[6]);parsed[FRAGMENT]=urlParts[7]}break;case RELATIVE:urlParts=url.match(URL_RELATIVE_REGEX);if(urlParts){parsed={};parsed[TYPE]=RELATIVE;parsed[PATH]=urlParts[1];parsed[QUERY]=this._parseQuery(urlParts[2]);parsed[FRAGMENT]=urlParts[3]}break;default:return(this._parse(url,ABSOLUTE)||this._parse(url,RELATIVE));break}if(parsed){this._url=parsed;return true}else{return false}},_parseQuery:function(queryString){if(!isString(queryString)){return}queryString=trim(queryString);var query=[],queryParts=queryString.split(AMP),queryPart,i,len;for(i=0,len=queryParts.length;i<len;i++){if(queryParts[i]){queryPart=queryParts[i].split(EQUALS);query.push(queryPart[1]?queryPart:[queryPart[0]])}}return query},_set:function(urlPart,val){this._url[urlPart]=val;if(val&&(urlPart===SCHEME||urlPart===USER_INFO||urlPart===HOST||urlPart===PORT)){this._url[TYPE]=ABSOLUTE}if(!val&&urlPart===HOST){this._url[TYPE]=RELATIVE}this._isValid=this._parse(this.toString());return this},_normalizePath:function(path){var pathParts,pathPart,pathStack,normalizedPath,i,len;if(path.indexOf(DOT_DOT_SLASH)>-1){pathParts=path.split(SLASH);pathStack=[];for(i=0,len=pathParts.length;i<len;i++){pathPart=pathParts[i];if(pathPart===DOT_DOT){pathStack.pop()}else{if(pathPart){pathStack.push(pathPart)}}}normalizedPath=pathStack.join(SLASH);if(path[0]===SLASH){normalizedPath=SLASH+normalizedPath}if(path[path.length-1]===SLASH&&normalizedPath.length>1){normalizedPath+=SLASH}}else{normalizedPath=path}return normalizedPath}}}());