import { flow } from 'fp-ts/es6/function'; import { pipe } from 'fp-ts/es6/pipeable'; import * as _ from './internal'; // ------------------------------------------------------------------------------------- // constructors // ------------------------------------------------------------------------------------- /** * @category constructors * @since 2.3.8 */ export var lens = _.lens; /** * @category constructors * @since 2.3.0 */ export var id = _.lensId; // ------------------------------------------------------------------------------------- // converters // ------------------------------------------------------------------------------------- /** * View a `Lens` as a `Optional`. * * @category converters * @since 2.3.0 */ export var asOptional = _.lensAsOptional; /** * View a `Lens` as a `Traversal`. * * @category converters * @since 2.3.0 */ export var asTraversal = _.lensAsTraversal; // ------------------------------------------------------------------------------------- // compositions // ------------------------------------------------------------------------------------- /** * Compose a `Lens` with a `Lens`. * * @category compositions * @since 2.3.0 */ export var compose = _.lensComposeLens; /** * Alias of `compose`. * * @category compositions * @since 2.3.8 */ export var composeLens = compose; /** * Compose a `Lens` with a `Iso`. * * @category compositions * @since 2.3.8 */ export var composeIso = /*#__PURE__*/ flow(_.isoAsLens, compose); /** * Compose a `Lens` with a `Prism`. * * @category compositions * @since 2.3.0 */ export var composePrism = _.lensComposePrism; /** * Compose a `Lens` with an `Optional`. * * @category compositions * @since 2.3.0 */ export var composeOptional = function (ab) { return flow(asOptional, _.optionalComposeOptional(ab)); }; /** * Compose a `Lens` 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 modify = function (f) { return function (sa) { return function (s) { var o = sa.get(s); var n = f(o); return o === n ? s : sa.set(n)(s); }; }; }; export function modifyF(F) { return function (f) { return function (sa) { return function (s) { return pipe(sa.get(s), f, function (fa) { return F.map(fa, function (a) { return sa.set(a)(s); }); }); }; }; }; } /** * Return a `Optional` from a `Lens` focused on a nullable value. * * @category combinators * @since 2.3.0 */ export var fromNullable = function (sa) { return composePrism(_.prismFromNullable())(sa); }; export function filter(predicate) { return composePrism(_.prismFromPredicate(predicate)); } /** * Return a `Lens` from a `Lens` and a prop. * * @category combinators * @since 2.3.0 */ export var prop = _.lensProp; /** * Return a `Lens` from a `Lens` and a list of props. * * @category combinators * @since 2.3.0 */ export var props = _.lensProps; /** * Return a `Lens` from a `Lens` focused on a component of a tuple. * * @category combinators * @since 2.3.0 */ export var component = _.lensComponent; /** * Return a `Optional` from a `Lens` 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 `Lens` 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 `Lens` 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 `Lens` from a `Lens` focused on a required key of a `ReadonlyRecord`. * * @category combinators * @since 2.3.0 */ export var atKey = _.lensAtKey; /** * Return a `Optional` from a `Lens` focused on the `Some` of a `Option` type. * * @category combinators * @since 2.3.0 */ export var some = /*#__PURE__*/ composePrism(/*#__PURE__*/ _.prismSome()); /** * Return a `Optional` from a `Lens` focused on the `Right` of a `Either` type. * * @category combinators * @since 2.3.0 */ export var right = /*#__PURE__*/ composePrism(/*#__PURE__*/ _.prismRight()); /** * Return a `Optional` from a `Lens` focused on the `Left` of a `Either` type. * * @category combinators * @since 2.3.0 */ export var left = /*#__PURE__*/ composePrism(/*#__PURE__*/ _.prismLeft()); /** * Return a `Traversal` from a `Lens` 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 lens(flow(ea.get, ab), flow(ba, ea.set)); }; /** * @category instances * @since 2.3.0 */ export var URI = 'monocle-ts/Lens'; /** * @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 };