Options
All
  • Public
  • Public/Protected
  • All
Menu

Module index

Index

Functions

  • assert<Value, Context, Global, Thrown>(throwable: Function<Value, Thrown, Context, Global>): Function<Value, NonNullable<Value>, Context, Global>
  • Creates a function that will assert that the given value is not undefined or null.

    If the value is undefined or null, it will throw the result of calling the given callback.

    If it's not, it will return the value it received.

    example
    const fn = assert((value, context, global) => new Error("no value"));

    fn(1, context, global); // 1
    fn(undefined, context, global); // Error: no value
    fn(null, context, global); // Error: no value

    Type parameters

    • Value

    • Context

    • Global

    • Thrown

    Parameters

    • throwable: Function<Value, Thrown, Context, Global>

    Returns Function<Value, NonNullable<Value>, Context, Global>

  • ifelse<Value, Condition, Then, Otherwise, Context, Global>(condition: Function<Value, Condition, Context, Global>, then: Function<Value, Then, Context, Global>, otherwise: Function<Value, Otherwise, Context, Global>): Function<Value, Awaited<Then>, Context, Global>
  • Creates a function that will call a condition callback, if it returns true it will run the then callback, if it returns false it will run the otherwise callback.

    Returns the value of either then or otherwise callbacks, depending on which one was called.

    If any of the three callbacks it receives returns a Promise, the returned value will be wrapped in a Promise.

    example
    const fn = ifelse(
    (value, context, global) => value > 5,
    (value, context, global) => value / 2,
    (value, context, global) => value * 2
    );

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

    const asyncFn = ifelse(
    async (value, context, global) => value > 5,
    (value, context, global) => value / 2,
    (value, context, global) => value * 2
    );

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

    Type parameters

    • Value

    • Condition: boolean | Promise<boolean>

    • Then

    • Otherwise: unknown

    • Context

    • Global

    Parameters

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

    Returns Function<Value, Awaited<Then>, Context, Global>

  • match<Value, Result, Context, Global, Async, Expected>(matchFn: MatchFunction<Value, Result, Context, Global, Async, Expected>): Function<Value, IsAsync<Result, Async, true>, Context, Global>
  • Creates a function that will run the given callback with a Match object passed as the argument, that will have it's method run be called with the received arguments.

    It will return the value that the Match object returns, if it's a Promise TypeScript will infer it.

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

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

    const asyncFn = match(m =>
    m
    .on(
    (value, context, global) => value > 5,
    async (value, context, global) => value / 2
    )
    .on(
    (value, context, global) => value < 5,
    (value, context, global) => value * 2
    )
    );

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

    Type parameters

    • Value

    • Result

    • Context

    • Global

    • Async

    • Expected = Awaited<Result>

    Parameters

    • matchFn: MatchFunction<Value, Result, Context, Global, Async, Expected>

    Returns Function<Value, IsAsync<Result, Async, true>, Context, Global>

  • pairwise<Value, Next, Context, Global>(fn: Function<Value, Next, Context, Global>): Function<Value, Next extends PromiseLike<unknown> ? Promise<[Value, Next]> : [Value, Next], Context, Global>
  • Creates a function that will run the given callback function with the arguments it receives.

    It will return an array with the value it received as the first element, and the value returned from the callback function as the second element.

    If the callback returns a Promise, the returned array will be wrapped in a Promise.

    example
    const fn = pairwise((value, context, global) => value + 1);

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

    const asyncFn = pairwise(async x => x + 1);

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

    Type parameters

    • Value

    • Next

    • Context

    • Global

    Parameters

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

    Returns Function<Value, Next extends PromiseLike<unknown> ? Promise<[Value, Next]> : [Value, Next], Context, Global>

  • tap<Value, Result, Context, Global>(fn: Function<Value, Result, Context, Global>): Function<Value, Result extends PromiseLike<unknown> ? Promise<Value> : Value, Context, Global>
  • Creates a function that will run the given callback function with the arguments it receives.

    The return value of the callback function will be ignored, and the function will return the value it received.

    If the callback returns a Promise, the returned value will be wrapped in a Promise.

    example
    const fn = tap((value, context, global) => value + 1);

    fn(1, context, global); // 1

    const asyncFn = tap(async x => x + 1);

    asyncFn(1, context, global); // Promise => 1

    Type parameters

    • Value

    • Result: void | Promise<void>

    • Context

    • Global

    Parameters

    • fn: Function<Value, Result, Context, Global>

    Returns Function<Value, Result extends PromiseLike<unknown> ? Promise<Value> : Value, Context, Global>

Generated using TypeDoc