Options
All
  • Public
  • Public/Protected
  • All
Menu

Class OptionStatic

Holds the "static methods" for Option

Hierarchy

  • OptionStatic

Index

Methods

isNone

  • isNone<T>(o: Option<T>): boolean

isSome

  • isSome<T>(o: Option<T>): boolean

lift

  • lift<T, U>(fn: function): function
  • 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()
    

    Type parameters

    • T: any[]

    • U

    Parameters

    • fn: function
        • (...args: T): U | undefined
        • Parameters

          • Rest ...args: T

          Returns U | undefined

    Returns function

      • Parameters

        • Rest ...args: T

        Returns Option<U>

liftA2

  • liftA2<T, U, V>(fn: function): function
  • 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()
    

    Type parameters

    • T

      the first option type

    • U

      the second option type

    • V

      the new type as returned by the combining function.

    Parameters

    • fn: function
        • (v1: T, v2: U): V
        • Parameters

          • v1: T
          • v2: U

          Returns V

    Returns function

liftAp

  • liftAp<A, B>(fn: function): 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()
    

    Type parameters

    • A

      the object property type specifying the parameters for your function

    • B

      the type returned by your function, returned wrapped in an option by liftAp.

    Parameters

    • fn: function
        • (x: A): B
        • Parameters

          • x: A

          Returns B

    Returns function

      • Parameters

        • x: object

        Returns Option<B>

liftNullable

  • liftNullable<T, U>(fn: function): function
  • 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()
    

    Type parameters

    • T: any[]

    • U

    Parameters

    • fn: function
        • (...args: T): U | null | undefined
        • Parameters

          • Rest ...args: T

          Returns U | null | undefined

    Returns function

      • Parameters

        • Rest ...args: T

        Returns Option<U>

none

  • The optional value expressing a missing value.

    Type parameters

    • T

    Returns Option<T>

of

  • of<T>(v: T | undefined): Option<T>

ofNullable

  • ofNullable<T>(v: T | undefined | null): Option<T>
  • Build an optional value from a nullable.

    • T is wrapped in a Some
    • undefined becomes a None
    • null becomes a None.
    Option.ofNullable(5).isSome()
    => true
    
    Option.ofNullable(undefined).isSome()
    => false
    
    Option.ofNullable(null).isSome()
    => false
    

    Also see OptionStatic.some, OptionStatic.of

    Type parameters

    • T

    Parameters

    • v: T | undefined | null

    Returns Option<T>

sequence

  • 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

    Type parameters

    • T

    Parameters

    Returns Option<Vector<T>>

some

  • some<T>(v: T): Some<T>

traverse

  • traverse<T, U>(elts: Iterable<T>, fn: function): Option<Vector<U>>
  • 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

    Type parameters

    • T

    • U

    Parameters

    • elts: Iterable<T>
    • fn: function

    Returns Option<Vector<U>>

tryNullable

  • tryNullable<T>(fn: function): Option<T>
  • 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_.

    Type parameters

    • T

    Parameters

    • fn: function
        • (): T | null | undefined
        • Returns T | null | undefined

    Returns Option<T>

try_

  • try_<T>(fn: function): Option<T>

Generated using TypeDoc