ruut

1.1.0 • Public • Published

ruut

ruut

A tiny and fast route parsing library, supporting parameters and more.

ruut

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

// 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' } }
 

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
                    "value": function (params) {
                        return "This can be a function or a non-null value. The dynamic value is: " + params.dynamic;
                    }
                }
            }
        }
    }
}

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 and params fields or null otherwise.

How to contribute

Have an idea? Found a bug? See how to contribute.

License

See the LICENSE file.

Package Sidebar

Install

npm i ruut

Weekly Downloads

1

Version

1.1.0

License

MIT

Last publish

Collaborators

  • ionicabizau