UNPKG

1.32 kBTypeScriptView Raw
1export 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/**
11Modifies 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```
23import mimicFunction from 'mimic-fn';
24
25function foo() {}
26foo.unicorn = '🦄';
27
28function wrapper() {
29 return foo();
30}
31
32console.log(wrapper.name);
33//=> 'wrapper'
34
35mimicFunction(wrapper, foo);
36
37console.log(wrapper.name);
38//=> 'foo'
39
40console.log(wrapper.unicorn);
41//=> '🦄'
42```
43*/
44export 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;