import { flow, identity } 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 prism = _.prism; /** * @category constructors * @since 2.3.0 */ export var id = function () { return prism(O.some, identity); }; /** * @category constructors * @since 2.3.0 */ export var fromPredicate = _.prismFromPredicate; // ------------------------------------------------------------------------------------- // converters // ------------------------------------------------------------------------------------- /** * View a `Prism` as a `Optional`. * * @category converters * @since 2.3.0 */ export var asOptional = _.prismAsOptional; /** * View a `Prism` as a `Traversal`. * * @category converters * @since 2.3.0 */ export var asTraversal = _.prismAsTraversal; // ------------------------------------------------------------------------------------- // compositions // ------------------------------------------------------------------------------------- /** * Compose a `Prism` with a `Prism`. * * @category compositions * @since 2.3.0 */ export var compose = _.prismComposePrism; /** * Alias of `compose`. * * @category compositions * @since 2.3.8 */ export var composePrism = compose; /** * Compose a `Prism` with a `Iso`. * * @category compositions * @since 2.3.8 */ export var composeIso = /*#__PURE__*/ flow(_.isoAsPrism, compose); /** * Compose a `Prism` with a `Lens`. * * @category compositions * @since 2.3.0 */ export var composeLens = _.prismComposeLens; /** * Compose a `Prism` with an `Optional`. * * @category compositions * @since 2.3.0 */ export var composeOptional = function (ab) { return flow(asOptional, _.optionalComposeOptional(ab)); }; /** * Compose a `Prism` 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 set = _.prismSet; /** * @category combinators * @since 2.3.0 */ export var modifyOption = _.prismModifyOption; /** * @category combinators * @since 2.3.0 */ export var modify = _.prismModify; 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), sa.reverseGet); })); }; }; }; } /** * Return a `Prism` from a `Prism` focused on a nullable value. * * @category combinators * @since 2.3.3 */ export var fromNullable = /*#__PURE__*/ compose(/*#__PURE__*/ _.prismFromNullable()); export function filter(predicate) { return compose(_.prismFromPredicate(predicate)); } /** * Return a `Optional` from a `Prism` and a prop. * * @category combinators * @since 2.3.0 */ export var prop = function (prop) { return composeLens(pipe(_.lensId(), _.lensProp(prop))); }; // TODO: simplify? /** * Return a `Optional` from a `Prism` 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 composeLens(pipe(_.lensId(), _.lensProps.apply(_, props))); }; /** * Return a `Optional` from a `Prism` focused on a component of a tuple. * * @category combinators * @since 2.3.0 */ export var component = function (prop) { return composeLens(pipe(_.lensId(), _.lensComponent(prop))); }; /** * Return a `Optional` from a `Prism` focused on an index of a `ReadonlyArray`. * * @category combinators * @since 2.3.0 */ export var index = function (i) { return flow(asOptional, _.optionalIndex(i)); }; /** * Return a `Optional` from a `Prism` focused on an index of a `ReadonlyNonEmptyArray`. * * @category combinators * @since 2.3.8 */ export var indexNonEmpty = function (i) { return flow(asOptional, _.optionalIndexNonEmpty(i)); }; /** * Return a `Optional` from a `Prism` focused on a key of a `ReadonlyRecord`. * * @category combinators * @since 2.3.0 */ export var key = function (key) { return flow(asOptional, _.optionalKey(key)); }; /** * Return a `Optional` from a `Prism` focused on a required key of a `ReadonlyRecord`. * * @category combinators * @since 2.3.0 */ export var atKey = function (key) { return function (sa) { return _.prismComposeLens(_.atReadonlyRecord().at(key))(sa); }; }; /** * Return a `Prism` from a `Prism` focused on the `Some` of a `Option` type. * * @category combinators * @since 2.3.0 */ export var some = /*#__PURE__*/ compose(/*#__PURE__*/ _.prismSome()); /** * Return a `Prism` from a `Prism` focused on the `Right` of a `Either` type. * * @category combinators * @since 2.3.0 */ export var right = /*#__PURE__*/ compose(/*#__PURE__*/ _.prismRight()); /** * Return a `Prism` from a `Prism` focused on the `Left` of a `Either` type. * * @category combinators * @since 2.3.0 */ export var left = /*#__PURE__*/ compose(/*#__PURE__*/ _.prismLeft()); /** * Return a `Traversal` from a `Prism` focused on a `Traversable`. * * @category combinators * @since 2.3.0 */ export function traverse(T) { return flow(asTraversal, _.traversalTraverse(T)); } export function findFirst(predicate) { return composeOptional(_.optionalFindFirst(predicate)); } export function findFirstNonEmpty(predicate) { return composeOptional(_.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 prism(flow(ea.getOption, O.map(ab)), flow(ba, ea.reverseGet)); }; /** * @category instances * @since 2.3.0 */ export var URI = 'monocle-ts/Prism'; /** * @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 };