Skip to content

Instantly share code, notes, and snippets.

@ezekielchentnik
Created July 22, 2019 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezekielchentnik/ea6d8f5a06650d797c7c575b667097ae to your computer and use it in GitHub Desktop.
Save ezekielchentnik/ea6d8f5a06650d797c7c575b667097ae to your computer and use it in GitHub Desktop.
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