UNPKG

monocle-ts-esm

Version:

A porting of scala monocle library to TypeScript

278 lines (277 loc) 8.97 kB
"use strict"; 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.URI = exports.getAll = exports.fold = exports.foldMap = 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.set = exports.modify = exports.composeOptional = exports.composePrism = exports.composeLens = exports.composeIso = exports.composeTraversal = exports.compose = exports.fromTraversable = exports.id = exports.traversal = void 0; const C = __importStar(require("fp-ts-esm/Const")); const function_1 = require("fp-ts-esm/function"); const pipeable_1 = require("fp-ts-esm/pipeable"); const RA = __importStar(require("fp-ts-esm/ReadonlyArray")); const _ = __importStar(require("./internal.js")); // ------------------------------------------------------------------------------------- // constructors // ------------------------------------------------------------------------------------- /** * @category constructors * @since 2.3.8 */ exports.traversal = _.traversal; /** * @category constructors * @since 2.3.0 */ const id = () => (0, exports.traversal)((_) => (f) => f); exports.id = id; /** * Create a `Traversal` from a `Traversable`. * * @category constructor * @since 2.3.0 */ exports.fromTraversable = _.fromTraversable; // ------------------------------------------------------------------------------------- // compositions // ------------------------------------------------------------------------------------- /** * Compose a `Traversal` with a `Traversal`. * * @category compositions * @since 2.3.0 */ exports.compose = _.traversalComposeTraversal; /** * Alias of `compose`. * * @category compositions * @since 2.3.8 */ exports.composeTraversal = exports.compose; /** * Compose a `Traversal` with a `Iso`. * * @category compositions * @since 2.3.8 */ exports.composeIso = /*#__PURE__*/ (0, function_1.flow)(_.isoAsTraversal, exports.compose); /** * Compose a `Traversal` with a `Lens`. * * @category compositions * @since 2.3.8 */ exports.composeLens = /*#__PURE__*/ (0, function_1.flow)(_.lensAsTraversal, _.traversalComposeTraversal); /** * Compose a `Traversal` with a `Prism`. * * @category compositions * @since 2.3.8 */ exports.composePrism = /*#__PURE__*/ (0, function_1.flow)(_.prismAsTraversal, _.traversalComposeTraversal); /** * Compose a `Traversal` with a `Optional`. * * @category compositions * @since 2.3.8 */ exports.composeOptional = /*#__PURE__*/ (0, function_1.flow)(_.optionalAsTraversal, _.traversalComposeTraversal); // ------------------------------------------------------------------------------------- // combinators // ------------------------------------------------------------------------------------- /** * @category combinators * @since 2.3.0 */ const modify = (f) => (sa) => sa.modifyF(_.ApplicativeIdentity)(f); exports.modify = modify; /** * @category combinators * @since 2.3.0 */ const set = (a) => (0, exports.modify)(() => a); exports.set = set; /** * Return a `Traversal` from a `Traversal` focused on a nullable value. * * @category combinators * @since 2.3.0 */ const fromNullable = (sa) => (0, exports.composePrism)(_.prismFromNullable())(sa); exports.fromNullable = fromNullable; function filter(predicate) { return (0, exports.compose)(_.prismAsTraversal(_.prismFromPredicate(predicate))); } exports.filter = filter; /** * Return a `Traversal` from a `Traversal` and a prop. * * @category combinators * @since 2.3.0 */ const prop = (prop) => (0, exports.compose)((0, pipeable_1.pipe)(_.lensId(), _.lensProp(prop), _.lensAsTraversal)); exports.prop = prop; /** * Return a `Traversal` from a `Traversal` and a list of props. * * @category combinators * @since 2.3.0 */ const props = (...props) => (0, exports.compose)((0, pipeable_1.pipe)(_.lensId(), _.lensProps(...props), _.lensAsTraversal)); exports.props = props; /** * Return a `Traversal` from a `Traversal` focused on a component of a tuple. * * @category combinators * @since 2.3.0 */ const component = (prop) => (0, exports.compose)((0, pipeable_1.pipe)(_.lensId(), _.lensComponent(prop), _.lensAsTraversal)); exports.component = component; /** * Return a `Traversal` from a `Traversal` focused on an index of a `ReadonlyArray`. * * @category combinators * @since 2.3.0 */ const index = (i) => (sa) => (0, pipeable_1.pipe)(sa, (0, exports.compose)(_.optionalAsTraversal(_.indexReadonlyArray().index(i)))); exports.index = index; /** * @category combinators * @since 2.3.8 */ const indexNonEmpty = (i) => (sa) => (0, pipeable_1.pipe)(sa, (0, exports.compose)(_.optionalAsTraversal(_.indexReadonlyNonEmptyArray().index(i)))); exports.indexNonEmpty = indexNonEmpty; /** * Return a `Traversal` from a `Traversal` focused on a key of a `ReadonlyRecord`. * * @category combinators * @since 2.3.0 */ const key = (key) => (sa) => (0, pipeable_1.pipe)(sa, (0, exports.compose)(_.optionalAsTraversal(_.indexReadonlyRecord().index(key)))); exports.key = key; /** * Return a `Traversal` from a `Traversal` focused on a required key of a `ReadonlyRecord`. * * @category combinators * @since 2.3.0 */ const atKey = (key) => (sa) => (0, pipeable_1.pipe)(sa, (0, exports.compose)(_.lensAsTraversal(_.atReadonlyRecord().at(key)))); exports.atKey = atKey; /** * Return a `Traversal` from a `Traversal` focused on the `Some` of a `Option` type. * * @category combinators * @since 2.3.0 */ exports.some = /*#__PURE__*/ (0, exports.compose)(/*#__PURE__*/ _.prismAsTraversal(/*#__PURE__*/ _.prismSome())); /** * Return a `Traversal` from a `Traversal` focused on the `Right` of a `Either` type. * * @category combinators * @since 2.3.0 */ exports.right = /*#__PURE__*/ (0, exports.compose)(/*#__PURE__*/ _.prismAsTraversal(/*#__PURE__*/ _.prismRight())); /** * Return a `Traversal` from a `Traversal` focused on the `Left` of a `Either` type. * * @category combinators * @since 2.3.0 */ exports.left = /*#__PURE__*/ (0, exports.compose)(/*#__PURE__*/ _.prismAsTraversal(/*#__PURE__*/ _.prismLeft())); /** * Return a `Traversal` from a `Traversal` focused on a `Traversable`. * * @category combinators * @since 2.3.0 */ exports.traverse = _.traversalTraverse; function findFirst(predicate) { return (0, exports.composeOptional)(_.optionalFindFirst(predicate)); } exports.findFirst = findFirst; function findFirstNonEmpty(predicate) { return (0, exports.composeOptional)(_.optionalFindFirstNonEmpty(predicate)); } exports.findFirstNonEmpty = findFirstNonEmpty; /** * Map each target to a `Monoid` and combine the results. * * @category combinators * @since 2.3.0 */ const foldMap = (M) => (f) => (sa) => sa.modifyF(C.getApplicative(M))((a) => C.make(f(a))); exports.foldMap = foldMap; /** * Map each target to a `Monoid` and combine the results. * * @category combinators * @since 2.3.0 */ const fold = (M) => (0, exports.foldMap)(M)(function_1.identity); exports.fold = fold; /** * Get all the targets of a `Traversal`. * * @category combinators * @since 2.3.0 */ const getAll = (s) => (sa) => (0, exports.foldMap)(RA.getMonoid())(RA.of)(sa)(s); exports.getAll = getAll; // ------------------------------------------------------------------------------------- // instances // ------------------------------------------------------------------------------------- /** * @category instances * @since 2.3.0 */ exports.URI = 'monocle-ts/Traversal'; /** * @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 };