Created
July 22, 2019 10:45
-
-
Save ezekielchentnik/ea6d8f5a06650d797c7c575b667097ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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