# es6-class-hooks

> Use classHooks to wrap any function in your class.

Latest version **2.0.0** (published 2017-10-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install es6-class-hooks
pnpm add es6-class-hooks
yarn add es6-class-hooks
bun add es6-class-hooks
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2017-10-16 |
| First published | 2017-05-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Guy Segev |
| Maintainers | guyguyon |
| Keywords | es6, class, hooks, hook, wrap, js, node, before, after |

## Links

- npm: https://www.npmjs.com/package/es6-class-hooks
- Repository: https://github.com/guyguyon/es6-class-hooks
- Homepage: https://github.com/guyguyon/es6-class-hooks#readme
- Issues: https://github.com/guyguyon/es6-class-hooks/issues
- npm.io page: https://npm.io/package/es6-class-hooks

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.0 (latest) — 2017-10-16
- 1.0.5 — 2017-05-17
- 1.0.4 — 2017-05-16
- 1.0.3 — 2017-05-15
- 1.0.2 — 2017-05-15
- 1.0.1 — 2017-05-15
- 1.0.0 — 2017-05-15

## README

# es6-class-hooks

Use classHooks to wrap any function in your class.

## Installation

```bash
$ npm install es6-class-hooks --save
```

## Quick Start

To wrap your function use `classHooks(classObject, wrapFunc, whitelist)`
The returned value will be your wrapped class. 

use `whitelist` parameter to whitelist which functions will be wrapped.

Usage example: 

```javascript 
const classHooks = require('es6-class-hooks');

class Test {
    constructor() {
        this.someVar = 'just an instance variable';
    }

    foo(x) {
        console.log(`Executing foo with x=${x}`);
    }

    goo(y) {
        console.log(`Executing goo with y=${y}`);
    }
}

function wrapHook(instance, func, args) {
    console.log(`Printing ${instance.someVar}`);
    console.log(`Calling ${func.name} with args: ${JSON.stringify(args)}`);
    const res = func.call(this, ...args);
    console.log(`Called ${func.name} with args: ${JSON.stringify(args)}`);
    return res;
}


const TestWithHooks = classHooks(Test, wrapHook, ['foo']);

const testWithHooks = new TestWithHooks();

testWithHooks.foo(4);
console.log('============');
testWithHooks.goo(3);

//  Printing just an instance variable
//  Calling foo with args: [4]
//  Executing foo with x=4
//  Called foo with args: [4]
//  ============
//  Executing goo with y=3
```

## Updates
v2.0.0 - using `receiver` to bind `this` currectly. It is *strongly* suggested to use regular function as wrapper and bind this to `func` instead of arrow function so `this` will pass to the function currectly. Look on the code sample for more information.

---
_Source: https://npm.io/package/es6-class-hooks · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
