-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath-utils.min.js
29 lines (29 loc) · 4.55 KB
/
path-utils.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* @license
* path-utils.js - version 0.0.2
* Copyright (c) 2011, Kin Blas
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
(function(root,factory){if(typeof define==="function"&&define.amd){define([],factory)}else if(typeof module==="object"&&module.exports){module.exports=factory()}else{root.PathUtils=factory()}})(this,function(){var PathUtils={urlParseRE:/^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#\.]*(?:\.[^\?#\.]+)*(\.[^\?#\.]+)|[^\?#]*)))?(\?[^#]+)?)(#.*)?/,parsedUrlPropNames:["href","hrefNoHash","hrefNoSearch","domain","protocol","doubleSlash","authority","userinfo","username","password","host","hostname","port","pathname","directory","filename","filenameExtension","search","hash"],defaultPorts:{http:"80",https:"443",ftp:"20",ftps:"990"},parseUrl:function(url){if(url&&typeof url==="object"){return url}var matches=PathUtils.urlParseRE.exec(url||"")||[],props=PathUtils.parsedUrlPropNames,cnt=props.length,result={},i;for(i=0;i<cnt;i++){result[props[i]]=matches[i]||""}return result},port:function(url){var u=PathUtils.parseUrl(url);return u.port||PathUtils.defaultPorts[u.protocol]},isSameDomain:function(absUrl1,absUrl2){return PathUtils.parseUrl(absUrl1).domain===PathUtils.parseUrl(absUrl2).domain},isRelativeUrl:function(url){return PathUtils.parseUrl(url).protocol===""},isAbsoluteUrl:function(url){return PathUtils.parseUrl(url).protocol!==""},makePathAbsolute:function(relPath,absPath){if(relPath&&relPath.charAt(0)==="/"){return relPath}relPath=relPath||"";absPath=absPath?absPath.replace(/^\/|(\/[^\/]*|[^\/]+)$/g,""):"";var absStack=absPath?absPath.split("/"):[],relStack=relPath.split("/");for(var i=0;i<relStack.length;i++){var d=relStack[i];switch(d){case".":break;case"..":if(absStack.length){absStack.pop()}break;default:absStack.push(d);break}}return"/"+absStack.join("/")},makePathRelative:function(pathA,pathB){pathB=pathB?pathB.replace(/^\/|\/?[^\/]*$/g,""):"";pathA=pathA?pathA.replace(/^\//,""):"";var stackB=pathB?pathB.split("/"):[],stackA=pathA.split("/"),stackC=[],len=stackB.length,upLevel=false,startIndex=0;for(var i=0;i<len;i++){upLevel=upLevel||stackA[0]!==stackB[i];if(upLevel){stackC.push("..")}else{stackA.shift()}}return stackC.concat(stackA).join("/")},makeUrlAbsolute:function(relUrl,absUrl){if(!PathUtils.isRelativeUrl(relUrl)){return relUrl}var relObj=PathUtils.parseUrl(relUrl),absObj=PathUtils.parseUrl(absUrl),protocol=relObj.protocol||absObj.protocol,doubleSlash=relObj.protocol?relObj.doubleSlash:relObj.doubleSlash||absObj.doubleSlash,authority=relObj.authority||absObj.authority,hasPath=relObj.pathname!=="",pathname=PathUtils.makePathAbsolute(relObj.pathname||absObj.filename,absObj.pathname),search=relObj.search||!hasPath&&absObj.search||"",hash=relObj.hash;return protocol+doubleSlash+authority+pathname+search+hash}};function getterFunc(propName){return function(url){return PathUtils.parseUrl(url)[propName]}}var i,prop,props=PathUtils.parsedUrlPropNames,cnt=props.length;for(i=0;i<cnt;i++){prop=props[i];if(!PathUtils[prop]){PathUtils[prop]=getterFunc(prop)}}return PathUtils});