tame-optional
v1.2.1
Published
The Tame optional library enabling the usage of optionals
Downloads
5
Maintainers
Readme
Optional - tame-optional
Optional is a maybe (option) type implemented in TypeScript for usage in TypeScript and JavaScript.
The implementation of optional is used and developed by Tame.
There is overall 3 reasons for using the Optional library:
- The Optional type allows you to avoid errors related to
undefined
andnull
values in your code. - It enforces you to think about what should happen if a given value is
undefined
ornull
in your program flow either avoiding run-time errors or ensuring you control the throwing of errors. - You can drastically reduce the amount of
undefined
andnull
checks required.
Installation
This module is available through the npm registry.
Yarn
yarn add tame-optional
npm
npm install --save tame-optional
Features
- Robust and tested
- Self-contained - no external dependencies required
- Written in TypeScript
Usage
Creating an optional
You can either use the optional constructor:
const valueOrNone = new Optional(1)
Or the Some
helper function:
const valueOrNone = Some(1);
Creating a 'none' Optional
If you are in need of a none value you either use the optional constructor:
const valueOrNone = new Optional(undefined);
const valueOrNone = new Optional(null);
Or use the None
function:
const valueOrNone = None();
Functions
match
- Calls the some function if the optional contains a value, otherwise calls the none function. This method allows you to convert the optional to any other type.
matchAsync
- Async variant of the match function above.
equals
- Compares two optionals
contains
- Checks if the given optional contains the specified variable
map
- Allows the optional to change its underlying value, for instance to a different type.
mapAsync
- Async variant of the
map
function.
- Async variant of the
flatMap
- Allows the optional to change from one optional type to the other.
flatMapAsync
- Async variant of
flatMap
.
- Async variant of
filter
- Filters the optional by checking if the optional satifies the given predicate. Returns a None option if the value does not satisfy the given predicate.
filterAsync
- Async varaitn of filter.
valueOr
- Returns the stored value or the specified alternative. The alternative can be an alternate value of the same type, a function returning a value of the same type, an error or a function throwing an error.
valueOrAsync
- Async variant of
valueOr
.
- Async variant of
valueOrFailure
- Returns the stored value or throws an error with an optionally specified error message. Is usually used together with
hasValue
value on the Optional type. `
- Returns the stored value or throws an error with an optionally specified error message. Is usually used together with
valueOrUndefined
- Returns the stored value or undefined
valueOrNull
- Returns the stored value or null
toJSON
- Return the stored value or null if no value exists. Used with JSON serialization.
Helper functions
Some
- Wraps a given value in an optional;
None
- Generates a None optional type
GetValues
- Gets the values out of a list of optionals
ConvertAndGetValues
- First converts a given value from one type, B, to another type, A, and then returns a list of type A.
ConvertAndGetValuesAsync
- Does the same as
ConvertAndGetValues
but takes an async mapper function instead.
- Does the same as
Why have async functions separate from 'regular' functions
The reason for this is we believe it makes the types from Typescript easier to reason about and deal with, as instead of having code like the following:
const valueOrNone = Some(1);
const value = await (<<Promise<number>> valueOrNone.valueOr(async () => 2));
We can instead have code like this:
const valueOrNone = Some(1);
const value = await valueOrNone.valueOrAsync(async () => 2);
Contributions
Contributions are welcome. If you find any issues or have a feature request, please open an issue.
Do not expect 'out of the blue' pull-requests to be accepted if they do not solve a reported issue.
Please ensure that your pull-requests are well-tested and pass all existing tests.