https://github.com/sigmasoldi3r/ts-option
Optional values for typescript
https://github.com/sigmasoldi3r/ts-option
functional-programming iterables optional optional-chaining
Last synced: 5 months ago
JSON representation
Optional values for typescript
- Host: GitHub
- URL: https://github.com/sigmasoldi3r/ts-option
- Owner: sigmasoldi3r
- License: mit
- Created: 2022-01-24T12:12:45.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-06-26T06:51:09.000Z (over 2 years ago)
- Last Synced: 2025-03-18T20:54:24.805Z (8 months ago)
- Topics: functional-programming, iterables, optional, optional-chaining
- Language: TypeScript
- Homepage: https://sigmasoldi3r.github.io/ts-option/
- Size: 60.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Option 

Optional values are meant to describe a value that is
_explicitly_ present or not.
This makes the presence of the value more clear, in contrast
to making the value nullable (Which will not work if the
compiler is not set to check strictly null values), or
undefined.
As a side effect, makes an optional value capable of holding
a null value, because you might encounter situations where
you want a null value to be a valid one.
## Installation
Run `yarn add @octantis/option` or `npm i @octantis/option`
## API
Option is iterable, in case that the value is present yields
the value.
The option value is meant to mimic Scala's `option[A]` type.
```ts
/**
* True if not empty
*/
isDefined(): this is Some;
/**
* Return value, throw exception if empty
*/
get(): A;
/**
* True if empty
*/
isEmpty(): this is None;
/**
* True if not empty
*/
nonEmpty(): this is Some;
/**
* Evaluate and return alternate optional value if empty
*/
orElse(alternative: option): option;
/**
* Evaluate and return alternate value if empty
*/
getOrElse(alternative: A): A;
/**
* Apply function on optional value, return default if empty
*/
fold(fallback: B, folder: (value: A) => B): B;
/**
* Apply a function on the optional value
*/
map(map: (value: A) => B): option;
/**
* Same as map but function must return an optional value
*/
flatMap(map: (value: A) => option): option;
/**
* Apply a procedure on option value
*/
forEach(consumer: (value: A) => void): void;
/**
* Apply partial pattern match on optional value
*/
collect(collector: (value: A) => B | undefined): option;
/**
* An optional value satisfies predicate
*/
filter(filter: (value: A) => boolean): option;
/**
* An optional value doesn't satisfy predicate
*/
filterNot(filter: (value: A) => boolean): option;
/**
* Apply predicate on optional value, or false if empty
*/
exists(predicate: (value: A) => boolean): boolean;
/**
* Apply predicate on optional value, or true if empty
*/
forAll(predicate: (value: A) => boolean): boolean;
/**
* Checks if value equals optional value, or false if empty
*/
contains(element: B): boolean;
/**
* Combine two optional values to make a paired optional value
*/
zip(that: option): option<[A, B]>;
/**
* Unary list of optional value, otherwise the empty list
*/
toList(): A[];
```
## Example usage
Simplest example:
```ts
// Assume a function
function maybe(): option
let n = maybe()
// This acts as a guard, so now n is of type Some
if (n.isDefined()) {
console.log(`n = ${n.value}`)
}
// Or at runtime
console.log(`n = ${n.get()}`)
// This throws if the value wasn't present.
```
With iteration:
```ts
for (const value of maybe()) {
console.log(`value = ${value}`)
}
```