Socket
Socket
Sign inDemoInstall

mimic-fn

Package Overview
Dependencies
0
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mimic-fn

Make a function mimic another one


Version published
Weekly downloads
52M
decreased by-15.02%
Maintainers
2
Install size
8.99 kB
Created
Weekly downloads
 

Package description

What is mimic-fn?

The mimic-fn package is a utility that allows you to copy the properties of a function onto another function. This includes the function's name, length (the number of arguments it expects), and custom properties. It is useful when you want to wrap a function without losing its original properties, which can be important for debugging, introspection, and certain function-based APIs that rely on these properties.

What are mimic-fn's main functionalities?

Copying function properties

This feature allows you to copy all properties from the original function to the wrapper function, including the function name and custom properties.

const mimicFn = require('mimic-fn');

function original() {}
original.customProp = 'hello';

function wrapper() {
  return original();
}

console.log(wrapper.name); // 'wrapper'
console.log(wrapper.customProp); // undefined

mimicFn(wrapper, original);

console.log(wrapper.name); // 'original'
console.log(wrapper.customProp); // 'hello'

Other packages similar to mimic-fn

Readme

Source
mimic-fn

Make a function mimic another one

Useful when you wrap a function in another function and like to preserve the original name and other properties.

Install

$ npm install mimic-fn

Usage

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);
//=> '🦄'

console.log(String(wrapper));
//=> '/* Wrapped with wrapper() */\nfunction foo() {}'

API

mimicFunction(to, from, options?)

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.

to

Type: Function

Mimicking function.

from

Type: Function

Function to mimic.

options

Type: object

ignoreNonConfigurable

Type: boolean
Default: false

Skip modifying non-configurable properties instead of throwing an error.

  • rename-fn - Rename a function
  • keep-func-props - Wrap a function without changing its name and other properties

Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

Keywords

FAQs

Last updated on 07 Apr 2021

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc