Tweakr
    Preparing search index...

    Function wrap

    • Wraps a function with a wrapper function, allowing pre- or post-processing.

      Type Parameters

      • T extends (...args: any[]) => any

      Parameters

      • fn: T

        The original function to wrap.

      • wrapper: (fn: T, ...args: Parameters<T>) => ReturnType<T>

        A function that receives the original function and arguments, can modify behavior or result.

      Returns (...args: Parameters<T>) => ReturnType<T>

      A new function wrapped with the provided wrapper.

      const logWrapper = wrap(
      (x: number) => x * 2,
      (fn, x) => {
      console.log("Input:", x);
      const result = fn(x);
      console.log("Output:", result);
      return result;
      }
      );
      logWrapper(5);
      // Logs: "Input: 5" then "Output: 10"

      1.1.0