Skip to content

Instantly share code, notes, and snippets.

@fd
Created July 24, 2015 13:17
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 fd/b1645507237dba364ee4 to your computer and use it in GitHub Desktop.
Save fd/b1645507237dba364ee4 to your computer and use it in GitHub Desktop.
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