call-hook
Hook function calls.
Prehooks execute before the callee (aka target) function executes and may alter the arguments sent to the callee or abort callee execution, while posthooks execute after the callee function, receive the same arguments as the callee, and may also access it's return value.
example
var pre = // or require('call-hook').post post = // or require('call-hook').pre { console} var quickVisit = var shakeGreet = // hello Jason console // hello Jason // goodbye Jason console // handshake // hello Anonymous
api
var pre = require('call-hook/pre'), // or require('call-hook').post
post = require('call-hook/post') // or require('call-hook').pre
hookedFunc = pre(callee, preCall)
Returns a new function, hookedFunc
, which when called executes the preCall
function prior to executing the callee
function. Normally, both functions
receive the arguments supplied to hookedFunc
, and the return value of
hookedFunc
is the return value of callee
. This behaviour may be changed (see
precall context below). The callee
function is executed in the same context as
hookedFunc, while the preCall
function is executed in the context of an object
that offers the following:
preCall context:
abort(returnValue)
- prevent thecallee
function from being executed and set the return value ofhookedFunc
toreturnValue
setArguments(arg1, arg2, ...)
- supply the given arguments tocallee
instead of the arguments supplied tohookedFunc
context
- the context thathookedFunc
was executed in
Example of altering arguments being sent to callee
:
var pre = { return Math} var rollD10 = console
Example of aborting:
var pre = { return Math} // hijack roll, if a 20 sided die is requested, always return 20var roll = console // 1 - 10console // always 20
hookedFunc = post(callee, postCall)
Returns a new function, hookedFunc
which executes the callee
function, followed
by the postCall
function. The return value of hookedFunc
is the return value
of the postCall
function. The postCall
context may be used to return the
callee
return value (see below). Both functions receive the same arguments passed to
hookedFunc
. The callee
function is executed in the same context that
hookedFunc
was, while the postCall
is executed in the context of an object
that offers the following:
postCall context:
returnValue
- contains the return value of thecallee
functioncontext
- the context thathookedFunc
was executed in
Example of accessing previous return value:
var post = { return Math} var printDieRoll =
install
With npm do:
npm install --save call-hook
testing
npm test
Or to run tests in phantom: npm run phantom
coverage
npm run view-cover
This will output a textual coverage report.
npm run open-cover
This will open an HTML coverage report in the default browser.