Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Pipeline<Current, Context, Global, Input, ContextInput, Err, Async>

Pipes functions with the given value, context, and global context.

Type parameters

  • Current

  • Context

  • Global

  • Input = Current

  • ContextInput = Context

  • Err = never

  • Async = false

Hierarchy

  • Pipeline

Index

Constructors

  • new Pipeline<Current, Context, Global, Input, ContextInput, Err, Async>(): Pipeline<Current, Context, Global, Input, ContextInput, Err, Async>
  • Type parameters

    • Current

    • Context

    • Global

    • Input = Current

    • ContextInput = Context

    • Err = never

    • Async = false

    Returns Pipeline<Current, Context, Global, Input, ContextInput, Err, Async>

Methods

  • assert<Thrown>(throwable: Function<Current, Thrown, Context, Global>): Pipeline<NonNullable<Current>, Context, Global, Input, ContextInput, Err, Persist<Async, IsPromise<Thrown>>>
  • catch<E>(errorHandler: (error: unknown, context: Context, global: Global) => E): Pipeline<Current, Context, Global, Input, ContextInput, ExtendsNever<Err, E, Err | E>, Async>
  • Adds an error handler that will be run if any of the piped functions fail.

    By default there's no error handler and any errors will be thrown.

    If this method is called more than once, the newest one will override the last one.

    This method should be called before piping any functions that you want to handle errors for.

    example
    const pipeline = new Pipeline()
    .catch((error, context, global) => console.log(error)) // Will log Syntax Error
    .pipe((value, context, global) => value + 1)
    .pipe((value, context, global) => value.bad_parameter) // Will throw error
    .pipe((value, context, global) => [value]);

    const pipeline2 = new Pipeline()
    .catch((error, context, global) => console.log("Error Handler 1"))
    .tap((value, context, global) => {
    if (value > 10) throw new Error("Value is too high");
    })
    .catch((error, context, global) => console.log("Error Handler 2"))
    .tap((value, context, global) => {
    if (value < 5) throw new Error("Value is too low");
    });

    pipeline2.run(15, context, global); // Returns nothing | Console log: Error Handler 1
    pipeline2.run(1, context, global); // Returns nothing | Console log: Error Handler 2
    pipeline2.run(7, context, global); // Returns 7

    Type parameters

    • E

    Parameters

    • errorHandler: (error: unknown, context: Context, global: Global) => E
        • (error: unknown, context: Context, global: Global): E
        • Parameters

          • error: unknown
          • context: Context
          • global: Global

          Returns E

    Returns Pipeline<Current, Context, Global, Input, ContextInput, ExtendsNever<Err, E, Err | E>, Async>

  • compose(): Function<Input, Async extends true ? Promise<ExtendsNever<Err, Current, Err extends void ? Current : Current | Err>> : ExtendsNever<Err, Current, Err extends void ? Current : Current | Err>, ContextInput, Global>
  • Creates a function that will run all the piped functions in order.

    The first function will receive the given values,the subsequent functions will receive the value returned from the previous function and the contexts.

    If any of the functions returns a Promise, the returned value will be wrapped in a Promise.

    example
    const fn = new Pipeline()
    .pipe((value, context, global) => value + 1)
    .pipe((value, context, global) => value.toString())
    .pipe((value, context, global) => [value])
    .compose();

    fn(1, context, global); ["2"]
    fn(5, context, global); // ["6"]

    const asyncFn = new Pipeline()
    .pipe((value, context, global) => value + 1)
    .pipe((value, context, global) => value.toString())
    .pipe((value, context, global) => [value])
    .compose();

    asyncFn(1, context, global); // Promise => ["2"]
    asyncFn(5, context, global); // Promise => ["6"]

    Returns Function<Input, Async extends true ? Promise<ExtendsNever<Err, Current, Err extends void ? Current : Current | Err>> : ExtendsNever<Err, Current, Err extends void ? Current : Current | Err>, ContextInput, Global>

  • context<Next, IsAsync>(fn: Function<Current, Next, Context, Global>): Pipeline<Current, Awaited<Next>, Global, Input, ContextInput, Err, Persist<Async, IsAsync>>
  • Changes the context passed to the following functions of the pipeline.

    example
    const pipeline = new Pipeline()
    .pipe((value, context, global) => value + 1) // context = "original context"
    .context((value, context, global) => context.split(" ")[1])
    .pipe((value, context, global) => context); // context = "context"

    Type parameters

    Parameters

    • fn: Function<Current, Next, Context, Global>

    Returns Pipeline<Current, Awaited<Next>, Global, Input, ContextInput, Err, Persist<Async, IsAsync>>

  • ifelse<Condition, Then, Otherwise>(condition: Function<Current, Condition, Context, Global>, then: Function<Current, Then, Context, Global>, otherwise: Function<Current, Otherwise, Context, Global>): Pipeline<Awaited<Then>, Context, Global, Input, ContextInput, Err, Persist<Async, Condition extends PromiseLike<unknown> ? true : Then extends PromiseLike<unknown> ? true : Otherwise extends PromiseLike<unknown> ? true : false>>
  • Adds an ifelse step to the pipeline.

    see

    ifelse

    Type parameters

    • Condition: boolean | Promise<boolean>

    • Then

    • Otherwise: unknown

    Parameters

    • condition: Function<Current, Condition, Context, Global>
    • then: Function<Current, Then, Context, Global>
    • otherwise: Function<Current, Otherwise, Context, Global>

    Returns Pipeline<Awaited<Then>, Context, Global, Input, ContextInput, Err, Persist<Async, Condition extends PromiseLike<unknown> ? true : Then extends PromiseLike<unknown> ? true : Otherwise extends PromiseLike<unknown> ? true : false>>

  • match<Result, MatchAsync>(matchFn: MatchFunction<Current, Result, Context, Global, MatchAsync, Awaited<Result>>): Pipeline<Awaited<Result>, Context, Global, Input, ContextInput, Err, Persist<Async, MatchAsync>>
  • Adds a match step to the pipeline.

    see

    match

    Type parameters

    • Result

    • MatchAsync: boolean

    Parameters

    • matchFn: MatchFunction<Current, Result, Context, Global, MatchAsync, Awaited<Result>>

    Returns Pipeline<Awaited<Result>, Context, Global, Input, ContextInput, Err, Persist<Async, MatchAsync>>

  • pairwise<Next>(fn: Function<Current, Next, Context, Global>): Pipeline<[Current, Awaited<Next>], Context, Global, Input, ContextInput, Err, Persist<Async, IsPromise<Next>>>
  • pipe<Next, IsAsync>(fn: Function<Current, Next, Context, Global>): Pipeline<Awaited<Next>, Context, Global, Input, ContextInput, Err, Persist<Async, IsAsync>>
  • Adds a function to the pipeline, the value returned will be passed to the next piped function.

    example
    const pipeline = new Pipeline()
    .pipe((value, context, global) => value + 1)
    .pipe((value, context, global) => value.toString())
    .pipe((value, context, global) => [value]);

    Type parameters

    Parameters

    • fn: Function<Current, Next, Context, Global>

    Returns Pipeline<Awaited<Next>, Context, Global, Input, ContextInput, Err, Persist<Async, IsAsync>>

  • run(value: Input, context: ContextInput, global: Global): Async extends true ? Promise<ExtendsNever<Err, Current, Err extends void ? Current : Current | Err>> : ExtendsNever<Err, Current, Err extends void ? Current : Current | Err>
  • Calls the composed function from Pipeline.compose with the given values and returns the result.

    see

    Pipeline#compose

    example
    const pipeline = new Pipeline()
    .pipe((value, context, global) => value + 1)
    .pipe((value, context, global) => value.toString())
    .pipe((value, context, global) => [value])
    .compose();

    pipeline.run(1, context, global); ["2"]
    pipeline.run(5, context, global); // ["6"]

    const asyncPipeline = new Pipeline()
    .pipe((value, context, global) => value + 1)
    .pipe((value, context, global) => value.toString())
    .pipe((value, context, global) => [value])
    .compose();

    asyncPipeline.run(1, context, global); // Promise => ["2"]
    asyncPipeline.run(5, context, global); // Promise => ["6"]

    Parameters

    • value: Input
    • context: ContextInput
    • global: Global

    Returns Async extends true ? Promise<ExtendsNever<Err, Current, Err extends void ? Current : Current | Err>> : ExtendsNever<Err, Current, Err extends void ? Current : Current | Err>

Generated using TypeDoc