tochkak-maybe
v1.0.1
Published
Implementation of monad Maybe in javascript.
Downloads
2
Readme
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;
}
}