switched v0.1.2
Route your data through a series of middleware functions.
$ npm install switched
var router = require('switched');
router.on(/[\w\s]+/, function (arg, next, end) {
arg.count = 0;
next();
});
router.on(function (arg, next, end) {
arg.count++;
next();
});
router.on(function (err, arg, next, end) {
console.error(err);
arg.count++;
next();
});
router.on('some event', function (arg, next, end) {
arg.count++;
console.log('count %s', arg.count);
});
router('some event', {some:'argument'});Features
- Express-like routing capabilties for data.
- Gives you more control over how data is handled.
- Attach
Switchedinstances to otherSwitchedinstances. - Support for "wildcard" (*) and Regular Expression matching.
API
Router
Get the Router class.
var Router = require('switched');The use and on methods are equivalent. They also can be chained.
var router = Router()
.use(function (arg, next) { })
.use(function (arg, next) { })
.use(function (arg, next) { });Router#()
Make a Router instance
var router = Router();Router#use(fn:Function, ...)
Attach a function to the router.
router.use(function (arg, next) {
//do something!
next();
});You can pass in multiple functions.
var a = function (arg, next, end) { next() };
var b = function (arg, next, end) { next() };
var c = function (arg, next, end) { next() };
router.use(a,b,c); You can pass in a function that accepts an Error object.
router.use(function (err, arg, next, end) {
console.error(err);
//calling next(err) will invoke the next error handler.
//to resume operation just call next()
next(err);
});Router#use(event:String, fn:Function, ...)
Bind the function to the event.
router.use('some event', function (arg, next) {
next();
});You can also pass in multiple functions for handling the event.
var chop = function (arg, next, end) { next() };
var clean = function (arg, next, end) { next() };
var pretty = function (arg, next, end) { next() };
router.use('some event', chop, clean, pretty);Router#use(event:RegExp, fn:Function, ...)
Bind the function using a RegExp pattern to match the event.
router.use(/\w+/, function (arg, next, end) {
next();
});You can also pass in multiple functions for handling the event.
var chop = function (arg, next, end) { next() };
var clean = function (arg, next, end) { next() };
var pretty = function (arg, next, end) { next() };
router.use(/\w+/, chop, clean, pretty);Router#use(router:Router, ...)
You can attach another Router instance to your Router instance.
var another = Router();
another.use(function (arg, next, end) { next(); });
router.use(another);Attach multiple routers in a single call.
var foo = Router();
foo.use(function (arg, next, end) { next(); });
var bar = Router();
bar.use(function (arg, next, end) { next(); });
var baz = Router();
baz.use(function (arg, next, end) { next(); });
router.use(foo, bar, baz);Router#use(name:String, router:Router, ...)
Just like attaching a function to the router given the event. You can attach Router
instance as well to the event.
var foo = Router();
foo.use(function (arg, next, end) { next(); });
router.use('some event', foo);Attach multiple routers in a single call to the event too.
var foo = Router();
foo.use(function (arg, next, end) { next(); });
var bar = Router();
bar.use(function (arg, next, end) { next(); });
var baz = Router();
baz.use(function (arg, next, end) { next(); });
router.use('some event', foo, bar, baz);Router#use(fns:Array, ...)
Attach an Array of Fuction's or Router instances, or an Array or Arrays .
var middleware = [
function (arg, next, end) { next(); },
[
function (arg, next, end) { next(); },
Router().use(function (arg, next, end) { next(); }),
function (arg, next, end) { next(); },
],
Router().use(function (arg, next, end) { next(); })
];
var errHandler = function (err, arg, next, end) { next(err); }
router.use(middleware, errHandler);Router#use(name:String, fns:Array, ...)
Attach everything to an event.
var middleware = [
function (arg, next, end) { next(); },
[
function (arg, next, end) { next(); },
Router().use(function (arg, next, end) { next(); }),
function (arg, next, end) { next(); },
],
Router().use(function (arg, next, end) { next(); })
];
var errHandler = function (err, arg, next, end) { next(err); }
router.use('only this event', middleware, errHandler);Router#on(...)
This is an alias to to the use method. It does the same thing.
router.on(function (arg, next, end) { next() });Installation and Environment Setup
Install node.js (See download and install instructions here: http://nodejs.org/).
Clone this repository
> git clone git@github.com:turbonetix/switched.gitcd into the directory and install the dependencies
> cd switched
> npm install && npm shrinkwrap --devRunning Tests
Install coffee-script
> npm install coffee-script -gTests are run using grunt. You must first globally install the grunt-cli with npm.
> sudo npm install -g grunt-cliUnit Tests
To run the tests, just run grunt
> grunt spec