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

deqorator

v1.1.0

Published

A very lightweight queued decorator for Node.js to easily decorate objects using middleware.

Downloads

4

Readme

Deqorator

A very lightweight queued decorator for Node.js to easily decorate objects using middleware.

Build Status

To make you feel immediately familiar with the API, the syntax is based on the Express.js middleware syntax.

What is deqorator used for?

Suppose you have an object and you want to decorate it with data (e.g. fill properties with data from several database queries).

Instead of nesting all kinds of callbacks, deqorator allows you to easily create a reusable decorator using one or more layers of middleware.

Middleware layers are processed in the order you define them, so you can easily depend on data you added in a previous layer.

All without worrying about asynchronous callbacks.

Quick demo

First create a decorator:

var deqorator = require('deqorator');
    decorator = deqorator.createDecorator();

Then add as many middleware handlers as you like:

decorator.use(function(item, next){

    // The item to decorate is passed from middleware to middleware
    // and you can make changes to it
    item.middleware1 = "done";
    next();
});

Asynchronous callbacks are supported, just call next() when finished:

decorator.use(function(item, next){

    // Some asynchronous call
    setTimeout(function(){
        next();
    }, 1000);
});

If you want to raise an error, just call next() with an error and all subsequent middleware handlers are skipped:

decorator.use(function(item, next){

    // This will skip all middleware handlers after this one
    next('Halt immediately!')
});

Each middleware handler is passed the item and is processed in the order you add it to the decorator:

decorator.use(function(item, next){

    // The changes from the previous middleware handler are available here
    console.log(item.middleware1); // done
    next();
});

Once you have defined all the middleware for the decorator, you can use it to decorate as many objects as you like.

The callback of the decorate() function is called when the object has been completely decorated or when an error was raised:

var item = {};

decorator.decorate(item, function(err, item){

    // Error handler
    if(err){

        // The partially decorated item is available in case of an error
        console.log(item);

        console.log('Oops, something went wrong while decorating the object');
        return;
    }

    // The completely decorated item is available here if all went well
    console.log(item);
});

Method of operation

There is no need to worry about asynchronous behavior:

  • all middleware handlers are processed in the order you define them.
  • all objects are decorated in the order you decorate them.

So a scenario like this:

var item1 = {},
    item2 = {},
    item3 = {},
    decorator = new Deqorator(),
    middleware1 = function(item, next){ ... },
    middleware2 = function(item, next){ ... },
    middleware3 = function(item, next){ ... };

decorator.use(middleware1);
decorator.use(middleware2);
decorator.use(middleware3);

decorator.decorate(item1, function(err, item){ ... });
decorator.decorate(item2, function(err, item){ ... });
decorator.decorate(item3, function(err, item){ ... });

will be processed like this:

  1. item1 is decorated:
    1. middleware1 function called with item1
    2. middleware2 function called with item1
    3. middleware3 function called with item1
    4. if a decorate callback is defined, it is called with (null, item1)
    5. if an error occurs in one of the middleware handlers and a decorate callback is defined, it is immediately called with (err, item1) and subsequent middleware handlers are skipped
  2. item2 is decorated:
    1. middleware1 function called with item2
    2. middleware2 function called with item2
    3. middleware3 function called with item2
    4. if a decorate callback is defined, it is called with (null, item2)
    5. if an error occurs in one of the middleware handlers and a decorate callback is defined, it is immediately called with (err, item2) and subsequent middleware handlers are skipped
  3. item3 is decorated:
    1. middleware1 function called with item3
    2. middleware2 function called with item3
    3. middleware3 function called with item3
    4. if a decorate callback is defined, it is called with (null, item3)
    5. if an error occurs in one of the middleware handlers and a decorate callback is defined, it is immediately called with (err, item3) and subsequent middleware handlers are skipped

Change log

v1.0.0

  • Added factory function to create new decorators
  • Updated API for user convenience

v0.3.0

  • Removed error handling from middleware

v0.2.0

  • Added error handling
  • Updated documentation
  • Added additional unit tests

v0.1.0

  • Initial version