// @flow import type { Result } from "../Result"; import type { ExtractValue } from "../types"; export type Option = A | null | void; declare export var Some: (value: $NonMaybeType) => Option; declare export var None: Option; /** * Returns `Some(value)` if the provided value is non-nullable, otherwise, returns `None`. */ declare export function fromNullable(value: A): Option>; /** * Returns `Some(value)` if the provided value is not falsy, otherwise, returns `None`. */ declare export function fromFalsy(value: A): Option>; /** * Returns `Some(value)` if the given predicate function returns `true`, otherwise, returns `None`. */ declare export function fromPredicate( value: A, predicateFn: (value: $NonMaybeType) => boolean ): Option>; declare export function fromPredicate( predicateFn: (value: $NonMaybeType) => boolean ): (value: A) => Option>; /** * Returns `Some(value)` (`value` is the result of `fn`) if `fn` didn't throw an error, otherwise, returns `None`. */ declare export function fromExecution(fn: () => A): Option>; /** * Returns `Some(value)` if `promise` is resolved successfully, otherwise, returns `None`. */ declare export function fromPromise( promise: Promise ): Promise>>; /** * Returns the result of `mapFn` if `option` is `Some(value)`, otherwise, returns `None` and `mapFn` is not called. */ declare export function map( option: Option, mapFn: (value: A) => $NonMaybeType ): Option; declare export function map( mapFn: (value: A) => $NonMaybeType ): (option: Option) => Option; /** * 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( option: Option, mapFn: (value: A) => Option ): Option; declare export function flatMap( mapFn: (value: A) => Option ): (option: Option) => Option; /** * Returns the result of `mapFn` if `option` is `Some(value)`, otherwise, returns a default value. */ declare export function mapWithDefault( option: Option, defaultValue: $NonMaybeType, mapFn: (value: A) => B ): B; declare export function mapWithDefault( defaultValue: $NonMaybeType, mapFn: (value: A) => B ): (option: Option) => B; /** * Returns `Some(value)` if the result of `mapFn` is non-nullable, otherwise, returns `None`. */ declare export function mapNullable( option: Option, mapFn: (value: A) => B | null | void ): Option>; declare export function mapNullable( mapFn: (value: A) => B | null | void ): (option: Option) => Option>; /** * Returns `Some(value)` if `option` is `Some(value)` and the result of `predicateFn` is truthy, otherwise, returns `None`. */ declare export function filter( option: Option, predicateFn: (value: A) => boolean ): Option; declare export function filter( predicateFn: (value: A) => boolean ): (option: Option) => Option; /** * Returns `value` if `option` is `Some(value)`, otherwise, returns a default value. */ declare export function getWithDefault( option: Option, defaultValue: $NonMaybeType ): A; declare export function getWithDefault( defaultValue: $NonMaybeType ): (option: Option) => A; /** * Returns `value` if `option` is `Some(value)`, otherwise, throws an exception. */ declare export function getExn(option: Option): A | empty; /** * Returns `value` if `option` is `Some(value)`, otherwise, returns `null`. */ declare export function toNullable(option: Option): A | null; /** * Returns `value` if `option` is `Some(value)`, otherwise, returns `undefined`. */ declare export function toUndefined(option: Option): 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( option: Option, errorValue: $NonMaybeType ): Result; declare export function toResult( errorValue: $NonMaybeType ): (option: Option) => Result; /** * Returns the result of `someFn` if `option` is `Some(value)`, otherwise, returns the result of `noneFn`. */ declare export function match( option: Option, someFn: (value: A) => B, noneFn: () => B ): B; declare export function match( someFn: (value: A) => B, noneFn: () => B ): (option: Option) => B; /** * Returns `true` if the provided `option` is `None`, otherwise, returns `false`. */ declare export function isNone(option: Option): boolean; /** * Returns `true` if the provided `option` is `Some(value)`, otherwise, returns `false`. */ declare export function isSome(option: Option): boolean; /** * Applies a side-effect function to the value in `Some`, and returns the original `option`. */ declare export function tap( option: Option, someFn: (value: A) => void ): Option; declare export function tap( someFn: (value: A) => void ): (option: Option) => Option; /** * Checks if `option` is the `Some` variant and contains the given value. */ declare export function contains(option: Option, value: any): boolean; declare export function contains(value: any): (option: Option) => 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( sndOption: Option ): (fstOption: Option) => Option<[A, B]>; declare export function zip( fstOption: Option, sndOption: Option ): 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( fstOption: Option, sndOption: Option, mapFn: (arg0: A, arg1: B) => Option ): Option; declare export function zipWith( sndOption: Option, mapFn: (arg0: A, arg1: B) => Option ): (fstOption: Option) => Option;