mimic-fn

  • Version 4.0.0
  • Published
  • 8.18 kB
  • No dependencies
  • MIT license

Install

npm i mimic-fn
yarn add mimic-fn
pnpm add mimic-fn

Overview

Make a function mimic another one

Index

Functions

function mimicFunction

mimicFunction: <
ArgumentsType extends unknown[],
ReturnType,
FunctionType extends (...arguments: ArgumentsType) => ReturnType
>(
to: (...arguments: ArgumentsType) => ReturnType,
from: FunctionType,
options?: Options
) => FunctionType;
  • Modifies the to function to mimic the from function. Returns the to function.

    name, displayName, and any other properties of from are copied. The length property is not copied. Prototype, class, and inherited properties are copied.

    to.toString() will return the same as from.toString() but prepended with a Wrapped with to() comment.

    Parameter to

    Mimicking function.

    Parameter from

    Function to mimic.

    Returns

    The modified to function.

    Example 1

    import mimicFunction from 'mimic-fn';
    function foo() {}
    foo.unicorn = '🦄';
    function wrapper() {
    return foo();
    }
    console.log(wrapper.name);
    //=> 'wrapper'
    mimicFunction(wrapper, foo);
    console.log(wrapper.name);
    //=> 'foo'
    console.log(wrapper.unicorn);
    //=> '🦄'

Interfaces

interface Options

interface Options {}

    property ignoreNonConfigurable

    readonly ignoreNonConfigurable?: boolean;
    • Skip modifying [non-configurable properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor#Description) instead of throwing an error.

      false

    Package Files (1)

    Dependencies (0)

    No dependencies.

    Dev Dependencies (3)

    Peer Dependencies (0)

    No peer dependencies.

    Badge

    To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

    You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/mimic-fn.

    • Markdown
      [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/mimic-fn)
    • HTML
      <a href="https://www.jsdocs.io/package/mimic-fn"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>