# magic-hook

> Extends functions with pre hooks.

Latest version **2.0.2** (published 2016-07-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install magic-hook
pnpm add magic-hook
yarn add magic-hook
bun add magic-hook
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2016-07-03 |
| First published | 2015-11-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Zoltan Kochan |
| Maintainers | zkochan |
| Keywords | pre, pre-hook, pre-hooks, hook, hooks, middleware, middlewares, ext, extension, extensions, extending |

## Links

- npm: https://www.npmjs.com/package/magic-hook
- Repository: https://github.com/zkochan/magic-hook
- Homepage: https://github.com/zkochan/magic-hook#readme
- Issues: https://github.com/zkochan/magic-hook/issues
- npm.io page: https://npm.io/package/magic-hook

## Dependencies (1)

- [flatten](https://npm.io/package/flatten.md) ^1.0.2

## Alternatives

- [csv-to-markdown-table](https://npm.io/package/csv-to-markdown-table.md) — 47.0K weekly downloads
- [@sapphire/ratelimits](https://npm.io/package/@sapphire/ratelimits.md) — 4.4K weekly downloads
- [js-csvparser](https://npm.io/package/js-csvparser.md) — 2.0K weekly downloads
- [@adadapted/js-sdk](https://npm.io/package/@adadapted/js-sdk.md) — 251 weekly downloads
- [@grapecity/spread-sheets-sparklines](https://npm.io/package/@grapecity/spread-sheets-sparklines.md) — 103 weekly downloads

## Recent versions

- 2.0.2 (latest) — 2016-07-03
- 1.1.0-4 (tsc) — 2016-06-09
- 1.0.0-0 (canary) — 2016-02-09
- 2.0.1 — 2016-07-03
- 2.0.0 — 2016-06-09
- 1.1.0-3 — 2016-03-20
- 1.1.0-2 — 2016-03-20
- 1.1.0-1 — 2016-03-20
- 1.1.0-0 — 2016-03-20
- 1.0.0 — 2016-02-21
- 0.2.3 — 2016-02-07
- 0.2.2 — 2016-02-05
- 0.2.1 — 2016-02-05
- 0.2.0 — 2016-02-04
- 0.1.1 — 2016-01-08
- … 2 more at https://npm.io/package/magic-hook/versions

## README

# magic-hook

> Extends functions with pre hooks.

<!--@shields.flatSquare('travis', 'coveralls', 'npm')-->
[![Build Status](https://img.shields.io/travis/zkochan/magic-hook/master.svg?style=flat-square)](https://travis-ci.org/zkochan/magic-hook) [![Coverage Status](https://img.shields.io/coveralls/zkochan/magic-hook/master.svg?style=flat-square)](https://coveralls.io/r/zkochan/magic-hook?branch=master) [![npm version](https://img.shields.io/npm/v/magic-hook.svg?style=flat-square)](https://www.npmjs.com/package/magic-hook)
<!--/@-->

## Installation

```sh
npm i -S magic-hook
```

## Usage

You can add `pre hooks` to extend your methods.

<!--@example('./example/hook-logger.js')-->
```js
'use strict'
var hook = require('magic-hook')

// The target function
function concat(a, b) {
  return a + b
}

// The hooked function
var hookedConcat = hook(concat)

// A pre hook
var msgNo = 0
function counterHook(next, a, b) {
  return next('concatenation #' + (++msgNo) + ': ' + a, b)
}
hookedConcat.pre(counterHook)

for (var i = 3; i--;) console.log(hookedConcat('Hello ', 'world!'))
//> concatenation #1: Hello world!
//> concatenation #2: Hello world!
//> concatenation #3: Hello world!
```

Hooks can be removed using `removePre`:

```js
hookedConcat.removePre(counterHook)

console.log(hookedConcat('Hello ', 'world!'))
//> Hello world!
```

To remove all pres associated with a hook
just call removePre with no arguments:

```js
hookedConcat.removePre()
```

To abort the target function's execution just
don't call the `next` function in the pre hook:

```js
hookedConcat.pre(function(next) {
  return 'The original function was overwritten';
})

console.log(hookedConcat("Doesn't matter what goes here"))
//> The original function was overwritten
```

You can overwrite the target function's result as well:

```js
var hookedSum = hook(function(a, b) { return a + b; })

hookedSum.pre(function(sum, a, b) {
  if (a === 1 && b === 1) return 3

  return sum(a, b)
})

console.log(hookedSum(2, 2))
//> 4

console.log(hookedSum(1, 1))
//> 3
```
<!--/@-->

## Motivation

Suppose you have an object with a `save` method.

It would be nice to be able to declare code that runs before `save`.
For example, you might want to run validation code before every `save`.
Or you might want to create plugins that will modify the input parameters of
your `save` method.

## License

[MIT](./LICENSE) © [Zoltan Kochan](http://kochan.io)

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