with-hooks

1.0.1 • Public • Published

with-hooks

This is a simple package that allows you to add hooks to class prototype methods.

Usage

Define a class

class Cat {
  constructor(name) {
    this.name = name
  }

  getGreeting(greeted) {
    return `${this.name} greets ${greeted}`
  }
}

Define the with-hooks config object

let config = {
  getGreeting: { // this name matches name of function
    pre: { 
      func: (...args) => {
        console.log(`getGreeting pre hook: ${args}`)
      }
    },
    post: {
      func: (results) => {
        console.log(`getGreeting post hook: ${results}`)
      }
    }
  }
}

Wrap the Cat class with the hooks

const CatWithHooks = withHooks(config)(Cat)

Instantiate a CatWithHooks object

let garfield = new CatWithHooks('Garfield')

Now when getGreeting is invoked, the functions you defined in the configuration object will be called.

garfield.getGreeting('Dave')

Readme

Keywords

Package Sidebar

Install

npm i with-hooks

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

3.17 kB

Total Files

5

Last publish

Collaborators

  • faradayfan