UNPKG

@snipsonian/core

Version:

Core/base reusable javascript code snippets

45 lines (44 loc) 1.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Maybe = void 0; const isNull_1 = require("../is/isNull"); const isUndefined_1 = require("../is/isUndefined"); class Maybe { constructor(value) { this.value = value; } static of(value) { return new Maybe(value); } static nothing() { return new Maybe(null); } isNothing() { return (0, isNull_1.default)(this.value) || (0, isUndefined_1.default)(this.value); } map(mapper) { return this.isNothing() ? Maybe.nothing() : Maybe.of(mapper(this.value)); } flatMap(mapper) { return this.isNothing() ? Maybe.nothing() : mapper(this.value); } getOrElse(elseValue) { return this.isNothing() ? elseValue : this.value; } getOrNull() { return this.getOrElse(null); } getOrEmptyArray() { return this.getOrElse([]); } getOrEmptyObject() { return this.getOrElse({}); } } exports.Maybe = Maybe;