84 lines
3.2 KiB
JavaScript
84 lines
3.2 KiB
JavaScript
|
"use strict";
|
||
|
/**
|
||
|
* The `FromEither` type class represents those data types which support errors.
|
||
|
*
|
||
|
* @since 2.10.0
|
||
|
*/
|
||
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||
|
if (k2 === undefined) k2 = k;
|
||
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
||
|
}
|
||
|
Object.defineProperty(o, k2, desc);
|
||
|
}) : (function(o, m, k, k2) {
|
||
|
if (k2 === undefined) k2 = k;
|
||
|
o[k2] = m[k];
|
||
|
}));
|
||
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||
|
}) : function(o, v) {
|
||
|
o["default"] = v;
|
||
|
});
|
||
|
var __importStar = (this && this.__importStar) || function (mod) {
|
||
|
if (mod && mod.__esModule) return mod;
|
||
|
var result = {};
|
||
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||
|
__setModuleDefault(result, mod);
|
||
|
return result;
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.filterOrElse = exports.chainFirstEitherK = exports.chainEitherK = exports.fromEitherK = exports.chainOptionK = exports.fromOptionK = exports.fromPredicate = exports.fromOption = void 0;
|
||
|
var Chain_1 = require("./Chain");
|
||
|
var function_1 = require("./function");
|
||
|
var _ = __importStar(require("./internal"));
|
||
|
function fromOption(F) {
|
||
|
return function (onNone) { return function (ma) { return F.fromEither(_.isNone(ma) ? _.left(onNone()) : _.right(ma.value)); }; };
|
||
|
}
|
||
|
exports.fromOption = fromOption;
|
||
|
function fromPredicate(F) {
|
||
|
return function (predicate, onFalse) {
|
||
|
return function (a) {
|
||
|
return F.fromEither(predicate(a) ? _.right(a) : _.left(onFalse(a)));
|
||
|
};
|
||
|
};
|
||
|
}
|
||
|
exports.fromPredicate = fromPredicate;
|
||
|
function fromOptionK(F) {
|
||
|
var fromOptionF = fromOption(F);
|
||
|
return function (onNone) {
|
||
|
var from = fromOptionF(onNone);
|
||
|
return function (f) { return (0, function_1.flow)(f, from); };
|
||
|
};
|
||
|
}
|
||
|
exports.fromOptionK = fromOptionK;
|
||
|
function chainOptionK(F, M) {
|
||
|
var fromOptionKF = fromOptionK(F);
|
||
|
return function (onNone) {
|
||
|
var from = fromOptionKF(onNone);
|
||
|
return function (f) { return function (ma) { return M.chain(ma, from(f)); }; };
|
||
|
};
|
||
|
}
|
||
|
exports.chainOptionK = chainOptionK;
|
||
|
function fromEitherK(F) {
|
||
|
return function (f) { return (0, function_1.flow)(f, F.fromEither); };
|
||
|
}
|
||
|
exports.fromEitherK = fromEitherK;
|
||
|
function chainEitherK(F, M) {
|
||
|
var fromEitherKF = fromEitherK(F);
|
||
|
return function (f) { return function (ma) { return M.chain(ma, fromEitherKF(f)); }; };
|
||
|
}
|
||
|
exports.chainEitherK = chainEitherK;
|
||
|
function chainFirstEitherK(F, M) {
|
||
|
return (0, function_1.flow)(fromEitherK(F), (0, Chain_1.chainFirst)(M));
|
||
|
}
|
||
|
exports.chainFirstEitherK = chainFirstEitherK;
|
||
|
function filterOrElse(F, M) {
|
||
|
return function (predicate, onFalse) {
|
||
|
return function (ma) {
|
||
|
return M.chain(ma, function (a) { return F.fromEither(predicate(a) ? _.right(a) : _.left(onFalse(a))); });
|
||
|
};
|
||
|
};
|
||
|
}
|
||
|
exports.filterOrElse = filterOrElse;
|