Maybe<T>
Maybe is used to represent a value that may or may not exist. It can either be a Just<T> or a Nothing.
1 2 3 4 5 6 7 | |
Methods
map(fn: (value: T) => U): Maybe<U>
Maps over the Just value. Does nothing for Nothing.
1 2 3 4 5 6 7 | |
chain(fn: (value: T) => Maybe<U>): Maybe<U>
Chains the transformation if the value is Just. Returns Nothing otherwise.
1 2 3 4 5 6 7 | |
unwrapOr(defaultValue: T): T
Returns the value of Just, or the default value for Nothing.
1 2 3 4 5 6 7 | |
isJust(): boolean
Checks if the value is Just.
1 2 3 4 5 6 7 | |
isNothing(): boolean
Checks if the value is Nothing.
1 2 3 4 5 6 7 | |
match<U>(cases: { just: (value: T) => U; nothing: () => U }): U
Matches the value to execute either the just or nothing case.
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
equals(other: Maybe<T>): boolean
Compares the values inside this and the other, returns true if both are Nothing or if the values are equal.
1 2 3 4 5 6 7 8 9 10 | |
Helpers
just(value: T): Maybe<T>
Creates a Just value with the given value, representing the presence of a value.
1 2 3 4 | |
nothing<T = never>(): Maybe<T>
Creates a Nothing value, representing the absence of a value.
1 2 3 4 | |
fromNullable(value)
Creates a Maybe from a value that might be null or undefined.
1 | |
- Returns
Just<T>if the value is notnullorundefined - Returns
Nothingotherwise
Curried Helpers
map
Curried version of map for Maybe. This allows functional composition with pipe.
1 2 3 4 5 6 7 8 9 | |
chain
Curried version of chain for Maybe. This allows chaining transformations in a functional pipeline.
1 2 3 4 5 6 7 8 9 | |
unwrapOr
Curried version of unwrapOr for Maybe. This provides a cleaner way to unwrap the value in a Maybe.
1 2 3 4 5 6 7 8 | |
match
Curried version of match for Maybe. This allows handling Just and Nothing in a functional way.
1 2 3 4 5 6 7 8 9 10 11 | |
equals
Curried version of equals for Maybe. Compares the values inside this and the other, returns true if both are Nothing or if the values are equal.
1 2 3 4 5 6 7 8 | |