@pacote/iff
Version:
Conditional expressions with Option support.
64 lines (43 loc) • 1.3 kB
Markdown



Conditional expressions with Option support.
```bash
yarn add @pacote/iff
```
```typescript
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()` 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.
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`.
- [`@pacote/option`](../option/)
MIT © [Luís Rodrigues](https://goblindegook.com).