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

hooks.js

v0.3.20

Published

Library to mount hooks to functions

Readme

hooks.js

License MIT NPM version Downloads Dependencies

Hooks library provides full support for adding pre and post hooks to independent functions and functions in objects.

I. Installation

npm install hooks.js --save

Use it like this:

var hooks = require('hooks.js');

// Hookify!
hooks.hookify(myObject);

// set hook before function
myObject.someFunction.$hooks.pre(function($input, $inspect) {...});

// set hook after function
myObject.someFunction.$hooks.post(function($input, $inspect, $output) {...});

// now hooks are called before and after someFunction
myObject.someFunction();

There is access to arguments inserted to function when it is called ($input) in both .pre() and .post() hooks.

There is output value returned from hookified function for .post() hook only ($output) which can be naturally optionally overwritten by .post() hook with simple return "myNewValue"; statement (if value in return statement is anything but undefined, if undefined is returned, hookified function will still return original value).

Also you've got some great references in $inspect object for both .pre() and .post() hooks, if it's setter or getter, property name is resolved (e.g. getName -> $inspect.property (name), setMyScores -> $inspect.property (myScores) ).

Use cases

Hooks can be very useful for hooking setters and getters if you don't want to put too much complex logic inside them in the class:

function myClass () {

  this._constructor = function() {
    hooks.hookify(this);
    this.setName.$hooks.pre(function($input, $inspect){
      if ($input[0].length < 5)// reference to firstArgument of myClass.setName(firstArgument)
        throw new Error($inspect.property + ' is too short'); // name is too short
    });
    this.getName.$hooks.post(function($input, $inspect, $output){
      return $output.toUpperCase(); // overwrite original output, e.g. "John" would change to "JOHN"
    });
    this.getName.$hooks.pre(function($input, $inspect){
      if (this[$inspect.property] === undefined) // this.name === undefined?
        throw new Error($inspect.property + ' is undefined'); // name is undefined
    });
  };

  this.setName = function(name) {
    this.name = name;
    return this;
  };

  this.getName = function() {
    return this.name;
  };

}

You can also add hooks in batch on only matching functions:

hooks.hookify(myObject);

myObject.$hooks.pre('^get(.*?)$', function($input, $inspect) {
  console.log('Getter fired');
});

myObject.$hooks.pre(new RegExp('^get(.*?)$'), function($input, $inspect) {
  var propertyName = $inspect.property;
  if (this[propertyName] === undefined)
    throw new Error(propertyName + ' property is undefined');
});

myObject.$hooks.$getters.pre(function($input, $inspect) {
  // Do something
});

To see logs of hooks actions in console, you can call hooks.setLog(true);.

II. API - ASSIGN

mount

Arguments: (@original_function:Array, @usePromise:Boolean(optional), @context:Object(optional))

myFunction = hooks.mount(myFunction);

hookify

Arguments: (@object:Object, @regex:String||RegExp(optional), @usePromise:Boolean(optional))

hooks.hookify(myObject);

or

hooks.hookify(myObject, 'getters');

or

hooks.hookify(myObject, '^get(.*?)$');

II. API - FUNCTION

You can use them on function on which you called first:

myFunction = hooks.mount(myFunction);

or

hooks.hookify(myObject);
var myFunction = myObject.myFunction;

pre

before

Arguments: (@prehook_callback:Function)

myFunction.$hooks.pre(function($input, $inspect){
  // Something
});

post

after

Arguments: (@posthook_callback:Function)

myFunction.$hooks.post(function($input, $inspect, $output){
  // Something
});

clean

Arguments: none

myFunction.$hooks.clean();

III. API - OBJECT

pre

before

Arguments: (@regex:String||RegExp, @prehook_callback:Function)

myObject.$hooks.pre('^get(.*?)$', function($input, $inspect) {
  // Something
});
// OR
myObject.$hooks.pre(new RegExp('^get(.*?)$'), function($input, $inspect) {
  // Something
});
// OR
myObject.$hooks.$getters.pre(function($input, $inspect) {
  // Something
});
// OR
myObject.$hooks.$setters.pre(function($input, $inspect) {
  // Something
});

post

after

Arguments: (@regex:String||RegExp, @posthook_callback:Function)

myObject.$hooks.post(new RegExp('^set(.*?)$'), function($input, $inspect, $output) {
  // Something
});

IV. Examples

1) Hookify

If you want to add hooks to all functions in the object:

var hooks = require('hooks.js');

...

var myCustomObject = {
  this.myFunction = function(arg1, arg2) {
  }
  this.myOtherFunction = function() {
  }
};

...

// Hookify!
hooks.hookify(myClassObj);

...

// Set custom hook actions
myCustomObject.myFunction.$hooks.pre(function($input, $inspect) {
  console.log('Do sth before');
});
myCustomObject.myOtherFunction.$hooks.post(function($input, $inspect, $output) {
  console.log('Do sth after');
});

...

// Call functions
myClassObj.myFunction();
myClassObj.myOtherFunction();
myClassObj.myFunction();

2) Mount

If you want to add hooks to only one specified function:

var myFunction = function  (user_name) {
  // Something
}

...

// Mount hook to the function
myFunction = hooks.mount(myFunction);

...

// Set custom hook actions
myFunction.$hooks.pre(function($input, $inspect) {
  console.log('Do sth before');
});

...

// Call function
myFunction('Anna');

License

MIT