JSFiddle - React, Tailwind, and code Playground

by mckabue

JavaScript

//https://stackoverflow.com/a/14033636
(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, "\\$&");
        },
        extend: function (obj, src) {
            for (var key in src) {
                if (src.hasOwnProperty(key)) obj[key] = src[key];
            }
            return obj;
        },
        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 = utils.extend({
            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: '([^/]*)'
                }
            ]
     ...