ts-utls
Version:
Utilities for TypeScript library
154 lines (150 loc) • 5.67 kB
JavaScript
"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.Right = exports.Left = exports.Either = void 0;
const _1 = require(".");
const __1 = require("..");
/**
* Implement an `Either<Left, Right>` instance
*
* @tparam `<E, T>` The type parameters with `E` as left and `T` as right types
* @param {E | T} val The value
* @param {boolean} isRightValue Set to `true` when the value is a right value of type `T`
*/
class EitherImpl {
constructor(val, isRightValue) {
this.isRightValue = isRightValue;
this.value = val;
}
/* Monad implementation */
ap(eitherWithFn) {
if (this.isRightValue) {
/* eslint-disable @typescript-eslint/no-this-alias */
const self = this;
return eitherWithFn.map(function (fn) {
return fn(self.value);
});
/* eslint-enable @typescript-eslint/no-this-alias */
}
else {
return (0, exports.Either)(this.value, false);
}
}
bind(bindFn) {
return this.isRightValue ? bindFn(this.value) : (0, exports.Either)(this.value, false);
}
flatMap(flatMapFn) {
return this.isRightValue ? flatMapFn(this.value) : (0, exports.Either)(this.value, false);
}
chain(chainFn) {
return this.isRightValue ? chainFn(this.value) : (0, exports.Either)(this.value, false);
}
join() {
return this.flatMap(_1.idFunction);
}
map(mapFn) {
return this.isRightValue
? (0, exports.Either)(mapFn(this.value), true)
: (0, exports.Either)(mapFn(this.value), false);
}
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;
});
}
/* Either specifics */
cata(leftFn, rightFn) {
return this.isRightValue ? rightFn(this.value) : leftFn(this.value);
}
equals(other) {
return this.cata(function (left) {
return other.cata(equals(left), _1.falseFunction);
}, function (right) {
return other.cata(_1.falseFunction, equals(right));
});
}
fold(leftFn, rightFn) {
return this.isRightValue ? rightFn(this.value) : leftFn(this.value);
}
leftMap(leftMapFn) {
return this.isLeft() ? (0, exports.Left)(leftMapFn(this.value)) : (0, exports.Either)(null, false);
}
swap() {
return this.isRight() ? (0, exports.Left)(this.value) : (0, exports.Right)(this.value);
}
isLeft() {
return !this.isRight();
}
isRight() {
return this.isRightValue;
}
left() {
if (this.isRightValue) {
throw new Error('Cannot call left() on a Right.');
}
return this.value;
}
right() {
if (this.isRightValue) {
return this.value;
}
throw new Error('Cannot call right() on a Left.');
}
forEach(fn) {
this.cata(_1.noop, fn);
}
forEachLeft(fn) {
this.cata(fn, _1.noop);
}
toMaybe() {
return this.isRight() ? (0, __1.Some)(this.value) : (0, __1.None)();
}
/* eslint-disable @typescript-eslint/prefer-promise-reject-errors */
toPromise() {
return this.cata(function (left) { return Promise.reject(left); }, function (right) { return Promise.resolve(right); });
}
}
const apply2 = (a1, a2, f) => a2.ap(a1.map(curry(f, []))); // eslint-disable-line @typescript-eslint/no-unsafe-argument
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 equals = (a) => (b) => (0, _1.areEqual)(a, b);
const Either = (val, isRightValue) => new EitherImpl(val, isRightValue);
exports.Either = Either;
const Left = (val) => new EitherImpl(val, false);
exports.Left = Left;
const Right = (val) => new EitherImpl(val, true);
exports.Right = Right;
//# sourceMappingURL=either.js.map