Skip to content

pipe

Composes functions left-to-right: the output of each function is passed as input to the next.

The inverse of compose, which applies functions right-to-left.

Available since v2.0.0

import { pipe } from '@helpers4/function';
pipe<A, B>(fn1: function): function
ParameterTypeDescription
fn1function

function — A function that applies fns in order

Functions are applied left-to-right; the output of each becomes the input of the next.

const process = pipe(
  (x: number) => x + 1,
  (x: number) => x * 2,
  String
);
process(3); // => "8"

Chain string transforms left-to-right with pipe.

const sanitize = pipe(
  (s: string) => s.trim(),
  (s: string) => s.toLowerCase(),
  (s: string) => s.replace(/\s+/g, '-')
);
sanitize('  Hello World  '); // => "hello-world"

View source on GitHub