hapi-swagger 'startswith' bug fix
Illustration of a bug fix for hapi-swagger that ensures all relevant routes, and only relevant routes, are sent to Swagger.
HTML
<div id="output"></div>
JavaScript
var o = document.getElementById('output');
var toFind = "movies";
var re = '^/' + toFind + '(/|$)';
var routes = ['/movies','/movies/','/movies/{id}','/movies/{id}/','/movies/scenes','/movies/scenes/','/moviescenes','/moviescenes/','/actor/movies','/actor/movies/'];
var arrLen = routes.length;
// Info... not needed for this example to function
o.innerHTML += "Illustration of a bug fix for <a href='https://github.com/glennjones/hapi-swagger'>hapi-swagger</a>. HAPI routes are selected to be sent to Swagger based on a simple string match. Previously the match would be for something like '/movies/', with a trailing '/' but this is, imo, uncommon and would exclude common route names like '/movies'. A <a href='https://github.com/glennjones/hapi-swagger/commit/dd3e8536d0bae6c6e3cde0dc7598f188d6da8b33'>fix</a> was applied so that the match would now accept '/movies' but that will allow routes which don't match but simply start with the same few characters see the '/moviescenes' example below).";
o.innerHTML += '<br><br>What is wanted is to identify routes which start with, for example, "/' + toFind + '/", and are followed by zero or more characters, or that contain only "/' + toFind + ' or "/' + toFind + '/". To achieve this I have used <i>search</i> with a regular expression rather than using <i>indexOf</i>.<br>';
o.innerHTML += '<br><b>Routes which return a value of 0 are valid.</b><br>';
o.innerHTML += '<b>Routes which return a non-zero value are not valid.</b><br>';
o.innerHTML += '<b>OLD denotes the original <i>indexOf</i> code.<br>NEW denotes the <i>search</i> with regular expression.</b><br><br>';
// Info end
for (var i = 0; i < arrLen; i++) {
o.innerHTML += 'OLD: ' + routes[i] + ' = ' + routes[i].indexOf('/' + toFind) + '<br>';
o.innerHTML += 'NEW: ' + routes[i] + ' = ' + routes[i].search(re) + '<br>';
o.innerHTML += '<br>';
}