UNPKG

uri-template-router

Version:

Match a URI to a pattern in a collection of URI Templates

49 lines (43 loc) 1.54 kB
'use strict'; var { Router, Route, toViz } = require('.'); const { parallel, union, reverse, compare, reduce } = require('./lib/fsm.js'); const router = new Router(); const a = new Route('f{a}x'); const b = new Route('{+b}'); [a, b].forEach(r => console.log(`/* ${r.uriTemplate} */\n`, toViz(r.fsm))); console.log('/* union */\n', toViz(union([a, b].map(r => r.fsm)))); // console.log(compare([a.fsm, b.fsm])); router.addTemplate(b); // console.log('/* router fsm */\n', toViz(union(router.routes.map(r => r.fsm)))); // router.routes.forEach(r => console.log(`/* ${r.uriTemplate} */\n`, toViz(r.fsm))); // console.log(router.toViz()); router.addTemplate(a); console.log(router.resolveURI('fox')); function subtract(fsms){ function partial_union(states){ } function final_union(states){ const finals = states.map(final => !!(final && (Array.isArray(final) ? final.length : final))); return finals[0]===true && finals[1]===false; } return parallel(fsms, partial_union, final_union); } // console.log(toViz(reduce(subtract([aa.fsm, bb.fsm])))); /* http://localhost/~{user} http://localhost/blog{/slug} http://localhost/blog http://localhost/blog/ http://localhost/blog/{slug}.md http://localhost/blog/{slug}.html http://localhost/static{/path*} http://localhost/ http://localhost/a/a/b/c http://localhost/a/a/b{/c} http://localhost/a/a{/b}{/c} http://localhost/a{/a}{/b}{/c} http://localhost/d/{document_any} http://localhost/d/{document_txt}.txt http://localhost/d/{document_html}.html http://localhost/d/{document_json}.json */