Wrap a function without changing its name, length and other properties.
Function wrappers are commonly used in functional programming. They take a
function as input and return it wrapped. Examples include
memoizing or ensuring a function is
only called once.
However those wrappers return a new function which means the original
function's name
, length
and other properties are lost. This module
enhances a function wrapper to keep those properties.
const keepFuncProps = require('keep-func-props')
const memoize = require('lodash/memoize')
const betterMemoize = keepFuncProps(memoize)
const anyFunction = function() {
return true
}
console.log(memoize(anyFunction))
console.log(betterMemoize(anyFunction))
Installation
npm install keep-func-props
Usage
const keepFuncProps = require('keep-func-props')
const functionWrapper = function(func) {
return (...args) => func(...args)
}
const betterWrapper = keepFuncProps(functionWrapper)
The function wrapper must:
- take a function as first argument
- take additional arguments (e.g. an options object)
- return a new function
Each of those requirements is optional.
Related projects
If you want to modify a function that is not a function wrapper, check out
mimic-fn
.