UNPKG

panels-normalise-uri

Version:
72 lines (56 loc) 1.58 kB
'use strict'; // original src: https://github.com/substack/path-browserify function normaliseArray(parts, allowAboveRoot) { // if the path tries to go above the root, `up` ends up > 0 var up = 0; for (var i = parts.length - 1; i >= 0; i--) { var last = parts[i]; if (last === '.') { parts.splice(i, 1); } else if (last === '..') { parts.splice(i, 1); up++; } else if (up) { parts.splice(i, 1); up--; } } // if the path is allowed to go above the root, restore leading ..s if (allowAboveRoot) { for (; up--; up) { parts.unshift('..'); } } return parts; } function normalisePath(rawPath) { var isAbsolute = rawPath.charAt(0) === '/'; var trailingSlash = rawPath.substr(-1) === '/'; // normalise the path var path = normaliseArray(rawPath.split('/').filter(function (p) { return !!p; }), !isAbsolute).join('/'); if (!path && !isAbsolute) { path = '.'; } if (path && trailingSlash) { path += '/'; } return (isAbsolute ? '/' : '') + path; } var TRAILING_SLASH_REGEX = /\/$/; function withTrailingSlash(uri) { return TRAILING_SLASH_REGEX.test(uri) ? uri : uri + "/"; } var ORPHAN_TELEPORT = /(.*)https?:\/$/; function normaliseUri(uri) { var finalUri = normalisePath(withTrailingSlash(uri)); var matchOrphanTeleport = finalUri.match(ORPHAN_TELEPORT); if (matchOrphanTeleport) { finalUri = matchOrphanTeleport[1]; } finalUri = finalUri.replace(/:\//g, '://'); return finalUri; } module.exports = normaliseUri; //# sourceMappingURL=cjs.js.map