uri-template-router
Version:
Match a URI to a pattern in a collection of URI Templates
38 lines (34 loc) • 1.01 kB
JavaScript
const { hrtime } = require('node:process');
const { Router } = require('./');
router = new Router;
var pieces = [
['http://'],
['', 'www.', 'subdomain.', 'users.'],
['google','youtube', 'facebook'],
['.com'],
['/', '/index', '/page/{id}', '/user/{id}', '/blog/{yyyy}{/mm,dd,slug}', '/~{user}', '/listing/{id}',
'/cart', '/status','/path/{base}{/path*}',
],
['', '.html', '.json', '.xml'],
// ['', '{?x}'],
];
var patterns = pieces.reduce(function(state, list){
var copy = [];
state.forEach(function(base){
list.forEach(function(segment){
copy.push(base+segment);
})
});
return copy;
}, ['']);
console.log(patterns.length);
function add(template){
const start = hrtime();
router.addTemplate(template);
const duration = hrtime(start);
console.log((duration[0] * 1e9 + duration[1]) / router.routes.length);
process.stdout.write(Math.round(router.routes.length/patterns.length*100)+'%\r');
}
patterns.forEach(add);
router.reindex();
console.log('Added '+patterns.length+' templates');