@pacote/iff
TypeScript icon, indicating that this package has built-in type declarations

3.0.2 • Public • Published

@pacote/iff

version minified minified + gzip

Conditional expressions with Option support.

Installation

yarn add @pacote/iff

Usage

import { iff } from '@pacote/iff'

iff(
  true,
  () => 1,
  () => 0
) // => 1

iff(
  false,
  () => 1,
  () => 0
) // => 0

iff(
  true,
  () => 1,
  () => 'zero'
) // type error, branches are not the same type

iff(true, () => 1) // => Some(1)

iff(false, () => 1) // => None

iff(predicate: boolean, onConsequent: () => T, onAlternative?: () => T): T

iff() evaluates a predicate and returns the result of calling onConsequent() if the predicate is true or the result of onAlternative() if the predicate is false.

onConsequent and onAlternative must be functions with the same return type.

iff(predicate: boolean, onConsequent: () => T): Option<T>

If onAlternative is not provided or is nullable, iff() will always return an Option<T> data type: Some<T> if the predicate is true, or None if the predicate is false.

See also

License

MIT © Luís Rodrigues.

/@pacote/iff/

    Package Sidebar

    Install

    npm i @pacote/iff

    Weekly Downloads

    1

    Version

    3.0.2

    License

    MIT

    Unpacked Size

    10.7 kB

    Total Files

    13

    Last publish

    Collaborators

    • goblindegook