# typed-option

> Typed option library

Latest version **3.2.0** (published 2017-12-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install typed-option
pnpm add typed-option
yarn add typed-option
bun add typed-option
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.2.0 |
| Published | 2017-12-04 |
| First published | 2017-08-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | anderscan |
| Maintainers | anderscan |
| Keywords | option, typed, typescript, functional |

## Links

- npm: https://www.npmjs.com/package/typed-option
- Repository: https://github.com/AndersCan/typed-option
- Homepage: https://github.com/AndersCan/typed-option#readme
- Issues: https://github.com/AndersCan/typed-option/issues
- npm.io page: https://npm.io/package/typed-option

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 3.2.0 (latest) — 2017-12-04
- 3.1.0 — 2017-11-27
- 3.0.1 — 2017-11-25
- 3.0.0 — 2017-11-23
- 2.1.1 — 2017-08-13
- 2.0.1 — 2017-08-11
- 2.0.0 — 2017-08-11
- 1.0.0 — 2017-08-10
- 0.0.1 — 2017-08-08

## README

# typed-option
[![Build Status](https://travis-ci.org/AndersCan/typed-option.svg?branch=master)](https://travis-ci.org/AndersCan/typed-option)
[![codecov](https://codecov.io/gh/AndersCan/typed-option/branch/master/graph/badge.svg)](https://codecov.io/gh/AndersCan/typed-option)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)

Library for working with `Option`s in a type safe manner.

## tl;dr
`Option`s makes working with, potentially, `undefined` values more concise and easy to read.

**Example**

Given the function:
```javascript
function canFailFn(x: string): string | undefined {
  if (Math.random() > 0.5) {
    return x
  }
  return undefined
}
```

Turn something like this:
```javascript
let result = 'FAILED'
const a = canFailFn('SUCCESS') // a is string | undefined
if (a) {
  const b = canFailFn(a) // b is string | undefined
  if (b) {
    const c = canFailFn(b) // c is string | undefined
    if (c) {
      result = c // c is string
    }
  }
}
console.log(result)
```

Into this:
```javascript
import { Option } from 'typed-option'

const result2 = Option.from('SUCCESS')
  .map(v => canFailFn(v)) // v is of type string.
  .map(v => canFailFn(v)) // v is of type string.
  .map(v => canFailFn(v)) // v is of type string.
  .getOrElse('FAILED')
console.log(result2)
```

## Intro
An `Option` is either of type `Some` or `None`.
* **Some** - represents a value of something (a *defined* value)
* **None** - represents a value of nothing (an *undefined* value)

In the example above, we start with a `Some('SUCCESS')`. Then we apply the function `canFailFn` multiple times to the value of our `Some`. If the computation fails at any point, we are returned a `None`.

With Options, you do not need to know if you have a `Some` or `None` when applying functions. Only when you need the result do you have to deal with the potential `None` case. This is typically done by calling the `.getOrElse` function (as seen above).

### Unions with undefined
One thing to note is that `undefined` is removed from union types:
```javascript
interface Foo {
  a?: number
}
const myFoo: Foo = {
  a: 5
}
myFoo.a // 'number | undefined`'
const fooOption = Option.from(myFoo.a) // Option<number>
fooOption.map(a => a) // typeof a is number
```
This means that you will not have to worry about manually testing for `undefined`. A `Some` will never contain an `undefined` value.
## Functions
### Explanation

`Option.from` gives you a `None` for `undefined` values and a `Some` for `defined` values.
```javascript
Option.from(true) // Some(true)
Option.from(false) // Some(false)
Option.from({}) // Some({})
Option.from(0) // Some(0)
Option.from('') // Some('')
Option.from(undefined) // None
```
You can also pass an additional predicate of what a legal value is.
```javascript
Option.from(false, () => true) // Some(false)
Option.from(true, () => false) // None
Option.from(undefined, () => true) // None

```

`do` run a fn to a Option if it is of type `Some`. Similar to `map`, but does not alter the Option.
```javascript
some('world').do(text => console.log(text)) // logs: 'world'
none().do((text) => console.log(text)) // logs nothing
```

`map` apply fn to a Option if it is of type `Some`
```javascript
some('world').map((text) => "Hello, " + text) // Some('Hello, world')
none().map((text) => "Hello, " + text) // None
```

`flatMap` Same as map, but for when your `fn` returns a Option. `flatMap` will remove the nested option.
```javascript
function getOptionFn(...): Option<string>
const option = ... // Some('Hello, Option')
option.map(getOptionFn) // Some(Some('Hello, Option'))
option.flatMap(getOptionFn) // Some('Hello, Option')
```

`getOrElse` get the value from a `Some` or return the `else` value for a `None`
```javascript
some(1).getOrElse(() => 999) // 1
none().getOrElse(() => 999) // 999
some(1).getOrElse(999) // 1
none().getOrElse(999) // 999
```

`orElse` is the same as `getOrElse`, but `else` returns a Option
```javascript
some(1).orElse(() => Some(999)) // Some(1)
none().orElse(() => Some(999)) // Some(999)
```

`filter` gives a predicate that a `Some` must hold. If not, returns `None`
```javascript
some(1).filter((v) => false) // None()
some(100).filter((v) => true) // Some(100)
none().filter((v) => true) // None()
```

`match` gives a `pattern matching` syntax way to modify a Option. Usefull for when you only care if the Option is of type `Some`
```javascript
some(1).match({
  none: () => 'a none',
  some: v => `a Some(${v})`
}) // a Some(1)
none().match({
  none: () => 'a none',
  some: v => `a Some(${v})`
}) // a none

optionValueIgnored().match({
  none: 'failure',
  some: `success`
})
```

`isNone` tests if the given `Option` is of type `None`,
`isSome` tests if the given `Option` is of type `Some`
```javascript
const opt1 = ...
if(opt1.isNone()){
  // opt1 is None in this block
}
if(opt1.isSome()){
  // opt1 is Some in this block
}
```

---
_Source: https://npm.io/package/typed-option · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
