Skip to main content

compose()

compose<Fns>(...fns): (...args) => ComposeReturn<Fns>

Allows you combine two or more functions to create a new function, which passes the results from one function to the next until all have be called. Has a right-to-left call order.

Type Parameters

Fns

Fns extends CompositionArray

The list of unary functions.

Parameters

fns

...Composable<Fns>

The functions to compose.

Returns

(...args): ComposeReturn<Fns>

Parameters

args

...ComposeParams<Fns>

Returns

ComposeReturn<Fns>

Remarks

The implementation follows right-to-left composition semantics:

  1. The rightmost function is applied first to the input arguments
  2. Each subsequent function (moving left) receives the result of the previous function
  3. The leftmost function's result becomes the final output

Example

const getActiveUsers = compose(
displayPage,
sortUserNames,
filterActive,
);

const activeUsers = getActiveUsersByPage(users);