MCPcopy Index your code
hub / github.com/mozilla/source-map / computeRelativeURL

Function computeRelativeURL

lib/util.js:255–283  ·  view source on GitHub ↗

* Given two URLs that are assumed to be on the same * protocol/host/user/password build a relative URL from the * path, params, and hash values. * * @param rootURL The root URL that the target will be relative to. * @param targetURL The target that the relative URL points to. * @return A rootU

(rootURL, targetURL)

Source from the content-addressed store, hash-verified

253 * @return A rootURL-relative, normalized URL value.
254 */
255function computeRelativeURL(rootURL, targetURL) {
256 if (typeof rootURL === "string") rootURL = new URL(rootURL);
257 if (typeof targetURL === "string") targetURL = new URL(targetURL);
258
259 const targetParts = targetURL.pathname.split("/");
260 const rootParts = rootURL.pathname.split("/");
261
262 // If we've got a URL path ending with a "/", we remove it since we'd
263 // otherwise be relative to the wrong location.
264 if (rootParts.length > 0 && !rootParts[rootParts.length - 1]) {
265 rootParts.pop();
266 }
267
268 while (
269 targetParts.length > 0 &&
270 rootParts.length > 0 &&
271 targetParts[0] === rootParts[0]
272 ) {
273 targetParts.shift();
274 rootParts.shift();
275 }
276
277 const relativePath = rootParts
278 .map(() => "..")
279 .concat(targetParts)
280 .join("/");
281
282 return relativePath + targetURL.search + targetURL.hash;
283}
284
285/**
286 * Given a URL, ensure that it is treated as a directory URL.

Callers 3

createSafeHandlerFunction · 0.85
joinFunction · 0.85
relativeIfPossibleFunction · 0.85

Calls 1

joinMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…