maybe-monade
Version:
Maybe monad implementation in Typescript
125 lines (124 loc) • 4.48 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MaybeCallback_1 = require("./MaybeCallback");
var utils_1 = require("./utils");
/**
* A wrapper (abstraction) for a value that may or may not exist
*/
var Maybe = /** @class */ (function () {
function Maybe(value) {
this.value = value;
}
/**
* Return an instance of Maybe wrapping an empty value
*/
Maybe.none = function () {
return new Maybe(null);
};
/**
* if the provided value is not null or undefined, return an instance of Maybe wrapping a nonempty value,
* otherwise throw an error
* @param value the value to wrap in an instance of Maybe
*/
Maybe.some = function (value) {
if (utils_1.isNullOrUndefined(value)) {
throw Error(utils_1.ErrorMessages.emptyValue);
}
return new Maybe(value);
};
/**
* return an instance of Maybe wrapping the provided value, otherwise return an instance of empty Maybe
* @param value value to wrap into a Maybe
*/
Maybe.fromValue = function (value) {
return utils_1.isNullOrUndefined(value) ? Maybe.none() : Maybe.some(value);
};
/**
* return an instance of Maybe wrapping the provided callback, otherwise return an instance of empty Maybe
* @param func callback to wrap into a Maybe
*/
Maybe.fromFunction = function (func) {
if (typeof func !== "function") {
return MaybeCallback_1.MaybeCallback.none();
}
return MaybeCallback_1.MaybeCallback.some(func);
};
/**
* return true if the wrapped value is empty, false otherwise
*/
Maybe.prototype.isEmpty = function () {
return utils_1.isNullOrUndefined(this.value);
};
/**
* return true if the wrapped value is nonempty, false otherwise
*/
Maybe.prototype.exists = function () {
return !utils_1.isNullOrUndefined(this.value);
};
/**
* get the wrapped value if nonempty, otherwise throw an error
*/
Maybe.prototype.get = function () {
if (this.exists()) {
return this.value;
}
else {
throw new Error(utils_1.ErrorMessages.getEmptyValue);
}
};
/**
* return the wrapped value if nonempty, otherwise the provided default value.
*/
Maybe.prototype.getOrElse = function (defaultValue) {
return this.isEmpty() ? defaultValue : this.value;
};
/**
* return the value if nonempty, otherwise invoke alternative
* and return the result of that invocation.
* @param alternative the function to invoke
*/
Maybe.prototype.orElse = function (alternative) {
return this.exists() ? Maybe.some(this.value) : alternative();
};
/**
* if the value exists, apply the provided mapping function to it,
* return an instance of Maybe wrapping the result.
* @param fmap the function to apply
*/
Maybe.prototype.map = function (fmap) {
return this.exists() ? Maybe.some(fmap(this.value)) : Maybe.none();
};
/**
* if the wrapped value is nonempty, apply the provided mapping function to it,
* return that result, otherwise return an instance of empty Maybe.
* @param func the function to apply
*/
Maybe.prototype.flatMap = function (func) {
return this.exists() ? func(this.value) : Maybe.none();
};
/**
* apply func to the wrapped value then return an instance of Maybe wrapping
* the value before applying the func function.
* func could be console.log for example.
* @param func function to apply
*/
Maybe.prototype.do = function (func) {
if (this.exists() && func) {
func(this.value);
return Maybe.fromValue(this.value);
}
else {
return Maybe.none();
}
};
/**
* if the wrapped value is nonempty, and the value matches the given predicate,
* return a Maybe wrapping the value, otherwise return an instance of empty Maybe
* @param predicate a predicate to apply to the value if nonempty
*/
Maybe.prototype.filter = function (predicate) {
return this.exists() && predicate(this.value) ? Maybe.some(this.value) : Maybe.none();
};
return Maybe;
}());
exports.Maybe = Maybe;