Similar to chain (in other languages called bind
or >>=
), but supports more functions passed at once (resembles do
notation in Haskell).
It is used to model a sequence of operations where each operation can fail (can return None).
// does addition when first argument is number
const f1 =
(x: string | number) => (y: number) => opt(x).narrow(isNumber).map(z => z + y);
// passes only even numbers
const f2 = (x: number): Opt<number> => x % 2 === 0 ? opt(x) : none;
opt(0).act( // Some(0)
f1(-2), // Some(-2)
f2, // Some(-2)
optNegative, // None
); // None
opt(0).act( // Some(0)
f1(1), // Some(1)
f2, // None
optNegative, // won't get called, still None
); // None
opt(3).act( // Some(3)
f1(1), // Some(4)
f2, // Some(4)
optNegative, // Some(4)
); // Some(4)
Similar to act, but functions return empty values instead of Opt.
It is useful for typical JavaScript functions (e.g. lodash), properly handles undefined
/null
/NaN
at any point of the chain.
const data = [{}, {f: true, a: [{b: 7, c: 1}]}, {a: [{}]}];
opt(data).actToOpt(
find(x => Boolean(x?.f)), // {f: true, a: [{b: 7, c: 1}]}
x => x?.a, // [{b: 7, c: 1}]
find(x => x.b === 8) // undefined
); // None
Alias of act
Alias of actToOpt.
Similar to map, but supports more functions which are called in succession, each on a result of a previous one.
const sq = (x: number) => x * x;
const dec = (x: number) => x - 1;
opt(4).mapFlow(sq, dec) // Some(15)
opt(null).mapFlow(sq, dec) // None
Applies passed function to this instance and returns function result.
Also known as a reverse function application, |>
(Reason/ReScript, F#, OCaml), &
(Haskell), #
(PureScript) or a pipe operator.
some(1).pipe(x => x.isEmpty) // false
none.pipe(x => x.isEmpty) // true
Supports multiple functions.
opt(1).pipe( // Some(1)
x => x.isEmpty, // false
x => !x, // true
) // true
Applies appropriate function and returns result from the function.
some(1).caseOf(x => x + 1, () => 0) // 2
none.caseOf(x => x + 1, () => 0) // 0
Processing function for Some.
Processing function for None.
some(1).chainToOpt(x => x === 1 ? null : x + 1) // None
some(2).chainToOpt(x => x === 1 ? null : x + 1) // Some(3)
Compares inner value with given value using ===
. Always false
for None.
some(1).contains(1) // true
some(0).contains(1) // false
none.contains(undefined) // false
Is a value of this instance and given other
instance the same?
Default comparator function is ===
(referential equality).
none.equals(none) // true
some(1).equals(none) // false
some(1).equals(some(1)) // true
some(1).equals(some(2)) // false
some({a: 1}).equals(some({a: 1})) // false (different objects)
some(1).equals(some(2), (x, y) => true) // true (custom comparator function)
const jsonCmp = <T>(a: T, b: T): boolean => JSON.stringify(a) === JSON.stringify(b);
some({a: 1}).equals(some({a: 1}), jsonCmp) // true (comparing values converted to JSON)
Applies p
to inner value and passes result. Always false
for None.
some(0).exists(x => x > 0) // false
some(1).exists(x => x > 0) // true
none.exists(x => x > 0) // false
Predicate.
some(1).flatBimap(x => some(x+1), () => some(0)) // Some(2)
none.flatBimap(x => some(x+1), () => some(0)) // Some(0)
some(5).flatBimap(x => none, () => some(0)) // None
Applies p
to inner value and passes result. Always true
for None.
some(0).forAll(x => x > 0) // false
some(1).forAll(x => x > 0) // true
none.forAll(x => x > 0) // true
Predicate.
Get a first item of an array.
Get a last item of an array.
Narrows type inside Opt using given type guard.
some('1' as string | number).narrow(isString) // Some('1'): Opt<string>
some(1 as string | number).narrow(isString) // None: Opt<string>
some(1).optOrCrash('fail') // Some(1)
none.optOrCrash('fail') // throws
Returns value when Some, throws error with msg
otherwise.
Error message.
some(1).orElse(2) // 1
none.orElse(2) // 2
Default value.
Print value to console.
opt(1).print() // logs 'Some:', '1'; returns Some(1)
opt(1).print('test') // logs '[test]', 'Some:', '1'; returns Some(1)
none.print('x') // logs '[x]', 'None'; returns None
Maps property of a wrapped object.
const a = {x: 1};
const xValue = opt(a).prop('x').orCrash('missing prop x'); // 1
some(1).someOrCrash('fail') // Some(1)
none.someOrCrash('fail') // throws
Converts Opt
to an array.
some(1).toArray() // [1]
none.toArray() // []
some(1).toString() // 'Some(1)'
none.toString() // 'None'
Widen union (typically union of strings to string). Experimental. May be removed if it is later found out it's unsafe and unfixable.
opt(someValueOfUnionType).widen<SuperOfThatUnion>() // :Opt<SuperOfThatUnion>
Create Opt instance from an array of one or zero items.
Opt.fromArray([]) // None
Opt.fromArray([1]) // Some(1)
Generated using TypeDoc
Wrapped value type.