monocle-ts-esm
Version:
A porting of scala monocle library to TypeScript
300 lines (299 loc) • 9.41 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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.reverse = exports.composeTraversal = exports.composeOptional = exports.composePrism = exports.composeLens = exports.composeIso = exports.compose = exports.asTraversal = exports.asOptional = exports.asPrism = exports.asLens = exports.id = exports.iso = void 0;
const function_1 = require("fp-ts-esm/function");
const pipeable_1 = require("fp-ts-esm/pipeable");
const _ = __importStar(require("./internal.js"));
// -------------------------------------------------------------------------------------
// constructors
// -------------------------------------------------------------------------------------
/**
* @category constructors
* @since 2.3.8
*/
exports.iso = _.iso;
/**
* @category constructors
* @since 2.3.0
*/
const id = () => (0, exports.iso)(function_1.identity, function_1.identity);
exports.id = id;
// -------------------------------------------------------------------------------------
// converters
// -------------------------------------------------------------------------------------
/**
* View an `Iso` as a `Lens`.
*
* @category converters
* @since 2.3.0
*/
exports.asLens = _.isoAsLens;
/**
* View an `Iso` as a `Prism`.
*
* @category converters
* @since 2.3.0
*/
exports.asPrism = _.isoAsPrism;
/**
* View an `Iso` as a `Optional`.
*
* @category converters
* @since 2.3.0
*/
exports.asOptional = _.isoAsOptional;
/**
* View an `Iso` as a `Traversal`.
*
* @category converters
* @since 2.3.0
*/
exports.asTraversal = _.isoAsTraversal;
// -------------------------------------------------------------------------------------
// compositions
// -------------------------------------------------------------------------------------
/**
* Compose an `Iso` with an `Iso`.
*
* @category compositions
* @since 2.3.0
*/
const compose = (ab) => (sa) => (0, exports.iso)((0, function_1.flow)(sa.get, ab.get), (0, function_1.flow)(ab.reverseGet, sa.reverseGet));
exports.compose = compose;
/**
* Alias of `compose`.
*
* @category compositions
* @since 2.3.8
*/
exports.composeIso = exports.compose;
/**
* Compose an `Iso` with a `Lens`.
*
* @category compositions
* @since 2.3.8
*/
const composeLens = (ab) => (0, function_1.flow)(exports.asLens, _.lensComposeLens(ab));
exports.composeLens = composeLens;
/**
* Compose an `Iso` with a `Prism`.
*
* @category compositions
* @since 2.3.8
*/
const composePrism = (ab) => (0, function_1.flow)(exports.asPrism, _.prismComposePrism(ab));
exports.composePrism = composePrism;
/**
* Compose an `Iso` with a `Optional`.
*
* @category compositions
* @since 2.3.8
*/
const composeOptional = (ab) => (0, function_1.flow)(exports.asOptional, _.optionalComposeOptional(ab));
exports.composeOptional = composeOptional;
/**
* Compose an `Iso` with a `Traversal`.
*
* @category compositions
* @since 2.3.8
*/
const composeTraversal = (ab) => (0, function_1.flow)(exports.asTraversal, _.traversalComposeTraversal(ab));
exports.composeTraversal = composeTraversal;
// -------------------------------------------------------------------------------------
// combinators
// -------------------------------------------------------------------------------------
/**
* @category constructors
* @since 2.3.0
*/
const reverse = (sa) => (0, exports.iso)(sa.reverseGet, sa.get);
exports.reverse = reverse;
/**
* @category combinators
* @since 2.3.0
*/
const modify = (f) => (sa) => (s) => sa.reverseGet(f(sa.get(s)));
exports.modify = modify;
function modifyF(F) {
return (f) => (sa) => (s) => (0, pipeable_1.pipe)(sa.get(s), f, (fa) => F.map(fa, sa.reverseGet));
}
exports.modifyF = modifyF;
/**
* Return a `Prism` from a `Iso` focused on a nullable value.
*
* @category combinators
* @since 2.3.8
*/
const fromNullable = (sa) => (0, exports.composePrism)(_.prismFromNullable())(sa);
exports.fromNullable = fromNullable;
function filter(predicate) {
return (0, exports.composePrism)(_.prismFromPredicate(predicate));
}
exports.filter = filter;
/**
* Return a `Lens` from a `Iso` and a prop.
*
* @category combinators
* @since 2.3.8
*/
const prop = (prop) => (0, function_1.flow)(exports.asLens, _.lensProp(prop));
exports.prop = prop;
/**
* Return a `Lens` from a `Iso` and a list of props.
*
* @category combinators
* @since 2.3.8
*/
const props = (...props) => (0, function_1.flow)(exports.asLens, _.lensProps(...props));
exports.props = props;
/**
* Return a `Lens` from a `Iso` focused on a component of a tuple.
*
* @category combinators
* @since 2.3.8
*/
const component = (prop) => (0, function_1.flow)(exports.asLens, _.lensComponent(prop));
exports.component = component;
/**
* Return a `Optional` from a `Iso` focused on an index of a `ReadonlyArray`.
*
* @category combinators
* @since 2.3.8
*/
const index = (i) => (0, function_1.flow)(exports.asOptional, _.optionalIndex(i));
exports.index = index;
/**
* Return a `Optional` from a `Iso` focused on an index of a `ReadonlyNonEmptyArray`.
*
* @category combinators
* @since 2.3.8
*/
const indexNonEmpty = (i) => (0, function_1.flow)(exports.asOptional, _.optionalIndexNonEmpty(i));
exports.indexNonEmpty = indexNonEmpty;
/**
* Return a `Optional` from a `Iso` focused on a key of a `ReadonlyRecord`.
*
* @category combinators
* @since 2.3.8
*/
const key = (key) => (0, function_1.flow)(exports.asOptional, _.optionalKey(key));
exports.key = key;
/**
* Return a `Lens` from a `Iso` focused on a required key of a `ReadonlyRecord`.
*
* @category combinators
* @since 2.3.8
*/
const atKey = (key) => (0, function_1.flow)(exports.asLens, _.lensAtKey(key));
exports.atKey = atKey;
/**
* Return a `Prism` from a `Iso` focused on the `Some` of a `Option` type.
*
* @category combinators
* @since 2.3.8
*/
exports.some =
/*#__PURE__*/
(0, exports.composePrism)(/*#__PURE__*/ _.prismSome());
/**
* Return a `Prism` from a `Iso` focused on the `Right` of a `Either` type.
*
* @category combinators
* @since 2.3.8
*/
exports.right =
/*#__PURE__*/
(0, exports.composePrism)(/*#__PURE__*/ _.prismRight());
/**
* Return a `Prism` from a `Iso` focused on the `Left` of a `Either` type.
*
* @category combinators
* @since 2.3.8
*/
exports.left =
/*#__PURE__*/
(0, exports.composePrism)(/*#__PURE__*/ _.prismLeft());
/**
* Return a `Traversal` from a `Iso` focused on a `Traversable`.
*
* @category combinators
* @since 2.3.8
*/
function traverse(T) {
return (0, function_1.flow)(exports.asTraversal, _.traversalTraverse(T));
}
exports.traverse = traverse;
function findFirst(predicate) {
return (0, exports.composeOptional)(_.optionalFindFirst(predicate));
}
exports.findFirst = findFirst;
function findFirstNonEmpty(predicate) {
return (0, exports.composeOptional)(_.optionalFindFirstNonEmpty(predicate));
}
exports.findFirstNonEmpty = findFirstNonEmpty;
// -------------------------------------------------------------------------------------
// pipeables
// -------------------------------------------------------------------------------------
/**
* @category Invariant
* @since 2.3.0
*/
const imap = (f, g) => (ea) => imap_(ea, f, g);
exports.imap = imap;
// -------------------------------------------------------------------------------------
// instances
// -------------------------------------------------------------------------------------
const imap_ = (ea, ab, ba) => (0, exports.iso)((0, function_1.flow)(ea.get, ab), (0, function_1.flow)(ba, ea.reverseGet));
/**
* @category instances
* @since 2.3.0
*/
exports.URI = 'monocle-ts/Iso';
/**
* @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: (ab, ea) => (0, exports.compose)(ab)(ea)
};
/**
* @category instances
* @since 2.3.0
*/
exports.Category = {
URI: exports.URI,
compose: exports.Semigroupoid.compose,
id: exports.id
};