JSFiddle - React, Tailwind, and code Playground
by mckabue
JavaScript
//https://stackoverflow.com/a/14033636
//http://medialize.github.io/URI.js/
;; // prevents code from breaking if the previous scrips is not syntactically correct
(function (name, definition) {
if (typeof module != 'undefined') module.exports = definition();
else if (typeof define == 'function' && typeof define.amd == 'object') define(definition);
else this[name] = definition();
}('ROUTER', function () {
var utils = {
quoteRegExp: function (string) {
return string.replace(/[\\\[\]\^$*+?.()|{}]/g, "\\$&");
},
slice: function (array, start, end) {
start || (start = 0);
if (typeof end == 'undefined') {
end = array ? array.length : 0;
}
var index = -1,
length = end - start || 0,
result = Array(length < 0 ? 0 : length);
while (++index < length) {
result[index] = array[start + index];
}
return result;
}
};
var ROUTER = function (routes, options) {
if (!routes)
throw new Error("no 'route' or [routes]");
var _options = Object.assign({
compileRegexes: [{
reg: /\{[\*](\w+)[^?]}/g, // catches {*param} https://regex101.com/r/gT8wK5/749 , https://regex101.com/r/gVZG3f/2
rep: '(.+)'
},
{
reg: /\{[\*](\w+)\?}/g, // catches {*param?} https://regex101.com/r/8rtbCm/1
rep: '(.*)'
},
{
reg: /\{(\w+[^?])}/g, // catches {param} https://regex101.com/r/gVZG3f/5
rep: '([^/]+)'
},
{
reg: /\{(\w+)\?}/g, // catches {param?} https://regex101.com/r/gVZG3f/5
rep: '([^/]*)'
}
]
}, options);
var _routes = this.routes = (routes instanceof Array) ?...