Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

call-hook

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

call-hook - npm Package Compare versions

Comparing version 2.1.0 to 3.0.0

test/context.js

6

example/post-return-value.js
var post = require('../post')
function roll (sides) {
return Math.ceil(Math.random() * sides)
return Math.ceil(Math.random() * sides)
}
var printDieRoll = post(roll, function print (sides) {
console.log(sides + '-sided die roll result: ' + this.returnValue)
return this.returnValue
console.log(sides + '-sided die roll result: ' + this.returnValue)
return this.returnValue
})
printDieRoll(6)
var pre = require('../pre')
function roll (sides) {
return Math.ceil(Math.random() * sides)
return Math.ceil(Math.random() * sides)
}
var rollD10 = pre(roll, function d10 () {
this.setArguments(10)
this.setArguments(10)
})
console.log('10-sided die roll result: ' + rollD10())

@@ -5,13 +5,13 @@ var pre = require('../pre')

function roll (sides) {
return Math.ceil(Math.random() * sides)
return Math.ceil(Math.random() * sides)
}
var printDieRoll = post(roll, function print (sides) {
console.log(sides + '-sided die roll result: ' + this.returnValue)
console.log(sides + '-sided die roll result: ' + this.returnValue)
})
var printD10Roll = pre(printDieRoll, function d10 () {
this.setArguments(10)
this.setArguments(10)
})
printD10Roll()

@@ -5,11 +5,11 @@ var pre = require('../pre'), // or require('..').post

function hello (name) {
console.log('hello ' + name)
console.log('hello ' + name)
}
var quickVisit = post(hello, function goodbye (name) {
console.log('goodbye ' + name)
console.log('goodbye ' + name)
})
var shakeGreet = pre(hello, function handshake () {
console.log('handshake')
console.log('handshake')
})

@@ -22,3 +22,3 @@

quickVisit('Jason') // hello Jason
// goodbye Jason
// goodbye Jason

@@ -28,2 +28,2 @@ console.log('\n')

shakeGreet('Jason') // handshake
// hello Anonymous
// hello Anonymous
module.exports = {
pre : require('./pre'),
post: require('./post')
pre : require('./pre'),
post: require('./post')
}
{
"name": "call-hook",
"version": "2.1.0",
"version": "3.0.0",
"description": "Hook function calls with other functions",

@@ -31,3 +31,3 @@ "main": "index.js",

"devDependencies": {
"@jasonpincin/standard": "~5.0.0-8",
"@jasonpincin/standard": "~5.3.1-3",
"faucet": "0.0.1",

@@ -45,2 +45,3 @@ "istanbul": "~0.4.0",

"travis-test": "npm run -s tap && ((cat coverage/lcov.info | coveralls) || exit 0)",
"travis-browser-test": "zuul -- test/*.js",
"faucet": "istanbul cover --report lcov --print none -- tape test/*.js | faucet",

@@ -47,0 +48,0 @@ "faucet-nocover": "tape test/**/*.js | faucet",

module.exports = function post (callee, postCall) {
callHook.callee = callee
return callHook
callHook.callee = callee
return callHook
function callHook () {
return postCall.apply({
returnValue: callee.apply(undefined, arguments)
}, arguments)
}
function callHook () {
return postCall.apply({
context : this,
returnValue: callee.apply(this, arguments)
}, arguments)
}
}
module.exports = function post (callee, preCall) {
callHook.callee = callee
return callHook
callHook.callee = callee
return callHook
function callHook () {
var aborted,
rewrittenArgs,
ctx = { abort: abort, setArguments: setArguments }
function callHook () {
var aborted,
rewrittenArgs,
ctx = {
abort : abort,
setArguments: setArguments,
context : this
}
preCall.apply(ctx, arguments)
if (aborted) return aborted.returnValue
return callee.apply(undefined, rewrittenArgs ? rewrittenArgs : arguments)
preCall.apply(ctx, arguments)
if (aborted) return aborted.returnValue
return callee.apply(this, rewrittenArgs || arguments)
function abort (returnValue) {
aborted = { returnValue: returnValue }
}
function abort (returnValue) {
aborted = { returnValue: returnValue }
}
function setArguments () {
rewrittenArgs = arguments
}
function setArguments () {
rewrittenArgs = arguments
}
}
}

@@ -6,2 +6,3 @@ # call-hook

[![Coverage Status](https://coveralls.io/repos/jasonpincin/call-hook/badge.png?branch=master)](https://coveralls.io/r/jasonpincin/call-hook?branch=master)
[![Sauce Test Status](https://saucelabs.com/browser-matrix/jp-project3.svg)](https://saucelabs.com/u/jp-project3)

@@ -22,11 +23,11 @@ Hook function calls.

function hello (name) {
console.log('hello ' + name)
console.log('hello ' + name)
}
var quickVisit = post(hello, function goodbye (name) {
console.log('goodbye ' + name)
console.log('goodbye ' + name)
})
var shakeGreet = pre(hello, function handshake () {
console.log('handshake')
console.log('handshake')
})

@@ -60,5 +61,5 @@

`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 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:

@@ -70,2 +71,3 @@ *preCall context:*

instead of the arguments supplied to `hookedFunc`
* `context` - the context that `hookedFunc` was executed in

@@ -112,7 +114,9 @@ Example of altering arguments being sent to `callee`:

`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 the following:
`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 the `callee` function
* `context` - the context that `hookedFunc` was executed in

@@ -119,0 +123,0 @@ Example of accessing previous return value:

@@ -7,5 +7,5 @@ var test = require('tape'),

test('index should expose pre and post', function (t) {
t.equal(hook.pre, pre, 'hook.pre = pre')
t.equal(hook.post, post, 'hook.post = post')
t.end()
t.equal(hook.pre, pre, 'hook.pre = pre')
t.equal(hook.post, post, 'hook.post = post')
t.end()
})

@@ -5,64 +5,64 @@ var test = require('tape'),

test('post should return a function with a callee property', function (t) {
t.plan(2)
t.plan(2)
var hooked = post(callee, function () {})
t.equal(typeof hooked, 'function', 'hooked is a function')
t.equal(hooked.callee, callee, 'callee set')
var hooked = post(callee, function () {})
t.equal(typeof hooked, 'function', 'hooked is a function')
t.equal(hooked.callee, callee, 'callee set')
function callee () {}
function callee () {}
})
test('post should execute postCall after callee', function (t) {
t.plan(2)
t.plan(2)
var postCalled = false,
calleeCalled = false
var postCalled = false,
calleeCalled = false
post(callee, function () {
postCalled = true
t.equal(calleeCalled, true, 'callee called before postCall')
})()
post(callee, function () {
postCalled = true
t.equal(calleeCalled, true, 'callee called before postCall')
})()
function callee () {
calleeCalled = true
t.equal(postCalled, false, 'postCall not called before callee')
}
function callee () {
calleeCalled = true
t.equal(postCalled, false, 'postCall not called before callee')
}
})
test('post should be called with same args as callee', function (t) {
t.plan(2)
t.plan(2)
post(callee, function (arg1) {
t.equal(arg1, 42, 'arg1 = 42')
})(42)
post(callee, function (arg1) {
t.equal(arg1, 42, 'arg1 = 42')
})(42)
function callee (arg1) {
t.equal(arg1, 42, 'arg1 = 42')
}
function callee (arg1) {
t.equal(arg1, 42, 'arg1 = 42')
}
})
test('post should have access to previous return value', function (t) {
t.plan(1)
t.plan(1)
post(callee, function () {
t.deepEqual(this.returnValue, { val: true }, 'this.returnValue is present')
})()
post(callee, function () {
t.deepEqual(this.returnValue, { val: true }, 'this.returnValue is present')
})()
function callee () {
return { val: true }
}
function callee () {
return { val: true }
}
})
test('post hookedFunc should return postCall returnValue', function (t) {
t.plan(1)
t.plan(1)
var returnValue = post(callee, function () {
return 'hi'
})()
var returnValue = post(callee, function () {
return 'hi'
})()
t.equal(returnValue, 'hi', 'returnValue = hi')
t.equal(returnValue, 'hi', 'returnValue = hi')
function callee () {
return 42
}
function callee () {
return 42
}
})

@@ -5,102 +5,104 @@ var test = require('tape'),

test('pre should return a function with a callee property', function (t) {
t.plan(2)
t.plan(2)
var hooked = pre(callee, function () {})
t.equal(typeof hooked, 'function', 'hooked is a function')
t.equal(hooked.callee, callee, 'callee set')
var hooked = pre(callee, function () {})
t.equal(typeof hooked, 'function', 'hooked is a function')
t.equal(hooked.callee, callee, 'callee set')
function callee () {}
function callee () {}
})
test('pre should execute preCall before callee', function (t) {
t.plan(2)
t.plan(2)
var preCalled = false,
calleeCalled = false
var preCalled = false,
calleeCalled = false
pre(callee, function () {
preCalled = true
t.equal(calleeCalled, false, 'callee not called before preCall')
})()
pre(callee, function () {
preCalled = true
t.equal(calleeCalled, false, 'callee not called before preCall')
})()
function callee () {
calleeCalled = true
t.equal(preCalled, true, 'preCall called before callee')
}
function callee () {
calleeCalled = true
t.equal(preCalled, true, 'preCall called before callee')
}
})
test('pre should not alter callee args if setArguments is not called', function (t) {
t.plan(18)
test('pre should not alter callee args by default', 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)
pre(callee, function () { return true }).apply(undefined, args)
pre(callee, function () { return false }).apply(undefined, args)
pre(callee, function () { return undefined }).apply(undefined, args)
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)
pre(callee, function () { return true }).apply(undefined, args)
pre(callee, function () { return false }).apply(undefined, args)
pre(callee, function () { return undefined }).apply(undefined, args)
function callee () {
t.equal(arguments.length, 2, 'arguments.length = 2')
t.equal(arguments[0], 'hello', 'arg 0 = hello')
t.equal(arguments[1], 'world', 'arg 1 = world')
}
function callee () {
t.equal(arguments.length, 2, 'arguments.length = 2')
t.equal(arguments[0], 'hello', 'arg 0 = hello')
t.equal(arguments[1], 'world', 'arg 1 = world')
}
})
test('pre should alter callee args if setArguments is called', function (t) {
t.plan(3)
t.plan(3)
pre(callee, function () { this.setArguments(42, 'pencil') })('this', 'is', 1, 'test')
pre(callee, function () {
this.setArguments(42, 'pencil')
})('this', 'is', 1, 'test')
function callee () {
t.equal(arguments.length, 2, 'arguments.length = 2')
t.equal(arguments[0], 42, 'arg[0] = 42')
t.equal(arguments[1], 'pencil', 'arg[1] = pencil')
}
function callee () {
t.equal(arguments.length, 2, 'arguments.length = 2')
t.equal(arguments[0], 42, 'arg[0] = 42')
t.equal(arguments[1], 'pencil', 'arg[1] = pencil')
}
})
test('pre hookedFunc returnValue is callee returnValue', function (t) {
t.plan(2)
t.plan(2)
t.equal(pre(callee, function () { return 'hi' })(), 42, 'returnValue = 42')
t.equal(pre(callee, function () {})(), 42, 'returnValue = 42')
t.equal(pre(callee, function () { return 'hi' })(), 42, 'returnValue = 42')
t.equal(pre(callee, function () {})(), 42, 'returnValue = 42')
function callee () {
return 42
}
function callee () {
return 42
}
})
test('pre abort prohibits call to callee', function (t) {
t.plan(1)
t.plan(1)
var returnValue = pre(callee, function () {
this.abort('interrupted!')
})()
var returnValue = pre(callee, function () {
this.abort('interrupted!')
})()
t.equal(returnValue, 'interrupted!', 'returnValue = interrupted!')
t.equal(returnValue, 'interrupted!', 'returnValue = interrupted!')
function callee () {
t.fail('callee should not be called')
return true
}
function callee () {
t.fail('callee should not be called')
return true
}
})
test('pre abort does not prohibit subsequent calls', function (t) {
t.plan(5)
t.plan(5)
var showEvens = pre(callee, filterOdd)
var showEvens = pre(callee, filterOdd)
t.equal(showEvens(0), 0)
t.equal(showEvens(1), undefined)
t.equal(showEvens(2), 2)
t.equal(showEvens(3), undefined)
t.equal(showEvens(4), 4)
t.equal(showEvens(0), 0)
t.equal(showEvens(1), undefined)
t.equal(showEvens(2), 2)
t.equal(showEvens(3), undefined)
t.equal(showEvens(4), 4)
function filterOdd (number) {
if (number % 2 === 1) this.abort()
}
function filterOdd (number) {
if (number % 2 === 1) this.abort()
}
function callee (number) {
return number
}
function callee (number) {
return number
}
})

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc