Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "Opt"

Index

Type aliases

OptSerialized

OptSerialized: { type: typeof noneSerializedType } | { type: typeof someSerializedType; value: any }

Variables

Const none

none: None<any> = Object.freeze(new None())

Single global instance of None.

Const noneSerializedType

noneSerializedType: "Opt/None" = "Opt/None"

Const noneSymbol

noneSymbol: unique symbol = Symbol('None')

Const someSerializedType

someSerializedType: "Opt/Some" = "Opt/Some"

Const someSymbol

someSymbol: unique symbol = Symbol('Some')

Functions

Const ap

  • ap<A, B>(of: Opt<(_: A) => B>): (Anonymous function)
  • <A, B>(of: Opt<(_: A) => B>) => (oa: Opt<A>): Opt<B>

    Apply oa to function of. If any argument is None then result is None.

    ap(opt(x => x > 0))(opt(1)) // Opt(true)
    ap(opt(x => x > 0))(none) // None
    ap(none)(opt(1)) // None
    ap(none)(none) // None

    Type parameters

    • A

      input of function inside of

    • B

      output of function inside of

    Parameters

    • of: Opt<(_: A) => B>

    Returns (Anonymous function)

Const apFn

  • apFn<A, B>(f: (_: A) => B): (Anonymous function)
  • <A, B>(f: (_: A) => B) => (oa: Opt<A>): Opt<B>

    Apply oa to function f. If argument is None then result is None.

    apFn(x => x > 0)(opt(1)) // Opt(true)
    apFn(x => x > 0)(none) // None

    Type parameters

    • A

      input of function f

    • B

      output of function f

    Parameters

    • f: (_: A) => B
        • (_: A): B
        • Parameters

          • _: A

          Returns B

    Returns (Anonymous function)

Const catOpts

  • catOpts<A>(xs: Opt<A>[]): A[]
  • Transforms array of opts into an array where Nones are omitted and Somes are unwrapped.

    catOpts([opt(1), opt(null)]) // [1]

    Type parameters

    • A

    Parameters

    Returns A[]

Const isNoneValue

  • isNoneValue(x: any): boolean
  • Parameters

    • x: any

    Returns boolean

Const isOpt

  • isOpt(x: unknown): x is Opt<unknown>
  • Is given value an instance of Opt?

    Parameters

    • x: unknown

    Returns x is Opt<unknown>

Const mapOpt

  • mapOpt<A, B>(f: (_: A) => Opt<B>): (Anonymous function)
  • Similar to Array.map, but also allows omitting elements.

    mapOpt((x: number) => x > 0 ? opt(x) : none)([-1, 0, 1]) // [1]

    Type parameters

    • A

    • B

    Parameters

    • f: (_: A) => Opt<B>
        • (_: A): Opt<B>
        • Parameters

          • _: A

          Returns Opt<B>

    Returns (Anonymous function)

Const opt

  • opt<T>(x: T | undefined | null): Opt<T>
  • Main constructor function - for undefined, null and NaN returns None. Anything else is wrapped into Some.

    Type parameters

    • T

    Parameters

    • x: T | undefined | null

    Returns Opt<T>

Const optEmptyArray

  • optEmptyArray<T>(x: T[] | undefined | null): Opt<T[]>
  • For empty array ([]) returns None, otherwise acts same as opt.

    Type parameters

    • T

    Parameters

    • x: T[] | undefined | null

    Returns Opt<T[]>

Const optEmptyObject

  • optEmptyObject<T>(x: T | undefined | null): Opt<T>
  • For empty object ({}) returns None, otherwise acts same as opt.

    Type parameters

    • T: object

    Parameters

    • x: T | undefined | null

    Returns Opt<T>

Const optEmptyString

  • optEmptyString<T>(x: T | undefined | null | ""): Opt<T>
  • For empty string ('') returns None, otherwise acts same as opt.

    Type parameters

    • T

    Parameters

    • x: T | undefined | null | ""

    Returns Opt<T>

Const optFalsy

  • optFalsy<T>(x: T | undefined | null | "" | false | 0): Opt<T>
  • For falsy values returns None, otherwise acts same as opt.

    optFalsy(''); // None
    optFalsy(0); // None
    optFalsy(false); // None
    optFalsy(NaN); // None

    Type parameters

    • T

    Parameters

    • x: T | undefined | null | "" | false | 0

    Returns Opt<T>

Const optZero

  • optZero<T>(x: T | undefined | null | 0): Opt<T>
  • For a number 0 returns None, otherwise acts same as opt.

    Type parameters

    • T

    Parameters

    • x: T | undefined | null | 0

    Returns Opt<T>

Const some

  • some<T>(x: T): {}
  • Constructs Some. Usually it is opt you are looking for (only in rare cases you want to have for example Some(undefined)).

    Type parameters

    • T

    Parameters

    • x: T

    Returns {}

Generated using TypeDoc