import type { BranchReturn, CanComposeInParallel, CanComposeInSequence, Composable, MapParametersReturn, MergeObjects, PipeReturn, RecordToTuple, Result, SequenceReturn, UnpackData } from './types.js'; import type { Internal } from './internal/types.js'; /** * Merges a list of objects into a single object. * * It is a type-safe version of Object.assign. * * @param objs the list of objects to merge * @returns the merged object * * @example * * ```ts * const obj1 = { a: 1, b: 2 } * const obj2 = { c: 3 } * const obj3 = { d: 4 } * const merged = mergeObjects([obj1, obj2, obj3]) * // ^? { a: number, b: number, c: number, d: number } * ``` */ declare function mergeObjects(objs: T): MergeObjects; /** * Composes functions to run in sequence returning the result of the rightmost function when all are successful. * * It will pass the output of a function as the next function's input in left-to-right order. * * @param {Fns} fns the list of composables to run in sequence * @returns a composable that runs the pipe * * @example * * ```ts * import { pipe } from 'composable-functions' * * const a = ({ aNumber }: { aNumber: number }) => ({ aString: String(aNumber) }) * const b = ({ aString }: { aString: string }) => ({ aBoolean: aString == '1' }) * const d = pipe(a, b) * // ^? Composable<({ aNumber }: { aNumber: number }) => { aBoolean: boolean }> * ``` */ declare function pipe(...fns: Fns): PipeReturn>>; /** * Composes functions to run in parallel returning a tuple of all results when all are successful. * * It will pass the same input to each provided function. The functions will run in parallel. * * @param {Fns} fns the list of composables to run in parallel * @returns a new composable that runs all inputs * * @example * * ```ts * import { all } from 'composable-functions' * * const a = (id: number) => id + 1 * const b = (x: unknown) => String(x) * const c = (x: unknown) => Boolean(x) * const cf = all(a, b, c) * // ^? Composable<(id: number) => [string, number, boolean]> * ``` */ declare function all(...fns: Fns): Composable<(...args: Parameters>[0]>>) => { [k in keyof Fns]: UnpackData[k]>; }>; /** * Composes functions to run in parallel returning a record with same keys as inputs with respective results when all are successful. * * @example * * ```ts * import { collect } from 'composable-functions' * * const a = () => '1' * const b = () => 2 * const aComposable = collect({ a, b }) * // ^? Composable<() => { a: string, b: number }> * ``` */ declare function collect>(fns: Fns): Fns extends Record ? Composable<(...args: Parameters>>[0], undefined>>) => { [key in keyof Fns]: UnpackData>; }> : never; /** * Works like `pipe` but it will collect the output of every function in a tuple. * * @example * * ```ts * import { sequence } from 'composable-functions' * * const a = (aNumber: number) => String(aNumber) * const b = (aString: string) => aString === '1' * const cf = sequence(a, b) * // ^? Composable<(aNumber: number) => [string, boolean]> * ``` */ declare function sequence(...fns: Fns): SequenceReturn>>; /** * It takes a Composable and a mapper to apply a transformation over the resulting output. It only runs if the function was successfull. When the given function fails, its error is returned wihout changes. * The mapper also receives the original input parameters. * * @example * * ```ts * import { map } from 'composable-functions' * * const increment = (n: number) => n + 1 * const incrementToString = map(increment, String) * // ^? Composable<(n: number) => string> * const result = await map(increment, (result, n) => `${n} -> ${result}`)(1) * // result === '1 -> 2' * ``` */ declare function map(fn: Fn, mapper: (res: UnpackData>>, ...originalInput: Parameters>) => O | Promise): Fn extends Internal.AnyFn ? Composable<(...args: Parameters) => O> : never; /** * It takes a Composable and a function that will map the input parameters to the expected input of the given Composable. Good to adequate the output of a composable into the input of the next composable in a composition. The function must return an array of parameters that will be passed to the Composable. * @returns a new Composable that will run the given Composable with the mapped parameters. * * @example * * ```ts * import { mapParameters } from 'composable-functions' * * const incrementId = ({ id }: { id: number }) => id + 1 * const increment = mapParameters(incrementId, (id: number) => [{ id }]) * // ^? Composable<(id: number) => number> * ``` */ declare function mapParameters>>>(fn: Fn, mapper: (...args: NewParameters) => Promise | MapperOutput): Fn extends Internal.AnyFn ? MapParametersReturn, NewParameters, MapperOutput> : never; /** * Try to recover from a resulting Failure. When the given function succeeds, its result is returned without changes. * * @example * * ```ts * import { catchFailure } from 'composable-functions' * * const increment = ({ id }: { id: number }) => id + 1 * const negativeOnError = catchFailure(increment, (result, originalInput) => ( * originalInput.id * -1 * )) * ``` */ declare function catchFailure>) => any>(fn: Fn, catcher: C): Fn extends Internal.AnyFn ? Composable<(...args: Parameters) => Awaited> extends never[] ? UnpackData> extends any[] ? UnpackData> : Awaited> | UnpackData> : Awaited> | UnpackData>> : never; /** * Creates a new function that will apply a transformation over the list of Errors of a Failure from a given function. When the given function succeeds, its result is returned without changes. The mapper receives the original input. * * @example * * ```ts * import { mapErrors } from 'composable-functions' * * const increment = ({ id }: { id: number }) => id + 1 * const incrementWithErrorSummary = mapErrors(increment, (result, {id}) => ({ * errors: [{ message: 'Errors count: ' + result.errors.length + ' for id: ' + String(id) }], * })) * ``` */ declare function mapErrors(fn: Fn, mapper: (err: Error[], ...originalInput: Parameters>) => Error[] | Promise): Fn extends Internal.AnyFn ? Composable : never; /** * Whenever you need to intercept inputs and a composable result without changing them you can use this function. * The most common use case is to log failures to the console or to an external service. * @param traceFn A function that receives the input and result of a composable. * * @example * * ```ts * import { trace } from 'composable-functions' * * const trackErrors = trace((result, ...args) => { * if(!result.success) sendToExternalService({ result, arguments: args }) * }) * const increment = (id: number) => id + 1 * const incrementAndTrackErrors = trackErrors(increment) * // ^? Composable<(id: number) => number> * ``` */ declare function trace(traceFn: (result: Result, ...originalInput: unknown[]) => Promise | void): (fn: Fn) => Fn extends Internal.AnyFn ? Composable : never; /** * Compose 2 functions conditionally. * * Uses a resolver to decide whether it should just call the first function or pipe its result into a second function returned by the resolver. * * @param cf first composable to be called * @param resolver when it returns null aborts the composition or else returns the next composable in the chain. * @returns a new composable with the conditional pipe. * * @example * * ```ts * import { composable, branch } from 'composable-functions' * * const increment = composable( * (a: number) => a + 1 * ) * const makeItEven = (sum: number) => sum % 2 != 0 ? increment : null * const incrementUntilEven = branch(increment, makeItEven) * ``` */ declare function branch>>) => Internal.AnyFn | null | Promise>(cf: SourceComposable, resolver: Resolver): BranchReturn>, Resolver>; export { all, branch, catchFailure, collect, map, mapErrors, mapParameters, mergeObjects, pipe, sequence, trace, }; //# sourceMappingURL=combinators.d.ts.map