Easily make one function imitate or mimic the properties and behaviors of another while preserving its identity.
Install via npm:
npm install function-imitator
import { imitate } from 'function-imitator';
function original() {
console.log('This is the original function');
}
function imitator() {
console.log('This is the imitator function');
}
// Make `imitator` mimic `original`
imitate(imitator, original);
// Calling `imitator` now behaves like `original`
imitator(); // Outputs: "This is the original function"
The imitate
function accepts an optional third argument for configuration:
- ignoreNonConfigurable (default: false): If set to true, the function won't attempt to copy non-configurable properties from the source to the target function.
Example:
imitate(to, from, { ignoreNonConfigurable: true });
There are scenarios where you might want to replace a function with another (e.g., for mocking, extending behaviors, or A/B testing) but still want to preserve some of the original function's identity and characteristics. This utility facilitates that.
- Protected properties like
length
,prototype
,arguments
, andcaller
are not copied from the source function. - Functions being passed should not be arrow functions, as they do not have properties like
prototype
.
MIT