UNPKG

ts-utls

Version:

Utilities for TypeScript library

184 lines (180 loc) 5.93 kB
"use strict"; /* MIT License Copyright (c) 2022 Cyril Dever Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Maybe = exports.None = exports.Some = void 0; const _1 = require("."); const __1 = require(".."); /** * Implement a `Maybe` instance * * @tparam `<T>` The type parameter * @param {boolean} isValue Set to `true` is passing an actual value of type `T` * @param {T} val The value */ class MaybeImpl { constructor(isValue, val) { this.hasValue = isValue; if (isValue && isNothing(val)) { throw new Error('Can not create Some with illegal value: ' + val + '.'); } this.val = val; } /* Monad implementation */ ap(maybeFn) { if (this.hasValue) { const value = this.val; return maybeFn.map(function (fn) { return fn(value); }); } else { return (0, exports.None)(); } } bind(bindFn) { return this.hasValue ? bindFn(this.val) : (0, exports.None)(); } flatMap(flatMapFn) { return this.hasValue ? flatMapFn(this.val) : (0, exports.None)(); } chain(chainFn) { return this.hasValue ? chainFn(this.val) : (0, exports.None)(); } join() { return this.flatMap(_1.idFunction); } map(mapFn) { try { const m = mapFn(this.val); return m !== undefined ? (0, exports.Some)(m) : (0, exports.None)(); } catch (_) { // eslint-disable-line @typescript-eslint/no-unused-vars return (0, exports.None)(); } } takeLeft(m) { // eslint-disable-next-line @typescript-eslint/no-unused-vars return apply2(this, m, function (a, b) { return a; }); } takeRight(m) { // eslint-disable-next-line @typescript-eslint/no-unused-vars return apply2(this, m, function (a, b) { return b; }); } /* Maybe specifics */ cata(none, some) { return this.isSome() ? some(this.val) : none(); } filter(fn) { const self = this; // eslint-disable-line @typescript-eslint/no-this-alias return self.flatMap(function (a) { return a !== null && fn(a) ? (0, exports.Some)(self.val) : (0, exports.None)(); }); } fold(defaultValue) { const self = this; // eslint-disable-line @typescript-eslint/no-this-alias return function (fn) { return self.isSome() ? fn(self.val) : defaultValue; }; } forEach(fn) { return this.cata(_1.noop, fn); } getOrElse(val) { return this.hasValue ? this.val : val; } orElse(maybe) { return this.isSome() ? this : maybe; } orSome(otherValue) { return this.hasValue ? this.val : otherValue; } orNull() { return this.orSome(null); } orUndefined() { return this.orSome(undefined); } isNone() { return !this.isSome(); } isSome() { return this.hasValue; } some() { if (this.hasValue) { return this.val; } else { throw new Error('Cannot call .some() on a None.'); } } toArray() { const maybeArr = this.map((val) => [val]); return maybeArr.isSome() ? maybeArr.some() : []; } toEither(failVal) { return this.isSome() ? (0, __1.Right)(this.val) : (0, __1.Left)(failVal); } toList() { return this.hasValue ? __1.List.of(this.val) : (0, __1.Nil)(); } } const apply2 = (a1, a2, f) => // eslint-disable-next-line @typescript-eslint/no-unsafe-argument a2.ap(a1.map(curry(f, []))); const curry = (fn, args) => { return function () { const args1 = args.concat((0, _1.getArgs)(arguments)); // eslint-disable-line prefer-rest-params // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access return args1.length >= fn.length ? // eslint-disable-next-line prefer-spread, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call fn.apply(null, args1.slice(0, args1.length)) : curry(fn, args1); }; }; const isNothing = (val) => val === null || val === undefined; const Some = (val) => new MaybeImpl(true, val); exports.Some = Some; const None = () => new MaybeImpl(false, null); exports.None = None; const fromNull = (val) => { return isNothing(val) ? (0, exports.None)() : (0, exports.Some)(val); }; const fromUndefined = (val) => { return val === undefined ? (0, exports.None)() : (0, exports.Some)(val); }; const toList = (val) => { return val.toList(); }; exports.Maybe = { fromNull, fromUndefined, None: exports.None, Some: exports.Some, toList }; //# sourceMappingURL=maybe.js.map