You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.2 KiB

4 years ago
  1. # mimic-fn [![Build Status](https://travis-ci.org/sindresorhus/mimic-fn.svg?branch=master)](https://travis-ci.org/sindresorhus/mimic-fn)
  2. > Make a function mimic another one
  3. Useful when you wrap a function in another function and like to preserve the original name and other properties.
  4. ## Install
  5. ```
  6. $ npm install mimic-fn
  7. ```
  8. ## Usage
  9. ```js
  10. const mimicFn = require('mimic-fn');
  11. function foo() {}
  12. foo.unicorn = '🦄';
  13. function wrapper() {
  14. return foo();
  15. }
  16. console.log(wrapper.name);
  17. //=> 'wrapper'
  18. mimicFn(wrapper, foo);
  19. console.log(wrapper.name);
  20. //=> 'foo'
  21. console.log(wrapper.unicorn);
  22. //=> '🦄'
  23. ```
  24. ## API
  25. It will copy over the properties `name`, `length`, `displayName`, and any custom properties you may have set.
  26. ### mimicFn(to, from)
  27. Modifies the `to` function and returns it.
  28. #### to
  29. Type: `Function`
  30. Mimicking function.
  31. #### from
  32. Type: `Function`
  33. Function to mimic.
  34. ## Related
  35. - [rename-fn](https://github.com/sindresorhus/rename-fn) - Rename a function
  36. - [keep-func-props](https://github.com/ehmicky/keep-func-props) - Wrap a function without changing its name, length and other properties
  37. ## License
  38. MIT © [Sindre Sorhus](https://sindresorhus.com)