Variable FunctionUtilsConst

FunctionUtils: {
    compose<T, R>(...funcs): ((arg) => R);
    debounce<T, R>(func, waitTime?, immediate?): ((...args) => undefined | R);
    defer<T, R>(func): ((...args) => void);
    once<T, R>(func): ((...args) => R);
    throttle<T, R>(func, waitTime?): ((...args) => undefined | R);
} = ...

Type declaration

  • compose:function
    • Composes multiple functions into a single function, where the output of each function is passed as input to the next function.

      Type Parameters

      • T

        The type of the input to the first function

      • R

        The type of the output of the last function

      Parameters

      • Rest ...funcs: ((arg) => any)[]

        The functions to compose

      Returns ((arg) => R)

      A function that is the composition of all provided functions

        • (arg): R
        • Parameters

          • arg: T

          Returns R

      Example

      const addOne = (x: number) => x + 1;
      const double = (x: number) => x * 2;
      const addOneAndDouble = FunctionUtils.compose(double, addOne);
      addOneAndDouble(5); // Returns 12 (5 + 1 = 6, then 6 * 2 = 12)
  • debounce:function
    • Creates a debounced function that delays invoking the provided function until after the specified wait time has elapsed since the last time it was invoked.

      Type Parameters

      • T extends any[]

        The type of the function's arguments

      • R

        The return type of the function

      Parameters

      • func: ((...args) => R)

        The function to debounce

          • (...args): R
          • Parameters

            • Rest ...args: T

            Returns R

      • waitTime: number = 1000

        The number of milliseconds to delay

      • immediate: boolean = false

        If true, the function will be called on the leading edge instead of the trailing edge

      Returns ((...args) => undefined | R)

      A debounced version of the provided function

        • (...args): undefined | R
        • Parameters

          • Rest ...args: T

          Returns undefined | R

      Example

      const debouncedSearch = FunctionUtils.debounce((query: string) => {
      // Perform search
      }, 300);

      // Will only execute after 300ms of no calls
      debouncedSearch("test");
  • defer:function
    • Creates a function that delays invoking the provided function until the current call stack has cleared.

      Type Parameters

      • T extends any[]

        The type of the function's arguments

      • R

        The return type of the function

      Parameters

      • func: ((...args) => R)

        The function to defer

          • (...args): R
          • Parameters

            • Rest ...args: T

            Returns R

      Returns ((...args) => void)

      A deferred version of the provided function

        • (...args): void
        • Parameters

          • Rest ...args: T

          Returns void

      Example

      const deferredLog = FunctionUtils.defer((message: string) => {
      console.log(message);
      });

      // Will execute after current execution context
      deferredLog("Hello");
  • once:function
    • Creates a function that can only be called once. Subsequent calls will return the result of the first call.

      Type Parameters

      • T extends any[]

        The type of the function's arguments

      • R

        The return type of the function

      Parameters

      • func: ((...args) => R)

        The function to make callable only once

          • (...args): R
          • Parameters

            • Rest ...args: T

            Returns R

      Returns ((...args) => R)

      A function that can only be called once

        • (...args): R
        • Parameters

          • Rest ...args: T

          Returns R

      Example

      const initialize = FunctionUtils.once(() => {
      // Expensive initialization
      return "initialized";
      });

      initialize(); // Returns "initialized"
      initialize(); // Returns "initialized" without executing the function again
  • throttle:function
    • Creates a throttled function that only invokes the provided function at most once per every wait milliseconds.

      Type Parameters

      • T extends any[]

        The type of the function's arguments

      • R

        The return type of the function

      Parameters

      • func: ((...args) => R)

        The function to throttle

          • (...args): R
          • Parameters

            • Rest ...args: T

            Returns R

      • waitTime: number = 1000

        The number of milliseconds to throttle invocations to

      Returns ((...args) => undefined | R)

      A throttled version of the provided function

        • (...args): undefined | R
        • Parameters

          • Rest ...args: T

          Returns undefined | R

      Example

      const throttledScroll = FunctionUtils.throttle(() => {
      // Handle scroll event
      }, 100);

      // Will execute at most once every 100ms
      window.addEventListener('scroll', throttledScroll);