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