UNPKG

yurl

Version:

A URL manipulation library that offers support for daisy chaining, pathname resolution and query args manipulation.

78 lines (77 loc) 2.46 kB
"use strict"; /* * What follows has been directly extracted from the `resolve-pathname` module, * written by Michael Jackson and MIT-licensed. All credit goes to the original * author! * * GitHub repo: https://github.com/mjackson/resolve-pathname * Original source: https://github.com/mjackson/resolve-pathname/blob/20152400021377da9aa5e25fc0846ebe2d17fdcb/modules/index.js */ Object.defineProperty(exports, "__esModule", { value: true }); exports.resolvePathname = void 0; const isAbsolute = (pathname) => { return pathname.charAt(0) === '/'; }; // About 1.5x faster than the two-arg version of Array#splice() const spliceOne = (list, index) => { for (let i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1) { list[i] = list[k]; } list.pop(); }; // This implementation is based heavily on node's url.parse const resolvePathname = (to, from) => { if (from === undefined) from = ''; var toParts = (to && to.split('/')) || []; var fromParts = (from && from.split('/')) || []; var isToAbs = to && isAbsolute(to); var isFromAbs = from && isAbsolute(from); var mustEndAbs = isToAbs || isFromAbs; if (to && isAbsolute(to)) { // to is absolute fromParts = toParts; } else if (toParts.length) { // to is relative, drop the filename fromParts.pop(); fromParts = fromParts.concat(toParts); } if (!fromParts.length) return '/'; var hasTrailingSlash; if (fromParts.length) { var last = fromParts[fromParts.length - 1]; hasTrailingSlash = last === '.' || last === '..' || last === ''; } else { hasTrailingSlash = false; } var up = 0; for (var i = fromParts.length; i >= 0; i--) { var part = fromParts[i]; if (part === '.') { spliceOne(fromParts, i); } else if (part === '..') { spliceOne(fromParts, i); up++; } else if (up) { spliceOne(fromParts, i); up--; } } if (!mustEndAbs) for (; up--; up) fromParts.unshift('..'); if (mustEndAbs && fromParts[0] !== '' && (!fromParts[0] || !isAbsolute(fromParts[0]))) fromParts.unshift(''); var result = fromParts.join('/'); if (hasTrailingSlash && result.slice(-1) !== '/') result += '/'; return result; }; exports.resolvePathname = resolvePathname;