UNPKG

@beenotung/tslib

Version:
57 lines (56 loc) 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Maybe = exports.Nothing = void 0; const either_1 = require("./either"); const lang_1 = require("./lang"); exports.Nothing = { get() { throw new Error('Cannot get value from Nothing.'); }, isJust: false, isNothing: true, map: () => exports.Nothing, withDefault: a => Maybe.fromNullable(a), then: () => { return exports.Nothing; }, otherwise: (f) => { f(); return exports.Nothing; }, toEither: (e) => (0, either_1.left)(e), }; var Maybe; (function (Maybe) { function fromNullable(a) { if ((0, lang_1.isDefined)(a)) { return { get: () => a, isJust: true, isNothing: false, map: (f) => fromNullable(f(a)), withDefault: _ => fromNullable(a), then: (f) => { f(a); return fromNullable(a); }, otherwise: _ => fromNullable(a), toEither: (_e) => (0, either_1.right)(a), }; } else { return exports.Nothing; } } Maybe.fromNullable = fromNullable; function or(a, b) { return a.isJust ? a : b.isJust ? b : exports.Nothing; } Maybe.or = or; function and(a, b) { return a.isJust && b.isJust ? fromNullable([a.get(), b.get()]) : exports.Nothing; } Maybe.and = and; })(Maybe || (exports.Maybe = Maybe = {}));