forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpath-utils.js
More file actions
1 lines (1 loc) · 2.87 KB
/
path-utils.js
File metadata and controls
1 lines (1 loc) · 2.87 KB
1
!function(root,factory){"function"==typeof define&&define.amd?define([],factory):"object"==typeof module&&module.exports?module.exports=factory():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&&"object"==typeof url)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||"";for(var absStack=(absPath=absPath?absPath.replace(/^\/|(\/[^\/]*|[^\/]+)$/g,""):"")?absPath.split("/"):[],relStack=relPath.split("/"),i=0;i<relStack.length;i++){var d=relStack[i];switch(d){case".":break;case"..":absStack.length&&absStack.pop();break;default:absStack.push(d)}}return"/"+absStack.join("/")},makePathRelative:function(pathA,pathB){pathB=pathB?pathB.replace(/^\/|\/?[^\/]*$/g,""):"",pathA=pathA?pathA.replace(/^\//,""):"";for(var stackB=pathB?pathB.split("/"):[],stackA=pathA.split("/"),stackC=[],len=stackB.length,upLevel=!1,startIndex=0,i=0;i<len;i++)(upLevel=upLevel||stackA[0]!==stackB[i])?stackC.push(".."):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,search,hash;return protocol+doubleSlash+authority+PathUtils.makePathAbsolute(relObj.pathname||absObj.filename,absObj.pathname)+(relObj.search||!hasPath&&absObj.search||"")+relObj.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],PathUtils[prop]||(PathUtils[prop]=getterFunc(prop));return PathUtils});