Options
All
  • Public
  • Public/Protected
  • All
Menu

This library provides means for representing values that may or may not exist. This can come handy for representing optional arguments, error handling, and records with optional fields.

Index

Type aliases

Variables

Functions

Type aliases

Just

Just: a

Type reprsenting some value. Library treats arbitrary value other than Nothing as Just that value.

Maybe

Maybe: Nothing | Just<a>

Type represent value that may not exist in which case it's Nothing or Just that value.

Nothing

Nothing: void | null | undefined

Type representing absence of value. Library treats all of the typescript primitives for absense of value as Nothing.

Variables

nothing

nothing: Maybe<any> = null

Just provides an alias to null.

Functions

and

  • Returns Nothing if the left Maybe is Nothing, otherwise returns the right Maybe.

    Maybe.and(Maybe.just(2), Maybe.nothing) // => Maybe.nothing
    Maybe.and(Maybe.nothing, Maybe.just('foo')) // => Maybe.nothing
    Maybe.and(Maybe.just(2), Maybe.just('foo')) // => Maybe.just('foo')
    Maybe.and(Maybe.nothing, Maybe.nothing) // => Maybe.nothing
    

    Type parameters

    • a

    • b

    Parameters

    Returns Maybe<b>

chain

  • chain<a, b>(then: function, maybe: Maybe<a>): Maybe<b>
  • Utility to chain together two computations that may fail (return Nothing).

    const makeGreeting = (name:string):string =>
     `Hello ${name}!`
    
    const greet = (name:Maybe<string>):Maybe<string> =>
     Maybe.chain(makeGreeting, name)
    
    greet(Maybe.nothing) // => Maybe.nothing
    greet(Maybe.just('world')) // => Maybe.just('Hello world!')
    

    Type parameters

    • a

    • b

    Parameters

    • then: function

      Function that performs second computation from the a result of the first one (if it was successful - returned Just).

        • Parameters

          • input: a

          Returns Maybe<b>

    • maybe: Maybe<a>

      Maybe value, representing result of first computation.

    Returns Maybe<b>

isJust

  • isJust<a>(maybe: Maybe<a>): boolean
  • Predicate that refines given Maybe. If returns true given Maybe is refined to Just, otherwise refined to Nothing.

    const name:Maybe<string> = Maybe.nothing
    if (isJust(name)) {
      console.log(name.toLowerCase())
    }
    

    Type parameters

    • a

    Parameters

    Returns boolean

isNothing

  • isNothing<a>(maybe: Maybe<a>): boolean
  • Predicate that refines given Maybe. If returns true given Maybe is refined to Nothing, otherwise refined to Just.

    const name:Maybe<string> = Maybe.nothing
    if (!isNothing(name)) {
      console.log(name.toLowerCase())
    }
    

    Type parameters

    • a

    Parameters

    Returns boolean

just

  • just<a>(value: a): Maybe<a>
  • Turns arbitrary value into maybe value.

    Type parameters

    • a

    Parameters

    • value: a

      arbitrary value

    Returns Maybe<a>

map

  • map<a, b>(f: function, maybe: Maybe<a>): Maybe<b>
  • Transform a Maybe value with a given function:

    Maybe.map(Math.sqrt, Maybe.just(9)) // => Maybe.just(3)
    Maybe.map(Math.sqrt, Maybe.nothing) // => Maybe.nothing
    

    Type parameters

    • a

    • b

    Parameters

    • f: function

      Function that maps underlayng value.

        • (input: a): b
        • Parameters

          • input: a

          Returns b

    • maybe: Maybe<a>

      Maybe value to be transformed.

    Returns Maybe<b>

or

  • Returns the left Maybe if it is a Just value, otherwise returns right Maybe.

    Maybe.or(Maybe.just(2), Maybe.nothing) // => Maybe.just(2)
    Maybe.or(Maybe.nothing, Maybe.just('foo')) // => Maybe.just('foo')
    Maybe.or(Maybe.just(2), Maybe.just(100)) // => Maybe.just(2)
    Maybe.or(Maybe.nothing, Maybe.nothing) // => Maybe.nothing
    

    Type parameters

    • a

    Parameters

    Returns Maybe<a>

toValue

  • toValue<a>(fallback: a, maybe: Maybe<a>): a
  • Provide a fallback value, turning an optional value into a normal value.

    Maybe.toValue(5, Maybe.just(9)) // => Maybe.just(9)
    Maybe.toValue(6, Maybe.nothing) // => Maybe.just(6)
    

    Type parameters

    • a

    Parameters

    • fallback: a

      Fallback value

    • maybe: Maybe<a>

      Optional value

    Returns a

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc