Pacote
    Preparing search index...

    Function iff

    • Evaluates a condition and returns the consequent in an Option.

      Type Parameters

      • T

      Parameters

      • predicate: boolean

        Condition to evaluate.

      • onConsequent: () => T

        Callback used when the condition is true.

      Returns Option<T>

      Some of the callback result when true, or None when false.

      import { iff } from '@pacote/iff'
      import { Some } from '@pacote/option'

      iff(true, () => 1, () => 0) // => 1
      iff(false, () => 1) // => None
      iff(false, () => 1, () => 0) // => 0
      iff(true, () => Some(1)) // => Some(1)
      // iff(true, () => 1, () => 'zero') // TypeScript error: branch types differ
    • Evaluates a condition and calls the matching branch.

      Type Parameters

      • T

      Parameters

      • predicate: boolean

        Condition to evaluate.

      • onConsequent: () => T

        Callback used when the condition is true.

      • onAlternative: () => T

        Callback used when the condition is false.

      Returns T

      The result returned by the selected callback.