ruut
A tiny and fast route parsing library, supporting parameters and more.
This module can be used on both server and client sides. For a demo using this on the client side see the demo page.
Installation
$ npm i ruut
Example
// Dependenciesvar Ruut = ; // Create the routervar router = ; console;// => { data: 'This is the home page', params: {} } console;// => { data: 'Blog homepage', params: {} } console;// => { data: 'Blog page', params: { page: '10' } } console;// => { data: 'Blog article', params: { article: 'some-article' } } console;// => { data: 'comments', params: { article: 'some-article' } } console;// => { data: 'Current comment', params: { article: 'some-article', id: 'my-comment' } } console;// => { data: 'Sign up route', params: {} } console;// => { data: 'Users list', params: {} } console;// => { data: 'User profile', params: { user: 'ionicabizau' } } console;// => { data: 'editor_file', params: { user: 'ionicabizau', project: 'my-project' } }
Documentation
Ruut(routes)
Creates a new router.
Params
-
Array|Object
routes
: An array or an object representing routes: If it's an array, then the format is: -
0
(Function|Non-null-values): If function, then it will be called and the data will be the returned value, otherwise the non-null
value. -
1
(Array|Object): A route object/array (recursive definition). -
2
(Function|Non-null-values): This value/function is returned/called when no other routes were found.
If the routes
value is an object it should be in the following format:
// /some "some": // /some/route "route": // /some/route/with "with": // /some/route/with/anything-you-want ":dynamic": // /some/route/with/anything-you-want/value { return "This can be a function or a non-null value. The dynamic value is: " + paramsdynamic; }
Return
- Function The
check
function getting the pathname as argument.
check(routes, pathname)
search Searches the pathname in the routes object, recursively. This is used internally.
Params
- Routes
routes
: The routes to check the pathname in. - String
pathname
: The pathname.
Return
- Object An object containing:
is_valid
(Boolean):true
if the route was foud,false
otherwise._
(Function|Non-null-value): The route data.params
(Object): The dynamic parameters.
check(routes, pathname)
Searches the pathname in the routes object and prepares the final object. This is used internally.
Params
- Routes
routes
: The routes to check the pathname in. - String
pathname
: The pathname.
Return
- Object The found route containing
data
andparams
fields ornull
otherwise.
How to contribute
Have an idea? Found a bug? See how to contribute.
License
See the LICENSE file.