JSFiddle - React, Tailwind, and code Playground

by aruss

HTML

<div id="output"></div>

JavaScript

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('&') : '');
	}; 
}

$('#output').append('/blog/{postId}/comments'.toUrl({ postId: 'awesome-post', take: 10, skip: 20 }));