Yet another port of the Scala Option type to TypeScript.
npm i --save option-monad-ts
import { Option, Some, None } from 'option-monad-ts';
// Create Some
let n = new Some(3);
let m = Some.of('four');
// Create Option
let c = Option.of(3);
let d = Option.of(null);
// Validate and use
n.get(); // 3
m.get(); // 'four'
c.isDefined(); // true
d.isDefined(); // false
Some.of(4)
.map(_ => _.toString())
.forEach(_ => console.log(_))
An Option factory which creates Some.of(x) if the argument is not null, and None if it is null.
Returns this Option if it is nonempty and applying the predicate p to this Option's value returns true. Otherwise, return None.
Returns the result of applying f to this Option's value if this Option is nonempty. Returns None if this Option is empty. Slightly different from map in that f is expected to return an Option (which could be None).
Apply the given procedure f to the option's value, if it is nonempty. Otherwise, do nothing.
Returns the option's value. The option must be nonEmpty.
Returns the option's value if the option is nonempty, otherwise return defaultValue.
Returns true if the option is an instance of Some, false otherwise.
Returns true if the option is None, false otherwise.
Returns a Some containing the result of applying f to this Option's value if this Option is nonempty. Otherwise, return None.
- I thought it would be a good exercise
- I didn't like the API/usage patterns of some existing libraries
- I wanted something as close to the Scala lib as possible, so I ported it trying to keep to its spirit