Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/debjitbis08/ub-router

A front-end router with path segment validation
https://github.com/debjitbis08/ub-router

Last synced: 22 days ago
JSON representation

A front-end router with path segment validation

Awesome Lists containing this project

README

        

ub-router
=========

A front-end router with path segment validation.

Examples
--------

###Basic Usage

```javascript
var StaticR = function () {
return function handle() {
var el = document.getElementById("content");
el.innerHTML = "Static 3";
};
};

var CountR = function (n) {
var map = [ "One", "Two" ];
return function handle() {
var el = document.getElementById("content");
el.innerHTML = map[n - 1];
};
};

Routes
.add("page/three", StaticR)
.add("page/#Number", CountR)
.listen();

/**
* "page/1" is valid
* "page/two" is invalid
* "page/three" is valid
*/
```

###Adding a custom path piece data type

To create a new PathPiece data type use the pathPiece
function. It will also be available in the Routes.types
namespace for future use.

```javascript
Routes.pathPiece("Natural", function (piece) {
var n = Routes.fromPathPiece(Routes.types.Integer)(piece);
if (n === null || n < 0) { return null; } else { return n; }
}, function (n) {
return String(n);
});

Routes
.add("page/#Natural", CountR)
.listen();
```