typed-utilities
Version:
Strongly typed general purpose utilities
65 lines (54 loc) • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Either = void 0;
var EitherTag;
(function (EitherTag) {
EitherTag["Left"] = "left";
EitherTag["Right"] = "right";
})(EitherTag || (EitherTag = {}));
function isLeft(either) {
return either[0] === EitherTag.Left;
}
function assertLeft(either, message) {
if (!isLeft(either)) {
throw new TypeError(message ?? `either should be 'left'`);
}
}
function isRight(either) {
return either[0] === EitherTag.Right;
}
function assertRight(either, message) {
if (!isRight(either)) {
throw new TypeError(message ?? `either should be 'right'`);
}
}
const toLeftValue = either => either[1];
const toRightValue = either => either[1];
const is = {
left: isLeft,
right: isRight
};
const to = {
leftValue: toLeftValue,
rightValue: toRightValue
};
const assert = {
left: assertLeft,
right: assertRight
};
const of = {
left: value => [EitherTag.Left, value],
right: value => [EitherTag.Right, value]
};
const match = (either, match) => isLeft(either) ? match.left(either[1]) : match.right(either[1]);
const Either = {
is,
to,
assert,
of,
match
};
exports.Either = Either;
//# sourceMappingURL=Either.js.map