Comparing version 1.1.1 to 2.0.0
@@ -8,6 +8,6 @@ var post = require('../post') | ||
var printDieRoll = post(roll, function print (sides) { | ||
console.log(sides + '-sided die roll result: ' + this.previousReturnValue) | ||
return this.previousReturnValue | ||
console.log(sides + '-sided die roll result: ' + this.returnValue) | ||
return this.returnValue | ||
}) | ||
printDieRoll(6) |
@@ -8,5 +8,5 @@ var pre = require('../pre') | ||
var rollD10 = pre(roll, function d10 () { | ||
return [10] | ||
this.setArguments(10) | ||
}) | ||
console.log('10-sided die roll result: ' + rollD10()) |
@@ -9,9 +9,9 @@ var pre = require('../pre') | ||
var printDieRoll = post(roll, function print (sides) { | ||
console.log(sides + '-sided die roll result: ' + this.previousReturnValue) | ||
console.log(sides + '-sided die roll result: ' + this.returnValue) | ||
}) | ||
var printD10Roll = pre(printDieRoll, function d10 () { | ||
return [10] | ||
this.setArguments(10) | ||
}) | ||
printD10Roll() |
{ | ||
"name": "call-hook", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "Hook function calls with other functions", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
module.exports = function post (callee, postCall) { | ||
return function callHook () { | ||
return postCall.apply({ | ||
previousReturnValue: callee.apply(undefined, arguments) | ||
returnValue: callee.apply(undefined, arguments) | ||
}, arguments) | ||
} | ||
} |
13
pre.js
module.exports = function post (callee, preCall) { | ||
return function callHook () { | ||
var aborted | ||
var result = preCall.apply({ abort: abort }, arguments) | ||
var aborted, | ||
rewrittenArgs, | ||
ctx = { abort: abort, setArguments: setArguments } | ||
preCall.apply(ctx, arguments) | ||
if (aborted) return aborted.returnValue | ||
return callee.apply(undefined, Array.isArray(result) ? result : arguments) | ||
return callee.apply(undefined, rewrittenArgs ? rewrittenArgs : arguments) | ||
@@ -11,3 +14,7 @@ function abort (returnValue) { | ||
} | ||
function setArguments () { | ||
rewrittenArgs = arguments | ||
} | ||
} | ||
} |
@@ -7,3 +7,3 @@ # call-hook | ||
Hook function calls with other functions. | ||
Hook function calls. | ||
@@ -55,13 +55,16 @@ Prehooks execute before the callee (aka target) function executes and may | ||
Returns a new function, `hookedFunc`, which executes the `preCall` function | ||
prior to executing the `callee` function. If `preCall` returns an `Array`, then | ||
that array will be applied to `callee` as arguments, otherwise both `preCall` | ||
and `callee` functions will receive the arguments of the `hookedFunc` function | ||
call. The callee is executed in an `undefined` context, while the `preCall` | ||
function is executed in the context of an object that offers the `abort` function. | ||
Calling `abort` will prevent `callee` from being called. The return value of | ||
the `hookedFunc` function call will be the return value of `callee`, unless | ||
`abort` was called, in which case the returnValue of `hookedFunc` will be the | ||
1st argument to `abort`. | ||
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 an undefined context, | ||
while the `preCall` function is executed in the context of an object that offers | ||
the following: | ||
*preCall context:* | ||
* `abort(returnValue)` - prevent the `callee` function from being executed and | ||
set the return value of `hookedFunc` to `returnValue` | ||
* `setArguments(arg1, arg2, ...)` - supply the given arguments to `callee` | ||
instead of the arguments supplied to `hookedFunc` | ||
Example of altering arguments being sent to `callee`: | ||
@@ -77,3 +80,3 @@ | ||
var rollD10 = pre(roll, function d10 () { | ||
return [10] | ||
this.setArguments(10) | ||
}) | ||
@@ -105,10 +108,11 @@ | ||
Returns a new function, `hookedFunc` which executes the `callee` function, followed | ||
by the `postCall` function. Both functions receive the same arguments passed to | ||
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 an `undefined` context, while | ||
the `postCall` is executed in the context of an object that offers `previousReturnValue`, | ||
which may be used to access the return value of the `callee` function. The | ||
return value of `hookedFunc` is the return value of `postCall`. If you do not | ||
wish to alter the return value of `callee`, then it's important to return | ||
`this.previousReturnValue` in `postCall`. | ||
the `postCall` is executed in the context of an object that offers the following: | ||
*postCall context:* | ||
* `returnValue` - contains the return value of the `callee` function | ||
Example of accessing previous return value: | ||
@@ -124,4 +128,4 @@ | ||
var printDieRoll = post(roll, function print (sides) { | ||
console.log(sides + '-sided die roll result: ' + this.previousReturnValue) | ||
return this.previousReturnValue | ||
console.log(sides + '-sided die roll result: ' + this.returnValue) | ||
return this.returnValue | ||
}) | ||
@@ -128,0 +132,0 @@ |
@@ -37,3 +37,3 @@ var test = require('tape'), | ||
post(callee, function () { | ||
t.deepEqual(this.previousReturnValue, { val: true }, 'this.previousReturnValue is present') | ||
t.deepEqual(this.returnValue, { val: true }, 'this.returnValue is present') | ||
})() | ||
@@ -40,0 +40,0 @@ |
@@ -21,7 +21,8 @@ var test = require('tape'), | ||
test('pre should not alter callee args if non-array is returned', function (t) { | ||
t.plan(15) | ||
test('pre should not alter callee args if setArguments is not called', function (t) { | ||
t.plan(18) | ||
var args = ['hello', 'world'] | ||
pre(callee, function () { return 1 }).apply(undefined, args) | ||
pre(callee, function () { return [1, 2, 3] }).apply(undefined, args) | ||
pre(callee, function () { return {} }).apply(undefined, args) | ||
@@ -39,6 +40,6 @@ pre(callee, function () { return true }).apply(undefined, args) | ||
test('pre should alter callee args if it returns an array', function (t) { | ||
test('pre should alter callee args if setArguments is called', function (t) { | ||
t.plan(3) | ||
pre(callee, function () { return [42, 'pencil'] })('this', 'is', 1, 'test') | ||
pre(callee, function () { this.setArguments(42, 'pencil') })('this', 'is', 1, 'test') | ||
@@ -45,0 +46,0 @@ function callee () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14216
199
155