sergeysova/es2-result-option

View on GitHub
docs/option.mdx

Summary

Maintainability
Test Coverage
---
name: Option
route: /option
order: 5
---

# `Option`

Package exports `Option` and `Some`, `None`.

`Option` represents an optional value: every `Option` is either `Some` and contains value, or `None`, and does not. `Option`s have a number of uses:
- Initial values
- Return values for functions that are not defined over their entire input range (partial functions)
- Optional fields
- Optional function arguments
- Nullable pointers

```js
import { Option, Some, None } from '@es2/result-option'
```

`Some` and `None` is a shortcuts for `Option.some` and `Option.none`.

## Table of contents

- [`Option.encase`](#optionencase)
- [`Option.fromNullable`](#optionfromnullable)
- [`Option.into`](#optioninto)
- [`Option.is`](#optionis)
- [`Option.none`](#optionnone)
- [`Option.of`](#optionof)
- [`Option.some`](#optionsome)
- [`Option.zero`](#optionzero)

### Instance

- [`alt`](#alt)
- [`and`](#and)
- [`ap`](#ap)
- [`chain`](#chain)
- [`equals`](#equals)
- [`exists`](#exists)
- [`extend`](#extend)
- [`extractOr`](#extractor)
- [`extractOrElse`](#extractorelse)
- [`filter`](#filter)
- [`inspect`](#inspect)
- [`isNone`](#isnone)
- [`isSome`](#issome)
- [`iter`](#iter)
- [`map`](#map)
- [`mapOr`](#mapor)
- [`mapOrElse`](#maporelse)
- [`okOr`](#okor)
- [`okOrElse`](#okorelse)
- [`or`](#or)
- [`orElse`](#orelse)
- [`orElse`](#orelse)
- [`toString`](#tostring)


## Documentation

### `alt`

```hs
alt :: Alt f => f a ~> f a -> f a
```

```ts
alt(option: OptionClass<A>): OptionClass<A>;
```

[`Alt`](https://github.com/fantasyland/fantasy-land#alt).

`alt` short for alternative, takes another `Option`.
If current `Option` is a `Some` type then it will returned, if it is a `None` type then it will return the next `Some` from arguments if it exists.

If both are `None` then it will return `None`.

```js
t.deepEqual(Some(1).alt(Some(2)), Some(1))
t.deepEqual(None().alt(Some(2)), Some(2))
t.deepEqual(None().alt(None()), None())
```