A simple router.
- path -> name + params
- name + params -> path
$ npm install simple-router-b
import makeRouter from 'simple-router-b';
const config = [
{ name: 'users', template: '/users' },
{ name: 'user', template: '/users/:id' },
{ name: 'roles', template: '/roles/:id' }
];
const router = makeRouter(config);
const route = router.route('/users/123');
assert.deepEqual(route, { name: 'user', params: { id: '123' } });
const unknownRoute = router.route('/unknown');
assert(typeof unknownRoute === 'undefined');
const path = router.path('user', { id: '123' });
assert(path === '/users/123');
const unknownPath = router.path('unknown', { id: '123' });
assert(typeof unknownPath === 'undefined');
type Name = string;
type Path = string;
type Params = { [key: string]: string; };
type Route = { name: Name; params: Params; };
type Router = {
route: (path: Path) => Route;
path: (name: Name, params?: Params) => Path;
};
type RouterConfig = { name: Name; template: Template; }[];
type Template = string;
type init = (config: RouterConfig) => Router;
export default init;
https://gist.github.com/bouzuya/647039fa9fe5173f276def55590b6500