Skip to content

combine

Combine multiple functions and execute them in the specified order of direction, by default of 'right' meaning executing from right to left. The return of each function takes the next function as an argument.

Added in v0.0.1

Usage

typescript
import { combine } from 'parsnip-kit'

function add(a: number, b: number): number { return a + b }
function multiply(a: number): number { return a * 2 }

const combinedRight = combine([multiply, add] as const)
combinedRight(2, 3) // 10

const combinedLeft = combine([add, multiply] as const, 'left')
combinedLeft(2, 3) // 10

// This could be useful for combining curried functions.
const multiplyCurried = (a: number) => (b: number) => a * b
const addCurried = (a: number) => (b: number) => a + b
const combinedRightCurried = combine([addCurried(2), multiplyCurried(3)] as const)

combinedRightCurried(5) // 17

API

Type Parameter

ArgTypeDescription
Textends readonly ((...args: any[]) => any)[]Function array type
Rextends 'left' | 'right' = 'left' | 'right'Combination direction type

Arguments

ArgTypeOptionalDefaultDescription
functionsTfalseundefinedThe array of functions to combine
directionRtrue'right'The direction in which to combine the functions

Returns

Type
(...args: EmptyOrParameters<Edge<T, R>>) => EmptyOrReturnType<EdgeReverse<T, R>>

Reference

Edge EdgeReverse EmptyOrParameters EmptyOrReturnType