UNPKG

pragmatic-fp-ts

Version:

Opinionated functional programming library with easy use in mind

118 lines (117 loc) 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isMaybe = exports.isNothing = exports.isJust = exports.maybe = exports.nothing = exports.just = exports.Just = exports.Nothing = void 0; const main_1 = require("./main"); const types_1 = require("./types"); class Nothing extends types_1.Monad { constructor() { super(...arguments); this.bind = this._; } _(_) { return this; } bindM(_) { return this; } filter(_) { return this; } effect(_) { return this; } getValue() { throw new Error("Can not get value of Nothing"); } getValueOr(alt) { return alt; } match(matcher) { return matcher.nothing ? (0, exports.maybe)(matcher.nothing()) : this; } isNothing() { return true; } isJust() { return false; } } exports.Nothing = Nothing; class Just extends types_1.Monad { constructor(value) { super(); this.bind = this._; this.value = value; } _(fn) { try { const result = fn(this.value); return (0, exports.maybe)((0, main_1.getValue)(result)); } catch (err) { return (0, exports.nothing)(); } } bindM(fn) { try { const result = fn(this); return (0, exports.maybe)((0, main_1.getValue)(result)); } catch (err) { return (0, exports.nothing)(); } } filter(fn) { try { return fn(this.value) ? this : (0, exports.nothing)(); } catch (_a) { return (0, exports.nothing)(); } } effect(fn) { try { fn(this.value); } catch (err) { } return this; } getValue() { return this.value; } getValueOr(_) { return this.value; } match(matcher) { const m = matcher.just || main_1.identity; return (0, exports.maybe)(m(this.value)); } isNothing() { return false; } isJust() { return true; } } exports.Just = Just; const just = (value) => { return new Just(value); }; exports.just = just; const nothing = () => { return new Nothing(); }; exports.nothing = nothing; const maybe = (value) => { const extractedValue = (0, main_1.getValue)(value); return extractedValue === null || extractedValue === undefined ? (0, exports.nothing)() : (0, exports.just)(extractedValue); }; exports.maybe = maybe; const isJust = (x) => x instanceof Just; exports.isJust = isJust; const isNothing = (x) => x instanceof Nothing; exports.isNothing = isNothing; const isMaybe = (x) => (0, exports.isJust)(x) || (0, exports.isNothing)(x); exports.isMaybe = isMaybe;