npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

switched

v0.1.2

Published

Route your data through a series of middleware functions.

Downloads

6

Readme

Build Status NPM version David DM

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 Switched instances to other Switched instances.
  • 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 [email protected]:turbonetix/switched.git

cd into the directory and install the dependencies

> cd switched
> npm install && npm shrinkwrap --dev

Running Tests

Install coffee-script

> npm install coffee-script -g

Tests are run using grunt. You must first globally install the grunt-cli with npm.

> sudo npm install -g grunt-cli

Unit Tests

To run the tests, just run grunt

> grunt spec