@pacote/iff

Conditional expressions with Option support.

Usage no npm install needed!

<script type="module">
  import pacoteIff from 'https://cdn.skypack.dev/@pacote/iff';
</script>

README

@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.