Wrapped value type.
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
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.
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
Converts Opt
to an array.
some(1).toArray() // [1]
none.toArray() // []
some(1).toString() // 'Some(1)'
none.toString() // 'None'
Create Opt instance from an array of one or zero items.
Opt.fromArray([]) // None
Opt.fromArray([1]) // Some(1)
Generated using TypeDoc