tochkak-maybe

1.0.1 • Public • Published

Implementation of monad Maybe in javascript. It used for working with data wich may not exist in runtime.

class Maybe {
    static of = (value) => new Maybe(value); // method for creating Maybe container
 
    constructor(value) {
        this.value = value;
    }
 
    isNothing() { // method for checking that Maybe value exist or not
        return (
            this.value === null ||
            this.value === undefined ||
            Number.isNaN(this.value)
        );
    }
 
    isJust() { // antonim of isNothing method
        return !this.isNothing();
    }
 
    map(transform) { // method for safely working with Maybe value
        return this.isNothing() ? Maybe.of(null) : Maybe.of(transform(this.value));
    }
 
    get() { // method for extract Maybe value
        return this.value;
    }
 
    getOrElse(defaultValue) { // method for extract Maybe value with default value
        return this.isJust() ? this.value : defaultValue;
    }
}

Readme

Keywords

Package Sidebar

Install

npm i tochkak-maybe

Weekly Downloads

0

Version

1.0.1

License

ISC

Last publish

Collaborators

  • mr_hardison
  • strizhakov
  • amberkatarina
  • rabamidab
  • rocketman
  • petryaeva.e
  • lankaa
  • ushakovigor
  • lewka
  • chernov_kp
  • vadsm
  • babak12
  • mrhardison
  • trojan_nikita
  • vismu