@brickify/m-hook

1.0.0 • Public • Published

Module Hook

Apply deep before/after hooks to any methods/functions

Statistics

Github All Releases npm

Social

GitHub forks GitHub stars GitHub watchers Gitter

Project Health

Travis branch Codecov bitHound bitHound Website

Install

$ npm install @brickify/m-hook

Usage

var hook = require('@brickify/m-hook');

var ns = {
  foo: function (next, arg) {
    return next(arg + 1);
  },
  bar: function (next, arg) {
    return next(arg + 1);
  }
};


ns.bar.after = function (next, arg) {
  return next ? next(arg + 1) : arg + 1;
};

// Register hook after definitions
ns.bar = hook(ns.bar);

// Register hook before definitions
hook('ns.foo', ns);

ns.foo.after = function (next, arg) {
  return next ? next(arg + 1) : arg + 1;
};

ns.foo.before = function (next, arg) {
  return next(arg + 1);
};

ns.foo.before.after = function (next, arg) {
  return next(arg + 1);
};

ns.foo.before.before = function (next, arg) {
  return next(arg + 1);
};

// Run method
ns.foo(1);
// Output: 6

// Run method
ns.bar(1);
// Output: 3

TODO

  • Add JSDoc, comment and Code Signature
  • Add beforeEach and afterEach hooks

Dependents (0)

Package Sidebar

Install

npm i @brickify/m-hook

Weekly Downloads

2

Version

1.0.0

License

MIT

Last publish

Collaborators

  • nraibaud