index.d.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. declare namespace onetime {
  2. interface Options {
  3. /**
  4. Throw an error when called more than once.
  5. @default false
  6. */
  7. 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. @param fn - Function that should only be called once.
  14. @returns A function that only calls `fn` once.
  15. @example
  16. ```
  17. import onetime = require('onetime');
  18. let i = 0;
  19. const foo = onetime(() => ++i);
  20. foo(); //=> 1
  21. foo(); //=> 1
  22. foo(); //=> 1
  23. onetime.callCount(foo); //=> 3
  24. ```
  25. */
  26. <ArgumentsType extends unknown[], ReturnType>(
  27. fn: (...arguments: ArgumentsType) => ReturnType,
  28. options?: onetime.Options
  29. ): (...arguments: ArgumentsType) => ReturnType;
  30. /**
  31. Get the number of times `fn` has been called.
  32. @param fn - Function to get call count from.
  33. @returns A number representing how many times `fn` has been called.
  34. @example
  35. ```
  36. import onetime = require('onetime');
  37. const foo = onetime(() => {});
  38. foo();
  39. foo();
  40. foo();
  41. console.log(onetime.callCount(foo));
  42. //=> 3
  43. ```
  44. */
  45. callCount(fn: (...arguments: any[]) => unknown): number;
  46. // TODO: Remove this for the next major release
  47. default: typeof onetime;
  48. };
  49. export = onetime;