Maybe Type Library
This library provides a light Maybe
type for handling optional values, inspired by functional programming languages. A Maybe
can either be Just
(containing a value) or Nothing
(no value).
Installation
Install via npm:
Usage
Creating Maybe
Instances
You can create Maybe
instances using the Just
and Nothing
static methods or helper functions.
Checking for Values
You can check if a Maybe
instance has a value (Just
) or not (Nothing
).
Retrieving Values
To retrieve the value of a Just
instance, use the FromJust
or ToChecked
methods. Be cautious as these methods will throw an error if called on a Nothing
instance.
Default Values
You can provide a default value when dealing with Maybe
instances, ensuring you always have a fallback.
Conditional Value Extraction
You can conditionally extract the value into an output object if the instance is Just
.
Equality Comparisons
You can compare Maybe
instances for equality.
Sure! Here are some examples of how to write functions that return Maybe<T>
instead of T | false
, along with how to handle and check the returned Maybe<T>
values.
Example Function Returning Maybe<T>
Let’s write a function that searches for an item in an array and returns a Maybe<T>
instead of the item or false
.
Handling and Checking Maybe<T>
Values
Here are several ways to handle and check Maybe<T>
values returned from functions.
Using IsJust
and IsNothing
Using FromMaybe
with a Default Value
Using To
for Conditional Extraction
Another Function Example
Let’s create another function that parses an integer from a string and returns Maybe<number>
.
Returning Maybe<T>
from Asynchronous Functions
Here’s an example of how to use Maybe<T>
in an asynchronous function:
These examples demonstrate how to use the Maybe
type to handle cases where values might be absent, making your code more expressive and robust.
API Reference
Maybe
Class
Static Methods
Maybe.Just(value: T): Maybe<T>
- Creates a
Just
instance containing the provided value.
- Creates a
Maybe.Nothing<T>(): Maybe<T>
- Creates a
Nothing
instance with no value.
- Creates a
Instance Methods
IsJust(): boolean
- Returns
true
if the instance isJust
, otherwisefalse
.
- Returns
IsNothing(): boolean
- Returns
true
if the instance isNothing
, otherwisefalse
.
- Returns
FromJust(): T
- Returns the value if the instance is
Just
, otherwise throws an error.
- Returns the value if the instance is
ToChecked(): T
- Alias for
FromJust
.
- Alias for
FromMaybe(defaultValue: T): T
- Returns the value if the instance is
Just
, otherwise returns the provided default value.
- Returns the value if the instance is
To(out: { value?: T }): boolean
- If the instance is
Just
, sets the value in the provided output object and returnstrue
, otherwise returnsfalse
.
- If the instance is
equals(other: Maybe<T>): boolean
- Returns
true
if the otherMaybe
instance is equal, otherwisefalse
.
- Returns
notEquals(other: Maybe<T>): boolean
- Returns
true
if the otherMaybe
instance is not equal, otherwisefalse
.
- Returns
License
This project is licensed under the MIT License. See the LICENSE file for details.
This library simplifies handling optional values in TypeScript, providing a clear and expressive way to manage cases where a value may or may not be present.