abstract-nested-router
Minimal nested-routing impl!
Last updated 7 months ago by pateketrueke .
MIT · Original npm · Tarball · package.json
$ cnpm install abstract-nested-router 
SYNC missed versions from official npm registry.

Abstract Nested Router

It tries to capture all matching routes from its root.

Build Status NPM version Coverage Status Known Vulnerabilities

import Router from 'abstract-nested-router';

const r = new Router();

// single routes
r.add('/', { is: 'home' });
r.add('/*_', { is: 'catch' });

// nested routes
r.mount('/:a', () => {
  r.add('/*_', { is: 'undef' });
  r.add('/:b/:c', { is: 'nested' });
});

r.find('/');
[ { is: 'home', params: {}, route: '/', path: '/' } ]

r.find('/test');
[ { is: 'home', params: {}, route: '/', path: '/' },
  { is: 'undef',
    params: { a: 'test' },
    route: '/:a',
    path: '/test' } ]

r.find('/x/y');
[ { is: 'home', params: {}, route: '/', path: '/' },
  { is: 'undef', params: { a: 'x' }, route: '/:a', path: '/x' },
  { is: 'nested',
    params: { a: 'x', b: 'y' },
    route: '/:a/:b',
    path: '/x/y' } ]

r.find('/x/y/z');
[ { is: 'home', params: {}, route: '/', path: '/' },
  { is: 'undef', params: { a: 'x' }, route: '/:a', path: '/x' },
  { is: 'nested',
    params: { a: 'x', b: 'y' },
    route: '/:a/:b',
    path: '/x/y' },
  { is: 'nested',
    params: { a: 'x', b: 'y', c: 'z' },
    route: '/:a/:b/:c',
    path: '/x/y/z' } ]

r.find('/x/y/z/0');
// Error: Unreachable '/x/y/z/0', segment '/0' is not defined

r.find('/x/y/z/0', true);
[ { is: 'home', params: {}, route: '/', path: '/' },
  { is: 'catch',
    params: { _: 'x/y/z/0' },
    route: '/*_',
    path: '/x/y/z/0' } ]

In the latter example catch is resolved just after the previous failure of /x/y/z/0 because we're trying at least twice.

API

Available methods:

  • resolve(path, cb) — Progressively finds and invoke callback with (err, routes) as input, useful for third-party integrations, e.g. yrv
  • mount(path, cb) — Allow to register routes under the same route
  • find(path[, retries]) — Look up routes by path, in case of failure try passing retries as true
  • add(path[, routeInfo]) — Register a single route by path, additional info will be returned on match
  • rm(path) — Remove a single route by full-path, it will fail if given route is not registered!

Options:

While routeInfo can include anything, but special keys are considered:

  • key — Unique identity for any route handler
  • exact — Tell if routing should match exactly or not
  • nested — If true, then key is inherited on any sub-handlers

Params

By default all segments are optional, e.g. /a/:b/:c matches with /a, /a/x and /a/x/y so you can say :b and :c are optional parameters.

More advanced cases would require fragments to be optional, e.g. /:foo(-bar) matches with /x and /x-bar because -bar is an optional fragment.

In the latter case params.foo will always be x regardless if -bar is appended, if you want to match bar then use /:foo(-:suffix) instead.

Splat parameters will consume the rest of the segments/fragments if they're present, e.g. /x*y captures anything that begins with x and stores it on params.y so it matches /xy, /xabc, /x/y, /x/a/b/c and so on.

Every parameter can hold simple regex-like patterns, e.g. /:id<\d+>

Supported patterns:

  • /:x and /*y are optional segments and they cannot be empty
  • <...> to hold regex-like patterns, -$. are escaped, / is forbidden
  • (...) are used to mark fragments as optional, it translates to (?:...)?

Please avoid / inside (...) or <...> as they will fail loudly!

Nesting

Consider the following examples:

// 1. regular
r.add('/a');
r.add('/a/:b');
r.add('/a/:b/:c');

// 2. nested
r.mount('/a', () => {
  r.mount('/:b', () => {
    r.add('/:c');
  });
});

// 3. concise
r.add('/a/:b/:c');

In the former way (1) we're declaring each route-level by hand, however they can be expressed at once as that latter one (3) which is more concise.

The middle form (2) is a shortcut to produce concise routes.

So which one is the best? It depends on the context:

  • Use concise routes to share the same routeInfo on all segments, it will be applied only if it's not yet defined on the route.
  • Use nested routes to use shared paths, it's convenient for creating stacks of context while mounting routes, etc.
  • Use regular routes to gain full control over its definition, this way each route can have its own separated context.

Routes are sorted and matched by priority and type, routes with splat params will be tried last. As more static and with less parameters the route will be matched sooner!

Current Tags

  • 0.1.20                                ...           latest (7 months ago)

24 Versions

  • 0.1.20                                ...           7 months ago
  • 0.1.19                                ...           7 months ago
  • 0.1.18                                ...           7 months ago
  • 0.1.17                                ...           7 months ago
  • 0.1.16                                ...           7 months ago
  • 0.1.15                                ...           7 months ago
  • 0.1.14                                ...           7 months ago
  • 0.1.13                                ...           a year ago
  • 0.1.12                                ...           a year ago
  • 0.1.11                                ...           a year ago
  • 0.1.10                                ...           a year ago
  • 0.1.9                                ...           a year ago
  • 0.1.8                                ...           a year ago
  • 0.1.7                                ...           a year ago
  • 0.1.6                                ...           a year ago
  • 0.1.5                                ...           a year ago
  • 0.1.4                                ...           a year ago
  • 0.1.3                                ...           a year ago
  • 0.1.2                                ...           a year ago
  • 0.1.1                                ...           a year ago
  • 0.1.0                                ...           a year ago
  • 0.0.3                                ...           a year ago
  • 0.0.2                                ...           a year ago
  • 0.0.1                                ...           a year ago
Maintainers (1)
Downloads
Today 3
This Week 14
This Month 113
Last Day 0
Last Week 54
Last Month 251
Dependencies (0)
None
Dev Dependencies (12)
Dependents (3)

Copyright 2014 - 2016 © taobao.org |