wfn
A module that prepares Wrapper Functions.
Ensures your wrapper has the same properties as the underlying function, such as name
and length
. When toString()
is called on the wrapper function, the code of the original function is shown, together with a comment identifying the wrapper(s).
Installation
Requires Node.js 6.0.0 or above.
npm i wfn
API
The module exports a single function.
Parameters
fn
(Function): The underlying functionwrapper
(Function): The wrapper function that callsfn
Return Value
The modified wrapper
function
Usage
const wfn = { return 'result' } { return } // 'result' // Beforewrappername // 'wrapper'wrapperlength // 0wrapper // 'function wrapper () { return func() }' // Apply the module // Afterwrappername // 'func'wrapperlength // 3wrapper // 'function func (a, b, c) { return 'result' } /* [wfn] Wrapped with wrapper() */'
Wrapper Function Stringification
In the above example, note that the wrapper’s toString()
method produces the code of the underlying function — not of the wrapper — but with a comment notation that the function has been wrapped.
If you wrap a function more than once, wfn
will recognize this and will generate a comment notation that mentions all the wrappers. Consider the following example:
{ return 'value'} const wrapper1 = const wrapper2 = const wrapper3 =
If you call wrapper3.toString()
, you will get the following:
function original () {
/* [wfn] Wrapped with wrapper1(), wrapper3(), and 1 other unnamed function */
return 'value'
}
If this is something you absolutely don’t care about, and you only want properties like name
and length
copied, consider swapping out this module for copy-own
so you have less overhead.
Related Modules
For more projects like this, check out the xfn family of modules.