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 🙏

© 2026 – Pkg Stats / Ryan Hefner

named-hooks

v3.0.4

Published

Define and invoke hooks based on function names

Readme

Build Status

#named-hooks

Allow hooks to be defined and invoked in order based on its names.

#Usage

By default, named-hooks use filenames and - as delimiters to define hook invoke order.

Inside ./hooks/

// master.js
module.exports = {
  // sync hook
  hook1: function (data/*, context */) {
    // manipulate `data`
    data.count += 1;

    // return new `data`
    return data;
  },

  // async hooks take a `resolve` parameter
  hook2: function (data, context, resolve) {
    // manipulate `data`
    resolve(data);
  },

  // async hooks take a `reject` parameter too
  hook3: function (data, context, resolve, reject) {
    // manipulate `data`
    reject(data);
  }

  // as many other hooks as you want
};
// Flow1.js
module.exports = {
  hook1Flow1: function (data) {
    // manipulate `data`
    data.count += 1;

    // return new `data`
    return data;
  }

  // ...
};
// Flow1-v2.js
module.exports = {
  hook1Flow1v2: function (data, context, resolve) {
    // make an async operation, like calling a service
    setTimeout(function () {
      // manipulate `data` after response
      data.count += 1;

      // resolve hook
      resolve(data);
    }, 1000);
  }

  // ...
};

Using named-hooks

var myHooks = require('named-hooks')('myHooks');

// Load hooks from `hooks` folder
myHooks.init('./hooks');

var data = {
  count: 0
};

// `invoke` will call hooks defined in `./hooks/` folder and return a promise
//    hooks called:
//      1. 'hook1'
//      2. 'hook1Flow1'
//      3. 'hook1Flow1v2'
myHooks.invoke('hook1', 'Flow1-v2', data/*, context*/).then(function (result) {
  console.log(data);   // { count: 0 }
  console.log(result); // { count: 3 }
});

#API

#init(folder)

Load all files from folder synchronously and populate hooks, you'll probably do this only once.

#getPossibleHookNames(hookName, identifier)

Returns an Array with all possible hook names defined by the combination of these arguments.

#defineHookResolutionRules(callback)

If the order doesn't make sense to your project and you have other business rules, you can define your own way to resolve the hook names.

#invoke(hookName, identifier, data, context)

Use invoke if any hook is async, it returns a promise with the transformed data. It'll invoke all hooks returned by #getPossibleHookNames(hookName, identifier) that are defined repassing all arguments provided. Arguments are passed by value, so each hook needs to return the new modified value if synchronous or resolve the promise if asynchronous. context is a way to make local variables available to hook implementations and it's optional.

#invokeChain(hookName, identifier, context)

Returns a transform function that will resolve a promise chain. Works the same way as #invoke.

// master.js
module.exports = {
  // async hook
  hookName: function (data, context, resolve, reject) {
    console.log(context.foo); // 'bar'
    setTimeout(resolve, 1000, data + 5);
  }
};

In the middle of a promise chain:

  // example using `q` module for promises, it'll work with
  // any promise implementation that complies with A+
  q(5)
    .then(namedHooks.invokeChain('hookName', 'indentifier', { foo: 'bar' }))
    .then(console.log);

  // outputs `10`

#invokeSync(hookName, identifier, data)

Works the same way as #invoke, but returns the actual transformed data instead of a promise, useful if all hooks are exclusively synchronous.All hooks needs to return the new modified value.

#Contributing Any PR is more than welcome, just make sure your stuff is tested and that all tests are passing.

tl;dr: npm test must be green and if I delete your code it should be red :)

#License

MIT