match
match<
T,R>(conditions): (input) =>R|undefined
Defined in: logic/match.ts:25
Returns a function which encapsulates if/else logic based on a provided array of condition-action pairs.
Type Parameters
T
T
R
R
Parameters
conditions
[(input) => boolean, (input) => R][]
An array of tuples, each containing a predicate function and an action function
Returns
A function that takes an input and returns the result of the first matching action
(
input):R|undefined
Parameters
input
T
Returns
R | undefined
Example
const isEven = (n: number) => n % 2 === 0;const isOdd = (n: number) => n % 2 !== 0;const double = (n: number) => n * 2;const triple = (n: number) => n * 3;const processNumber = match<number, number>([ [isEven, double], [isOdd, triple],]);
console.log(processNumber(4)); // Outputs: 8 (4 is even, so double it)console.log(processNumber(5)); // Outputs: 15 (5 is odd, so triple it)