Options
All
  • Public
  • Public/Protected
  • All
Menu

Module either

Index

Type aliases

CaseOf

CaseOf<L, R, U>: { Left: (value: L) => U; Right: (value: R) => U }

Mather type for caseOf

Type parameters

  • L

  • R

  • U

Type declaration

  • Left: (value: L) => U
      • (value: L): U
      • Parameters

        • value: L

        Returns U

  • Right: (value: R) => U
      • (value: R): U
      • Parameters

        • value: R

        Returns U

Shape

Shape<L, R>: Either<L, R>

This is alias for normal display from context (Either.Either => Either.Shape)

Type parameters

  • L

  • R

Functions

asyncAttempt

  • asyncAttempt<R>(f: (...args: any[]) => Promise<R>, args: any[]): Promise<Either<Error, R>>
  • asyncAttempt<R>(f: (...args: any[]) => Promise<R>): (args: any[]) => Promise<Either<Error, R>>
  • Function like Either.attempt This is asynctry {} catch(error) {} for Either. If the function throws exception then the error will catch and placed in Left, and in Right otherwise.

    import { Either } from 'monad-maniac'
    
    const errorAsyncFunction = (x: number): Promise<number> => new Promise((resolve, reject) => {
      if (x === 0) {
        reject(new Error('Number is zero!'))
      }
      resolve(x)
    })
    
    const left = await Either.asyncAttempt(errorAsyncFunction, [0])
    const right = await Either.asyncAttempt(errorAsyncFunction, [10])
    left.map(double).toString() // Left(Error: Number is zero!)
    right.map(double).toString() // Right(20)
    
    

    Type parameters

    • R

    Parameters

    • f: (...args: any[]) => Promise<R>
        • (...args: any[]): Promise<R>
        • Parameters

          • Rest ...args: any[]

          Returns Promise<R>

    • args: any[]

    Returns Promise<Either<Error, R>>

  • Type parameters

    • R

    Parameters

    • f: (...args: any[]) => Promise<R>
        • (...args: any[]): Promise<R>
        • Parameters

          • Rest ...args: any[]

          Returns Promise<R>

    Returns (args: any[]) => Promise<Either<Error, R>>

      • (args: any[]): Promise<Either<Error, R>>
      • Parameters

        • args: any[]

        Returns Promise<Either<Error, R>>

attempt

  • attempt<R>(f: (...args: any[]) => R, args: any[]): Either<Error, R>
  • attempt<R>(f: (...args: any[]) => R): (args: any[]) => Either<Error, R>
  • This is try {} catch(error) {} for Either. If the function throws exception then the error will catch and placed in Left, and in Right otherwise.

    import { Either } from 'monad-maniac'
    
    const errorFunction = (x: number): number => {
      if (x === 0) {
        throw new Error('Number is zero!')
      }
      return x
    }
    
    Either.attempt(errorFunction, [0]).map(double).toString() // Left(Error: Number is zero!)
    Either.attempt(errorFunction, [10]).map(double).toString() // Right(20)
    
    

    Type parameters

    • R

    Parameters

    • f: (...args: any[]) => R
        • (...args: any[]): R
        • Parameters

          • Rest ...args: any[]

          Returns R

    • args: any[]

    Returns Either<Error, R>

  • Type parameters

    • R

    Parameters

    • f: (...args: any[]) => R
        • (...args: any[]): R
        • Parameters

          • Rest ...args: any[]

          Returns R

    Returns (args: any[]) => Either<Error, R>

      • (args: any[]): Either<Error, R>
      • Parameters

        • args: any[]

        Returns Either<Error, R>

caseOf

  • caseOf<L, R, U>(matcher: CaseOf<L, R, U>, either: Either<L, R>): U
  • caseOf<L, R, U>(matcher: CaseOf<L, R, U>): (either: Either<L, R>) => U
  • Method like Either.caseOf

    import { Either } from 'monad-maniac'
    
    const matcher: Either.CaseOf<string, number, number> = {
      Right: (x) => x * 2,
      Left: (message) => message.length,
    }
    
    const left: Either.Shape<string, number> = new Either.Left('Server error')
    const right: Either.Shape<string, number> = new Either.Right(150)
    
    Either.caseOf(matcher, left) // 12
    Either.caseOf(matcher, right) // 300
    

    Type parameters

    • L

    • R

    • U

    Parameters

    Returns U

  • Just curried caseOf.

    Type parameters

    • L

    • R

    • U

    Parameters

    Returns (either: Either<L, R>) => U

      • Parameters

        Returns U

chain

  • Method like Either.chain

    import { Either } from 'monad-maniac'
    
    const name: string | void = Math.random() > 0.5 ? 'Jake' : undefined
    const result: Either.Shape<string, string> = typeof name === 'string'
     ? new Either.Right(name)
     : new Either.Left('Server error')
    const greeting = Either.chain(
       (name: string) => Either.right(`Welcome, ${name}!`)
    ), result).get() // 'Welcome, Jake' or 'Server error'
    

    Type parameters

    • L

    • R

    • U

    Parameters

    Returns Either<L, U>

  • Just curried chain.

    Type parameters

    • L

    • R

    • U

    Parameters

    • f: (value: R) => Either<L, U>
        • Parameters

          • value: R

          Returns Either<L, U>

    Returns (either: Either<L, R>) => Either<L, U>

filter

  • filter<L, R>(predicate: (value: R) => boolean, either: Either<L, R>): Either<L | R, R>
  • filter<L, R>(predicate: (value: R) => boolean): (either: Either<L, R>) => Either<L | R, R>
  • Method like Either.filter

    Apple some function to Right value and if the function returns not true then value will be placed to Left.

    import { Either } from 'monad-maniac'
    const right: Either.Shape<number, number> = new Either.Right(150)
    
    Either.filter((x) => x > 150, right) // Left(150)
    Either.filter((x) => x < 300, right) // Right(150)
    

    Type parameters

    • L

    • R

    Parameters

    • predicate: (value: R) => boolean
        • (value: R): boolean
        • Parameters

          • value: R

          Returns boolean

    • either: Either<L, R>

    Returns Either<L | R, R>

  • Just curried filter.

    Type parameters

    • L

    • R

    Parameters

    • predicate: (value: R) => boolean
        • (value: R): boolean
        • Parameters

          • value: R

          Returns boolean

    Returns (either: Either<L, R>) => Either<L | R, R>

fromNullable

  • fromNullable<L, R>(value: Nullable<R | L>): Either<Nullable<L>, R>
  • Returns value from Left or Right

    import { Either } from 'monad-maniac'
    
    Either.fromNullable<string, number>(150).toString() // Right(150)
    Either.fromNullable<string, number>(null).toString() // Left(null)
    

    Type parameters

    • L

    • R

    Parameters

    • value: Nullable<R | L>

    Returns Either<Nullable<L>, R>

get

  • get<L, R>(either: Either<L, R>): L | R
  • Returns value from Left or Right

    import { Either } from 'monad-maniac'
    
    const right = Either.right<string, number>(10)
    const left = Either.left<string, number>('Some error')
    
    Either.get(left) // 'Some error'
    Either.get(right) // 10
    

    Type parameters

    • L

    • R

    Parameters

    Returns L | R

getOrElse

  • getOrElse<L, R, U>(defaultValue: U, either: Either<L, R>): R | U
  • getOrElse<L, R, U>(defaultValue: U): (either: Either<L, R>) => R | U
  • Method like Either.getOrElse

    import { Either } from 'monad-maniac'
    
    const left = Either.left<number, number>(150)
    const right = Either.right<number, number>(150)
    
    const resultLeft = Either.getOrElse(0, left) // 0
    const resultRight = Either.getOrElse(0, right) // 150
    

    Type parameters

    • L

    • R

    • U

    Parameters

    • defaultValue: U
    • either: Either<L, R>

    Returns R | U

  • Just curried getOrElse.

    Type parameters

    • L

    • R

    • U

    Parameters

    • defaultValue: U

    Returns (either: Either<L, R>) => R | U

      • (either: Either<L, R>): R | U
      • Parameters

        Returns R | U

isLeft

  • isLeft<L, R>(either: Either<L, R>): boolean
  • Returns true if thisLeft or false otherwise.

    import { Either } from 'monad-maniac'
    
    const right = Either.right<string, number>(10)
    const left = Either.left<string, number>('Some error')
    
    Either.isLeft(left) // true
    Either.isLeft(right) // false
    

    Type parameters

    • L

    • R

    Parameters

    Returns boolean

isRight

  • isRight<L, R>(either: Either<L, R>): boolean
  • Returns true if thisRight or false otherwise.

    import { Either } from 'monad-maniac'
    
    const right = Either.right<string, number>(10)
    const left = Either.left<string, number>('Some error')
    
    Either.isRight(left) // false
    Either.isRight(right) // true
    

    Type parameters

    • L

    • R

    Parameters

    Returns boolean

left

  • left<L, R>(value: L): Either<L, R>
  • Making Right from value

    import { Either } from 'monad-maniac'
    
    const left = Either.left<string, number>('Some error')
    

    Type parameters

    • L

    • R

    Parameters

    • value: L

    Returns Either<L, R>

map

  • map<L, R, U>(f: (value: R) => U, either: Either<L, R>): Either<L, U>
  • map<L, R, U>(f: (value: R) => U): (either: Either<L, R>) => Either<L, U>
  • Method like Either.map

    import { Either } from 'monad-maniac'
    
    const name: string | void = Math.random() > 0.5 ? 'Jake' : undefined
    const result: Either.Shape<string, string> = typeof name === 'string'
     ? new Either.Right(name)
     : new Either.Left('Server error')
    const greeting = Either.map((name) => `Welcome, ${name}!`, result)
    
    // ...
    
    if (greeting.isLeft()) {
     console.error(greeting.get())
    } else {
     showGreeting(greeting.get())
    }
    
    

    Type parameters

    • L

    • R

    • U

    Parameters

    • f: (value: R) => U
        • (value: R): U
        • Parameters

          • value: R

          Returns U

    • either: Either<L, R>

    Returns Either<L, U>

  • Just curried map.

    Type parameters

    • L

    • R

    • U

    Parameters

    • f: (value: R) => U
        • (value: R): U
        • Parameters

          • value: R

          Returns U

    Returns (either: Either<L, R>) => Either<L, U>

of

  • of<L, R>(value: R): Either<L, R>

orElse

  • orElse<L, R, U>(f: (value: L) => U, either: Either<L, R>): R | U
  • orElse<L, R, U>(f: (value: L) => U): (either: Either<L, R>) => R | U
  • Method like Either.orElse

    Apple some function to Left value.

    import { Either } from 'monad-maniac'
    const left: Either.Shape<string, number> = new Either.Left('Server error')
    const right: Either.Shape<string, number> = new Either.Right(150)
    
    const orElse = (message: string) => ({ message })
    
    Either.orElse(orElse, left) // { message: 'Server error' }
    Either.orElse(orElse, right) // 150
    

    Type parameters

    • L

    • R

    • U

    Parameters

    • f: (value: L) => U
        • (value: L): U
        • Parameters

          • value: L

          Returns U

    • either: Either<L, R>

    Returns R | U

  • Just curried orElse.

    Type parameters

    • L

    • R

    • U

    Parameters

    • f: (value: L) => U
        • (value: L): U
        • Parameters

          • value: L

          Returns U

    Returns (either: Either<L, R>) => R | U

      • (either: Either<L, R>): R | U
      • Parameters

        Returns R | U

right

  • right<L, R>(value: R): Either<L, R>
  • Making Right from value

    import { Either } from 'monad-maniac'
    
    const right = Either.right<string, number>(10)
    

    Type parameters

    • L

    • R

    Parameters

    • value: R

    Returns Either<L, R>

toMaybe

  • Method like Either.toMaybe

    import { Either } from 'monad-maniac'
    
    const left = Either.left<string, number>('error')
    const right = Either.right<string, number>(144)
    
    const nothing = Either.toMaybe(left)
    const just = Either.toMaybe(right)
    
    

    Type parameters

    • L

    • R

    Parameters

    Returns Shape<R>

toString

  • toString<L, R>(either: Either<L, R>): string
  • Returns Left or Right as string.

    import { Either } from 'monad-maniac'
    
    const right = Either.right<string, number>(10)
    const left = Either.left<string, number>('Some error')
    
    Either.toString(left) // Left(Some error)
    Either.toString(right) // Right(10)
    

    Type parameters

    • L

    • R

    Parameters

    Returns string

Generated using TypeDoc