Options
All
  • Public
  • Public/Protected
  • All
Menu

@kherge/result - v1.0.0

Index

Interfaces

Type aliases

Functions

Type aliases

Compute

Compute<T, U>: (value: T) => U

A closure that computes a new value based on the given value.

Type parameters

  • T

    The type of the given value.

  • U

    The type of the computed value.

Type declaration

    • (value: T): U
    • Parameters

      • value: T

      Returns U

Predicate

Predicate<T>: (value: T) => boolean

A closure that matches the given value for a specific condition.

Type parameters

  • T

    The type of the given value.

Type declaration

    • (value: T): boolean
    • Parameters

      • value: T

      Returns boolean

Produce

Produce<T>: () => T

A closure that returns a value.

Type parameters

  • T

    The type of the given value.

Type declaration

    • (): T
    • Returns T

Functions

Const attempt

  • Invokes a function that may throw an error and returns a Result.

    The given function is invoked within a try/catch block. If an error is caught, it is returned as an instance of Err. If no error is thrown, then the result is returned as an instance of Ok.

    It is important to know that this function is unable to determine the proper type of the thrown error. While the type defaults to the built-in JavaScript Error type, it is very much encouraged that you specify the error type.

    // A simple function that can throw an error.
    const throws = (fail: boolean) => {
      if (fail) {
        throw new Error("An example error.");
      }
    
      return "example";
    };
    
    // An example of the error being caught.
    let result: Result<string, Error> = attempt(() => throws(true));
    
    assert(result.isErr() === true);
    
    // An example of the result being returned.
    result = attempt(() => throws(false));
    
    assert(result.isOk() === true);
    

    Type parameters

    • T

      The type of the function's return value.

    • E = Error

      The type of the thrown value.

    Parameters

    • fn: Produce<T>

      The function to invoke.

    Returns Result<T, E>

    A Result for the function return value.

Const err

  • err<T, E>(value: E): Err<T, E>
  • Creates a new instance of Err.

    let result: Result<string, string> = err('error');
    
    assert(result.isErr() === true);
    

    Type parameters

    • T

      The type of the success value.

    • E

      The type of the failure value.

    Parameters

    • value: E

      The failure value.

    Returns Err<T, E>

    The instance.

Const maybe

  • maybe<T>(value: T): None<unknown> | Some<T>
  • Wraps a value as an Option.

    This function provides a convenient way of wrapping the result of a function that could return null or undefined. If null or undefined is given, an instance of None is returned. If any other value is provided, including other falsy values, an instance of Some is returned.

    let option: Option<string> = maybe(null);
    
    assert(option.isNone() === true);
    
    option = maybe("example");
    
    assert(option.isSome() === true);
    

    Type parameters

    • T

      The type of the value.

    Parameters

    • value: T

    Returns None<unknown> | Some<T>

    The instance.

Const none

  • none<T>(): None<T>
  • Creates a new instance of None.

    let option: Option<string> = none();
    
    assert(option.isNone() === true);
    

    Type parameters

    • T

      The type of the value.

    Returns None<T>

    The instance.

Const ok

  • ok<T, E>(value: T): Ok<T, E>
  • Creates a new instance of Ok.

    let result: Result<string, string> = ok('value');
    
    assert(result.isOk() === true);
    

    Type parameters

    • T

      The type of the success value.

    • E

      The type of the failure value.

    Parameters

    • value: T

      The success value.

    Returns Ok<T, E>

    The instance.

Const some

  • some<T>(value: T): Some<T>
  • Creates a new instance of Some.

    let option: Option<string> = some('example');
    
    assert(option.isSome() === true);
    

    Type parameters

    • T

      The type of the value.

    Parameters

    • value: T

      The value to wrap.

    Returns Some<T>

    The instance.

Generated using TypeDoc