UNPKG

standard-data-structures

Version:

A collection of standard data-structures for node and browser

110 lines (109 loc) 2.82 kB
import { NoSuchValueException } from '../internals/noSuchValueException'; import { Either } from './either'; /** * Used to represent an optional value */ export declare abstract class Option<A> { /** * Returns `true` if the instance is [[Some]] */ readonly isSome: boolean; /** * Creates an [[Option]] from an [[Either]] */ static fromEither<L, R>(either: Either<L, R>): Option<R>; /** * Checks if the Option type is of [[None]] type */ static isNone<A>(A: Option<A>): A is None; /** * Checks if the Option type is of [[Some]] type */ static isSome<A>(A: Option<A>): A is Some<A>; /** * Creates a [[None]] object */ static none(): Option<never>; /** * Creates a [[Some]] object */ static some<A>(A: A): Option<A>; /** * Converts an [[Option]] to an [[Either]] */ abstract asEither: Either<NoSuchValueException, A>; /** * Returns `true` if the instance is [[None]] */ abstract isNone: boolean; /** * Flattens the [[Option]] type */ abstract chain<B>(ab: (A: A) => Option<B>): Option<B>; /** * Takes a seed `S` value and applies the FN if this is [[Some]] * Returns the seed `S` if this is [[None]] */ abstract fold<S>(S: S, FN: (r: A, s: S) => S): S; /** * Gets the real value if this is of [[Some]] type. * If its not a [[Some]] type it returns the provided value. */ abstract getOrElse(A: A): A; /** * Transforms the value inside the [[Option]] type. */ map<B>(ab: (A: A) => B): Option<B>; } /** * Is an [[Option]] type that represents existence of a value. */ export declare class Some<A> extends Option<A> { readonly value: A; constructor(value: A); /** * Refer [[Option.chain]] */ chain<B>(ab: (A: A) => Option<B>): Option<B>; /** * Refer [[Option.fold]] */ fold<S>(S: S, some: (r: A, s: S) => S): S; /** * Refer [[Option.getOrElse]] */ getOrElse(a: A): A; /** * Refer [[Option.isNone]] */ readonly isNone: boolean; /** * Refer [[Option.asEither]] */ readonly asEither: Either<NoSuchValueException, A>; } /** * Is an [[Option]] type that represents absence of any value. */ export declare class None extends Option<never> { /** * Refer [[Option.chain]] */ chain<B>(ab: (A: never) => Option<B>): Option<B>; /** * Refer [[Option.fold]] */ fold<S>(S: S, some: (r: never, s: S) => S): S; /** * Refer [[Option.getOrElse]] */ getOrElse(A: never): never; /** * Refer [[Option.isNone]] */ readonly isNone: boolean; /** * Refer [[Option.asEither]] */ readonly asEither: Either<NoSuchValueException, never>; }