@tempots/std
Version:
Std library for TypeScript. Natural complement to the Tempo libraries.
92 lines (91 loc) • 3.01 kB
TypeScript
import { Result } from './result';
/**
* Represents a valid result.
* @public
*/
export type Valid = {
readonly type: 'valid';
};
/**
* Represents an invalid value with an associated error.
* @typeParam E - The type of the error.
* @public
*/
export type Invalid<E> = {
readonly type: 'invalid';
readonly error: E;
};
/**
* Represents a type that can either be `Valid` or `Invalid`.
* @typeParam E - The type of the error associated with an `Invalid` value.
* @public
*/
export type Validation<E> = Valid | Invalid<E>;
/**
* Represents a promise that resolves to a `Validation` object.
* @typeParam E - The type of the error value in the `Validation` object.
* @public
*/
export type PromiseValidation<E> = PromiseLike<Validation<E>>;
/**
* Utility functions for working with `Validation` types.
* @public
*/
export declare const Validation: {
/**
* Creates a valid `Validation`.
* @returns A `Validation` that is `Valid`.
*/
valid: {
type: "valid";
};
/**
* Creates an invalid `Validation`.
* @param error - The error associated with the invalid value.
* @returns A `Validation` that is `Invalid`.
*/
invalid<E>(error: E): Validation<E>;
/**
* Checks if a `Validation` is `Valid`.
* @param r - The `Validation` to check.
* @returns `true` if the `Validation` is `Valid`, otherwise `false`.
*/
isValid<E>(r: Validation<E>): r is Valid;
/**
* Checks if a `Validation` is `Invalid`.
* @param r - The `Validation` to check.
* @returns `true` if the `Validation` is `Invalid`, otherwise `false`.
*/
isInvalid<E>(r: Validation<E>): r is Invalid<E>;
/**
* Maps the value of a `Validation` to a new value.
* @param r - The `Validation` to map.
* @param valid - The mapping function for a valid value.
* @param invalid - The mapping function for an invalid value.
* @returns The mapped value.
*/
match: <V, E>(r: Validation<E>, valid: () => V, invalid: (error: E) => V) => V;
/**
* Maps the value of a `Validation` to a new `Validation`.
* @param validation - The `Validation` to map.
* @param value - The value to map.
* @returns A new `Validation` with the mapped value.
*/
toResult: <T, E>(validation: Validation<E>, value: T) => Result<T, E>;
/**
* Execute a function when the `Validation` is valid.
*
* @param r - The `Validation` to check.
* @param apply - The function to execute when the `Validation` is valid.
* @returns The `Validation` object.
*/
whenValid: <E>(r: Validation<E>, apply: () => void) => Validation<E>;
/**
* Execute a function when the `Validation` is invalid.
*
* @param r - The `Validation` to check.
* @param apply - The function to execute when the `Validation` is invalid.
* @returns The `Validation` object.
*/
whenInvalid: <E>(r: Validation<E>, apply: (e: E) => void) => Validation<E>;
};