284 lines
7.0 KiB
JavaScript
284 lines
7.0 KiB
JavaScript
import { flow, identity } 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 iso = _.iso;
|
|
/**
|
|
* @category constructors
|
|
* @since 2.3.0
|
|
*/
|
|
export var id = function () { return iso(identity, identity); };
|
|
// -------------------------------------------------------------------------------------
|
|
// converters
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* View an `Iso` as a `Lens`.
|
|
*
|
|
* @category converters
|
|
* @since 2.3.0
|
|
*/
|
|
export var asLens = _.isoAsLens;
|
|
/**
|
|
* View an `Iso` as a `Prism`.
|
|
*
|
|
* @category converters
|
|
* @since 2.3.0
|
|
*/
|
|
export var asPrism = _.isoAsPrism;
|
|
/**
|
|
* View an `Iso` as a `Optional`.
|
|
*
|
|
* @category converters
|
|
* @since 2.3.0
|
|
*/
|
|
export var asOptional = _.isoAsOptional;
|
|
/**
|
|
* View an `Iso` as a `Traversal`.
|
|
*
|
|
* @category converters
|
|
* @since 2.3.0
|
|
*/
|
|
export var asTraversal = _.isoAsTraversal;
|
|
// -------------------------------------------------------------------------------------
|
|
// compositions
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* Compose an `Iso` with an `Iso`.
|
|
*
|
|
* @category compositions
|
|
* @since 2.3.0
|
|
*/
|
|
export var compose = function (ab) { return function (sa) {
|
|
return iso(flow(sa.get, ab.get), flow(ab.reverseGet, sa.reverseGet));
|
|
}; };
|
|
/**
|
|
* Alias of `compose`.
|
|
*
|
|
* @category compositions
|
|
* @since 2.3.8
|
|
*/
|
|
export var composeIso = compose;
|
|
/**
|
|
* Compose an `Iso` with a `Lens`.
|
|
*
|
|
* @category compositions
|
|
* @since 2.3.8
|
|
*/
|
|
export var composeLens = function (ab) {
|
|
return flow(asLens, _.lensComposeLens(ab));
|
|
};
|
|
/**
|
|
* Compose an `Iso` with a `Prism`.
|
|
*
|
|
* @category compositions
|
|
* @since 2.3.8
|
|
*/
|
|
export var composePrism = function (ab) {
|
|
return flow(asPrism, _.prismComposePrism(ab));
|
|
};
|
|
/**
|
|
* Compose an `Iso` with a `Optional`.
|
|
*
|
|
* @category compositions
|
|
* @since 2.3.8
|
|
*/
|
|
export var composeOptional = function (ab) {
|
|
return flow(asOptional, _.optionalComposeOptional(ab));
|
|
};
|
|
/**
|
|
* Compose an `Iso` with a `Traversal`.
|
|
*
|
|
* @category compositions
|
|
* @since 2.3.8
|
|
*/
|
|
export var composeTraversal = function (ab) {
|
|
return flow(asTraversal, _.traversalComposeTraversal(ab));
|
|
};
|
|
// -------------------------------------------------------------------------------------
|
|
// combinators
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* @category constructors
|
|
* @since 2.3.0
|
|
*/
|
|
export var reverse = function (sa) { return iso(sa.reverseGet, sa.get); };
|
|
/**
|
|
* @category combinators
|
|
* @since 2.3.0
|
|
*/
|
|
export var modify = function (f) { return function (sa) { return function (s) {
|
|
return sa.reverseGet(f(sa.get(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, sa.reverseGet); }); }; }; };
|
|
}
|
|
/**
|
|
* Return a `Prism` from a `Iso` focused on a nullable value.
|
|
*
|
|
* @category combinators
|
|
* @since 2.3.8
|
|
*/
|
|
export var fromNullable = function (sa) {
|
|
return composePrism(_.prismFromNullable())(sa);
|
|
};
|
|
export function filter(predicate) {
|
|
return composePrism(_.prismFromPredicate(predicate));
|
|
}
|
|
/**
|
|
* Return a `Lens` from a `Iso` and a prop.
|
|
*
|
|
* @category combinators
|
|
* @since 2.3.8
|
|
*/
|
|
export var prop = function (prop) {
|
|
return flow(asLens, _.lensProp(prop));
|
|
};
|
|
/**
|
|
* Return a `Lens` from a `Iso` and a list of props.
|
|
*
|
|
* @category combinators
|
|
* @since 2.3.8
|
|
*/
|
|
export var props = function () {
|
|
var props = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
props[_i] = arguments[_i];
|
|
}
|
|
return flow(asLens, _.lensProps.apply(_, props));
|
|
};
|
|
/**
|
|
* Return a `Lens` from a `Iso` focused on a component of a tuple.
|
|
*
|
|
* @category combinators
|
|
* @since 2.3.8
|
|
*/
|
|
export var component = function (prop) { return flow(asLens, _.lensComponent(prop)); };
|
|
/**
|
|
* Return a `Optional` from a `Iso` focused on an index of a `ReadonlyArray`.
|
|
*
|
|
* @category combinators
|
|
* @since 2.3.8
|
|
*/
|
|
export var index = function (i) {
|
|
return flow(asOptional, _.optionalIndex(i));
|
|
};
|
|
/**
|
|
* Return a `Optional` from a `Iso` 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 `Iso` focused on a key of a `ReadonlyRecord`.
|
|
*
|
|
* @category combinators
|
|
* @since 2.3.8
|
|
*/
|
|
export var key = function (key) {
|
|
return flow(asOptional, _.optionalKey(key));
|
|
};
|
|
/**
|
|
* Return a `Lens` from a `Iso` focused on a required key of a `ReadonlyRecord`.
|
|
*
|
|
* @category combinators
|
|
* @since 2.3.8
|
|
*/
|
|
export var atKey = function (key) {
|
|
return flow(asLens, _.lensAtKey(key));
|
|
};
|
|
/**
|
|
* Return a `Prism` from a `Iso` focused on the `Some` of a `Option` type.
|
|
*
|
|
* @category combinators
|
|
* @since 2.3.8
|
|
*/
|
|
export var some =
|
|
/*#__PURE__*/
|
|
composePrism(/*#__PURE__*/ _.prismSome());
|
|
/**
|
|
* Return a `Prism` from a `Iso` focused on the `Right` of a `Either` type.
|
|
*
|
|
* @category combinators
|
|
* @since 2.3.8
|
|
*/
|
|
export var right =
|
|
/*#__PURE__*/
|
|
composePrism(/*#__PURE__*/ _.prismRight());
|
|
/**
|
|
* Return a `Prism` from a `Iso` focused on the `Left` of a `Either` type.
|
|
*
|
|
* @category combinators
|
|
* @since 2.3.8
|
|
*/
|
|
export var left =
|
|
/*#__PURE__*/
|
|
composePrism(/*#__PURE__*/ _.prismLeft());
|
|
/**
|
|
* Return a `Traversal` from a `Iso` focused on a `Traversable`.
|
|
*
|
|
* @category combinators
|
|
* @since 2.3.8
|
|
*/
|
|
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 iso(flow(ea.get, ab), flow(ba, ea.reverseGet)); };
|
|
/**
|
|
* @category instances
|
|
* @since 2.3.0
|
|
*/
|
|
export var URI = 'monocle-ts/Iso';
|
|
/**
|
|
* @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
|
|
};
|