wrapper-roo v0.2.9
wrapper-roo
Wrap any function with custom hooks, executing code before and after the function, controlling arguments, result and so on, while still being as transparent as possible to the rest of the code.

Installation
The package can be locally installed from npm:
$ npm i wrapper-rooUsage
Pre- and post-hooks
A hook is a function to be executed before or after a given one:
const wrap = require('wrapper-roo')
function hello (name) { console.log('hello ' + name) }
const wrapped = wrap(hello).withPrePostHooks(
() => console.log('before'), // pre-hook
() => console.log('after')) // post-hook
wrapped('Crash')before
hello Crash
afterArguments and return value are forwarded, thus the wrapper can be used just like the original function. See the wiki for more information.
Function Invocation Information
Hooks receive information about the function call as their first argument:
const wrap = require('wrapper-roo')
function foo () { /* ... */ }
const wrapped = wrap(foo).withPreHook(data => {
console.log('Calling function ' + data.function.name)
console.log('Args: ' + data.arguments)
})
wrapped(1, 'hey')Calling function foo
Args: [1,hey]Function invocation metadata object received by hooks contains the following properties:
function: original function object to be wrapped;arguments: array of arguments that were given to the function;constructor: value ofnew.targetin the original call, namely, the function after thenewkeyword in constructor calls;this: object bound tothisfor the call;boundFunction: a version of the wrapped function that is already bound to arguments,thisandnew.target.
Additional fields are available to post-hooks:
result: return value;exception: thrown exception;success: whether the execution of the function completed normally or an exception was thrown.
Note: invocation data is read-only: modifications to properties above are not allowed and will have no effect on the function execution.
Custom Hooks
Greater control can be achieved with custom hooks, if needed:
const wrap = require('wrapper-roo')
function foo () { /* ... */ }
wrap(foo).withCustomHook((data, f) => { /* ... f() ... */ })Custom hooks conveniently receive as a second argument the wrapped function already bound to this and arguments, and already set up as a constructor call if needed.
Just call f().
Note that no automatic handling of binding, arguments, exceptions etc is performed when directly invoking the original function as data.function().
(Fluent) API
The following functions are exposed:
const wrap = require('wrapper-roo')
wrap(func).withPreHook(preHook)
wrap(func).withPostHook(postHook)
wrap(func).withPrePostHooks(preHook, postHook)
wrap(func).withCustomHook(hook)
wrap.the(func) // just wrap itCaveats
Of course, identity is not preserved: wrap.the(func) !== func.
The current implementation is based on the Proxy API. Caveats of this approach (corner cases) are listed here.
Contributing
Install Node.js and npm, clone the repo and cd into it.
To install dependencies:
$ npm iTo run linter, tests and get coverage report:
$ npm testWiki
See the wiki page for more information.
5 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago