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.
$ npm i ruut
// Dependencies
var Ruut = require("ruut");
// Create the router
var router = Ruut([
function (route) {
console.log("This is the home page.", route);
return "This is the home page"
}
, {
":user": ["user-profile", {
":project": {
"build": "build"
, "editor": [null, {}, "editor_file" ]
}
}]
, "blog": ["Blog homepage", {
"page": {
":page": "Blog page"
}
, ":article": ["Blog article", {
"comments": ["comments", {
":id": "Current comment"
}]
}]
}]
// Route: /signup
, "signup": "Sign up route"
, "users": ["Users list", {
":user": "User profile"
}]
}
]);
console.log(router("/"));
// => { data: 'This is the home page', params: {} }
console.log(router("/blog"));
// => { data: 'Blog homepage', params: {} }
console.log(router("/blog/page/10"));
// => { data: 'Blog page', params: { page: '10' } }
console.log(router("/blog/some-article"));
// => { data: 'Blog article', params: { article: 'some-article' } }
console.log(router("/blog/some-article/comments"));
// => { data: 'comments', params: { article: 'some-article' } }
console.log(router("/blog/some-article/comments/my-comment"));
// => { data: 'Current comment', params: { article: 'some-article', id: 'my-comment' } }
console.log(router("/signup"));
// => { data: 'Sign up route', params: {} }
console.log(router("/users"));
// => { data: 'Users list', params: {} }
console.log(router("/users/ionicabizau"));
// => { data: 'User profile', params: { user: 'ionicabizau' } }
console.log(router("/ionicabizau/my-project/editor/edit/some/file.js"));
// => { data: 'editor_file', params: { user: 'ionicabizau', project: 'my-project' } }
Creates a new router.
-
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
"value": function (params) {
return "This can be a function or a non-null value. The dynamic value is: " + params.dynamic;
}
}
}
}
}
}
- Function The
check
function getting the pathname as argument.
search Searches the pathname in the routes object, recursively. This is used internally.
- Routes
routes
: The routes to check the pathname in. - String
pathname
: The pathname.
- 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.
Searches the pathname in the routes object and prepares the final object. This is used internally.
- Routes
routes
: The routes to check the pathname in. - String
pathname
: The pathname.
- Object The found route containing
data
andparams
fields ornull
otherwise.
Have an idea? Found a bug? See how to contribute.
See the LICENSE file.