Options
All
  • Public
  • Public/Protected
  • All
Menu

ts-opt

Index

Type aliases

EqualityFunction

EqualityFunction: <T>(a: T, b: T) => boolean

Type declaration

    • <T>(a: T, b: T): boolean
    • Type parameters

      • T

      Parameters

      • a: T
      • b: T

      Returns boolean

Variables

Const none

none: None<any> = ...

Single global instance of None.

Functions

Const ap

  • ap<A, B>(of: Opt<(_: A) => B>): (oa: Opt<A>) => Opt<B>
  • <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 (oa: Opt<A>) => Opt<B>

Const apFn

  • apFn<A, B>(f: (_: A) => B): (oa: Opt<A>) => Opt<B>
  • <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 (oa: Opt<A>) => Opt<B>

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 isOpt

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

    Parameters

    • x: unknown

    Returns x is Opt<unknown>

Const joinOpt

  • Unwraps one level of nested Opts. Similar to flatten in other libraries or languages.

    joinOpt(some(none)) // None
    joinOpt(some(some(1))) // Some(1)
    

    Type parameters

    • T

    Parameters

    Returns Opt<T>

Const mapOpt

  • mapOpt<A, B>(f: (_: A) => Opt<B>): (xs: A[]) => B[]
  • 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 (xs: A[]) => B[]

      • (xs: A[]): B[]
      • Parameters

        • xs: A[]

        Returns B[]

Const opt

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

    Type parameters

    • T

    Parameters

    • x: undefined | null | T

    Returns Opt<T>

Const optEmptyArray

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

    Type parameters

    • T

    Parameters

    • x: undefined | null | T[]

    Returns Opt<T[]>

Const optEmptyObject

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

    Type parameters

    • T: object

    Parameters

    • x: undefined | null | T

    Returns Opt<T>

Const optEmptyString

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

    Type parameters

    • T

    Parameters

    • x: undefined | null | "" | T

    Returns Opt<T>

Const optFalsy

  • optFalsy<T>(x: undefined | null | false | "" | 0 | T): 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: undefined | null | false | "" | 0 | T

    Returns Opt<T>

Const optNegative

  • optNegative(x: undefined | null | number): Opt<number>
  • For numbers lesser than 0 returns None, otherwise acts same as opt. Useful for strange functions which return -1 or other negative numbers on failure.

    optNegative(undefined) // None
    optNegative(1) // Some(1)
    optNegative(0) // Some(0)
    optNegative(-1) // None
    

    Parameters

    • x: undefined | null | number

    Returns Opt<number>

Const optZero

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

    Type parameters

    • T

    Parameters

    • x: undefined | null | 0 | T

    Returns Opt<T>

Const some

  • some<T>(x: T): Readonly<Some<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 Readonly<Some<T>>

Generated using TypeDoc