monocle-ts-esm
Version:
A porting of scala monocle library to TypeScript
295 lines (294 loc) • 9.2 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.modifyOption = exports.set = exports.composeTraversal = exports.composeOptional = exports.composeLens = exports.composeIso = exports.composePrism = exports.compose = exports.asTraversal = exports.asOptional = exports.fromPredicate = exports.id = exports.prism = void 0;
const function_1 = require("fp-ts-esm/function");
const O = __importStar(require("fp-ts-esm/Option"));
const pipeable_1 = require("fp-ts-esm/pipeable");
const _ = __importStar(require("./internal.js"));
// -------------------------------------------------------------------------------------
// constructors
// -------------------------------------------------------------------------------------
/**
* @category constructors
* @since 2.3.8
*/
exports.prism = _.prism;
/**
* @category constructors
* @since 2.3.0
*/
const id = () => (0, exports.prism)(O.some, function_1.identity);
exports.id = id;
/**
* @category constructors
* @since 2.3.0
*/
exports.fromPredicate = _.prismFromPredicate;
// -------------------------------------------------------------------------------------
// converters
// -------------------------------------------------------------------------------------
/**
* View a `Prism` as a `Optional`.
*
* @category converters
* @since 2.3.0
*/
exports.asOptional = _.prismAsOptional;
/**
* View a `Prism` as a `Traversal`.
*
* @category converters
* @since 2.3.0
*/
exports.asTraversal = _.prismAsTraversal;
// -------------------------------------------------------------------------------------
// compositions
// -------------------------------------------------------------------------------------
/**
* Compose a `Prism` with a `Prism`.
*
* @category compositions
* @since 2.3.0
*/
exports.compose = _.prismComposePrism;
/**
* Alias of `compose`.
*
* @category compositions
* @since 2.3.8
*/
exports.composePrism = exports.compose;
/**
* Compose a `Prism` with a `Iso`.
*
* @category compositions
* @since 2.3.8
*/
exports.composeIso =
/*#__PURE__*/
(0, function_1.flow)(_.isoAsPrism, exports.compose);
/**
* Compose a `Prism` with a `Lens`.
*
* @category compositions
* @since 2.3.0
*/
exports.composeLens = _.prismComposeLens;
/**
* Compose a `Prism` with an `Optional`.
*
* @category compositions
* @since 2.3.0
*/
const composeOptional = (ab) => (0, function_1.flow)(exports.asOptional, _.optionalComposeOptional(ab));
exports.composeOptional = composeOptional;
/**
* Compose a `Prism` with an `Traversal`.
*
* @category compositions
* @since 2.3.8
*/
const composeTraversal = (ab) => (0, function_1.flow)(exports.asTraversal, _.traversalComposeTraversal(ab));
exports.composeTraversal = composeTraversal;
// -------------------------------------------------------------------------------------
// combinators
// -------------------------------------------------------------------------------------
/**
* @category combinators
* @since 2.3.0
*/
exports.set = _.prismSet;
/**
* @category combinators
* @since 2.3.0
*/
exports.modifyOption = _.prismModifyOption;
/**
* @category combinators
* @since 2.3.0
*/
exports.modify = _.prismModify;
function modifyF(F) {
return (f) => (sa) => (s) => (0, pipeable_1.pipe)(sa.getOption(s), O.fold(() => F.of(s), (a) => F.map(f(a), sa.reverseGet)));
}
exports.modifyF = modifyF;
/**
* Return a `Prism` from a `Prism` focused on a nullable value.
*
* @category combinators
* @since 2.3.3
*/
exports.fromNullable =
/*#__PURE__*/
(0, exports.compose)(/*#__PURE__*/ _.prismFromNullable());
function filter(predicate) {
return (0, exports.compose)(_.prismFromPredicate(predicate));
}
exports.filter = filter;
/**
* Return a `Optional` from a `Prism` and a prop.
*
* @category combinators
* @since 2.3.0
*/
const prop = (prop) => (0, exports.composeLens)((0, pipeable_1.pipe)(_.lensId(), _.lensProp(prop))); // TODO: simplify?
exports.prop = prop;
/**
* Return a `Optional` from a `Prism` and a list of props.
*
* @category combinators
* @since 2.3.0
*/
const props = (...props) => (0, exports.composeLens)((0, pipeable_1.pipe)(_.lensId(), _.lensProps(...props)));
exports.props = props;
/**
* Return a `Optional` from a `Prism` focused on a component of a tuple.
*
* @category combinators
* @since 2.3.0
*/
const component = (prop) => (0, exports.composeLens)((0, pipeable_1.pipe)(_.lensId(), _.lensComponent(prop)));
exports.component = component;
/**
* Return a `Optional` from a `Prism` focused on an index of a `ReadonlyArray`.
*
* @category combinators
* @since 2.3.0
*/
const index = (i) => (0, function_1.flow)(exports.asOptional, _.optionalIndex(i));
exports.index = index;
/**
* Return a `Optional` from a `Prism` 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 `Prism` focused on a key of a `ReadonlyRecord`.
*
* @category combinators
* @since 2.3.0
*/
const key = (key) => (0, function_1.flow)(exports.asOptional, _.optionalKey(key));
exports.key = key;
/**
* Return a `Optional` from a `Prism` focused on a required key of a `ReadonlyRecord`.
*
* @category combinators
* @since 2.3.0
*/
const atKey = (key) => (sa) => _.prismComposeLens(_.atReadonlyRecord().at(key))(sa);
exports.atKey = atKey;
/**
* Return a `Prism` from a `Prism` focused on the `Some` of a `Option` type.
*
* @category combinators
* @since 2.3.0
*/
exports.some =
/*#__PURE__*/
(0, exports.compose)(/*#__PURE__*/ _.prismSome());
/**
* Return a `Prism` from a `Prism` focused on the `Right` of a `Either` type.
*
* @category combinators
* @since 2.3.0
*/
exports.right =
/*#__PURE__*/
(0, exports.compose)(/*#__PURE__*/ _.prismRight());
/**
* Return a `Prism` from a `Prism` focused on the `Left` of a `Either` type.
*
* @category combinators
* @since 2.3.0
*/
exports.left =
/*#__PURE__*/
(0, exports.compose)(/*#__PURE__*/ _.prismLeft());
/**
* Return a `Traversal` from a `Prism` focused on a `Traversable`.
*
* @category combinators
* @since 2.3.0
*/
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.prism)((0, function_1.flow)(ea.getOption, O.map(ab)), (0, function_1.flow)(ba, ea.reverseGet));
/**
* @category instances
* @since 2.3.0
*/
exports.URI = 'monocle-ts/Prism';
/**
* @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
};