import { MaybeAsync } from './maybeAsync.js'; import { Result } from './result.js'; import { Unit } from './unit.js'; import { Action, ActionOfT, AsyncAction, AsyncActionOfT, FunctionOfT, FunctionOfTtoK, MaybeMatcher, MaybeMatcherNoReturn, None, PredicateOfT, Some } from './utilities.js'; /** * Represents a value that might not exist. Undefined and null values are always represented as Maybe.none. */ export declare class Maybe { /** * Creates a new Maybe with a value * @param value The value of the new maybe * @returns */ static some(value: Some): Maybe; /** * Creates a new Maybe with no value * @returns {Maybe} */ static none(): Maybe; /** * Creates a new Maybe. If no value is provided, it is equivalent to calling Maybe.none(), and * if a value is provided, it is equivalent to calling Maybe.some(val) * @param value The value of the new Maybe. * @returns {Maybe} */ static from(value: Some | None): Maybe; /** * Returns a Maybe containing the first value of the array, * and a Maybe.none if the array is empty * @param values */ static tryFirst(values: TValue[]): Maybe; /** * Returns a Maybe containing the value of the first element * of the array matching the condition of the predicate, and * a Maybe.none if there are no matches * @param values * @param predicate */ static tryFirst(values: Some[], predicate: PredicateOfT>): Maybe; /** * Returns a Maybe containing the last value of the array, * and a Maybe.none if the array is empty * @param values */ static tryLast(values: TValue[]): Maybe; /** * Returns a Maybe containing the value of the last element * of the array matching the condition of the predicate, and * a Maybe.none if there are no matches * @param values * @param predicate */ static tryLast(values: Some[], predicate: PredicateOfT>): Maybe; /** * Returns only the Maybe instances of the array that have * values * @param maybes */ static choose(maybes: Maybe[]): TValue[]; /** * Returns only the Maybe instances of the array that have values, * passing each value to the given projection to be transformed to a new * value * @param maybes * @param projection */ static choose(maybes: Maybe[], projection: FunctionOfTtoK>): TNewValue[]; private value; /** * Returns true if the Maybe contains a value */ get hasValue(): boolean; /** * Returns true if the Maybe has no value */ get hasNoValue(): boolean; protected constructor(value: Some | None); /** * Returns the value of the Maybe if it has one, * and the default value if there is none * @param defaultValue */ getValueOrDefault(defaultValue: Some): TValue; /** * Returns the value of the Maybe if it has one, * and returns the result of the factory function if * there is none * @param factory */ getValueOrDefault(factory: FunctionOfT>): TValue; /** * Returns the value of the Maybe and throws * and Error if there is none * @returns */ getValueOrThrow(): Some; pipe(): Maybe; pipe(op1: MaybeOpFnAsync): MaybeAsync; pipe(op1: MaybeOpFn): Maybe; pipe(op1: MaybeOpFn, op2: MaybeOpFnAsync): MaybeAsync; pipe(op1: MaybeOpFn, op2: MaybeOpFn): Maybe; pipe(op1: MaybeOpFn, op2: MaybeOpFn, op3: MaybeOpFn): Maybe; pipe(op1: MaybeOpFn, op2: MaybeOpFn, op3: MaybeOpFnAsync): Maybe; pipe(op1: MaybeOpFn, op2: MaybeOpFn, op3: MaybeOpFn, op4: MaybeOpFn): Maybe; pipe(op1: MaybeOpFn, op2: MaybeOpFn, op3: MaybeOpFn, op4: MaybeOpFnAsync): Maybe; pipe(op1: MaybeOpFn, op2: MaybeOpFn, op3: MaybeOpFn, op4: MaybeOpFn, op5: MaybeOpFn): Maybe; pipe(op1: MaybeOpFn, op2: MaybeOpFn, op3: MaybeOpFn, op4: MaybeOpFn, op5: MaybeOpFnAsync): Maybe; pipe(op1: MaybeOpFn, op2: MaybeOpFn, op3: MaybeOpFn, op4: MaybeOpFn, op5: MaybeOpFn, op6: MaybeOpFn): Maybe; pipe(op1: MaybeOpFn, op2: MaybeOpFn, op3: MaybeOpFn, op4: MaybeOpFn, op5: MaybeOpFn, op6: MaybeOpFnAsync): Maybe; pipe(op1: MaybeOpFn, op2: MaybeOpFn, op3: MaybeOpFn, op4: MaybeOpFn, op5: MaybeOpFn, op6: MaybeOpFn, op7: MaybeOpFn): Maybe; pipe(op1: MaybeOpFn, op2: MaybeOpFn, op3: MaybeOpFn, op4: MaybeOpFn, op5: MaybeOpFn, op6: MaybeOpFn, op7: MaybeOpFnAsync): Maybe; pipe(op1: MaybeOpFn, op2: MaybeOpFn, op3: MaybeOpFn, op4: MaybeOpFn, op5: MaybeOpFn, op6: MaybeOpFn, op7: MaybeOpFn, op8: MaybeOpFn): Maybe; pipe(op1: MaybeOpFn, op2: MaybeOpFn, op3: MaybeOpFn, op4: MaybeOpFn, op5: MaybeOpFn, op6: MaybeOpFn, op7: MaybeOpFn, op8: MaybeOpFnAsync): Maybe; /** * Converts the value of the Maybe, if there is one, to a new value * as defined by the provided projection function * @param projection * @returns */ map(projection: FunctionOfTtoK>): Maybe; /** * Converts the value of the Maybe, if there is one, to a new value * as defined by the provided projection, wrapping the asynchronous result in a MaybeAsync * @param projection * @returns */ mapAsync(projection: FunctionOfTtoK>>): MaybeAsync; /** * Executes the given action if the Maybe has a value * @param action * @returns */ tap(action: ActionOfT): Maybe; /** * Executes the given asynchronous action if the Maybe has a * value and retursn a new MaybeAsync * @param asyncAction * @returns */ tapAsync(asyncAction: FunctionOfTtoK>): MaybeAsync; /** * Executes an action if the Maybe has no value * @param action */ tapNone(action: Action): Maybe; /** * Executes an action if the Maybe has no value * @param action */ tapNoneAsync(action: AsyncAction): MaybeAsync; /** * Converts the value of the Maybe, if it has one, to a new Maybe * @param projection * @returns */ bind(projection: FunctionOfTtoK>>): Maybe; /** * Converts the value of the Maybe, if it has one, to a new * MaybeAsync * @param projection * @returns */ bindAsync(projection: FunctionOfTtoK>>): MaybeAsync; /** * Maps the value of the Maybe, if it has one, using the given projection some function, * and the none function otherwise * @param projection */ match(projection: MaybeMatcher): TNewValue; /** * Executes the some function of the given matcher if the Maybe has a value, * and the none function if there is no value * @param matcher * @returns */ match(matcher: MaybeMatcherNoReturn): Unit; /** * Executes the given action if the Maybe has a value * @param action */ execute(action: ActionOfT): Unit; /** * Executes the given async action if the Maybe has a value * @param action A void Promise returning function * @returns A Promise containing Unit */ executeAsync(action: AsyncActionOfT): Promise; /** * Returns the Maybe if it has a value, otherwise it returns * the fallbackValue returned in a Maybe * @param fallbackValue */ or(fallbackValue: Some): Maybe; /** * Returns the Maybe if it has a value, otherwise it returns the fallbackMaybe * @param fallbackMaybe */ or(fallbackMaybe: Maybe): Maybe; /** * Returns the Maybe if it has a value, otherwise executes and * returns the value of the fallbackFactory wrapped in a Maybe * @param fallbackfactory */ or(fallbackfactory: FunctionOfT>): Maybe; /** * Returns the Maybe if it has a value, otherwise executes the fallbackMaybeFactory * and returns its result * @param fallbackMaybefactory */ or(fallbackMaybefactory: FunctionOfT>): Maybe; /** * Returns the Maybe, wrapped in a MaybeAsync, if it has a value, otherwise * returns the fallback MaybeAsync * @param fallbackPromise * @returns */ orAsync(fallbackMaybeAsync: MaybeAsync): MaybeAsync; /** * Returns the Maybe, wrapped in a MaybeAsync, if it has a value, otherwise * returns executes the fallbackPromiseFactory and returns its value wrapped in a MaybeAsync * @param fallbackPromise * @returns */ orAsync(fallbackPromiseFactory: FunctionOfT>>): MaybeAsync; /** * Returns the Maybe, wrapped in a MaybeAsync, if it has a value, otherwise * returns the fallbackPromise, wrapped in a MaybeAsync * @param fallbackPromise */ orAsync(fallbackPromise: Promise>): MaybeAsync; /** * Converts the Maybe into a Result. The Result is successful if there is a value * and a failure, with the given error, if there is not * @param error * @returns */ toResult(error: Some): Result; /** * Returns the string representation of the Maybe (either some or none) * @returns */ toString(): string; /** * Returns true if the Maybes both have values and the values are strictly equal * @param maybe * @returns */ equals(maybe: Maybe): boolean; } export type MaybeOpFn = FunctionOfTtoK, Maybe>; export type MaybeOpFnAsync = FunctionOfTtoK, MaybeAsync>;