Skip to content

Instantly share code, notes, and snippets.

@aruss
Last active December 19, 2015 18:29
Show Gist options
  • Save aruss/5999464 to your computer and use it in GitHub Desktop.
Save aruss/5999464 to your computer and use it in GitHub Desktop.
Generate URI with path and query http://jsfiddle.net/aruss/wfCLT/1/
if (!String.prototype.toUrl) {
String.prototype.toUrl = function(o) {
var r = this.replace(/\{([^{}]*)\}/g,function (a, b) {
var r = o[b];
delete o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
), p = [];
for (var k in o) {
if (o[k] !== undefined && o[k] !== null) {
var v = o[k];
if (isNaN(v))
v = escape(v);
p.push(k + '=' + v);
}
}
return r + ((p.length > 0) ? '?' + p.join('&') : '');
};
}
@dreikant
Copy link

i like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment