Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function relativize(url, to) {
let [,base,origin] = to.match(/^(([a-z]+:\/\/[^\/]+)(\/.*?)?)(\/[^\/]*)?$/i)
url = String(url).replace(/^\/([^\/])/g, `${origin}/$1`);
if (!url.match(/^([a-z]+\:)\/\//i)) url = `${base}/${url}`;
return url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment