when
when() function
It returns the original value based on the condition of the first argument or the result of executing the function passed as the second argument.
Signature:declare function when<T, R>(predicate: (input: T) => boolean, callback: (input: T) => R, a: T): T | R;
declare function when<T, R>(predicate: (input: T) => boolean, callback: (input: T) => R): (a: T) => T | R;
declare function when<T, S extends T, R>(predicate: (input: T) => input is S, callback: (input: S) => R, a: T): Exclude<T, S>;
declare function when<T, S extends T, R>(predicate: (input: T) => input is S, callback: (input: S) => R): (a: T) => Exclude<T, S>;
Example
when(
isNumber,
() => `This is number`
100,
); // This is number
with pipe
pipe(
100,
when(
isNumber,
() => `This is number`
)
) // This is number
pipe(
100,
when(
isNumber,
(value) => value * 2
)
) // 200
pipe(
100,
when(
isString,
() => `This is number` // not work
),
) // 100