Last change
on this file since 6fe77af was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | # mimic-fn [![Build Status](https://travis-ci.org/sindresorhus/mimic-fn.svg?branch=master)](https://travis-ci.org/sindresorhus/mimic-fn)
|
---|
| 2 |
|
---|
| 3 | > Make a function mimic another one
|
---|
| 4 |
|
---|
| 5 | Useful when you wrap a function in another function and like to preserve the original name and other properties.
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | ## Install
|
---|
| 9 |
|
---|
| 10 | ```
|
---|
| 11 | $ npm install mimic-fn
|
---|
| 12 | ```
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | ## Usage
|
---|
| 16 |
|
---|
| 17 | ```js
|
---|
| 18 | const mimicFn = require('mimic-fn');
|
---|
| 19 |
|
---|
| 20 | function foo() {}
|
---|
| 21 | foo.unicorn = '🦄';
|
---|
| 22 |
|
---|
| 23 | function wrapper() {
|
---|
| 24 | return foo();
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | console.log(wrapper.name);
|
---|
| 28 | //=> 'wrapper'
|
---|
| 29 |
|
---|
| 30 | mimicFn(wrapper, foo);
|
---|
| 31 |
|
---|
| 32 | console.log(wrapper.name);
|
---|
| 33 | //=> 'foo'
|
---|
| 34 |
|
---|
| 35 | console.log(wrapper.unicorn);
|
---|
| 36 | //=> '🦄'
|
---|
| 37 | ```
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | ## API
|
---|
| 41 |
|
---|
| 42 | It will copy over the properties `name`, `length`, `displayName`, and any custom properties you may have set.
|
---|
| 43 |
|
---|
| 44 | ### mimicFn(to, from)
|
---|
| 45 |
|
---|
| 46 | Modifies the `to` function and returns it.
|
---|
| 47 |
|
---|
| 48 | #### to
|
---|
| 49 |
|
---|
| 50 | Type: `Function`
|
---|
| 51 |
|
---|
| 52 | Mimicking function.
|
---|
| 53 |
|
---|
| 54 | #### from
|
---|
| 55 |
|
---|
| 56 | Type: `Function`
|
---|
| 57 |
|
---|
| 58 | Function to mimic.
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | ## Related
|
---|
| 62 |
|
---|
| 63 | - [rename-fn](https://github.com/sindresorhus/rename-fn) - Rename a function
|
---|
| 64 | - [keep-func-props](https://github.com/ehmicky/keep-func-props) - Wrap a function without changing its name, length and other properties
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | ## License
|
---|
| 68 |
|
---|
| 69 | MIT © [Sindre Sorhus](https://sindresorhus.com)
|
---|
Note:
See
TracBrowser
for help on using the repository browser.