Wrap a function without changing its name and other properties.
Function wrappers return a new function which means the original function's
name and other properties are usually lost. This module enhances them to keep
those properties instead.
The new function will also print the original function's body when stringified.
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.
Hire me
Please
reach out
if you're looking for a Node.js API or CLI engineer (11 years of experience).
Most recently I have been Netlify Build's
and Netlify Plugins'
technical lead for 2.5 years. I am available for full-time remote positions.
Example
import keepFuncProps from'keep-func-props'// Any function wrapper worksimport memoize from'lodash/memoize.js'const betterMemoize = keepFuncProps(memoize)
constanyFunction = () => true// Function `name` and other properties are keptconsole.log(anyFunction) // `[Function: anyFunction]`console.log(memoize(anyFunction)) // `[Function: memoized]`console.log(betterMemoize(anyFunction)) // `[Function: anyFunction]`// Function body is kept when stringifiedconsole.log(String(anyFunction))
// () => trueconsole.log(String(memoize(anyFunction)))
// function() {// var args = arguments,// key = resolver ? resolver.apply(this, args) : args[0],// cache = memoized.cache;// ...// }console.log(String(betterMemoize(anyFunction)))
// /* Wrapped with memoized() */// () => true
import keepFuncProps from'keep-func-props'// Any function wrapper worksconstwrapper =
(anyFunction) =>
(...args) =>anyFunction(...args)
// `betterWrapper` is like `wrapper` but it keeps the function propertiesconst betterWrapper = keepFuncProps(wrapper)
keepFuncProps(wrapper)
wrapper: (anyFunction, [...args]) => newFunction Returns: new wrapper
A function wrapper is passed as argument. A copy of it is returned.
See also
mimic-function: same but
for functions that do not wrap other functions.
Wrap a function without changing its name and other properties
The npm package keep-func-props receives a total of 102,444 weekly downloads. As such, keep-func-props popularity was classified as popular.
We found that keep-func-props demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.It has 1 open source maintainer collaborating on the project.
Package last updated on 05 Nov 2023
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.