This is Perhaps
Perhaps is a lightweight Maybe
type implementation for handling optional values in TypeScript. I designed the Maybe
type to manage situations where values may be present or absent more gracefully and robustly, based on Haskall’s Maybe
type monad.
Installation
Install Perhaps via npm:
Usage
Creating Maybe
Instances
Creating Maybe
instances is straightforward. You can use the static methods Just
and Nothing
or their corresponding helper functions.
Checking for Values
To determine if a Maybe
instance has a value (Just
) or is empty (Nothing
), use the IsJust
and IsNothing
methods.
Retrieving Values
To safely retrieve the value from 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
Provide a default value when dealing with Maybe
instances to ensure you always have a fallback.
Conditional Value Extraction
Extract the value into an output object conditionally if the instance is Just
.
Alternatively, use the To
method with a custom key.
Equality Comparisons
Compare Maybe
instances for equality with the equals
and notEquals
methods.
Practical Function Examples
Here are some examples of how to write functions that return Maybe<T>
instead of T | false
, and how to handle these returned values.
Example Function Returning Maybe<T>
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
Use IsJust
and IsNothing
to handle Maybe<T>
values.
Use FromMaybe
with a default value.
Use To
for conditional extraction.
Another Function Example
A function that parses an integer from a string and returns Maybe<number>
.
Returning Maybe<T>
from Asynchronous Functions
Using Maybe<T>
in an asynchronous function.
API Reference
Maybe
Class
Static Methods
Maybe.Just(value: T): Maybe<T>
: Creates aJust
instance containing the provided value.Maybe.Nothing<T>(): Maybe<T>
: Creates aNothing
instance with no value.
Instance Methods
IsJust(): boolean
: Returnstrue
if the instance isJust
, otherwisefalse
.IsNothing(): boolean
: Returnstrue
if the instance isNothing
, otherwisefalse
.FromJust(): T
: Returns the value if the instance isJust
, otherwise throws an error.ToChecked(): T
: Alias forFromJust
.FromMaybe(defaultValue: T): T
: Returns the value if the instance isJust
, otherwise returns the provided default value.To(out: { value?: T }): boolean
: If the instance isJust
, sets the value in the provided output object and returnstrue
, otherwise returnsfalse
.equals(other: Maybe<T>): boolean
: Returnstrue
if the otherMaybe
instance is equal, otherwisefalse
.notEquals(other: Maybe<T>): boolean
: Returnstrue
if the otherMaybe
instance is not equal, otherwisefalse
.
I built Perhaps to simplify handling optional values in TypeScript, providing a clear and expressive way to manage cases where a value may or may not be present. It is built to help you write more robust and expressive code.
Cheers.