0.3.8 • Published 4 years ago

@typed-f/maybe v0.3.8

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

@typed-f/maybe

NPM Version

Known Vulnerabilities Supported TypeScript Version PRs Welcome

Watch on GitHub Star on GitHub

Partial type for Typed-F

Installation

# for NPM>=5
npm install @typed-f/maybe
# or
npm i @typed-f/maybe
# for NPM<5
npm install --save @typed-f/maybe
# or
npm i -S @typed-f/maybe

APIs

This package includes type definition for Maybe type, which is an union type of Just and Nothing. This package also includes their methods and some utility functions for them.

Types

  • Maybe<T>
  • Just<T>
  • Nothing<T>

Methods only for Maybe

  • isJust(this: Maybe<T>): this is Just<T> Checks whether this is Just or not. You can use this with if statement like following code.
    declare const m: Maybe<number>;
    
    if (m.isJust()) {
      // Now `m` is `Just<number>`
      console.log(m.value.toFixed(4));
    }
  • isNothing(this: Maybe<T>): this is Nothing<any> Counterpart of isJust. You can use this with if statement.
  • valueOr(this: Maybe<T>, def: T): T If this is Just, this function returns the inner value of this. If not, this returns def (default value) you passed.
  • valueOrThrow(this: Maybe<T>, err: Error): T If this is Just, this function returns the inner value of this. If not, this throws err you passed.
  • valueOrCompute(this: Maybe<T>, f: Fun<[], T>): T If this is Just, this function returns the inner value of this. If not, this invoke f with inner value of current this, and return the result.

Implemented Typeclasses

  • Monad
    • bind<U>(this: Maybe<T>, f: Fun<[T], Maybe<U>>): Maybe<U> If this is Just, applies f to its inner value and return the result. If this is Nothing, returns this.
  • Applicative
    • unit<U>(v: U): Maybe<any, U> Return Just of v.
    • ap<U>(this: Maybe<T>, f: Maybe<Fun<[T], U>>): Maybe<U> If this is Just and f is Just, applies the inner value of f to the inner value of this and returns Just of the result. If this is Just but f is Nothing, returns f. If this is Nothing, returns this.
  • Functor
    • map<U>(this: Maybe<T>, f: Fun<[R], U>): Maybe<U> If this is Just, applies f to the inner value of this and returns Just of the result. If this is Nothing, returns this.
  • Matchable
    • matchWith<U>(this: Maybe<T>, cases: MaybePatterns<T, U>): U If this is Just, applies cases.just to the inner value of this and returns the result. If this is Nothing, invoke cases.nothing and returns the result.
  • Setoid
    • equals(other: Maybe<any>): boolean If this equals to other, return true. If this does not equal to other, return false. When both this and other are Justs and have complex inner values (object), this function tries equals method of inner value of this. In other words, this.equals(other) === this.value.equals(other.value)
    • notEquals(other: Maybe<any>): boolean Returns true if and only if this.equals(other) returns false.

Utility Functions

You can use these functions like Maybe.<function name>, for example, in case of map, you can access it with Maybe.map.

  • unit<T>(value: T): Maybe<T> Returns Just of value.
  • of Alias of unit
  • from<T>(value?: null): Nothing<T>
  • from<T>(value: T): Just<T> Returns Nothing for value that is null or undefined, and returns Just of value for other values.
  • maybe Alias of from
  • sequenceObject<O extends object>(obj: { [K in keyof O]: Maybe<O[K]> }): Maybe<O> Takes an obj of Maybes and returns an Maybe of object. If for all keys of obj, correspoding values are Just, then this will return a Just of object whose keys are original keys and correspoding values are inner value of original values (Justs). If for one or more keys of obj, values are Nothing, then this will return Nothing.
  • sequenceArray<T>(array: Maybe<T>[]): Maybe<T[]> Takes an array of Maybes and returns an Maybe of array. If all entries of array are Just, this will return a Just of array whose entries are inner values of original entries. Corresponding entries will have same indices. If one or more entries of array are Nothing, this will return a Nothing.
  • map<T, U>(f: Fun<[T], U>): Fun<[Maybe<T>], Maybe<U>> Returns a function that takes Maybe<T> and maps its inner value using f. map(f)(a) is same with a.map(f).