import {asyncPipe, pipe} from '../../function/work'; import {isError, isOk} from '../../internal/result'; import type {GenericCallback} from '../../models'; import {attempt} from '../index'; import type {Result, UnwrapValue} from '../models'; // #region Types type WrapValue = Result>; // #endregion // #region Functions // #region Asynchronous /** * Attempt to pipe a value through a function * * _Available as `attemptAsyncPipe` and `attemptPipe.async`_ * * @param initial Initial value * @returns _Piped_ result */ export async function attemptAsyncPipe( initial: GenericCallback | Initial | Promise | Result, pipe: (value: UnwrapValue) => Piped, ): Promise>; /** * Attempt to pipe a value through a series of functions * * _Available as `attemptAsyncPipe` and `attemptPipe.async`_ * * @param initial Initial value * @returns _Piped_ result */ export async function attemptAsyncPipe( initial: GenericCallback | Initial | Promise | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, ): Promise>; /** * Attempt to pipe a value through a series of functions * * _Available as `attemptAsyncPipe` and `attemptPipe.async`_ * * @param initial Initial value * @returns _Piped_ result */ export async function attemptAsyncPipe( initial: GenericCallback | Initial | Promise | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, ): Promise>; /** * Attempt to pipe a value through a series of functions * * _Available as `attemptAsyncPipe` and `attemptPipe.async`_ * * @param initial Initial value * @returns _Piped_ result */ export async function attemptAsyncPipe( initial: GenericCallback | Initial | Promise | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, fourth: (value: UnwrapValue) => Fourth, ): Promise>; /** * Attempt to pipe a value through a series of functions * * _Available as `attemptAsyncPipe` and `attemptPipe.async`_ * * @param initial Initial value * @returns _Piped_ result */ export async function attemptAsyncPipe( initial: GenericCallback | Initial | Promise | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, fourth: (value: UnwrapValue) => Fourth, fifth: (value: UnwrapValue) => Fifth, ): Promise>; /** * Attempt to pipe a value through a series of functions * * _Available as `attemptAsyncPipe` and `attemptPipe.async`_ * * @param initial Initial value * @returns _Piped_ result */ export async function attemptAsyncPipe( initial: GenericCallback | Initial | Promise | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, fourth: (value: UnwrapValue) => Fourth, fifth: (value: UnwrapValue) => Fifth, sixth: (value: UnwrapValue) => Sixth, ): Promise>; /** * Attempt to pipe a value through a series of functions * * _Available as `attemptAsyncPipe` and `attemptPipe.async`_ * * @param initial Initial value * @returns _Piped_ result */ export async function attemptAsyncPipe< Initial, First, Second, Third, Fourth, Fifth, Sixth, Seventh, >( initial: GenericCallback | Initial | Promise | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, fourth: (value: UnwrapValue) => Fourth, fifth: (value: UnwrapValue) => Fifth, sixth: (value: UnwrapValue) => Sixth, seventh: (value: UnwrapValue) => Seventh, ): Promise>; /** * Attempt to pipe a value through a series of functions * * _Available as `attemptAsyncPipe` and `attemptPipe.async`_ * * @param initial Initial value * @returns _Piped_ result */ export async function attemptAsyncPipe< Initial, First, Second, Third, Fourth, Fifth, Sixth, Seventh, Eighth, >( initial: GenericCallback | Initial | Promise | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, fourth: (value: UnwrapValue) => Fourth, fifth: (value: UnwrapValue) => Fifth, sixth: (value: UnwrapValue) => Sixth, seventh: (value: UnwrapValue) => Seventh, eighth: (value: UnwrapValue) => Eighth, ): Promise>; /** * Attempt to pipe a value through a series of functions * * _Available as `attemptAsyncPipe` and `attemptPipe.async`_ * * @param initial Initial value * @returns _Piped_ result */ export async function attemptAsyncPipe< Initial, First, Second, Third, Fourth, Fifth, Sixth, Seventh, Eighth, Ninth, >( initial: GenericCallback | Initial | Promise | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, fourth: (value: UnwrapValue) => Fourth, fifth: (value: UnwrapValue) => Fifth, sixth: (value: UnwrapValue) => Sixth, seventh: (value: UnwrapValue) => Seventh, eighth: (value: UnwrapValue) => Eighth, ninth: (value: UnwrapValue) => Ninth, ): Promise>; /** * Attempt to pipe a value through a series of functions * * _Available as `attemptAsyncPipe` and `attemptPipe.async`_ * * @param initial Initial value * @returns _Piped_ result */ export async function attemptAsyncPipe< Initial, First, Second, Third, Fourth, Fifth, Sixth, Seventh, Eighth, Ninth, Tenth, >( initial: GenericCallback | Initial | Promise | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, fourth: (value: UnwrapValue) => Fourth, fifth: (value: UnwrapValue) => Fifth, sixth: (value: UnwrapValue) => Sixth, seventh: (value: UnwrapValue) => Seventh, eighth: (value: UnwrapValue) => Eighth, ninth: (value: UnwrapValue) => Ninth, tenth: (value: UnwrapValue) => Tenth, ): Promise>; /** * Attempt to pipe a result through a series of functions * * _Available as `attemptAsyncPipe` and `attemptPipe.async`_ * * @param initial Initial result * @returns _Piped_ result */ export async function attemptAsyncPipe( initial: GenericCallback | Initial | Promise | Result, first?: (value: UnwrapValue) => unknown, ...seconds: Array<(value: unknown) => unknown> ): Promise>; export async function attemptAsyncPipe( initial: unknown, first?: (value: unknown) => unknown, ...seconds: Array<(value: unknown) => unknown> ): Promise> { return attempt.async(() => { if (isError(initial)) { throw initial.error; } const value = typeof initial === 'function' ? (initial as Function)() : isOk(initial) ? initial.value : initial; if (first == null) { return value; } return asyncPipe(value, ...([first, ...seconds] as GenericCallback[])); }); } // #endregion // #region Synchronous /** * Attempt to pipe a value through a function * * @param initial Initial value * @returns _Piped_ result */ export function attemptPipe( initial: GenericCallback | Initial | Result, pipe: (value: UnwrapValue) => Pipe, ): WrapValue; /** * Attempt to pipe a value through a series of functions * * @param initial Initial value * @returns _Piped_ result */ export function attemptPipe( initial: GenericCallback | Initial | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, ): WrapValue; /** * Attempt to pipe a value through a series of functions * * @param initial Initial value * @returns _Piped_ result */ export function attemptPipe( initial: GenericCallback | Initial | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, ): WrapValue; /** * Attempt to pipe a value through a series of functions * * @param initial Initial value * @returns _Piped_ result */ export function attemptPipe( initial: GenericCallback | Initial | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, fourth: (value: UnwrapValue) => Fourth, ): WrapValue; /** * Attempt to pipe a value through a series of functions * * @param initial Initial value * @returns _Piped_ result */ export function attemptPipe( initial: GenericCallback | Initial | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, fourth: (value: UnwrapValue) => Fourth, fifth: (value: UnwrapValue) => Fifth, ): WrapValue; /** * Attempt to pipe a value through a series of functions * * @param initial Initial value * @returns _Piped_ result */ export function attemptPipe( initial: GenericCallback | Initial | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, fourth: (value: UnwrapValue) => Fourth, fifth: (value: UnwrapValue) => Fifth, sixth: (value: UnwrapValue) => Sixth, ): WrapValue; /** * Attempt to pipe a value through a series of functions * * @param initial Initial value * @returns _Piped_ result */ export function attemptPipe( initial: GenericCallback | Initial | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, fourth: (value: UnwrapValue) => Fourth, fifth: (value: UnwrapValue) => Fifth, sixth: (value: UnwrapValue) => Sixth, seventh: (value: UnwrapValue) => Seventh, ): WrapValue; /** * Attempt to pipe a value through a series of functions * * @param initial Initial value * @returns _Piped_ result */ export function attemptPipe( initial: GenericCallback | Initial | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, fourth: (value: UnwrapValue) => Fourth, fifth: (value: UnwrapValue) => Fifth, sixth: (value: UnwrapValue) => Sixth, seventh: (value: UnwrapValue) => Seventh, eighth: (value: UnwrapValue) => Eighth, ): WrapValue; /** * Attempt to pipe a value through a series of functions * * @param initial Initial value * @returns _Piped_ result */ export function attemptPipe< Initial, First, Second, Third, Fourth, Fifth, Sixth, Seventh, Eighth, Ninth, >( initial: GenericCallback | Initial | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, fourth: (value: UnwrapValue) => Fourth, fifth: (value: UnwrapValue) => Fifth, sixth: (value: UnwrapValue) => Sixth, seventh: (value: UnwrapValue) => Seventh, eighth: (value: UnwrapValue) => Eighth, ninth: (value: UnwrapValue) => Ninth, ): WrapValue; /** * Attempt to pipe a value through a series of functions * * @param initial Initial value * @returns _Piped_ result */ export function attemptPipe< Initial, First, Second, Third, Fourth, Fifth, Sixth, Seventh, Eighth, Ninth, Tenth, >( initial: GenericCallback | Initial | Result, first: (value: UnwrapValue) => First, second: (value: UnwrapValue) => Second, third: (value: UnwrapValue) => Third, fourth: (value: UnwrapValue) => Fourth, fifth: (value: UnwrapValue) => Fifth, sixth: (value: UnwrapValue) => Sixth, seventh: (value: UnwrapValue) => Seventh, eighth: (value: UnwrapValue) => Eighth, ninth: (value: UnwrapValue) => Ninth, tenth: (value: UnwrapValue) => Tenth, ): WrapValue; /** * Attempt to pipe a result through a series of functions * * @param initial Initial result * @returns _Piped_ result */ export function attemptPipe( initial: GenericCallback | Initial | Result, first?: (value: UnwrapValue) => unknown, ...seconds: Array<(value: unknown) => unknown> ): WrapValue; export function attemptPipe( initial: unknown, first?: (value: unknown) => unknown, ...seconds: Array<(value: unknown) => unknown> ): WrapValue { return attempt(() => { if (isError(initial)) { throw initial.error; } const value = typeof initial === 'function' ? (initial as Function)() : isOk(initial) ? initial.value : initial; if (first == null) { return value; } return pipe(value, ...([first, ...seconds] as GenericCallback[])); }); } attemptPipe.async = attemptAsyncPipe; // #endregion // #endregion