JSFiddle - React, Tailwind, and code Playground
JavaScript
var Router = {
'routes': [
{
'uri' : 'about'
},
{
'uri' : /rank\/([0-9]{1,})$/
},
]
};
doesUriExistInRouter = function(uri) {
var routeFound = null;
_(Router.routes).each(function(route){
var routeUri = route.uri;
var regexPattern = null;
if( routeUri.toString()[routeUri.toString().length - 2] == '$' ) {// is defined as regex
regexPattern = new RegExp( '^' + routeUri.toString().substr(0, routeUri.toString().length - 2) + '$' );
} else {
regexPattern = new RegExp( '^' + routeUri + '$' );
}
if (!_.isNull(uri.match(regexPattern))) {
routeFound = routeUri;
return;
}
});
return routeFound != null ? routeFound : false;
}
_(['about', '/rank/2', 'nope']).each(function(uri){
if(doesUriExistInRouter(uri) !== false) {
console.log('Router for '+uri+' found');
} else {
console.log('Router for '+uri+' NOT found');
}
});