Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Match<Value, Context, Global, Result, Async>

Stores conditions and callbacks to execute with the given values.

Type parameters

  • Value

  • Context

  • Global

  • Result = never

  • Async = false

Hierarchy

  • Match

Index

Constructors

  • new Match<Value, Context, Global, Result, Async>(): Match<Value, Context, Global, Result, Async>
  • Type parameters

    • Value

    • Context

    • Global

    • Result = never

    • Async = false

    Returns Match<Value, Context, Global, Result, Async>

Methods

  • compose(): Function<Value, Async extends true ? Promise<Result> : Result, Context, Global>
  • Creates a pipeline function that will run all the conditions with the given values and will return the result of the called callback.

    If any of the conditions or callbacks returns a Promise, the returned value will be wrapped in a Promise.

    example
    const fn = new Match()
    .on(
    (value, context, global) => value > 5,
    (value, context, global) => value / 2
    )
    .on(
    (value, context, global) => value < 5,
    (value, context, global) => value * 2
    )
    .otherwise((value, context, global) => 0)
    .compose();

    fn(20, context, global); // 10
    fn(2, context, global); // 4
    fn(5, context, global); // 0

    const asyncFn = new Match()
    .on(
    (value, context, global) => value > 5,
    async (value, context, global) => value / 2
    )
    .on(
    async (value, context, global) => value < 5,
    (value, context, global) => value * 2
    )
    .otherwise((value, context, global) => 0)
    .compose();

    asyncFn(20, context, global); // Promise => 10
    asyncFn(2, context, global); // Promise => 4
    asyncFn(5, context, global); // Promise => 0

    Returns Function<Value, Async extends true ? Promise<Result> : Result, Context, Global>

  • Adds a condition and a callback that will be run if the condition matches.

    example
    const match = new Match()
    .on(
    (value, context, global) => value > 5,
    (value, context, global) => value / 2
    )
    .on(
    (value, context, global) => value < 5,
    (value, context, global) => value * 2
    );

    Type parameters

    • Next

    Parameters

    • matcher: Function<Value, boolean, Context, Global>
    • pipeline: Function<Value, Next, Context, Global>

    Returns Match<Value, Context, Global, ExtendsNever<Result, Awaited<Next>, Result>, Persist<Async, IsPromise<Next>>>

  • Adds a callback that will be called if no conditions match.

    example
    const match = new Match()
    .on(
    (value, context, global) => value > 5,
    (value, context, global) => value / 2
    )
    .otherwise((value, context, global) => value * 2);

    Type parameters

    • Next

    Parameters

    • pipeline: Function<Value, Next, Context, Global>

    Returns Match<Value, Context, Global, ExtendsNever<Result, Awaited<Next>, Result>, Persist<Async, IsPromise<Next>>>

  • run(value: Value, context: Context, global: Global): Async extends true ? Promise<Result> : Result
  • Calls the composed function from Match.compose with the given values and returns the result.

    see

    Match#compose

    example
    const match = new Match()
    .on(
    (value, context, global) => value > 5,
    (value, context, global) => value / 2
    )
    .on(
    (value, context, global) => value < 5,
    (value, context, global) => value * 2
    )
    .otherwise((value, context, global) => 0);

    match.run(20, context, global); // 10
    match.run(2, context, global); // 4
    match.run(5, context, global); // 0

    const asyncMatch = new Match()
    .on(
    (value, context, global) => value > 5,
    async (value, context, global) => value / 2
    )
    .on(
    async (value, context, global) => value < 5,
    (value, context, global) => value * 2
    )
    .otherwise((value, context, global) => 0);

    asyncMatch.run(20, context, global); // Promise => 10
    asyncMatch.run(2, context, global); // Promise => 4
    asyncMatch.run(5, context, global); // Promise => 0

    Parameters

    • value: Value
    • context: Context
    • global: Global

    Returns Async extends true ? Promise<Result> : Result

Generated using TypeDoc