Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Maybe<T>

Type parameters

  • T

Hierarchy

Implemented by

Index

Methods

apply

  • apply<U>(maybe: Maybe<U>): ApplicativeResult<T, U>
  • Type parameters

    • U: (value: T) => any

    Parameters

    • maybe: Maybe<U>

      Getting Maybe(fn) and apply function from Maybe to value from current Maybe

      import { Maybe } from 'monad-maniac'
      const just = Maybe.of(5)
      const maybeAdd = Maybe.of((x) => x + 1)
      const maybeAddNull = Maybe.of(null)
      
      const resultAdd = just.apply(maybeAdd).toString() // Just(6)
      const resultAddToNothing = just.filter((x) => x > 10000).apply(maybeAdd).toString() // Nothing()
      const resultAddNull= just.apply(maybeAddNull).toString() // Nothing()
      

    Returns ApplicativeResult<T, U>

caseOf

  • caseOf<U>(matcher: CaseOf<T, U>): U
  • If need unwrap Maybe and call some function and save type without null and undefined then use caseOf.

    import { Maybe } from 'monad-maniac'
    
    const brokenDataMaybe = Maybe.of<number>(null)
    const normalDataMaybe = Maybe.of(10)
    
    const matcher: Maybe.CaseOf<number> = {
     Just: (x) => x * x,
     Nothing: () => -1,
    }
    
    const unwrappedNormal = normalDataMaybe.caseOf(matcher) // 100
    const unwrappedBroken = normalDataMaybe.caseOf(matcher) // -1
    
    // More example:
    
    brokenDataMaybe.caseOf({
     Just: (x) => Maybe.of(x * x),
     Nothing: () => Maybe.of(-1),
    }).map((x) => x ^ 2) // Just(1)
    
    unwrappedNormal.caseOf({
     Just: (x) => Maybe.of(x * x),
     Nothing: () => Maybe.of(-1),
    }).map((x) => x ^ 2) // Nothing
    

    Type parameters

    • U

    Parameters

    • matcher: CaseOf<T, U>

      This is object with two fields Just and Nothing what contains functions.

    Returns U

cata

  • cata<U>(justFn: (value: NonNullable<T>) => U, nothingFn: () => U): U
  • Works like caseOf, but use two functions as arguments for unwrap instead of object allocation.

    Type parameters

    • U

    Parameters

    • justFn: (value: NonNullable<T>) => U

      Function will called with Just

        • (value: NonNullable<T>): U
        • Parameters

          • value: NonNullable<T>

          Returns U

    • nothingFn: () => U

      Function will called with Nothing

        • (): U
        • Returns U

    Returns U

chain

  • Working like Maybe.map but returns not Maybe, but the function result. If chain will call on Nothing then return undefined.

    import { Maybe } from 'monad-maniac'
    
    const result = Maybe
     .of(100)
     .map((x) => x / 2) // 50
     .map((x) => x + 10) // 60
     .chain((x) => Maybe.of(x / 3)) // 20
    console.log(result) // Just(20)
    
    const resultBad = Maybe
     .of(100)
     .map((x) => x / 2) // 50
     .filter((x) => x > 1000) // Nothing() - next function will be called never
     .map((x) => x + 10) // 60
     .chain((x) => Maybe.of(x / 3)) // Nothing()
    console.log(result) // Nothing()
    

    Type parameters

    • U

    Parameters

    • f: (value: T) => Maybe<U>
        • Parameters

          • value: T

          Returns Maybe<U>

    Returns Maybe<U>

equals

  • equals(value: Maybe<T>): boolean
  • Checking equals value from Maybe and value from other Maybe.

    import { Maybe } from 'monad-maniac'
    
    const just = Maybe.of(10)
    const sameJust = Maybe.of(10)
    
    const isEqual = equalsValue.equals(sameJust) // true
    const isMoreEqual = equalsValue.equals(Maybe.of(15)) // false
    

    Parameters

    Returns boolean

equalsValue

  • equalsValue(value: Nullable<T>): boolean
  • Checking equals value from Maybe and what value in parameter.

    import { Maybe } from 'monad-maniac'
    
    const just = Maybe.of(10)
    
    const isSame = equalsValue.equalsValue(10) // true
    const isEqual = equalsValue.equalsValue(15) // false
    

    Parameters

    • value: Nullable<T>

    Returns boolean

filter

  • filter(f: (value: T) => boolean): Maybe<T>
  • Filtering value in Maybe. Takes predicate function and return new Maybe. If the function return false, then maybe will Noting otherwise Just.

    import { Maybe } from 'monad-maniac'
    
    const result = Maybe
     .of(10)
     .map((x) => x * x)
     .filter((x) => x > 50) // result will Just(100)
     .filter((x) => x < 90) // result will Nothing()
    
    console.log(result.toString()) // Nothing()
    

    Parameters

    • f: (value: T) => boolean
        • (value: T): boolean
        • Parameters

          • value: T

          Returns boolean

    Returns Maybe<T>

getOrElse

  • getOrElse<U>(defaultValue: U): T | U
  • Unwrap value from monad and return value from Just or defaultValue if it was Nothing.

    import { Maybe } from 'monad-maniac'
    
    const brokenDataMaybe = Maybe.of(null)
    const normalDataMaybe = Maybe.of(10)
    
    const brokenData = brokenDataMaybe.unwrapOr('This data was broken') // This data was broken')
    const normalData = Maybe.of(10).unwrapOr('This data was broken') // 10
    

    Type parameters

    • U

    Parameters

    • defaultValue: U

    Returns T | U

isJust

  • isJust(): boolean
  • Return true if Just

    Returns boolean

isNothing

  • isNothing(): boolean
  • Return true if Nothing

    Returns boolean

join

  • join(): JoinMaybe<T>
  • Unwrap Maybe from Maybe, if in Maybe will not Maybe then returns Nothing.

    Maybe(Maybe a) -> Maybe a

    import { Maybe } from 'monad-maniac'
    
    const just = Maybe.of(10)
    const nestedJust = Maybe.of(just) // Just(Just(10))
    
    const backJust = nestedJust.join() // Just(10)
    const backMoreJust = backJust.join() // Nothing
    

    Returns JoinMaybe<T>

map

  • map<U>(f: (value: NonNullable<T>) => Nullable<U>): Maybe<NonNullable<U>>
  • Apply some function to value in container. map for Just will call the function with value, for Nothing return Nothing. If map will return null or undefined then return Nothing.

    import { Maybe } from 'monad-maniac'
    
    const zeroDivider = 0
    const tenDivide = (n: number): number => n === 0 ? null : 10 / n
    const foo = Maybe.of(zeroDivider).map(tenDivide) // Noting
    
    const nonZeroDivider = 2
    const bar = Maybe.of(nonZeroDivider).map(tenDivide) // Just(5)
    

    Type parameters

    • U

    Parameters

    • f: (value: NonNullable<T>) => Nullable<U>
        • (value: NonNullable<T>): Nullable<U>
        • Parameters

          • value: NonNullable<T>

          Returns Nullable<U>

    Returns Maybe<NonNullable<U>>

or

  • or(f: () => Nullable<T>): Maybe<NonNullable<T>>
  • If your Maybe contains Nothing and you want make some function in the case you need use or. Your function will be called only on Nothing() and result wrap in Maybe. In just your function will be ignored.

    import { Maybe } from 'monad-maniac'
    
    const zeroDivider = 0
    const tenDivide = (n: number): number => n === 0 ? null : 10 / n
    // map(tenDivide) will returns `Nothing()`
    const foo = Maybe.of(zeroDivider).map(tenDivide).or(() => 10) // Just(10)
    

    Parameters

    • f: () => Nullable<T>
        • (): Nullable<T>
        • Returns Nullable<T>

    Returns Maybe<NonNullable<T>>

toEither

  • toEither<U>(leftValue: U): Shape<U, T>
  • Converting Maybe to Either.

    import { Maybe } from 'monad-maniac'
    
    const just = Maybe.of(10)
    const nothing = Maybe.of<number>(null)
    
    just.toEither('error') // Either.Right(10)
    nothing.toEither('error') // Either.Left(error)
    
    

    Type parameters

    • U

    Parameters

    • leftValue: U

      the value will contains in Either.Left

    Returns Shape<U, T>

toString

  • toString(): string
  • Returns Nothing() if Maybe is Nothing, else Just(value converted to string).

    Returns string

Generated using TypeDoc