Curried type guard for Option Sometimes needed also due to https://github.com/Microsoft/TypeScript/issues/20218
Vector.of(Option.of(2), Option.none<number>())
.filter(Option.isNone)
=> Vector.of(Option.none<number>())
Curried type guard for Option Sometimes needed also due to https://github.com/Microsoft/TypeScript/issues/20218
Vector.of(Option.of(2), Option.none<number>())
.filter(Option.isSome)
.map(o => o.get())
=> Vector.of(2)
Take a partial function (may return undefined or throw), and lift it to return an Option instead. undefined becomes a None, everything else a Some
const plus = Option.lift((x:number,y:number)=>x+y);
plus(1,2);
=> Option.of(3)
const undef = Option.lift((x:number)=>undefined);
undef(1);
=> Option.none()
const nl = Option.lift((x:number,y:number,z:number)=>null);
nl(1,2,3);
=> Option.some(null)
const throws = Option.lift((x:number,y:number)=>{throw "x"});
throws(1,2);
=> Option.none()
Applicative lifting for Option. Takes a function which operates on basic values, and turns it in a function that operates on options of these values ('lifts' the function). The 2 is because it works on functions taking two parameters.
const lifted = Option.liftA2((x:number,y:number) => x+y);
lifted(Option.of(5), Option.of(6));
=> Option.of(11)
const lifted2 = Option.liftA2((x:number,y:number) => x+y);
lifted2(Option.of(5), Option.none<number>());
=> Option.none()
the first option type
the second option type
the new type as returned by the combining function.
Applicative lifting for Option. 'p' stands for 'properties'.
Takes a function which operates on a simple JS object, and turns it in a function that operates on the same JS object type except which each field wrapped in an Option ('lifts' the function). It's an alternative to OptionStatic.liftA2 when the number of parameters is not two.
const lifted = Option.liftAp((x:{a:number,b:number,c:number}) => x.a+x.b+x.c);
lifted({a:Option.of(5), b:Option.of(6), c:Option.of(3)});
=> Option.of(14)
const lifted = Option.liftAp((x:{a:number,b:number}) => x.a+x.b);
lifted({a:Option.of(5), b:Option.none<number>()});
=> Option.none()
the object property type specifying the parameters for your function
the type returned by your function, returned wrapped in an option by liftAp.
Take a partial function (may return undefined or throw), and lift it to return an Option instead. null and undefined become a None, everything else a Some
const plus = Option.liftNullable((x:number,y:number)=>x+y);
plus(1,2);
=> Option.of(3)
const undef = Option.liftNullable((x:number,y:number,z:string)=>undefined);
undef(1,2,"");
=> Option.none()
const nl = Option.liftNullable((x:number)=>null);
nl(1);
=> Option.none()
const throws = Option.liftNullable((x:number,y:number)=>{throw "x"});
throws(1,2);
=> Option.none()
The optional value expressing a missing value.
Option.of(5).isSome()
=> true
Option.of(undefined).isSome()
=> false
Option.of(null).isSome()
=> true
Also see OptionStatic.some, OptionStatic.ofNullable
Build an optional value from a nullable.
Option.ofNullable(5).isSome()
=> true
Option.ofNullable(undefined).isSome()
=> false
Option.ofNullable(null).isSome()
=> false
Also see OptionStatic.some, OptionStatic.of
Turns a list of options in an option containing a list of items. Useful in many contexts.
Option.sequence(Vector.of(Option.of(1),Option.of(2)))
=> Option.of(Vector.of(1,2))
But if a single element is None, everything is discarded:
Option.sequence(Vector.of(Option.of(1), Option.none()))
=> Option.none()
Also see OptionStatic.traverse
Build a Some, unlike OptionStatic.of, which may build a Some or a None. Will throw if given undefined.
Option.some(5).isSome()
=> true
Option.some(undefined).isSome()
=> throws
Option.some(null).isSome()
=> true
Also see OptionStatic.of, OptionStatic.ofNullable
Takes a list, a function that can transform list elements to options, then return an option containing a list of the transformed elements.
const getUserById: (x:number)=>Option<string> = x => x > 0 ?
Option.of("user" + x.toString()) : Option.none();
Option.traverse([4, 3, 2], getUserById);
=> Option.of(Vector.of("user4", "user3", "user2"))
But if a single element results in None, everything is discarded:
const getUserById: (x:number)=>Option<string> = x => x > 0 ?
Option.of("user" + x.toString()) : Option.none();
Option.traverse([4, -3, 2], getUserById);
=> Option.none()
Also see OptionStatic.sequence
Take a no-parameter partial function (may return null, undefined or throw), and call it, return an Option instead. null and undefined become a None, everything else a Some
Option.tryNullable(Math.random);
=> Option.of(0.49884723907769635)
Option.tryNullable(()=>undefined);
=> Option.none()
Option.tryNullable(()=>null);
=> Option.none()
Option.tryNullable(()=>{throw "x"});
=> Option.none()
Also see OptionStatic.try_, OptionStatic.liftNullable, OptionStatic.lift, EitherStatic.try_.
Take a no-parameter partial function (may return undefined or throw), and call it, return an Option instead. undefined becomes a None, everything else a Some
Option.try_(Math.random);
=> Option.of(0.49884723907769635)
Option.try_(()=>undefined);
=> Option.none()
Option.try_(()=>null);
=> Option.of(null)
Option.try_(()=>{throw "x"});
=> Option.none()
Also see OptionStatic.tryNullable, OptionStatic.lift, OptionStatic.liftNullable, EitherStatic.try_.
Generated using TypeDoc
Holds the "static methods" for Option