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