Function compose

  • Composes multiple functions from right to left.

    Type Parameters

    • T

      The type of input and output for all functions.

    Parameters

    • Rest...fns: ((arg: T) => T)[]

      The functions to compose.

    Returns ((arg: T) => T)

    • A function that applies the composed functions from right to left.
      • (arg): T
      • Parameters

        • arg: T

        Returns T

    const trim = (s: string): string => s.trim();
    const toUpperCase = (s: string): string => s.toUpperCase();
    const exclaim = (s: string): string => `${s}!`;

    const composedFn = compose(exclaim, toUpperCase, trim);
    console.log(composedFn(" hello ")); // "HELLO!"