Options
All
  • Public
  • Public/Protected
  • All
Menu

@tsfun/pipe — Reference

Index

Classes

Functions

Functions

Const pass

  • Start a pipeline with a value (pass a value to a series of functions in a pipeline)

    Example:

    const increase = (x: number) => x + 1
    const double = (x: number) => x * 2
    const square = (x: number) => x * x
    const result = pass(2) // x0 = 2
      .to(increase) // x1 = increase(x0)
      .to(double) // x2 = double(x1)
      .to(square) // x3 = square(x2)
      .to(String) // x4 = String(x3)
      .get() // result = x4 = String(square(double(increase(2))))
    expect(result).toBe(String(square(double(increase(2)))))

    Example:

    const sum = (...args: number[]) => args.reduce((a, b) => a + b)
    const product = (...args: number[]) => args.reduce((a, b) => a * b)
    const result = pass(2) // x0 = 2
      .to(sum, -3, 4) // x1 = sum(x0, -3, 4)
      .to(product, 2) // x2 = product(x1, 2)
      .to(String) // x3 = String(x2)
      .get() // result = x3 = String(product(sum(2, -3, 4), 2))
    expect(result).toBe(String(product(sum(2, -3, 4), 2)))

    Usage:

    pass(x).to(f).to(g).to(h).get() is equivalent to x |> f |> g |> h, which in turn is equivalent to h(g(f(x)))

    You can also pass extra arguments to function that takes more than one argument, for example: pass(x).to(f, y, z).get() is equivalent to f(x, y, z)

    Note:

    This is a temporary solution to replace yet-to-be-implemented pipeline operator. This function will be deprecated when the proposal get to stage 3 and removed when it is implemented

    Read more about pipeline operator in the tc39 proposal

    Type parameters

    • X

    Parameters

    • x: X

      First value in the pipeline

    Returns PipeNode<[], X>

    First node of the pipeline

Const pipe

  • pipe<Args, Return>(fn: (...args: Args) => Return): PipeNode<Args, Return>
  • Start a pipeline with a function

    Example:

    const sum = (...args: number[]) => args.reduce((a, b) => a + b, 0)
    const increase = (x: number) => x + 1
    const double = (x: number) => x * 2
    const square = (x: number) => x * x
    const pipeline = pipe(sum) // x0 = sum(...args)
      .to(increase) // x1 = increase(x0)
      .to(double) // x2 = double(x1)
      .to(square) // x3 = square(x2)
      .to(String) // x4 = String(x3)
      .get // pipeline(...args) = x4 = String(square(double(increase(sum(...args)))))
    expect(pipeline(2, 3, -1)).toBe(String(square(double(increase(sum(2, 3, -1))))))

    Example:

    const sum = (...args: number[]) => args.reduce((a, b) => a + b)
    const product = (...args: number[]) => args.reduce((a, b) => a * b)
    const pipeline = pipe(sum) // x0 = sum(...args)
      .to(product, 2) // x1 = product(x0, 2)
      .to(String) // x2 = String(x1)
      .get // pipeline(...args) = x2 = String(product(sum(2, -3, 4), 2))
    expect(pipeline(2, -3, 4)).toBe(String(product(sum(2, -3, 4), 2)))

    Usage: pipe(f).to(g).to(h).get(...args) is equivalent to f(...args) |> g |> h, which in turn is equivalent to h(g(f(...args)))

    You can also pass extra arguments to function that takes more than takes more than one arguments, for example: pipe(f).to(g, a, b).get is equivalent to (...args) => g(f(...args), a, b)

    Note: This is a temporary solution to replace yet-to-be-implemented pipeline operator. This function will be deprecated when the proposal get to stage 3 and removed when it is implemented

    Read more about pipeline operator in the tc39 proposal

    Type parameters

    • Args: any[]

    • Return

    Parameters

    • fn: (...args: Args) => Return

      First function in the pipeline

        • (...args: Args): Return
        • Parameters

          • Rest ...args: Args

          Returns Return

    Returns PipeNode<Args, Return>

    First node of the pipeline

Generated using TypeDoc