For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/function/ifElse.md.

ifElse

Returns a function that branches between two functions based on a predicate.

const format = ifElse(
  (n: number) => n > 0,
  (n) => `+${n}`,
  (n) => `${n}`,
)
format(5)  // '+5'
format(-3) // '-3'

Usage

ifElse<T, R>(predicate: (value: T) => boolean, onTrue: (value: T) => R, onFalse: (value: T) => R)

Returns a function that branches between two functions based on a predicate.

import { ifElse } from 'massaman'
// or:  import { ifElse } from 'massaman/function'

const format = ifElse(
  (n: number) => n > 0,
  (n) => `+${n}`,
  (n) => `${n}`,
)
format(5)  // '+5'
format(-3) // '-3'

Parameters

predicate(value: T) => booleanrequired

the condition used to select behavior.

onTrue(value: T) => Rrequired

the function called when predicate returns true.

onFalse(value: T) => Rrequired

the function called when predicate returns false.

Returns

a function that dispatches each value to onTrue or onFalse.

(value: T) => R