monocle-ts-esm
Version:
A porting of scala monocle library to TypeScript
286 lines (285 loc) • 9.12 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.setOption = exports.modifyOption = exports.composeTraversal = exports.composePrism = exports.composeLens = exports.composeIso = exports.composeOptional = exports.compose = exports.asTraversal = exports.id = exports.optional = 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.optional = _.optional;
/**
* @category constructors
* @since 2.3.0
*/
const id = () => (0, exports.optional)(O.some, function_1.constant);
exports.id = id;
// -------------------------------------------------------------------------------------
// converters
// -------------------------------------------------------------------------------------
/**
* View a `Optional` as a `Traversal`.
*
* @category converters
* @since 2.3.0
*/
exports.asTraversal = _.optionalAsTraversal;
// -------------------------------------------------------------------------------------
// compositions
// -------------------------------------------------------------------------------------
/**
* Compose a `Optional` with a `Optional`.
*
* @category compositions
* @since 2.3.0
*/
exports.compose = _.optionalComposeOptional;
/**
* Alias of `compose`.
*
* @category compositions
* @since 2.3.8
*/
exports.composeOptional = exports.compose;
/**
* Compose a `Optional` with a `Iso`.
*
* @category compositions
* @since 2.3.8
*/
exports.composeIso =
/*#__PURE__*/
(0, function_1.flow)(_.isoAsOptional, exports.compose);
/**
* Compose a `Optional` with a `Lens`.
*
* @category compositions
* @since 2.3.7
*/
exports.composeLens =
/*#__PURE__*/
(0, function_1.flow)(_.lensAsOptional, _.optionalComposeOptional);
/**
* Compose a `Optional` with a `Prism`.
*
* @category compositions
* @since 2.3.7
*/
exports.composePrism =
/*#__PURE__*/
(0, function_1.flow)(_.prismAsOptional, _.optionalComposeOptional);
/**
* Compose a `Optional` 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.modifyOption = _.optionalModifyOption;
/**
* @category combinators
* @since 2.3.7
*/
const setOption = (a) => (0, exports.modifyOption)(() => a);
exports.setOption = setOption;
/**
* @category combinators
* @since 2.3.0
*/
exports.modify = _.optionalModify;
function modifyF(F) {
return (f) => (sa) => (s) => (0, pipeable_1.pipe)(sa.getOption(s), O.fold(() => F.of(s),
// @ts-expect-error
(a) => F.map(f(a), (a) => sa.set(a)(s))));
}
exports.modifyF = modifyF;
/**
* Return an `Optional` from a `Optional` focused on a nullable value.
*
* @category combinators
* @since 2.3.3
*/
exports.fromNullable =
/*#__PURE__*/
(0, exports.compose)(/*#__PURE__*/ _.prismAsOptional(/*#__PURE__*/ _.prismFromNullable()));
function filter(predicate) {
return (0, exports.compose)(_.prismAsOptional(_.prismFromPredicate(predicate)));
}
exports.filter = filter;
/**
* Return a `Optional` from a `Optional` and a prop.
*
* @category combinators
* @since 2.3.0
*/
const prop = (prop) => (0, exports.compose)((0, pipeable_1.pipe)(_.lensId(), _.lensProp(prop), _.lensAsOptional));
exports.prop = prop;
/**
* Return a `Optional` from a `Optional` and a list of props.
*
* @category combinators
* @since 2.3.0
*/
const props = (...props) => (0, exports.compose)((0, pipeable_1.pipe)(_.lensId(), _.lensProps(...props), _.lensAsOptional));
exports.props = props;
/**
* Return a `Optional` from a `Optional` 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), _.lensAsOptional));
exports.component = component;
/**
* Return a `Optional` from a `Optional` focused on an index of a `ReadonlyArray`.
*
* @category combinators
* @since 2.3.0
*/
exports.index = _.optionalIndex;
/**
* Return a `Optional` from a `Optional` focused on an index of a `ReadonlyNonEmptyArray`.
*
* @category combinators
* @since 2.3.8
*/
exports.indexNonEmpty = _.optionalIndexNonEmpty;
/**
* Return a `Optional` from a `Optional` focused on a key of a `ReadonlyRecord`.
*
* @category combinators
* @since 2.3.0
*/
exports.key = _.optionalKey;
/**
* Return a `Optional` from a `Optional` 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)(_.lensAsOptional(_.atReadonlyRecord().at(key))));
exports.atKey = atKey;
/**
* Return a `Optional` from a `Optional` focused on the `Some` of a `Option` type.
*
* @category combinators
* @since 2.3.0
*/
exports.some =
/*#__PURE__*/
(0, exports.compose)(/*#__PURE__*/ _.prismAsOptional(/*#__PURE__*/ _.prismSome()));
/**
* Return a `Optional` from a `Optional` focused on the `Right` of a `Either` type.
*
* @category combinators
* @since 2.3.0
*/
exports.right =
/*#__PURE__*/
(0, exports.compose)(/*#__PURE__*/ _.prismAsOptional(/*#__PURE__*/ _.prismRight()));
/**
* Return a `Optional` from a `Optional` focused on the `Left` of a `Either` type.
*
* @category combinators
* @since 2.3.0
*/
exports.left =
/*#__PURE__*/
(0, exports.compose)(/*#__PURE__*/ _.prismAsOptional(/*#__PURE__*/ _.prismLeft()));
/**
* Return a `Traversal` from a `Optional` 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.compose)(_.optionalFindFirst(predicate));
}
exports.findFirst = findFirst;
function findFirstNonEmpty(predicate) {
return (0, exports.compose)(_.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.optional)((0, function_1.flow)(ea.getOption, O.map(ab)), (0, function_1.flow)(ba, ea.set));
/**
* @category instances
* @since 2.3.0
*/
exports.URI = 'monocle-ts/Optional';
/**
* @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
};