index.d.ts (1156B)
1 export interface Options { 2 /** 3 Throw an error when called more than once. 4 5 @default false 6 */ 7 readonly throw?: boolean; 8 } 9 10 declare const onetime: { 11 /** 12 Ensure a function is only called once. When called multiple times it will return the return value from the first call. 13 14 @param fn - Function that should only be called once. 15 @returns A function that only calls `fn` once. 16 17 @example 18 ``` 19 import onetime from 'onetime'; 20 21 let index = 0; 22 23 const foo = onetime(() => ++index); 24 25 foo(); //=> 1 26 foo(); //=> 1 27 foo(); //=> 1 28 29 onetime.callCount(foo); //=> 3 30 ``` 31 */ 32 <ArgumentsType extends unknown[], ReturnType>( 33 fn: (...arguments: ArgumentsType) => ReturnType, 34 options?: Options 35 ): (...arguments: ArgumentsType) => ReturnType; 36 37 /** 38 Get the number of times `fn` has been called. 39 40 @param fn - Function to get call count from. 41 @returns A number representing how many times `fn` has been called. 42 43 @example 44 ``` 45 import onetime from 'onetime'; 46 47 const foo = onetime(() => {}); 48 foo(); 49 foo(); 50 foo(); 51 52 console.log(onetime.callCount(foo)); 53 //=> 3 54 ``` 55 */ 56 callCount(fn: (...arguments: any[]) => unknown): number; 57 }; 58 59 export default onetime;