UNPKG

@mobily/ts-belt

Version:

🔧 Fast, modern, and practical utility library for FP in TypeScript.

198 lines (175 loc) • 6.34 kB
// @flow import type { Result } from "../Result"; import type { ExtractValue } from "../types"; export type Option<A> = A | null | void; declare export var Some: <A>(value: $NonMaybeType<A>) => Option<A>; declare export var None: Option<empty>; /** * Returns `Some(value)` if the provided value is non-nullable, otherwise, returns `None`. */ declare export function fromNullable<A>(value: A): Option<ExtractValue<A>>; /** * Returns `Some(value)` if the provided value is not falsy, otherwise, returns `None`. */ declare export function fromFalsy<A>(value: A): Option<ExtractValue<A>>; /** * Returns `Some(value)` if the given predicate function returns `true`, otherwise, returns `None`. */ declare export function fromPredicate<A>( value: A, predicateFn: (value: $NonMaybeType<A>) => boolean ): Option<ExtractValue<A>>; declare export function fromPredicate<A>( predicateFn: (value: $NonMaybeType<A>) => boolean ): (value: A) => Option<ExtractValue<A>>; /** * Returns `Some(value)` (`value` is the result of `fn`) if `fn` didn't throw an error, otherwise, returns `None`. */ declare export function fromExecution<A>(fn: () => A): Option<ExtractValue<A>>; /** * Returns `Some(value)` if `promise` is resolved successfully, otherwise, returns `None`. */ declare export function fromPromise<A>( promise: Promise<A> ): Promise<Option<ExtractValue<A>>>; /** * Returns the result of `mapFn` if `option` is `Some(value)`, otherwise, returns `None` and `mapFn` is not called. */ declare export function map<A, B>( option: Option<A>, mapFn: (value: A) => $NonMaybeType<B> ): Option<B>; declare export function map<A, B>( mapFn: (value: A) => $NonMaybeType<B> ): (option: Option<A>) => Option<B>; /** * Returns the result of `mapFn` (it must have a return type of `Option`) if `option` is `Some(value)`, otherwise, returns `None`. */ declare export function flatMap<A, B>( option: Option<A>, mapFn: (value: A) => Option<B> ): Option<B>; declare export function flatMap<A, B>( mapFn: (value: A) => Option<B> ): (option: Option<A>) => Option<B>; /** * Returns the result of `mapFn` if `option` is `Some(value)`, otherwise, returns a default value. */ declare export function mapWithDefault<A, B>( option: Option<A>, defaultValue: $NonMaybeType<B>, mapFn: (value: A) => B ): B; declare export function mapWithDefault<A, B>( defaultValue: $NonMaybeType<B>, mapFn: (value: A) => B ): (option: Option<A>) => B; /** * Returns `Some(value)` if the result of `mapFn` is non-nullable, otherwise, returns `None`. */ declare export function mapNullable<A, B>( option: Option<A>, mapFn: (value: A) => B | null | void ): Option<ExtractValue<B>>; declare export function mapNullable<A, B>( mapFn: (value: A) => B | null | void ): (option: Option<A>) => Option<ExtractValue<B>>; /** * Returns `Some(value)` if `option` is `Some(value)` and the result of `predicateFn` is truthy, otherwise, returns `None`. */ declare export function filter<A>( option: Option<A>, predicateFn: (value: A) => boolean ): Option<A>; declare export function filter<A>( predicateFn: (value: A) => boolean ): (option: Option<A>) => Option<A>; /** * Returns `value` if `option` is `Some(value)`, otherwise, returns a default value. */ declare export function getWithDefault<A>( option: Option<A>, defaultValue: $NonMaybeType<A> ): A; declare export function getWithDefault<A>( defaultValue: $NonMaybeType<A> ): (option: Option<A>) => A; /** * Returns `value` if `option` is `Some(value)`, otherwise, throws an exception. */ declare export function getExn<A>(option: Option<A>): A | empty; /** * Returns `value` if `option` is `Some(value)`, otherwise, returns `null`. */ declare export function toNullable<A>(option: Option<A>): A | null; /** * Returns `value` if `option` is `Some(value)`, otherwise, returns `undefined`. */ declare export function toUndefined<A>(option: Option<A>): A | void; /** * Returns `Ok(value)` if `option` is `Some(value)`, otherwise, returns `Error(errorValue)`, both `Ok` and `Error` come from the `Result` type. */ declare export function toResult<A, B>( option: Option<A>, errorValue: $NonMaybeType<B> ): Result<A, B>; declare export function toResult<A, B>( errorValue: $NonMaybeType<B> ): (option: Option<A>) => Result<A, B>; /** * Returns the result of `someFn` if `option` is `Some(value)`, otherwise, returns the result of `noneFn`. */ declare export function match<A, B>( option: Option<A>, someFn: (value: A) => B, noneFn: () => B ): B; declare export function match<A, B>( someFn: (value: A) => B, noneFn: () => B ): (option: Option<A>) => B; /** * Returns `true` if the provided `option` is `None`, otherwise, returns `false`. */ declare export function isNone<A>(option: Option<A>): boolean; /** * Returns `true` if the provided `option` is `Some(value)`, otherwise, returns `false`. */ declare export function isSome<A>(option: Option<A>): boolean; /** * Applies a side-effect function to the value in `Some`, and returns the original `option`. */ declare export function tap<A>( option: Option<A>, someFn: (value: A) => void ): Option<A>; declare export function tap<A>( someFn: (value: A) => void ): (option: Option<A>) => Option<A>; /** * Checks if `option` is the `Some` variant and contains the given value. */ declare export function contains<A>(option: Option<A>, value: any): boolean; declare export function contains<A>(value: any): (option: Option<A>) => boolean; /** * Combines two Options into a single Option containing a tuple of their values, if both Options are `Some` variants, otherwise, returns `None`. */ declare export function zip<A, B>( sndOption: Option<B> ): (fstOption: Option<A>) => Option<[A, B]>; declare export function zip<A, B>( fstOption: Option<A>, sndOption: Option<B> ): Option<[A, B]>; /** * Combines two Options into a single Option. The new value is produced by applying the given function to both values, if both Options are `Some` variants, otherwise, returns `None`. */ declare export function zipWith<A, B, C>( fstOption: Option<A>, sndOption: Option<B>, mapFn: (arg0: A, arg1: B) => Option<C> ): Option<C>; declare export function zipWith<A, B, C>( sndOption: Option<B>, mapFn: (arg0: A, arg1: B) => Option<C> ): (fstOption: Option<A>) => Option<C>;