Package Exports
- magic-hook
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (magic-hook) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
magic-hook
Extends functions with pre hooks. Inspired by hooks-js.
Installation
$ npm install --save magic-hookMotivation
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.
Why no post hook as well?
post hooks are the same as events, so no need to replicate the functionality of
a regular event emitter.
Usage
You can add pre hooks to extend your methods.
const magicHook = require('magic-hook')
let log = magicHook(function(msg) {
console.log(msg)
})
let messageNumber = 0
function counterHook(next, msg) {
messageNumber++
next('message #' + messageNumber + ': ' + msg)
}
log.pre('log', counterHook);
// will output "message #1: Hello world!" to the console
log('Hello world!')
// hooks can be removed
log.removePre(counterHook)
// will output "Hello world!" to the console, because the counter hook was removed
log('Hello world!')
// To remove all pres associated with a hook just call removePre with no arguments:
log.removePre()License
The MIT License (MIT)