Created
July 24, 2015 13:17
-
-
Save fd/b1645507237dba364ee4 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
let who = "Simon Menke"; | |
let spec = ['age', 28]; | |
let u = url`/hello/${who}/${spec}`; | |
// => "/hello/Simon%20Menke/age/28" | |
function url(tmpl, ...args) { | |
let len = tmpl.length-1; | |
let url = tmpl[0]; | |
for (let i = 0; i<len; i++) { | |
let val = args[i]; | |
if (val instanceof Array) { | |
for (let idx in val) { | |
if (idx > 0) url += "/"; | |
url += window.encodeURIComponent(`${val[idx]}`); | |
} | |
} else { | |
url += window.encodeURIComponent(`${val}`); | |
} | |
url += tmpl[i+1]; | |
} | |
return url; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment