@cprussin/option-result
Version:
Yet another Typescript clone of the rust `Option` & `Result` enums.
1,063 lines (600 loc) • 20.4 kB
Markdown
[**@cprussin/option-result v2.0.0**](../README.md)
***
# Class: Option\<T\>
Defined in: [option.ts:15](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L15)
A type which represents values that may or may not be present, without using
`undefined` or `null`.
## Type Parameters
### T
`T` *extends* `NonNullable`\<`unknown`\>
if this Option contains a value, this is the type of the value
## Boolean operators
### and()
> **and**\<`U`\>(`this`, `other`): `Option`\<`U`\>
Defined in: [option.ts:233](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L233)
Takes another Option and returns it if `this` is `Some`, otherwise
returns `None`.
#### Type Parameters
##### U
`U` *extends* `object`
the type contained in the Option returned by `fn`
#### Parameters
##### this
`Option`\<`T`\>
##### other
`Option`\<`U`\>
an Option to return if `this` is `Some`
#### Returns
`Option`\<`U`\>
`other` if `this` is `Some`, `None` otherwise
#### See
- [andThen](#andthen)
- [andThenAsync](#andthenasync)
- [or](#or)
- [orElse](#orelse)
- [orElseAsync](#orelseasync)
- [xor](#xor)
***
### andThen()
> **andThen**\<`U`\>(`this`, `fn`): `Option`\<`U`\>
Defined in: [option.ts:208](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L208)
Takes a function which takes the value contained in `this` and returns a
new Option; calls it and returns the result if `this` is `Some`,
otherwise returns `None`. Also sometimes known in other languages or
libraries as `flatmap` or `bind`.
#### Type Parameters
##### U
`U` *extends* `object`
the type contained in the Option returned by `fn`
#### Parameters
##### this
`Option`\<`T`\>
##### fn
(`value`) => `Option`\<`U`\>
a function that will be called with the value in `this` if
`this` is `Some`
#### Returns
`Option`\<`U`\>
the result of calling `fn` if `this` is `Some`, `None` otherwise
#### See
- [and](#and)
- [andThenAsync](#andthenasync)
- [or](#or)
- [orElse](#orelse)
- [orElseAsync](#orelseasync)
- [xor](#xor)
***
### andThenAsync()
> **andThenAsync**\<`U`\>(`this`, `fn`): `Promise`\<`Option`\<`U`\>\>
Defined in: [option.ts:258](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L258)
Takes a function which takes the value contained in `this` and returns a
promise which resolves to a new Option; calls it and returns the
result if `this` is `Some`, otherwise returns a promise which resolves to
`None`. This is an async version of [andThen](#andthen).
#### Type Parameters
##### U
`U` *extends* `object`
the type contained in the Option returned by `fn`
#### Parameters
##### this
`Option`\<`T`\>
##### fn
(`value`) => `Promise`\<`Option`\<`U`\>\>
a function that will be called with the value in `this` if
`this` is `Some`
#### Returns
`Promise`\<`Option`\<`U`\>\>
the result of calling `fn` if `this` is `Some`, `None` otherwise
#### See
- [and](#and)
- [andThen](#andthen)
- [or](#or)
- [orElse](#orelse)
- [orElseAsync](#orelseasync)
- [xor](#xor)
***
### or()
> **or**(`this`, `other`): `Option`\<`T`\>
Defined in: [option.ts:303](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L303)
Takes another Option and returns it if `this` is `None`, otherwise
returns `this`.
#### Parameters
##### this
`Option`\<`T`\>
##### other
`Option`\<`T`\>
an Option to return if `this` is `None`
#### Returns
`Option`\<`T`\>
`other` if `this` is `None`, `this` otherwise
#### See
- [and](#and)
- [andThen](#andthen)
- [andThenAsync](#andthenasync)
- [orElse](#orelse)
- [orElseAsync](#orelseasync)
- [xor](#xor)
***
### orElse()
> **orElse**(`this`, `fn`): `Option`\<`T`\>
Defined in: [option.ts:282](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L282)
Takes a function that returns an Option; calls it and returns the
result if `this` is `None`, otherwise returns `this`.
#### Parameters
##### this
`Option`\<`T`\>
##### fn
() => `Option`\<`T`\>
a function that will be called if `this` is `None`
#### Returns
`Option`\<`T`\>
the result of calling `fn` if `this` is `None`, `this` otherwise
#### See
- [and](#and)
- [andThen](#andthen)
- [andThenAsync](#andthenasync)
- [or](#or)
- [orElseAsync](#orelseasync)
- [xor](#xor)
***
### orElseAsync()
> **orElseAsync**(`this`, `fn`): `Promise`\<`Option`\<`T`\>\>
Defined in: [option.ts:322](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L322)
Takes a function that returns a promise which resolves to a new Option; calls it and returns the result if `this` is `None`, otherwise
returns a promise which resolves to `this`.
#### Parameters
##### this
`Option`\<`T`\>
##### fn
() => `Promise`\<`Option`\<`T`\>\>
a function that will be called if `this` is `None`
#### Returns
`Promise`\<`Option`\<`T`\>\>
the result of calling `fn` if `this` is `None`, `this` otherwise
#### See
- [and](#and)
- [andThen](#andthen)
- [andThenAsync](#andthenasync)
- [or](#or)
- [orElse](#orelse)
- [xor](#xor)
***
### xor()
> **xor**(`this`, `other`): `Option`\<`T`\>
Defined in: [option.ts:348](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L348)
Takes another Option, if both `this` and the other Option
are either `Some` or `None`, returns `None`. Otherwise, returns whichever
Option is `Some`.
#### Parameters
##### this
`Option`\<`T`\>
##### other
`Option`\<`T`\>
the other Option to compare with `this`
#### Returns
`Option`\<`T`\>
`None` if both `this` and `other` are either `Some` or `None`;
otherwise returns whichever Option is `Some`
#### See
- [and](#and)
- [andThen](#andthen)
- [andThenAsync](#andthenasync)
- [or](#or)
- [orElse](#orelse)
- [orElseAsync](#orelseasync)
## Constructing
### None()
> `static` **None**\<`T`\>(): `Option`\<`T`\>
Defined in: [option.ts:47](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L47)
Construct an empty Option.
#### Type Parameters
##### T
`T` *extends* `object`
the type contained by the Option
#### Returns
`Option`\<`T`\>
an empty Option
#### See
- [Some](../functions/Some.md)
- [wrap](#wrap)
***
### Some()
> `static` **Some**\<`T`\>(`value`): `Option`\<`T`\>
Defined in: [option.ts:34](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L34)
Construct an Option containing a value.
#### Type Parameters
##### T
`T` *extends* `object`
the type contained by the Option
#### Parameters
##### value
`T`
the value contained by the Option
#### Returns
`Option`\<`T`\>
an Option containing `value`
#### See
- [None](../functions/None.md)
- [wrap](#wrap)
***
### wrap()
> `static` **wrap**\<`T`\>(`value`): `Option`\<`NonNullable`\<`T`\>\>
Defined in: [option.ts:96](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L96)
Takes a value that could be `null` or `undefined` and converts it into an
Option.
#### Type Parameters
##### T
`T`
the type contained by the Option values
#### Parameters
##### value
`T`
the nullable value
#### Returns
`Option`\<`NonNullable`\<`T`\>\>
`None` if `value` is `null` or `undefined`, `Some` containing
`value` otherwise
#### See
- [Some](../functions/Some.md)
- [None](../functions/None.md)
## Extracting the contained value
### match()
> **match**\<`U`\>(`this`, `matchers`): `U`
Defined in: [option.ts:135](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L135)
Takes two functions, one is called with the contained value if `this` is
`Some` and the other is called if `this` is `None`.
#### Type Parameters
##### U
`U`
the type of the return value of the matcher functions
#### Parameters
##### this
`Option`\<`T`\>
##### matchers
an object containing matcher functions
###### None
() => `U`
A function that will be called if `this` is `None`.
###### Some
(`value`) => `U`
A function that will be called on the value contained if `this` is
`Some`.
#### Returns
`U`
the return value of the `Some` or `None` matcher function
#### See
- [unwrapOrElse](#unwraporelse)
- [unwrapOr](#unwrapor)
***
### unwrapOr()
> **unwrapOr**(`this`, `defaultValue`): `T`
Defined in: [option.ts:186](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L186)
Returns the contained value if `this` is `Some`, otherwise return the
provided default value.
#### Parameters
##### this
`Option`\<`T`\>
##### defaultValue
`T`
the value to return if `this` is `None`
#### Returns
`T`
the contained if `this` is `Some`, otherwise `defaultValue`
#### See
- [unwrapOrElse](#unwraporelse)
- [match](#match)
***
### unwrapOrElse()
> **unwrapOrElse**(`this`, `defaultValue`): `T`
Defined in: [option.ts:169](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L169)
Returns the contained value if `this` is `Some`, otherwise call the
provided function and return the result.
#### Parameters
##### this
`Option`\<`T`\>
##### defaultValue
() => `T`
a function that will be called if `this` is `None`
#### Returns
`T`
the contained if `this` is `Some`, otherwise the return value from
calling `defaultValue`
#### See
- [unwrapOr](#unwrapor)
- [match](#match)
## Interacting with Result
### okOr()
> **okOr**\<`E`\>(`this`, `error`): [`Result`](Result.md)\<`T`, `E`\>
Defined in: [option.ts:537](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L537)
If `this` is a `Some`, return an [Result.Ok](Result.md#ok-2) with the contained
value. Otherwise, return an [Result.Err](Result.md#err-2) containing the passed
default error value.
#### Type Parameters
##### E
`E` *extends* `object`
the type of `error` (and the error type of the returned
[Result](Result.md))
#### Parameters
##### this
`Option`\<`T`\>
##### error
`E`
an error value to use if `this` is `None`
#### Returns
[`Result`](Result.md)\<`T`, `E`\>
[Result.Ok](Result.md#ok-2) containing the value in `this` if `this` is
`Some`, otherwise [Result.Err](Result.md#err-2) containing `error`
#### See
- [okOrElse](#okorelse)
- [transpose](#transpose)
***
### okOrElse()
> **okOrElse**\<`E`\>(`this`, `error`): [`Result`](Result.md)\<`T`, `E`\>
Defined in: [option.ts:513](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L513)
If `this` is a `Some`, return an [Result.Ok](Result.md#ok-2) with the contained
value. Otherwise, return an [Result.Err](Result.md#err-2) with the value returned by
calling the passed function.
#### Type Parameters
##### E
`E` *extends* `object`
the return type of `fn` (and the error type of the returned
[Result](Result.md))
#### Parameters
##### this
`Option`\<`T`\>
##### error
() => `E`
a function which returns an error value to use if `this` is
`None`
#### Returns
[`Result`](Result.md)\<`T`, `E`\>
[Result.Ok](Result.md#ok-2) containing the value in `this` if `this` is
`Some`, otherwise [Result.Err](Result.md#err-2) containing the value returned by
`error`
#### See
- [okOr](#okor)
- [transpose](#transpose)
***
### transpose()
> **transpose**\<`T`, `E`\>(`this`): [`Result`](Result.md)\<`Option`\<`T`\>, `E`\>
Defined in: [option.ts:559](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L559)
Convert an Option containing a [Result](Result.md) into a [Result](Result.md)
containing an Option.
#### Type Parameters
##### T
`T` *extends* `object`
the type of the value contained in the [Result](Result.md)
contained in `this`
##### E
`E` *extends* `object`
the error type of the [Result](Result.md) contained in `this`
#### Parameters
##### this
`Option`\<[`Result`](Result.md)\<`T`, `E`\>\>
#### Returns
[`Result`](Result.md)\<`Option`\<`T`\>, `E`\>
if `this` is `None`, returns [Result.Ok](Result.md#ok-2) containing `None`;
if `this` is `Some` and the [Result](Result.md) in `this` is [Result.Err](Result.md#err-2),
returns the [Result.Err](Result.md#err-2); otherwise returns [Result.Ok](Result.md#ok-2)
containing `Some` containing the value in the [Result](Result.md) in `this`
#### See
- [okOr](#okor)
- [okOrElse](#okorelse)
## Querying the variant
### isNone()
> **isNone**(`this`): `boolean`
Defined in: [option.ts:120](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L120)
Check if `this` is `None`.
#### Parameters
##### this
`Option`\<`T`\>
#### Returns
`boolean`
true if `this` is `None`, `false` otherwise
#### See
[isSome](#issome)
***
### isSome()
> **isSome**(`this`): `boolean`
Defined in: [option.ts:109](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L109)
Check if `this` is `Some`.
#### Parameters
##### this
`Option`\<`T`\>
#### Returns
`boolean`
true if `this` is `Some`, `false` otherwise
#### See
[isNone](#isnone)
## Transforming contained values
### filter()
> **filter**(`this`, `fn`): `Option`\<`T`\>
Defined in: [option.ts:442](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L442)
If `this` is `None`, returns `None`. Otherwise, calls the predicate on the
value contained in `this`; if the predicate returns `true` then `this` is
returned, if the predicate returns `false` then `None` is returned.
#### Parameters
##### this
`Option`\<`T`\>
##### fn
(`value`) => `boolean`
the predicate function
#### Returns
`Option`\<`T`\>
`None` if `this` is `None` or if `fn` returns `false` when applied
to the value in `this`, `this` otherwise
#### See
- [map](#map)
- [mapAsync](#mapasync)
- [mapOr](#mapor)
- [zip](#zip)
- [unzip](#unzip)
***
### map()
> **map**\<`U`\>(`this`, `fn`): `Option`\<`U`\>
Defined in: [option.ts:368](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L368)
Transforms `Option<T>` to `Option<U>` by applying the provided function to
the contained value of `Some` and leaving `None` values unchanged.
#### Type Parameters
##### U
`U` *extends* `object`
the type of the return value of `fn`
#### Parameters
##### this
`Option`\<`T`\>
##### fn
(`value`) => `U`
the function to apply to the value contained if `this` is `Some`
#### Returns
`Option`\<`U`\>
`None` if `this` is `None`, otherwise `Some` containing the result
of applying `fn` to the value in `this`
#### See
- [mapAsync](#mapasync)
- [mapOr](#mapor)
- [filter](#filter)
- [zip](#zip)
- [unzip](#unzip)
- [collect](#collect)
***
### mapAsync()
> **mapAsync**\<`U`\>(`this`, `fn`): `Promise`\<`Option`\<`U`\>\>
Defined in: [option.ts:394](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L394)
Transforms `Option<T>` to `Promise<Option<U>>` by applying the provided
async function to the contained value of `Some` and resolving `None` values
unchanged.
#### Type Parameters
##### U
`U` *extends* `object`
the type of the value in the promise returned by `fn`
#### Parameters
##### this
`Option`\<`T`\>
##### fn
(`value`) => `Promise`\<`U`\>
the function to apply to the value contained if `this` is
`Some`
#### Returns
`Promise`\<`Option`\<`U`\>\>
a promise resolving to `None` if `this` is `None`, otherwise a
promise resolving to `Some` containing the value resolved by the promise
returned from applying `fn` to the value in `this`
#### See
- [map](#map)
- [mapOr](#mapor)
- [filter](#filter)
- [zip](#zip)
- [unzip](#unzip)
- [collect](#collect)
***
### mapOr()
> **mapOr**\<`U`\>(`this`, `defaultValue`, `fn`): `U`
Defined in: [option.ts:419](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L419)
Applies the provided function to the contained value if `this` is `Some`,
otherwise returns the provided default value.
#### Type Parameters
##### U
`U` *extends* `object`
the type of `defaultValue` and the value returned by `fn`
#### Parameters
##### this
`Option`\<`T`\>
##### defaultValue
`U`
the value to return if `this` is `None`
##### fn
(`value`) => `U`
the function to apply to the value contained if `this` is
`Some`
#### Returns
`U`
the result of applying `fn` to the value in `this` if `this` is
`Some`, otherwise `defaultValue`
#### See
- [map](#map)
- [mapAsync](#mapasync)
- [filter](#filter)
- [zip](#zip)
- [unzip](#unzip)
- [collect](#collect)
***
### unzip()
> **unzip**\<`T`, `U`\>(`this`): readonly \[`Option`\<`T`\>, `Option`\<`U`\>\]
Defined in: [option.ts:488](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L488)
Called on an Option containing a tuple -- if `this` is `Some`,
returns a tuple of `Some` containing the contained values. Otherwise,
returns a tuple of `None`.
#### Type Parameters
##### T
`T` *extends* `object`
the type of the first value in the tuple in `this`
##### U
`U` *extends* `object`
the type of the second value in the tuple in `this`
#### Parameters
##### this
`Option`\<readonly \[`T`, `U`\]\>
#### Returns
readonly \[`Option`\<`T`\>, `Option`\<`U`\>\]
a tuple of `Some` containing the values in the tuple contained in
`this` if `this` is `Some`, a tuple of `None` otherwise
#### See
- [map](#map)
- [mapAsync](#mapasync)
- [mapOr](#mapor)
- [filter](#filter)
- [zip](#zip)
***
### zip()
> **zip**\<`U`\>(`this`, `other`): `Option`\<readonly \[`T`, `U`\]\>
Defined in: [option.ts:462](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L462)
Takes another Option, if both Option values are `Some`,
returns a `Some` containing a tuple of the contained values. Otherwise
returns `None`.
#### Type Parameters
##### U
`U` *extends* `object`
the type of the value in `other`
#### Parameters
##### this
`Option`\<`T`\>
##### other
`Option`\<`U`\>
the other Option to zip with `this`
#### Returns
`Option`\<readonly \[`T`, `U`\]\>
`Some` containing a tuple with the values in `this` and `other` if
both `this` and `other` are `Some`, `None` otherwise
#### See
- [map](#map)
- [mapAsync](#mapasync)
- [mapOr](#mapor)
- [filter](#filter)
- [unzip](#unzip)
***
### collect()
> `static` **collect**\<`T`\>(`options`): `Option`\<`T`[]\>
Defined in: [option.ts:70](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/option.ts#L70)
Takes an Array of Option values. If any of the Option
values in the array is `None`, returns `None`. Otherwise, returns `Some`
containing an Array of each value inside each Option in the
original list.
#### Type Parameters
##### T
`T` *extends* `object`
the type contained by the Option values
#### Parameters
##### options
`Option`\<`T`\>[]
the Option values to collect
#### Returns
`Option`\<`T`[]\>
`Some` containing an Array of values contained by each Option in the original Array if all Option values in the original
list are `Some`, `None` otherwise
#### See
- [map](#map)
- [mapAsync](#mapasync)
- [mapOr](#mapor)
- [filter](#filter)
- [zip](#zip)
- [unzip](#unzip)