"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Category = exports.Semigroupoid = exports.Invariant = exports.URI = exports.imap = exports.findFirstNonEmpty = exports.findFirst = exports.traverse = exports.left = exports.right = exports.some = exports.atKey = exports.key = exports.indexNonEmpty = exports.index = exports.component = exports.props = exports.prop = exports.filter = exports.fromNullable = exports.modifyF = exports.modify = exports.setOption = exports.modifyOption = exports.composeTraversal = exports.composePrism = exports.composeLens = exports.composeIso = exports.composeOptional = exports.compose = exports.asTraversal = exports.id = exports.optional = void 0; var function_1 = require("fp-ts/lib/function"); var O = require("fp-ts/lib/Option"); var pipeable_1 = require("fp-ts/lib/pipeable"); var _ = require("./internal"); // ------------------------------------------------------------------------------------- // constructors // ------------------------------------------------------------------------------------- /** * @category constructors * @since 2.3.8 */ exports.optional = _.optional; /** * @category constructors * @since 2.3.0 */ var id = function () { return (0, exports.optional)(O.some, function_1.constant); }; exports.id = id; // ------------------------------------------------------------------------------------- // converters // ------------------------------------------------------------------------------------- /** * View a `Optional` as a `Traversal`. * * @category converters * @since 2.3.0 */ exports.asTraversal = _.optionalAsTraversal; // ------------------------------------------------------------------------------------- // compositions // ------------------------------------------------------------------------------------- /** * Compose a `Optional` with a `Optional`. * * @category compositions * @since 2.3.0 */ exports.compose = _.optionalComposeOptional; /** * Alias of `compose`. * * @category compositions * @since 2.3.8 */ exports.composeOptional = exports.compose; /** * Compose a `Optional` with a `Iso`. * * @category compositions * @since 2.3.8 */ exports.composeIso = /*#__PURE__*/ (0, function_1.flow)(_.isoAsOptional, exports.compose); /** * Compose a `Optional` with a `Lens`. * * @category compositions * @since 2.3.7 */ exports.composeLens = /*#__PURE__*/ (0, function_1.flow)(_.lensAsOptional, _.optionalComposeOptional); /** * Compose a `Optional` with a `Prism`. * * @category compositions * @since 2.3.7 */ exports.composePrism = /*#__PURE__*/ (0, function_1.flow)(_.prismAsOptional, _.optionalComposeOptional); /** * Compose a `Optional` with an `Traversal`. * * @category compositions * @since 2.3.8 */ var composeTraversal = function (ab) { return (0, function_1.flow)(exports.asTraversal, _.traversalComposeTraversal(ab)); }; exports.composeTraversal = composeTraversal; // ------------------------------------------------------------------------------------- // combinators // ------------------------------------------------------------------------------------- /** * @category combinators * @since 2.3.0 */ exports.modifyOption = _.optionalModifyOption; /** * @category combinators * @since 2.3.7 */ var setOption = function (a) { return (0, exports.modifyOption)(function () { return a; }); }; exports.setOption = setOption; /** * @category combinators * @since 2.3.0 */ exports.modify = _.optionalModify; function modifyF(F) { return function (f) { return function (sa) { return function (s) { return (0, pipeable_1.pipe)(sa.getOption(s), O.fold(function () { return F.of(s); }, function (a) { return F.map(f(a), function (a) { return sa.set(a)(s); }); })); }; }; }; } exports.modifyF = modifyF; /** * Return an `Optional` from a `Optional` focused on a nullable value. * * @category combinators * @since 2.3.3 */ exports.fromNullable = /*#__PURE__*/ (0, exports.compose)(/*#__PURE__*/ _.prismAsOptional(/*#__PURE__*/ _.prismFromNullable())); function filter(predicate) { return (0, exports.compose)(_.prismAsOptional(_.prismFromPredicate(predicate))); } exports.filter = filter; /** * Return a `Optional` from a `Optional` and a prop. * * @category combinators * @since 2.3.0 */ var prop = function (prop) { return (0, exports.compose)((0, pipeable_1.pipe)(_.lensId(), _.lensProp(prop), _.lensAsOptional)); }; exports.prop = prop; /** * Return a `Optional` from a `Optional` and a list of props. * * @category combinators * @since 2.3.0 */ var props = function () { var props = []; for (var _i = 0; _i < arguments.length; _i++) { props[_i] = arguments[_i]; } return (0, exports.compose)((0, pipeable_1.pipe)(_.lensId(), _.lensProps.apply(_, props), _.lensAsOptional)); }; exports.props = props; /** * Return a `Optional` from a `Optional` focused on a component of a tuple. * * @category combinators * @since 2.3.0 */ var component = function (prop) { return (0, exports.compose)((0, pipeable_1.pipe)(_.lensId(), _.lensComponent(prop), _.lensAsOptional)); }; exports.component = component; /** * Return a `Optional` from a `Optional` focused on an index of a `ReadonlyArray`. * * @category combinators * @since 2.3.0 */ exports.index = _.optionalIndex; /** * Return a `Optional` from a `Optional` focused on an index of a `ReadonlyNonEmptyArray`. * * @category combinators * @since 2.3.8 */ exports.indexNonEmpty = _.optionalIndexNonEmpty; /** * Return a `Optional` from a `Optional` focused on a key of a `ReadonlyRecord`. * * @category combinators * @since 2.3.0 */ exports.key = _.optionalKey; /** * Return a `Optional` from a `Optional` focused on a required key of a `ReadonlyRecord`. * * @category combinators * @since 2.3.0 */ var atKey = function (key) { return function (sa) { return (0, pipeable_1.pipe)(sa, (0, exports.compose)(_.lensAsOptional(_.atReadonlyRecord().at(key)))); }; }; exports.atKey = atKey; /** * Return a `Optional` from a `Optional` focused on the `Some` of a `Option` type. * * @category combinators * @since 2.3.0 */ exports.some = /*#__PURE__*/ (0, exports.compose)(/*#__PURE__*/ _.prismAsOptional(/*#__PURE__*/ _.prismSome())); /** * Return a `Optional` from a `Optional` focused on the `Right` of a `Either` type. * * @category combinators * @since 2.3.0 */ exports.right = /*#__PURE__*/ (0, exports.compose)(/*#__PURE__*/ _.prismAsOptional(/*#__PURE__*/ _.prismRight())); /** * Return a `Optional` from a `Optional` focused on the `Left` of a `Either` type. * * @category combinators * @since 2.3.0 */ exports.left = /*#__PURE__*/ (0, exports.compose)(/*#__PURE__*/ _.prismAsOptional(/*#__PURE__*/ _.prismLeft())); /** * Return a `Traversal` from a `Optional` focused on a `Traversable`. * * @category combinators * @since 2.3.0 */ function traverse(T) { return (0, function_1.flow)(exports.asTraversal, _.traversalTraverse(T)); } exports.traverse = traverse; function findFirst(predicate) { return (0, exports.compose)(_.optionalFindFirst(predicate)); } exports.findFirst = findFirst; function findFirstNonEmpty(predicate) { return (0, exports.compose)(_.optionalFindFirstNonEmpty(predicate)); } exports.findFirstNonEmpty = findFirstNonEmpty; // ------------------------------------------------------------------------------------- // pipeables // ------------------------------------------------------------------------------------- /** * @category Invariant * @since 2.3.0 */ var imap = function (f, g) { return function (ea) { return imap_(ea, f, g); }; }; exports.imap = imap; // ------------------------------------------------------------------------------------- // instances // ------------------------------------------------------------------------------------- var imap_ = function (ea, ab, ba) { return (0, exports.optional)((0, function_1.flow)(ea.getOption, O.map(ab)), (0, function_1.flow)(ba, ea.set)); }; /** * @category instances * @since 2.3.0 */ exports.URI = 'monocle-ts/Optional'; /** * @category instances * @since 2.3.0 */ exports.Invariant = { URI: exports.URI, imap: imap_ }; /** * @category instances * @since 2.3.8 */ exports.Semigroupoid = { URI: exports.URI, compose: function (ab, ea) { return (0, exports.compose)(ab)(ea); } }; /** * @category instances * @since 2.3.0 */ exports.Category = { URI: exports.URI, compose: exports.Semigroupoid.compose, id: exports.id };