Maybe
Maybe:
object
Defined in: Core/Maybe.ts:25
Type Declaration
Section titled “Type Declaration”ap: <
A>(arg) => <B>(data) =>Maybe<B>
Applies a function wrapped in a Maybe to a value wrapped in a Maybe.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”Maybe<A>
Returns
Section titled “Returns”<B>(data) => Maybe<B>
Example
Section titled “Example”bind: <
K,A,B>(key,f) => (data) =>Maybe<A&{ [P in string]: B }>
Evaluates a new Maybe using the current accumulator and attaches the output to a new key.
Type Parameters
Section titled “Type Parameters”K extends string
A
B
Parameters
Section titled “Parameters”K
(a) => Maybe<B>
Returns
Section titled “Returns”(data) => Maybe<A & { [P in string]: B }>
Example
Section titled “Example”bindTo
Section titled “bindTo”bindTo: <
K>(key) => <A>(data) =>Maybe<{ [P in string]: A }>
Converts a Maybe value into an object containing a single property. Initiates the pipeline accumulator record.
Type Parameters
Section titled “Type Parameters”K extends string
Parameters
Section titled “Parameters”K
Returns
Section titled “Returns”<A>(data) => Maybe<{ [P in string]: A }>
Example
Section titled “Example”chain: <
A,B>(f) => (data) =>Maybe<B>
Chains Maybe computations. If the first is Some, passes the value to f. If the first is None, propagates None.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”(a) => Maybe<B>
Returns
Section titled “Returns”(data) => Maybe<B>
Example
Section titled “Example”filter
Section titled “filter”filter: <
A>(predicate) => (data) =>Maybe<A>
Filters a Maybe based on a predicate. Returns None if the predicate returns false or if the Maybe is already None.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”predicate
Section titled “predicate”(a) => boolean
Returns
Section titled “Returns”(data) => Maybe<A>
Example
Section titled “Example”fold: <
A,B>(onNone,onSome) => (data) =>B
Extracts the value from a Maybe by providing handlers for both cases.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”onNone
Section titled “onNone”() => B
onSome
Section titled “onSome”(a) => B
Returns
Section titled “Returns”(data) => B
Example
Section titled “Example”from:
object
from.nullable
Section titled “from.nullable”nullable: <
A>(value) =>Maybe<A>
Creates a Maybe from a nullable value. Returns None if the value is null or undefined, Some otherwise.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”A | null | undefined
Returns
Section titled “Returns”Maybe<A>
Example
Section titled “Example”from.Predicate
Section titled “from.Predicate”Predicate: <
A>(pred) => (a) =>Maybe<A>
Creates a Maybe from a predicate applied to a value. Returns Some if the predicate passes, None otherwise.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”(a) => boolean
Returns
Section titled “Returns”(a) => Maybe<A>
Example
Section titled “Example”from.Result
Section titled “from.Result”Result: <
E,A>(data) =>Maybe<A>
Creates a Maybe from a Result. Ok becomes Some, Err becomes None (the error is discarded).
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”Result<E, A>
Returns
Section titled “Returns”Maybe<A>
Example
Section titled “Example”getOrElse
Section titled “getOrElse”getOrElse: <
B>(defaultValue) => <A>(data) =>B|A
Returns the value inside a Maybe, or a default value if None.
The default is a thunk () => B — evaluated only when the Maybe is None.
The default can be a different type, widening the result to A | B.
Type Parameters
Section titled “Type Parameters”B
Parameters
Section titled “Parameters”defaultValue
Section titled “defaultValue”() => B
Returns
Section titled “Returns”<A>(data) => B | A
Example
Section titled “Example”is:
object
is.none
Section titled “is.none”none: <
A>(data) =>data is None=isNone
Type guard that checks if a Maybe is None.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”Maybe<A>
Returns
Section titled “Returns”data is None
Example
Section titled “Example”is.some
Section titled “is.some”some: <
A>(data) =>data is Some<A>=isSome
Type guard that checks if a Maybe is Some.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”Maybe<A>
Returns
Section titled “Returns”data is Some<A>
Example
Section titled “Example”make:
object
make.none
Section titled “make.none”none: () =>
None=makeNone
Creates a None (empty Maybe).
Returns
Section titled “Returns”Example
Section titled “Example”make.some
Section titled “make.some”some: <
A>(value) =>Some<A> =makeSome
Creates a Some containing the given value.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”A
Returns
Section titled “Returns”Some<A>
Example
Section titled “Example”map: <
A,B>(f) => (data) =>Maybe<B>
Transforms the value inside a Maybe if it exists.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”(a) => B
Returns
Section titled “Returns”(data) => Maybe<B>
Example
Section titled “Example”match: <
A,B>(cases) => (data) =>B
Pattern matches on a Maybe, returning the result of the matching case.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”() => B
(a) => B
Returns
Section titled “Returns”(data) => B
Example
Section titled “Example”recover
Section titled “recover”recover: <
B>(fallback) => <A>(data) =>Maybe<B|A>
Recovers from a None by providing a fallback Maybe.
The fallback can produce a different type, widening the result to Maybe<A | B>.
Type Parameters
Section titled “Type Parameters”B
Parameters
Section titled “Parameters”fallback
Section titled “fallback”() => Maybe<B>
Returns
Section titled “Returns”<A>(data) => Maybe<B | A>
Example
Section titled “Example”struct
Section titled “struct”struct: <
R>(fields) =>Maybe<R>
Combines a record of Maybes into a single Maybe of a record. Evaluates fields in key order and short-circuits on the first None.
Type Parameters
Section titled “Type Parameters”R extends Record<string, any>
Parameters
Section titled “Parameters”fields
Section titled “fields”{ [K in string | number | symbol]: Maybe<R[K]> }
Returns
Section titled “Returns”Maybe<R>
Example
Section titled “Example”tap: <
A>(f) => (data) =>Maybe<A>
Executes a side effect on the value without changing the Maybe. Useful for logging or debugging.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”(a) => void
Returns
Section titled “Returns”(data) => Maybe<A>
Example
Section titled “Example”to:
object
to.nullable
Section titled “to.nullable”nullable: <
A>(data) =>A|null
Extracts the value from a Maybe, returning null if None.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”Maybe<A>
Returns
Section titled “Returns”A | null
Example
Section titled “Example”to.Result
Section titled “to.Result”Result: <
E>(onNone) => <A>(data) =>Result<E,A>
Converts a Maybe to a Result. Some becomes Ok, None becomes Err with the provided error.
Type Parameters
Section titled “Type Parameters”E
Parameters
Section titled “Parameters”onNone
Section titled “onNone”() => E
Returns
Section titled “Returns”<A>(data) => Result<E, A>
Example
Section titled “Example”to.undefined
Section titled “to.undefined”undefined: <
A>(data) =>A|undefined
Extracts the value from a Maybe, returning undefined if None.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”Maybe<A>
Returns
Section titled “Returns”A | undefined
Example
Section titled “Example”transposeResult
Section titled “transposeResult”Swaps the outer Maybe and inner Result context.
Some(Ok(a)) becomes Ok(Some(a)), Some(Err(e)) becomes Err(e), and None becomes Ok(None).
Type Parameters
Section titled “Type Parameters”E
A