index.d.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. export type Options = {
  2. /**
  3. 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.
  4. @default false
  5. */
  6. readonly ignoreNonConfigurable?: boolean;
  7. };
  8. /**
  9. Modifies the `to` function to mimic the `from` function. Returns the `to` function.
  10. `name`, `displayName`, and any other properties of `from` are copied. The `length` property is not copied. Prototype, class, and inherited properties are copied.
  11. `to.toString()` will return the same as `from.toString()` but prepended with a `Wrapped with to()` comment.
  12. @param to - Mimicking function.
  13. @param from - Function to mimic.
  14. @returns The modified `to` function.
  15. @example
  16. ```
  17. import mimicFunction from 'mimic-function';
  18. function foo() {}
  19. foo.unicorn = '🦄';
  20. function wrapper() {
  21. return foo();
  22. }
  23. console.log(wrapper.name);
  24. //=> 'wrapper'
  25. mimicFunction(wrapper, foo);
  26. console.log(wrapper.name);
  27. //=> 'foo'
  28. console.log(wrapper.unicorn);
  29. //=> '🦄'
  30. ```
  31. */
  32. export default function mimicFunction<
  33. ArgumentsType extends unknown[],
  34. ReturnType,
  35. FunctionType extends (...arguments_: ArgumentsType) => ReturnType,
  36. >(
  37. to: (...arguments_: ArgumentsType) => ReturnType,
  38. from: FunctionType,
  39. options?: Options,
  40. ): FunctionType;