monocle-ts
Advanced tools
Comparing version 2.3.7 to 2.3.8
@@ -16,2 +16,84 @@ # Changelog | ||
# 2.3.8 | ||
Experimental modules require `fp-ts@^2.5.0`. | ||
- **Experimental** | ||
- `At` | ||
- add `at` constructor (@gcanti) | ||
- `Iso` | ||
- add `iso` constructor (@gcanti) | ||
- add `composeLens` (@gcanti) | ||
- add `composePrism` (@gcanti) | ||
- add `composeOptional` (@gcanti) | ||
- add `composeTraversal` (@gcanti) | ||
- add `fromNullable` (@gcanti) | ||
- add `filter` (@gcanti) | ||
- add `fromNullable` (@gcanti) | ||
- add `prop` (@gcanti) | ||
- add `props` (@gcanti) | ||
- add `component` (@gcanti) | ||
- add `index` (@gcanti) | ||
- add `indexNonEmpty` (@gcanti) | ||
- add `key` (@gcanti) | ||
- add `atKey` (@gcanti) | ||
- add `some` (@gcanti) | ||
- add `right` (@gcanti) | ||
- add `left` (@gcanti) | ||
- add `traverse` (@gcanti) | ||
- add `findFirst` (@gcanti) | ||
- add `findFirstNonEmpty` (@gcanti) | ||
- add `composeIso` (@gcanti) | ||
- add `Semigroupoid` (@gcanti) | ||
- (\*) rename `invariantIso` to `Invariant` (@gcanti) | ||
- (\*) rename `categoryIso` to `Category` (@gcanti) | ||
- `Ix` | ||
- add `index` constructor (@gcanti) | ||
- add `indexReadonlyNonEmptyArray` (@gcanti) | ||
- `Lens` | ||
- add `lens` constructor (@gcanti) | ||
- add `composeIso` (@gcanti) | ||
- add `composeTraversal` (@gcanti) | ||
- add `indexNonEmpty` (@gcanti) | ||
- add `findFirstNonEmpty` (@gcanti) | ||
- add `composeLens` (@gcanti) | ||
- add `Semigroupoid` (@gcanti) | ||
- (\*) rename `invariantIso` to `Invariant` (@gcanti) | ||
- (\*) rename `categoryIso` to `Category` (@gcanti) | ||
- `Prism` | ||
- add `prism` constructor (@gcanti) | ||
- add `composeIso` (@gcanti) | ||
- add `composeTraversal` (@gcanti) | ||
- add `indexNonEmpty` (@gcanti) | ||
- add `findFirstNonEmpty` (@gcanti) | ||
- add `composePrism` (@gcanti) | ||
- add `Semigroupoid` (@gcanti) | ||
- (\*) rename `invariantIso` to `Invariant` (@gcanti) | ||
- (\*) rename `categoryIso` to `Category` (@gcanti) | ||
- `Optional` | ||
- add `optional` constructor (@gcanti) | ||
- add `composeIso` (@gcanti) | ||
- add `composeTraversal` (@gcanti) | ||
- add `indexNonEmpty` (@gcanti) | ||
- add `findFirstNonEmpty` (@gcanti) | ||
- add `composeOptional` (@gcanti) | ||
- add `Semigroupoid` (@gcanti) | ||
- (\*) rename `invariantIso` to `Invariant` (@gcanti) | ||
- (\*) rename `categoryIso` to `Category` (@gcanti) | ||
- `Traversal` | ||
- add `traversal` constructor (@gcanti) | ||
- add `composeIso` (@gcanti) | ||
- add `composeLens` (@gcanti) | ||
- add `composePrism` (@gcanti) | ||
- add `composeOptional` (@gcanti) | ||
- add `findFirst` (@gcanti) | ||
- add `findFirstNonEmpty` (@gcanti) | ||
- add `fromNullable` (@gcanti) | ||
- add `indexNonEmpty` (@gcanti) | ||
- add `composeTraversal` (@gcanti) | ||
- add `Semigroupoid` (@gcanti) | ||
- (\*) rename `categoryIso` to `Category` (@gcanti) | ||
(\*) breaking change | ||
# 2.3.7 | ||
@@ -18,0 +100,0 @@ |
@@ -12,2 +12,3 @@ /** | ||
import * as O from 'fp-ts/es6/Option' | ||
import { ReadonlyRecord } from 'fp-ts/es6/ReadonlyRecord' | ||
import { Iso } from './Iso' | ||
@@ -24,3 +25,8 @@ import { Lens } from './Lens' | ||
/** | ||
* Lift an instance of `At` using an `Iso` | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const at: <S, I, A>(at: At<S, I, A>['at']) => At<S, I, A> | ||
/** | ||
* Lift an instance of `At` using an `Iso`. | ||
* | ||
@@ -35,3 +41,3 @@ * @category constructors | ||
*/ | ||
export declare const atReadonlyRecord: <A = never>() => At<Readonly<Record<string, A>>, string, Option<A>> | ||
export declare const atReadonlyRecord: <A = never>() => At<ReadonlyRecord<string, A>, string, Option<A>> | ||
/** | ||
@@ -54,2 +60,2 @@ * @category constructors | ||
*/ | ||
export declare const atRecord: <A = never>() => At<Readonly<Record<string, A>>, string, Option<A>> | ||
export declare const atRecord: <A = never>() => At<ReadonlyRecord<string, A>, string, Option<A>> |
@@ -10,3 +10,8 @@ import * as O from 'fp-ts/es6/Option'; | ||
/** | ||
* Lift an instance of `At` using an `Iso` | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export var at = _.at; | ||
/** | ||
* Lift an instance of `At` using an `Iso`. | ||
* | ||
@@ -16,5 +21,5 @@ * @category constructors | ||
*/ | ||
export var fromIso = function (iso) { return function (sia) { return ({ | ||
at: function (i) { return pipe(iso, _.isoAsLens, _.lensComposeLens(sia.at(i))); } | ||
}); }; }; | ||
export var fromIso = function (iso) { return function (sia) { | ||
return at(function (i) { return pipe(iso, _.isoAsLens, _.lensComposeLens(sia.at(i))); }); | ||
}; }; | ||
/** | ||
@@ -29,13 +34,12 @@ * @category constructors | ||
*/ | ||
export var atReadonlyMap = function (E) { return function () { | ||
export var atReadonlyMap = function (E) { | ||
var lookupE = RM.lookup(E); | ||
var deleteAtE = RM.deleteAt(E); | ||
var insertAtE = RM.insertAt(E); | ||
return { | ||
at: function (key) { return ({ | ||
get: lookupE(key), | ||
set: O.fold(function () { return deleteAtE(key); }, function (a) { return insertAtE(key, a); }) | ||
}); } | ||
return function () { | ||
return at(function (key) { | ||
return _.lens(function (s) { return lookupE(key, s); }, O.fold(function () { return deleteAtE(key); }, function (a) { return insertAtE(key, a); })); | ||
}); | ||
}; | ||
}; }; | ||
}; | ||
/** | ||
@@ -49,12 +53,7 @@ * @category constructors | ||
var removeE = RS.remove(E); | ||
return { | ||
at: function (a) { | ||
var insert = insertE(a); | ||
var remove = removeE(a); | ||
return { | ||
get: elemE(a), | ||
set: function (b) { return function (s) { return (b ? insert(s) : remove(s)); }; } | ||
}; | ||
} | ||
}; | ||
return at(function (a) { | ||
var insert = insertE(a); | ||
var remove = removeE(a); | ||
return _.lens(function (s) { return elemE(a, s); }, function (b) { return function (s) { return (b ? insert(s) : remove(s)); }; }); | ||
}); | ||
}; | ||
@@ -61,0 +60,0 @@ // ------------------------------------------------------------------------------------- |
@@ -5,2 +5,3 @@ /** | ||
import { Option } from 'fp-ts/es6/Option' | ||
import { ReadonlyRecord } from 'fp-ts/es6/ReadonlyRecord' | ||
import { At } from '..' | ||
@@ -11,2 +12,2 @@ /** | ||
*/ | ||
export declare const atReadonlyRecord: <A = never>() => At<Readonly<Record<string, A>>, string, Option<A>> | ||
export declare const atReadonlyRecord: <A = never>() => At<ReadonlyRecord<string, A>, string, Option<A>> |
@@ -989,3 +989,3 @@ import { Foldable, Foldable1, Foldable2, Foldable3 } from 'fp-ts/es6/Foldable' | ||
readonly all: (p: Predicate<A>) => Predicate<S> | ||
private foldMapFirst | ||
private readonly foldMapFirst | ||
constructor(foldMap: <M>(M: Monoid<M>) => (f: (a: A) => M) => (s: S) => M) | ||
@@ -992,0 +992,0 @@ /** |
/** | ||
* @since 2.2.0 | ||
*/ | ||
import { ReadonlyRecord } from 'fp-ts/es6/ReadonlyRecord' | ||
import { Index } from '..' | ||
@@ -9,2 +10,2 @@ /** | ||
*/ | ||
export declare const indexReadonlyRecord: <A = never>() => Index<Readonly<Record<string, A>>, string, A> | ||
export declare const indexReadonlyRecord: <A = never>() => Index<ReadonlyRecord<string, A>, string, A> |
@@ -1,3 +0,10 @@ | ||
import * as A from 'fp-ts/es6/Array'; // TODO: replace with ReadonlyArray in v3 | ||
import * as R from 'fp-ts/es6/Record'; // TODO: replace with ReadonlyRecord in v3 | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
import * as RA from 'fp-ts/es6/ReadonlyArray'; | ||
import * as RR from 'fp-ts/es6/ReadonlyRecord'; | ||
import { constant, flow, identity } from 'fp-ts/es6/function'; | ||
@@ -11,11 +18,20 @@ import * as O from 'fp-ts/es6/Option'; | ||
/** @internal */ | ||
export var isoAsLens = function (sa) { return ({ | ||
get: sa.get, | ||
set: flow(sa.reverseGet, constant) | ||
export var iso = function (get, reverseGet) { return ({ | ||
get: get, | ||
reverseGet: reverseGet | ||
}); }; | ||
/** @internal */ | ||
export var isoAsOptional = function (sa) { return ({ | ||
getOption: flow(sa.get, O.some), | ||
set: flow(sa.reverseGet, constant) | ||
}); }; | ||
export var isoAsLens = function (sa) { return lens(sa.get, flow(sa.reverseGet, constant)); }; | ||
/** @internal */ | ||
export var isoAsPrism = function (sa) { return prism(flow(sa.get, O.some), sa.reverseGet); }; | ||
/** @internal */ | ||
export var isoAsOptional = function (sa) { | ||
return optional(flow(sa.get, O.some), flow(sa.reverseGet, constant)); | ||
}; | ||
/** @internal */ | ||
export var isoAsTraversal = function (sa) { | ||
return traversal(function (F) { return function (f) { return function (s) { | ||
return F.map(f(sa.get(s)), function (a) { return sa.reverseGet(a); }); | ||
}; }; }); | ||
}; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -25,16 +41,18 @@ // Lens | ||
/** @internal */ | ||
export var lensAsOptional = function (sa) { return ({ | ||
getOption: flow(sa.get, O.some), | ||
set: sa.set | ||
}); }; | ||
export var lens = function (get, set) { return ({ get: get, set: set }); }; | ||
/** @internal */ | ||
export var lensAsTraversal = function (sa) { return ({ | ||
modifyF: function (F) { return function (f) { return function (s) { return F.map(f(sa.get(s)), function (a) { return sa.set(a)(s); }); }; }; } | ||
}); }; | ||
export var lensAsOptional = function (sa) { return optional(flow(sa.get, O.some), sa.set); }; | ||
/** @internal */ | ||
export var lensComposeLens = function (ab) { return function (sa) { return ({ | ||
get: function (s) { return ab.get(sa.get(s)); }, | ||
set: function (b) { return function (s) { return sa.set(ab.set(b)(sa.get(s)))(s); }; } | ||
}); }; }; | ||
export var lensAsTraversal = function (sa) { | ||
return traversal(function (F) { return function (f) { return function (s) { return F.map(f(sa.get(s)), function (a) { return sa.set(a)(s); }); }; }; }); | ||
}; | ||
/** @internal */ | ||
export var lensComposeLens = function (ab) { return function (sa) { | ||
return lens(function (s) { return ab.get(sa.get(s)); }, function (b) { return function (s) { return sa.set(ab.set(b)(sa.get(s)))(s); }; }); | ||
}; }; | ||
/** @internal */ | ||
export var prismComposePrism = function (ab) { return function (sa) { | ||
return prism(flow(sa.getOption, O.chain(ab.getOption)), flow(ab.reverseGet, sa.reverseGet)); | ||
}; }; | ||
/** @internal */ | ||
export var lensComposePrism = function (ab) { return function (sa) { | ||
@@ -44,18 +62,14 @@ return optionalComposeOptional(prismAsOptional(ab))(lensAsOptional(sa)); | ||
/** @internal */ | ||
export var lensId = function () { return ({ | ||
get: identity, | ||
set: constant | ||
}); }; | ||
export var lensId = function () { return lens(identity, constant); }; | ||
/** @internal */ | ||
export var lensProp = function (prop) { return function (lens) { return ({ | ||
get: function (s) { return lens.get(s)[prop]; }, | ||
set: function (ap) { return function (s) { | ||
export var lensProp = function (prop) { return function (sa) { | ||
return lens(function (s) { return sa.get(s)[prop]; }, function (ap) { return function (s) { | ||
var _a; | ||
var oa = lens.get(s); | ||
var oa = sa.get(s); | ||
if (ap === oa[prop]) { | ||
return s; | ||
} | ||
return lens.set(Object.assign({}, oa, (_a = {}, _a[prop] = ap, _a)))(s); | ||
}; } | ||
}); }; }; | ||
return sa.set(Object.assign({}, oa, (_a = {}, _a[prop] = ap, _a)))(s); | ||
}; }); | ||
}; }; | ||
/** @internal */ | ||
@@ -67,5 +81,5 @@ export var lensProps = function () { | ||
} | ||
return function (lens) { return ({ | ||
get: function (s) { | ||
var a = lens.get(s); | ||
return function (sa) { | ||
return lens(function (s) { | ||
var a = sa.get(s); | ||
var r = {}; | ||
@@ -77,20 +91,18 @@ for (var _i = 0, props_1 = props; _i < props_1.length; _i++) { | ||
return r; | ||
}, | ||
set: function (a) { return function (s) { | ||
var oa = lens.get(s); | ||
}, function (a) { return function (s) { | ||
var oa = sa.get(s); | ||
for (var _i = 0, props_2 = props; _i < props_2.length; _i++) { | ||
var k = props_2[_i]; | ||
if (a[k] !== oa[k]) { | ||
return lens.set(Object.assign({}, oa, a))(s); | ||
return sa.set(Object.assign({}, oa, a))(s); | ||
} | ||
} | ||
return s; | ||
}; } | ||
}); }; | ||
}; }); | ||
}; | ||
}; | ||
/** @internal */ | ||
export var lensComponent = function (prop) { return function (lens) { return ({ | ||
get: function (s) { return lens.get(s)[prop]; }, | ||
set: function (ap) { return function (s) { | ||
var oa = lens.get(s); | ||
export var lensComponent = function (prop) { return function (sa) { | ||
return lens(function (s) { return sa.get(s)[prop]; }, function (ap) { return function (s) { | ||
var oa = sa.get(s); | ||
if (ap === oa[prop]) { | ||
@@ -101,5 +113,9 @@ return s; | ||
copy[prop] = ap; | ||
return lens.set(copy)(s); | ||
}; } | ||
}); }; }; | ||
return sa.set(copy)(s); | ||
}; }); | ||
}; }; | ||
/** @internal */ | ||
export var lensAtKey = function (key) { return function (sa) { | ||
return pipe(sa, lensComposeLens(atReadonlyRecord().at(key))); | ||
}; }; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -109,12 +125,11 @@ // Prism | ||
/** @internal */ | ||
export var prismAsOptional = function (sa) { return ({ | ||
getOption: sa.getOption, | ||
set: function (a) { return prismSet(a)(sa); } | ||
}); }; | ||
export var prism = function (getOption, reverseGet) { return ({ getOption: getOption, reverseGet: reverseGet }); }; | ||
/** @internal */ | ||
export var prismAsTraversal = function (sa) { return ({ | ||
modifyF: function (F) { return function (f) { return function (s) { | ||
export var prismAsOptional = function (sa) { return optional(sa.getOption, function (a) { return prismSet(a)(sa); }); }; | ||
/** @internal */ | ||
export var prismAsTraversal = function (sa) { | ||
return traversal(function (F) { return function (f) { return function (s) { | ||
return pipe(sa.getOption(s), O.fold(function () { return F.of(s); }, function (a) { return F.map(f(a), function (a) { return prismSet(a)(sa)(s); }); })); | ||
}; }; } | ||
}); }; | ||
}; }; }); | ||
}; | ||
/** @internal */ | ||
@@ -141,28 +156,16 @@ export var prismModifyOption = function (f) { return function (sa) { return function (s) { | ||
/** @internal */ | ||
export var prismFromNullable = function () { return ({ | ||
getOption: O.fromNullable, | ||
reverseGet: identity | ||
}); }; | ||
export var prismFromNullable = function () { return prism(O.fromNullable, identity); }; | ||
/** @internal */ | ||
export function prismFromPredicate(predicate) { | ||
return { | ||
getOption: O.fromPredicate(predicate), | ||
reverseGet: identity | ||
}; | ||
} | ||
export var prismFromPredicate = function (predicate) { | ||
return prism(O.fromPredicate(predicate), identity); | ||
}; | ||
/** @internal */ | ||
export var prismSome = function () { return ({ | ||
getOption: identity, | ||
reverseGet: O.some | ||
}); }; | ||
export var prismSome = function () { return prism(identity, O.some); }; | ||
/** @internal */ | ||
export var prismRight = function () { return ({ | ||
getOption: O.fromEither, | ||
reverseGet: E.right | ||
}); }; | ||
export var prismRight = function () { return prism(O.fromEither, E.right); }; | ||
/** @internal */ | ||
export var prismLeft = function () { return ({ | ||
getOption: function (s) { return (E.isLeft(s) ? O.some(s.left) : O.none); }, | ||
reverseGet: E.left | ||
}); }; | ||
export var prismLeft = function () { | ||
return prism(function (s) { return (E.isLeft(s) ? O.some(s.left) : O.none); }, // TODO: replace with E.getLeft in v3 | ||
E.left); | ||
}; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -172,8 +175,13 @@ // Optional | ||
/** @internal */ | ||
export var optionalAsTraversal = function (sa) { return ({ | ||
modifyF: function (F) { return function (f) { return function (s) { | ||
return pipe(sa.getOption(s), O.fold(function () { return F.of(s); }, function (a) { return F.map(f(a), function (a) { return sa.set(a)(s); }); })); | ||
}; }; } | ||
export var optional = function (getOption, set) { return ({ | ||
getOption: getOption, | ||
set: set | ||
}); }; | ||
/** @internal */ | ||
export var optionalAsTraversal = function (sa) { | ||
return traversal(function (F) { return function (f) { return function (s) { | ||
return pipe(sa.getOption(s), O.fold(function () { return F.of(s); }, function (a) { return F.map(f(a), function (a) { return sa.set(a)(s); }); })); | ||
}; }; }); | ||
}; | ||
/** @internal */ | ||
export var optionalModifyOption = function (f) { return function (optional) { return function (s) { | ||
@@ -193,14 +201,37 @@ return pipe(optional.getOption(s), O.map(function (a) { | ||
/** @internal */ | ||
export var optionalComposeOptional = function (ab) { return function (sa) { return ({ | ||
getOption: flow(sa.getOption, O.chain(ab.getOption)), | ||
set: function (b) { return optionalModify(ab.set(b))(sa); } | ||
}); }; }; | ||
var findFirstMutable = function (predicate) { return ({ | ||
getOption: A.findFirst(predicate), | ||
set: function (a) { return function (s) { | ||
return pipe(A.findIndex(predicate)(s), O.fold(function () { return s; }, function (i) { return A.unsafeUpdateAt(i, a, s); })); | ||
}; } | ||
}); }; | ||
export var optionalComposeOptional = function (ab) { return function (sa) { | ||
return optional(flow(sa.getOption, O.chain(ab.getOption)), function (b) { return optionalModify(ab.set(b))(sa); }); | ||
}; }; | ||
/** @internal */ | ||
export var findFirst = findFirstMutable; | ||
export var optionalIndex = function (i) { return function (sa) { | ||
return pipe(sa, optionalComposeOptional(indexReadonlyArray().index(i))); | ||
}; }; | ||
/** @internal */ | ||
export var optionalIndexNonEmpty = function (i) { return function (sa) { return pipe(sa, optionalComposeOptional(indexReadonlyNonEmptyArray().index(i))); }; }; | ||
/** @internal */ | ||
export var optionalKey = function (key) { return function (sa) { | ||
return pipe(sa, optionalComposeOptional(indexReadonlyRecord().index(key))); | ||
}; }; | ||
/** @internal */ | ||
export var optionalFindFirst = function (predicate) { | ||
return optional(RA.findFirst(predicate), function (a) { return function (s) { | ||
return pipe(RA.findIndex(predicate)(s), O.fold(function () { return s; }, function (i) { return RA.unsafeUpdateAt(i, a, s); })); | ||
}; }); | ||
}; | ||
var unsafeUpdateAt = function (i, a, as) { | ||
if (as[i] === a) { | ||
return as; | ||
} | ||
else { | ||
var xs = __spreadArrays([as[0]], as.slice(1)); | ||
xs[i] = a; | ||
return xs; | ||
} | ||
}; | ||
/** @internal */ | ||
export var optionalFindFirstNonEmpty = function (predicate) { | ||
return optional(RA.findFirst(predicate), function (a) { return function (as) { | ||
return pipe(RA.findIndex(predicate)(as), O.fold(function () { return as; }, function (i) { return unsafeUpdateAt(i, a, as); })); | ||
}; }); | ||
}; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -210,45 +241,54 @@ // Traversal | ||
/** @internal */ | ||
export var traversal = function (modifyF) { return ({ | ||
modifyF: modifyF | ||
}); }; | ||
/** @internal */ | ||
export function traversalComposeTraversal(ab) { | ||
return function (sa) { return ({ | ||
modifyF: function (F) { return function (f) { return sa.modifyF(F)(ab.modifyF(F)(f)); }; } | ||
}); }; | ||
return function (sa) { return traversal(function (F) { return function (f) { return sa.modifyF(F)(ab.modifyF(F)(f)); }; }); }; | ||
} | ||
/** @internal */ | ||
export function fromTraversable(T) { | ||
return function () { return ({ | ||
modifyF: function (F) { | ||
return function () { | ||
return traversal(function (F) { | ||
var traverseF = T.traverse(F); | ||
return function (f) { return function (s) { return traverseF(s, f); }; }; | ||
} | ||
}); }; | ||
}); | ||
}; | ||
} | ||
/** @internal */ | ||
export function traversalTraverse(T) { | ||
return traversalComposeTraversal(fromTraversable(T)()); | ||
} | ||
// ------------------------------------------------------------------------------------- | ||
// Ix | ||
// ------------------------------------------------------------------------------------- | ||
function indexMutableArray() { | ||
return { | ||
index: function (i) { return ({ | ||
getOption: function (as) { return A.lookup(i, as); }, | ||
set: function (a) { return function (as) { | ||
return pipe(A.updateAt(i, a)(as), O.getOrElse(function () { return as; })); | ||
}; } | ||
}); } | ||
}; | ||
} | ||
/** @internal */ | ||
export var indexReadonlyArray = indexMutableArray; | ||
export var index = function (index) { return ({ index: index }); }; | ||
/** @internal */ | ||
export function indexReadonlyRecord() { | ||
return { | ||
index: function (k) { return ({ | ||
getOption: function (r) { return R.lookup(k, r); }, | ||
set: function (a) { return function (r) { | ||
if (r[k] === a || O.isNone(R.lookup(k, r))) { | ||
return r; | ||
} | ||
return R.insertAt(k, a)(r); | ||
}; } | ||
}); } | ||
}; | ||
} | ||
export var indexReadonlyArray = function () { | ||
return index(function (i) { | ||
return optional(function (as) { return RA.lookup(i, as); }, function (a) { return function (as) { | ||
return pipe(RA.lookup(i, as), O.fold(function () { return as; }, function () { return RA.unsafeUpdateAt(i, a, as); })); | ||
}; }); | ||
}); | ||
}; | ||
/** @internal */ | ||
export var indexReadonlyNonEmptyArray = function () { | ||
return index(function (i) { | ||
return optional(function (as) { return RA.lookup(i, as); }, function (a) { return function (as) { | ||
return pipe(RA.lookup(i, as), O.fold(function () { return as; }, function () { return unsafeUpdateAt(i, a, as); })); | ||
}; }); | ||
}); | ||
}; | ||
/** @internal */ | ||
export var indexReadonlyRecord = function () { | ||
return index(function (k) { | ||
return optional(function (r) { return RR.lookup(k, r); }, function (a) { return function (r) { | ||
if (r[k] === a || O.isNone(RR.lookup(k, r))) { | ||
return r; | ||
} | ||
return RR.insertAt(k, a)(r); | ||
}; }); | ||
}); | ||
}; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -258,9 +298,8 @@ // At | ||
/** @internal */ | ||
export var at = function (at) { return ({ at: at }); }; | ||
/** @internal */ | ||
export function atReadonlyRecord() { | ||
return { | ||
at: function (key) { return ({ | ||
get: function (r) { return R.lookup(key, r); }, | ||
set: O.fold(function () { return R.deleteAt(key); }, function (a) { return R.insertAt(key, a); }) | ||
}); } | ||
}; | ||
return at(function (key) { | ||
return lens(function (r) { return RR.lookup(key, r); }, O.fold(function () { return RR.deleteAt(key); }, function (a) { return RR.insertAt(key, a); })); | ||
}); | ||
} |
196
es6/Iso.d.ts
@@ -0,5 +1,28 @@ | ||
/** | ||
* **This module is experimental** | ||
* | ||
* Experimental features are published in order to get early feedback from the community. | ||
* | ||
* A feature tagged as _Experimental_ is in a high state of flux, you're at risk of it changing without notice. | ||
* | ||
* An `Iso` is an optic which converts elements of type `S` into elements of type `A` without loss. | ||
* | ||
* Laws: | ||
* | ||
* 1. `reverseGet(get(s)) = s` | ||
* 2. `get(reversetGet(a)) = a` | ||
* | ||
* @since 2.3.0 | ||
*/ | ||
import { Category2 } from 'fp-ts/es6/Category' | ||
import { Either } from 'fp-ts/es6/Either' | ||
import { Predicate, Refinement } from 'fp-ts/es6/function' | ||
import { Functor, Functor1, Functor2, Functor3 } from 'fp-ts/es6/Functor' | ||
import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from 'fp-ts/es6/HKT' | ||
import { Invariant2 } from 'fp-ts/es6/Invariant' | ||
import { Option } from 'fp-ts/es6/Option' | ||
import { ReadonlyNonEmptyArray } from 'fp-ts/es6/ReadonlyNonEmptyArray' | ||
import { ReadonlyRecord } from 'fp-ts/es6/ReadonlyRecord' | ||
import { Semigroupoid2 } from 'fp-ts/es6/Semigroupoid' | ||
import { Traversable1 } from 'fp-ts/es6/Traversable' | ||
import { Lens } from './Lens' | ||
@@ -19,2 +42,7 @@ import { Optional } from './Optional' | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const iso: <S, A>(get: Iso<S, A>['get'], reverseGet: Iso<S, A>['reverseGet']) => Iso<S, A> | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
@@ -24,3 +52,3 @@ */ | ||
/** | ||
* View an `Iso` as a `Lens` | ||
* View an `Iso` as a `Lens`. | ||
* | ||
@@ -32,3 +60,3 @@ * @category converters | ||
/** | ||
* View an `Iso` as a `Prism` | ||
* View an `Iso` as a `Prism`. | ||
* | ||
@@ -40,3 +68,3 @@ * @category converters | ||
/** | ||
* View an `Iso` as a `Optional` | ||
* View an `Iso` as a `Optional`. | ||
* | ||
@@ -48,3 +76,3 @@ * @category converters | ||
/** | ||
* View an `Iso` as a `Traversal` | ||
* View an `Iso` as a `Traversal`. | ||
* | ||
@@ -56,3 +84,3 @@ * @category converters | ||
/** | ||
* Compose an `Iso` with an `Iso` | ||
* Compose an `Iso` with an `Iso`. | ||
* | ||
@@ -64,2 +92,37 @@ * @category compositions | ||
/** | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeIso: <A, B>(ab: Iso<A, B>) => <S>(sa: Iso<S, A>) => Iso<S, B> | ||
/** | ||
* Compose an `Iso` with a `Lens`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeLens: <A, B>(ab: Lens<A, B>) => <S>(sa: Iso<S, A>) => Lens<S, B> | ||
/** | ||
* Compose an `Iso` with a `Prism`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composePrism: <A, B>(ab: Prism<A, B>) => <S>(sa: Iso<S, A>) => Prism<S, B> | ||
/** | ||
* Compose an `Iso` with a `Optional`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeOptional: <A, B>(ab: Optional<A, B>) => <S>(sa: Iso<S, A>) => Optional<S, B> | ||
/** | ||
* Compose an `Iso` with a `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeTraversal: <A, B>(ab: Traversal<A, B>) => <S>(sa: Iso<S, A>) => Traversal<S, B> | ||
/** | ||
* @category constructors | ||
@@ -91,2 +154,116 @@ * @since 2.3.0 | ||
/** | ||
* Return a `Prism` from a `Iso` focused on a nullable value. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const fromNullable: <S, A>(sa: Iso<S, A>) => Prism<S, NonNullable<A>> | ||
/** | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function filter<A, B extends A>(refinement: Refinement<A, B>): <S>(sa: Iso<S, A>) => Prism<S, B> | ||
export declare function filter<A>(predicate: Predicate<A>): <S>(sa: Iso<S, A>) => Prism<S, A> | ||
/** | ||
* Return a `Lens` from a `Iso` and a prop. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const prop: <A, P extends keyof A>(prop: P) => <S>(sa: Iso<S, A>) => Lens<S, A[P]> | ||
/** | ||
* Return a `Lens` from a `Iso` and a list of props. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const props: <A, P extends keyof A>( | ||
props_0: P, | ||
props_1: P, | ||
...props_2: P[] | ||
) => <S>(sa: Iso<S, A>) => Lens<S, { [K in P]: A[K] }> | ||
/** | ||
* Return a `Lens` from a `Iso` focused on a component of a tuple. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const component: <A extends readonly unknown[], P extends keyof A>( | ||
prop: P | ||
) => <S>(sa: Iso<S, A>) => Lens<S, A[P]> | ||
/** | ||
* Return a `Optional` from a `Iso` focused on an index of a `ReadonlyArray`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const index: (i: number) => <S, A>(sa: Iso<S, readonly A[]>) => Optional<S, A> | ||
/** | ||
* Return a `Optional` from a `Iso` focused on an index of a `ReadonlyNonEmptyArray`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const indexNonEmpty: (i: number) => <S, A>(sa: Iso<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* Return a `Optional` from a `Iso` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const key: (key: string) => <S, A>(sa: Iso<S, Readonly<Record<string, A>>>) => Optional<S, A> | ||
/** | ||
* Return a `Lens` from a `Iso` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const atKey: (key: string) => <S, A>(sa: Iso<S, Readonly<Record<string, A>>>) => Lens<S, Option<A>> | ||
/** | ||
* Return a `Prism` from a `Iso` focused on the `Some` of a `Option` type. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const some: <S, A>(soa: Iso<S, Option<A>>) => Prism<S, A> | ||
/** | ||
* Return a `Prism` from a `Iso` focused on the `Right` of a `Either` type. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const right: <S, E, A>(sea: Iso<S, Either<E, A>>) => Prism<S, A> | ||
/** | ||
* Return a `Prism` from a `Iso` focused on the `Left` of a `Either` type. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const left: <S, E, A>(sea: Iso<S, Either<E, A>>) => Prism<S, E> | ||
/** | ||
* Return a `Traversal` from a `Iso` focused on a `Traversable`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function traverse<T extends URIS>(T: Traversable1<T>): <S, A>(sta: Iso<S, Kind<T, A>>) => Traversal<S, A> | ||
/** | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function findFirst<A, B extends A>( | ||
refinement: Refinement<A, B> | ||
): <S>(sa: Iso<S, ReadonlyArray<A>>) => Optional<S, B> | ||
export declare function findFirst<A>(predicate: Predicate<A>): <S>(sa: Iso<S, ReadonlyArray<A>>) => Optional<S, A> | ||
/** | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function findFirstNonEmpty<A, B extends A>( | ||
refinement: Refinement<A, B> | ||
): <S>(sa: Iso<S, ReadonlyNonEmptyArray<A>>) => Optional<S, B> | ||
export declare function findFirstNonEmpty<A>( | ||
predicate: Predicate<A> | ||
): <S>(sa: Iso<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* @category Invariant | ||
@@ -115,7 +292,12 @@ * @since 2.3.0 | ||
*/ | ||
export declare const invariantIso: Invariant2<URI> | ||
export declare const Invariant: Invariant2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
export declare const Semigroupoid: Semigroupoid2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
export declare const categoryIso: Category2<URI> | ||
export declare const Category: Category2<URI> |
226
es6/Iso.js
@@ -1,3 +0,3 @@ | ||
import { flow, identity, pipe } from 'fp-ts/es6/function'; | ||
import * as O from 'fp-ts/es6/Option'; | ||
import { flow, identity } from 'fp-ts/es6/function'; | ||
import { pipe } from 'fp-ts/es6/pipeable'; | ||
import * as _ from './internal'; | ||
@@ -9,8 +9,10 @@ // ------------------------------------------------------------------------------------- | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export var iso = _.iso; | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
*/ | ||
export var id = function () { return ({ | ||
get: identity, | ||
reverseGet: identity | ||
}); }; | ||
export var id = function () { return iso(identity, identity); }; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -20,3 +22,3 @@ // converters | ||
/** | ||
* View an `Iso` as a `Lens` | ||
* View an `Iso` as a `Lens`. | ||
* | ||
@@ -28,3 +30,3 @@ * @category converters | ||
/** | ||
* View an `Iso` as a `Prism` | ||
* View an `Iso` as a `Prism`. | ||
* | ||
@@ -34,8 +36,5 @@ * @category converters | ||
*/ | ||
export var asPrism = function (sa) { return ({ | ||
getOption: flow(sa.get, O.some), | ||
reverseGet: sa.reverseGet | ||
}); }; | ||
export var asPrism = _.isoAsPrism; | ||
/** | ||
* View an `Iso` as a `Optional` | ||
* View an `Iso` as a `Optional`. | ||
* | ||
@@ -47,3 +46,3 @@ * @category converters | ||
/** | ||
* View an `Iso` as a `Traversal` | ||
* View an `Iso` as a `Traversal`. | ||
* | ||
@@ -53,5 +52,3 @@ * @category converters | ||
*/ | ||
export var asTraversal = function (sa) { return ({ | ||
modifyF: function (F) { return function (f) { return function (s) { return F.map(f(sa.get(s)), function (a) { return sa.reverseGet(a); }); }; }; } | ||
}); }; | ||
export var asTraversal = _.isoAsTraversal; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -61,3 +58,3 @@ // compositions | ||
/** | ||
* Compose an `Iso` with an `Iso` | ||
* Compose an `Iso` with an `Iso`. | ||
* | ||
@@ -67,6 +64,48 @@ * @category compositions | ||
*/ | ||
export var compose = function (ab) { return function (sa) { return ({ | ||
get: flow(sa.get, ab.get), | ||
reverseGet: flow(ab.reverseGet, sa.reverseGet) | ||
}); }; }; | ||
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)); | ||
}; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -79,6 +118,3 @@ // combinators | ||
*/ | ||
export var reverse = function (sa) { return ({ | ||
get: sa.reverseGet, | ||
reverseGet: sa.get | ||
}); }; | ||
export var reverse = function (sa) { return iso(sa.reverseGet, sa.get); }; | ||
/** | ||
@@ -92,2 +128,121 @@ * @category combinators | ||
} | ||
/** | ||
* 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(_.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(_.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(_.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)); | ||
} | ||
// ------------------------------------------------------------------------------------- | ||
@@ -106,6 +261,3 @@ // pipeables | ||
// ------------------------------------------------------------------------------------- | ||
var imap_ = function (ea, ab, ba) { return ({ | ||
get: flow(ea.get, ab), | ||
reverseGet: flow(ba, ea.reverseGet) | ||
}); }; | ||
var imap_ = function (ea, ab, ba) { return iso(flow(ea.get, ab), flow(ba, ea.reverseGet)); }; | ||
/** | ||
@@ -120,3 +272,3 @@ * @category instances | ||
*/ | ||
export var invariantIso = { | ||
export var Invariant = { | ||
URI: URI, | ||
@@ -127,8 +279,16 @@ 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 categoryIso = { | ||
export var Category = { | ||
URI: URI, | ||
compose: function (ab, ea) { return compose(ab)(ea); }, | ||
compose: Semigroupoid.compose, | ||
id: id | ||
}; |
@@ -16,2 +16,4 @@ /** | ||
import Option = O.Option | ||
import { ReadonlyRecord } from 'fp-ts/es6/ReadonlyRecord' | ||
import { ReadonlyNonEmptyArray } from 'fp-ts/es6/ReadonlyNonEmptyArray' | ||
/** | ||
@@ -26,2 +28,7 @@ * @category model | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const index: <S, I, A>(index: Index<S, I, A>['index']) => Index<S, I, A> | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
@@ -31,3 +38,3 @@ */ | ||
/** | ||
* Lift an instance of `Index` using an `Iso` | ||
* Lift an instance of `Index` using an `Iso`. | ||
* | ||
@@ -45,5 +52,10 @@ * @category constructors | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const indexReadonlyNonEmptyArray: <A = never>() => Index<ReadonlyNonEmptyArray<A>, number, A> | ||
/** | ||
* @category constructors | ||
* @since 2.3.7 | ||
*/ | ||
export declare const indexReadonlyRecord: <A = never>() => Index<Readonly<Record<string, A>>, string, A> | ||
export declare const indexReadonlyRecord: <A = never>() => Index<ReadonlyRecord<string, A>, string, A> | ||
/** | ||
@@ -69,2 +81,2 @@ * @category constructors | ||
*/ | ||
export declare const indexRecord: <A = never>() => Index<Readonly<Record<string, A>>, string, A> | ||
export declare const indexRecord: <A = never>() => Index<ReadonlyRecord<string, A>, string, A> |
@@ -19,9 +19,14 @@ /** | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export var index = _.index; | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
*/ | ||
export var fromAt = function (at) { return ({ | ||
index: function (i) { return _.lensComposePrism(_.prismSome())(at.at(i)); } | ||
}); }; | ||
export var fromAt = function (at) { | ||
return index(function (i) { return _.lensComposePrism(_.prismSome())(at.at(i)); }); | ||
}; | ||
/** | ||
* Lift an instance of `Index` using an `Iso` | ||
* Lift an instance of `Index` using an `Iso`. | ||
* | ||
@@ -31,5 +36,5 @@ * @category constructors | ||
*/ | ||
export var fromIso = function (iso) { return function (sia) { return ({ | ||
index: function (i) { return pipe(iso, _.isoAsOptional, _.optionalComposeOptional(sia.index(i))); } | ||
}); }; }; | ||
export var fromIso = function (iso) { return function (sia) { | ||
return index(function (i) { return pipe(iso, _.isoAsOptional, _.optionalComposeOptional(sia.index(i))); }); | ||
}; }; | ||
/** | ||
@@ -42,2 +47,7 @@ * @category constructors | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export var indexReadonlyNonEmptyArray = _.indexReadonlyNonEmptyArray; | ||
/** | ||
* @category constructors | ||
* @since 2.3.7 | ||
@@ -50,20 +60,16 @@ */ | ||
*/ | ||
export var indexReadonlyMap = function (E) { return function () { | ||
export var indexReadonlyMap = function (E) { | ||
var lookupE = RM.lookup(E); | ||
var insertAtE = RM.insertAt(E); | ||
return { | ||
index: function (key) { | ||
var lookup = lookupE(key); | ||
return { | ||
getOption: lookup, | ||
set: function (next) { | ||
var insert = insertAtE(key, next); | ||
return function (s) { | ||
return pipe(lookup(s), O.fold(function () { return s; }, function (prev) { return (next === prev ? s : insert(s)); })); | ||
}; | ||
} | ||
}; | ||
} | ||
return function () { | ||
return index(function (key) { | ||
return _.optional(function (s) { return lookupE(key, s); }, function (next) { | ||
var insert = insertAtE(key, next); | ||
return function (s) { | ||
return pipe(lookupE(key, s), O.fold(function () { return s; }, function (prev) { return (next === prev ? s : insert(s)); })); | ||
}; | ||
}); | ||
}); | ||
}; | ||
}; }; | ||
}; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -70,0 +76,0 @@ // deprecated |
@@ -28,3 +28,7 @@ /** | ||
import { Option } from 'fp-ts/es6/Option' | ||
import { ReadonlyNonEmptyArray } from 'fp-ts/es6/ReadonlyNonEmptyArray' | ||
import { ReadonlyRecord } from 'fp-ts/es6/ReadonlyRecord' | ||
import { Semigroupoid2 } from 'fp-ts/es6/Semigroupoid' | ||
import { Traversable1 } from 'fp-ts/es6/Traversable' | ||
import { Iso } from './Iso' | ||
import { Optional } from './Optional' | ||
@@ -43,2 +47,7 @@ import { Prism } from './Prism' | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const lens: <S, A>(get: Lens<S, A>['get'], set: Lens<S, A>['set']) => Lens<S, A> | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
@@ -48,3 +57,3 @@ */ | ||
/** | ||
* View a `Lens` as a `Optional` | ||
* View a `Lens` as a `Optional`. | ||
* | ||
@@ -56,3 +65,3 @@ * @category converters | ||
/** | ||
* View a `Lens` as a `Traversal` | ||
* View a `Lens` as a `Traversal`. | ||
* | ||
@@ -64,3 +73,3 @@ * @category converters | ||
/** | ||
* Compose a `Lens` with a `Lens` | ||
* Compose a `Lens` with a `Lens`. | ||
* | ||
@@ -72,5 +81,19 @@ * @category compositions | ||
/** | ||
* Compose a `Lens` with a `Prism` | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeLens: <A, B>(ab: Lens<A, B>) => <S>(sa: Lens<S, A>) => Lens<S, B> | ||
/** | ||
* Compose a `Lens` with a `Iso`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeIso: <A, B>(ab: Iso<A, B>) => <S>(sa: Lens<S, A>) => Lens<S, B> | ||
/** | ||
* Compose a `Lens` with a `Prism`. | ||
* | ||
* @category compositions | ||
* @since 2.3.0 | ||
@@ -80,3 +103,3 @@ */ | ||
/** | ||
* Compose a `Lens` with an `Optional` | ||
* Compose a `Lens` with an `Optional`. | ||
* | ||
@@ -88,2 +111,9 @@ * @category compositions | ||
/** | ||
* Compose a `Lens` with an `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeTraversal: <A, B>(ab: Traversal<A, B>) => <S>(sa: Lens<S, A>) => Traversal<S, B> | ||
/** | ||
* @category combinators | ||
@@ -110,3 +140,3 @@ * @since 2.3.0 | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a nullable value | ||
* Return a `Optional` from a `Lens` focused on a nullable value. | ||
* | ||
@@ -124,3 +154,3 @@ * @category combinators | ||
/** | ||
* Return a `Lens` from a `Lens` and a prop | ||
* Return a `Lens` from a `Lens` and a prop. | ||
* | ||
@@ -132,3 +162,3 @@ * @category combinators | ||
/** | ||
* Return a `Lens` from a `Lens` and a list of props | ||
* Return a `Lens` from a `Lens` and a list of props. | ||
* | ||
@@ -139,3 +169,3 @@ * @category combinators | ||
export declare const props: <A, P extends keyof A>( | ||
...props: [P, P, ...Array<P>] | ||
...props: readonly [P, P, ...ReadonlyArray<P>] | ||
) => <S>( | ||
@@ -150,3 +180,3 @@ sa: Lens<S, A> | ||
/** | ||
* Return a `Lens` from a `Lens` and a component | ||
* Return a `Lens` from a `Lens` focused on a component of a tuple. | ||
* | ||
@@ -160,3 +190,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a `ReadonlyArray` | ||
* Return a `Optional` from a `Lens` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -168,5 +198,12 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a `ReadonlyRecord` and a key | ||
* Return a `Optional` from a `Lens` focused on an index of a `ReadonlyNonEmptyArray`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const indexNonEmpty: (i: number) => <S, A>(sa: Lens<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
@@ -176,3 +213,3 @@ */ | ||
/** | ||
* Return a `Lens` from a `Lens` focused on a `ReadonlyRecord` and a required key | ||
* Return a `Lens` from a `Lens` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -182,5 +219,5 @@ * @category combinators | ||
*/ | ||
export declare const atKey: (key: string) => <S, A>(sa: Lens<S, Readonly<Record<string, A>>>) => Lens<S, Option<A>> | ||
export declare const atKey: (key: string) => <S, A>(sa: Lens<S, ReadonlyRecord<string, A>>) => Lens<S, Option<A>> | ||
/** | ||
* Return a `Optional` from a `Lens` focused on the `Some` of a `Option` type | ||
* Return a `Optional` from a `Lens` focused on the `Some` of a `Option` type. | ||
* | ||
@@ -192,3 +229,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on the `Right` of a `Either` type | ||
* Return a `Optional` from a `Lens` focused on the `Right` of a `Either` type. | ||
* | ||
@@ -200,3 +237,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on the `Left` of a `Either` type | ||
* Return a `Optional` from a `Lens` focused on the `Left` of a `Either` type. | ||
* | ||
@@ -208,3 +245,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Lens` focused on a `Traversable` | ||
* Return a `Traversal` from a `Lens` focused on a `Traversable`. | ||
* | ||
@@ -226,2 +263,12 @@ * @category combinators | ||
/** | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function findFirstNonEmpty<A, B extends A>( | ||
refinement: Refinement<A, B> | ||
): <S>(sa: Lens<S, ReadonlyNonEmptyArray<A>>) => Optional<S, B> | ||
export declare function findFirstNonEmpty<A>( | ||
predicate: Predicate<A> | ||
): <S>(sa: Lens<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* @category Invariant | ||
@@ -250,7 +297,12 @@ * @since 2.3.0 | ||
*/ | ||
export declare const invariantLens: Invariant2<URI> | ||
export declare const Invariant: Invariant2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
export declare const Semigroupoid: Semigroupoid2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
export declare const categoryLens: Category2<URI> | ||
export declare const Category: Category2<URI> |
121
es6/Lens.js
@@ -9,2 +9,7 @@ import { flow } from 'fp-ts/es6/function'; | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export var lens = _.lens; | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
@@ -17,3 +22,3 @@ */ | ||
/** | ||
* View a `Lens` as a `Optional` | ||
* View a `Lens` as a `Optional`. | ||
* | ||
@@ -25,3 +30,3 @@ * @category converters | ||
/** | ||
* View a `Lens` as a `Traversal` | ||
* View a `Lens` as a `Traversal`. | ||
* | ||
@@ -36,3 +41,3 @@ * @category converters | ||
/** | ||
* Compose a `Lens` with a `Lens` | ||
* Compose a `Lens` with a `Lens`. | ||
* | ||
@@ -44,5 +49,21 @@ * @category compositions | ||
/** | ||
* Compose a `Lens` with a `Prism` | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export var composeLens = compose; | ||
/** | ||
* Compose a `Lens` with a `Iso`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export var composeIso = | ||
/*#__PURE__*/ | ||
flow(_.isoAsLens, compose); | ||
/** | ||
* Compose a `Lens` with a `Prism`. | ||
* | ||
* @category compositions | ||
* @since 2.3.0 | ||
@@ -52,3 +73,3 @@ */ | ||
/** | ||
* Compose a `Lens` with an `Optional` | ||
* Compose a `Lens` with an `Optional`. | ||
* | ||
@@ -58,5 +79,14 @@ * @category compositions | ||
*/ | ||
export var composeOptional = function (ab) { return function (sa) { | ||
return _.optionalComposeOptional(ab)(asOptional(sa)); | ||
}; }; | ||
export var composeOptional = function (ab) { | ||
return flow(asOptional, _.optionalComposeOptional(ab)); | ||
}; | ||
/** | ||
* Compose a `Lens` with an `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export var composeTraversal = function (ab) { | ||
return flow(asTraversal, _.traversalComposeTraversal(ab)); | ||
}; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -78,3 +108,3 @@ // combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a nullable value | ||
* Return a `Optional` from a `Lens` focused on a nullable value. | ||
* | ||
@@ -85,3 +115,3 @@ * @category combinators | ||
export var fromNullable = function (sa) { | ||
return _.lensComposePrism(_.prismFromNullable())(sa); | ||
return composePrism(_.prismFromNullable())(sa); | ||
}; | ||
@@ -92,3 +122,3 @@ export function filter(predicate) { | ||
/** | ||
* Return a `Lens` from a `Lens` and a prop | ||
* Return a `Lens` from a `Lens` and a prop. | ||
* | ||
@@ -100,3 +130,3 @@ * @category combinators | ||
/** | ||
* Return a `Lens` from a `Lens` and a list of props | ||
* Return a `Lens` from a `Lens` and a list of props. | ||
* | ||
@@ -108,3 +138,3 @@ * @category combinators | ||
/** | ||
* Return a `Lens` from a `Lens` and a component | ||
* Return a `Lens` from a `Lens` focused on a component of a tuple. | ||
* | ||
@@ -116,3 +146,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a `ReadonlyArray` | ||
* Return a `Optional` from a `Lens` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -122,16 +152,25 @@ * @category combinators | ||
*/ | ||
export var index = function (i) { return function (sa) { | ||
return pipe(sa, asOptional, _.optionalComposeOptional(_.indexReadonlyArray().index(i))); | ||
}; }; | ||
export var index = function (i) { | ||
return flow(asOptional, _.optionalIndex(i)); | ||
}; | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a `ReadonlyRecord` and a key | ||
* Return a `Optional` from a `Lens` 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 `Lens` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
*/ | ||
export var key = function (key) { return function (sa) { | ||
return pipe(sa, asOptional, _.optionalComposeOptional(_.indexReadonlyRecord().index(key))); | ||
}; }; | ||
export var key = function (key) { | ||
return flow(asOptional, _.optionalKey(key)); | ||
}; | ||
/** | ||
* Return a `Lens` from a `Lens` focused on a `ReadonlyRecord` and a required key | ||
* Return a `Lens` from a `Lens` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -141,7 +180,5 @@ * @category combinators | ||
*/ | ||
export var atKey = function (key) { return function (sa) { | ||
return pipe(sa, compose(_.atReadonlyRecord().at(key))); | ||
}; }; | ||
export var atKey = _.lensAtKey; | ||
/** | ||
* Return a `Optional` from a `Lens` focused on the `Some` of a `Option` type | ||
* Return a `Optional` from a `Lens` focused on the `Some` of a `Option` type. | ||
* | ||
@@ -155,3 +192,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on the `Right` of a `Either` type | ||
* Return a `Optional` from a `Lens` focused on the `Right` of a `Either` type. | ||
* | ||
@@ -165,3 +202,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on the `Left` of a `Either` type | ||
* Return a `Optional` from a `Lens` focused on the `Left` of a `Either` type. | ||
* | ||
@@ -175,3 +212,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Lens` focused on a `Traversable` | ||
* Return a `Traversal` from a `Lens` focused on a `Traversable`. | ||
* | ||
@@ -182,7 +219,10 @@ * @category combinators | ||
export function traverse(T) { | ||
return flow(asTraversal, _.traversalComposeTraversal(_.fromTraversable(T)())); | ||
return flow(asTraversal, _.traversalTraverse(T)); | ||
} | ||
export function findFirst(predicate) { | ||
return composeOptional(_.findFirst(predicate)); | ||
return composeOptional(_.optionalFindFirst(predicate)); | ||
} | ||
export function findFirstNonEmpty(predicate) { | ||
return composeOptional(_.optionalFindFirstNonEmpty(predicate)); | ||
} | ||
// ------------------------------------------------------------------------------------- | ||
@@ -201,6 +241,3 @@ // pipeables | ||
// ------------------------------------------------------------------------------------- | ||
var imap_ = function (ea, ab, ba) { return ({ | ||
get: flow(ea.get, ab), | ||
set: flow(ba, ea.set) | ||
}); }; | ||
var imap_ = function (ea, ab, ba) { return lens(flow(ea.get, ab), flow(ba, ea.set)); }; | ||
/** | ||
@@ -215,3 +252,3 @@ * @category instances | ||
*/ | ||
export var invariantLens = { | ||
export var Invariant = { | ||
URI: URI, | ||
@@ -222,8 +259,16 @@ 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 categoryLens = { | ||
export var Category = { | ||
URI: URI, | ||
compose: function (ab, ea) { return compose(ab)(ea); }, | ||
compose: Semigroupoid.compose, | ||
id: id | ||
}; |
@@ -29,3 +29,7 @@ /** | ||
import * as O from 'fp-ts/es6/Option' | ||
import { ReadonlyNonEmptyArray } from 'fp-ts/es6/ReadonlyNonEmptyArray' | ||
import { ReadonlyRecord } from 'fp-ts/es6/ReadonlyRecord' | ||
import { Semigroupoid2 } from 'fp-ts/es6/Semigroupoid' | ||
import { Traversable1 } from 'fp-ts/es6/Traversable' | ||
import { Iso } from './Iso' | ||
import { Lens } from './Lens' | ||
@@ -45,2 +49,10 @@ import { Prism } from './Prism' | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const optional: <S, A>( | ||
getOption: Optional<S, A>['getOption'], | ||
set: Optional<S, A>['set'] | ||
) => Optional<S, A> | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
@@ -64,2 +76,16 @@ */ | ||
/** | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeOptional: <A, B>(ab: Optional<A, B>) => <S>(sa: Optional<S, A>) => Optional<S, B> | ||
/** | ||
* Compose a `Optional` with a `Iso`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeIso: <A, B>(ab: Iso<A, B>) => <S>(sa: Optional<S, A>) => Optional<S, B> | ||
/** | ||
* Compose a `Optional` with a `Lens`. | ||
@@ -79,2 +105,9 @@ * | ||
/** | ||
* Compose a `Optional` with an `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeTraversal: <A, B>(ab: Traversal<A, B>) => <S>(sa: Optional<S, A>) => Traversal<S, B> | ||
/** | ||
* @category combinators | ||
@@ -142,3 +175,3 @@ * @since 2.3.0 | ||
/** | ||
* Return a `Optional` from a `Optional` and a component. | ||
* Return a `Optional` from a `Optional` focused on a component of a tuple. | ||
* | ||
@@ -152,3 +185,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a `ReadonlyArray`. | ||
* Return a `Optional` from a `Optional` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -158,12 +191,19 @@ * @category combinators | ||
*/ | ||
export declare const index: (i: number) => <S, A>(sa: Optional<S, readonly A[]>) => Optional<S, A> | ||
export declare const index: (i: number) => <S, A>(sa: Optional<S, ReadonlyArray<A>>) => Optional<S, A> | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a `ReadonlyRecord` and a key. | ||
* Return a `Optional` from a `Optional` focused on an index of a `ReadonlyNonEmptyArray`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const indexNonEmpty: (i: number) => <S, A>(sa: Optional<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
*/ | ||
export declare const key: (key: string) => <S, A>(sa: Optional<S, Readonly<Record<string, A>>>) => Optional<S, A> | ||
export declare const key: (key: string) => <S, A>(sa: Optional<S, ReadonlyRecord<string, A>>) => Optional<S, A> | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a `ReadonlyRecord` and a required key. | ||
* Return a `Optional` from a `Optional` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -215,2 +255,12 @@ * @category combinators | ||
/** | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function findFirstNonEmpty<A, B extends A>( | ||
refinement: Refinement<A, B> | ||
): <S>(sa: Optional<S, ReadonlyNonEmptyArray<A>>) => Optional<S, B> | ||
export declare function findFirstNonEmpty<A>( | ||
predicate: Predicate<A> | ||
): <S>(sa: Optional<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* @category Invariant | ||
@@ -239,7 +289,12 @@ * @since 2.3.0 | ||
*/ | ||
export declare const invariantOptional: Invariant2<URI> | ||
export declare const Invariant: Invariant2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
export declare const Semigroupoid: Semigroupoid2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
export declare const categoryOptional: Category2<URI> | ||
export declare const Category: Category2<URI> |
@@ -10,8 +10,10 @@ import { constant, flow } from 'fp-ts/es6/function'; | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export var optional = _.optional; | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
*/ | ||
export var id = function () { return ({ | ||
getOption: O.some, | ||
set: constant | ||
}); }; | ||
export var id = function () { return optional(O.some, constant); }; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -38,2 +40,18 @@ // converters | ||
/** | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export var composeOptional = compose; | ||
/** | ||
* Compose a `Optional` with a `Iso`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export var composeIso = | ||
/*#__PURE__*/ | ||
flow(_.isoAsOptional, compose); | ||
/** | ||
* Compose a `Optional` with a `Lens`. | ||
@@ -44,5 +62,5 @@ * | ||
*/ | ||
export var composeLens = function (ab) { return function (sa) { | ||
return _.optionalComposeOptional(_.lensAsOptional(ab))(sa); | ||
}; }; | ||
export var composeLens = | ||
/*#__PURE__*/ | ||
flow(_.lensAsOptional, _.optionalComposeOptional); | ||
/** | ||
@@ -54,5 +72,14 @@ * Compose a `Optional` with a `Prism`. | ||
*/ | ||
export var composePrism = function (ab) { return function (sa) { | ||
return _.optionalComposeOptional(_.prismAsOptional(ab))(sa); | ||
}; }; | ||
export var composePrism = | ||
/*#__PURE__*/ | ||
flow(_.prismAsOptional, _.optionalComposeOptional); | ||
/** | ||
* Compose a `Optional` with an `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export var composeTraversal = function (ab) { | ||
return flow(asTraversal, _.traversalComposeTraversal(ab)); | ||
}; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -116,3 +143,3 @@ // combinators | ||
/** | ||
* Return a `Optional` from a `Optional` and a component. | ||
* Return a `Optional` from a `Optional` focused on a component of a tuple. | ||
* | ||
@@ -126,3 +153,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a `ReadonlyArray`. | ||
* Return a `Optional` from a `Optional` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -132,16 +159,19 @@ * @category combinators | ||
*/ | ||
export var index = function (i) { return function (sa) { | ||
return pipe(sa, _.optionalComposeOptional(_.indexReadonlyArray().index(i))); | ||
}; }; | ||
export var index = _.optionalIndex; | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a `ReadonlyRecord` and a key. | ||
* Return a `Optional` from a `Optional` focused on an index of a `ReadonlyNonEmptyArray`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export var indexNonEmpty = _.optionalIndexNonEmpty; | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
*/ | ||
export var key = function (key) { return function (sa) { | ||
return pipe(sa, _.optionalComposeOptional(_.indexReadonlyRecord().index(key))); | ||
}; }; | ||
export var key = _.optionalKey; | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a `ReadonlyRecord` and a required key. | ||
* Return a `Optional` from a `Optional` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -188,7 +218,10 @@ * @category combinators | ||
export function traverse(T) { | ||
return flow(asTraversal, _.traversalComposeTraversal(_.fromTraversable(T)())); | ||
return flow(asTraversal, _.traversalTraverse(T)); | ||
} | ||
export function findFirst(predicate) { | ||
return compose(_.findFirst(predicate)); | ||
return compose(_.optionalFindFirst(predicate)); | ||
} | ||
export function findFirstNonEmpty(predicate) { | ||
return compose(_.optionalFindFirstNonEmpty(predicate)); | ||
} | ||
// ------------------------------------------------------------------------------------- | ||
@@ -205,6 +238,3 @@ // pipeables | ||
// ------------------------------------------------------------------------------------- | ||
var imap_ = function (ea, ab, ba) { return ({ | ||
getOption: flow(ea.getOption, O.map(ab)), | ||
set: flow(ba, ea.set) | ||
}); }; | ||
var imap_ = function (ea, ab, ba) { return optional(flow(ea.getOption, O.map(ab)), flow(ba, ea.set)); }; | ||
/** | ||
@@ -219,3 +249,3 @@ * @category instances | ||
*/ | ||
export var invariantOptional = { | ||
export var Invariant = { | ||
URI: URI, | ||
@@ -226,8 +256,16 @@ 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 categoryOptional = { | ||
export var Category = { | ||
URI: URI, | ||
compose: function (ab, ea) { return compose(ab)(ea); }, | ||
compose: Semigroupoid.compose, | ||
id: id | ||
}; |
@@ -24,3 +24,7 @@ /** | ||
import * as O from 'fp-ts/es6/Option' | ||
import { ReadonlyNonEmptyArray } from 'fp-ts/es6/ReadonlyNonEmptyArray' | ||
import { ReadonlyRecord } from 'fp-ts/es6/ReadonlyRecord' | ||
import { Semigroupoid2 } from 'fp-ts/es6/Semigroupoid' | ||
import { Traversable1 } from 'fp-ts/es6/Traversable' | ||
import { Iso } from './Iso' | ||
import { Lens } from './Lens' | ||
@@ -40,2 +44,10 @@ import { Optional } from './Optional' | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const prism: <S, A>( | ||
getOption: Prism<S, A>['getOption'], | ||
reverseGet: Prism<S, A>['reverseGet'] | ||
) => Prism<S, A> | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
@@ -53,3 +65,3 @@ */ | ||
/** | ||
* View a `Prism` as a `Optional` | ||
* View a `Prism` as a `Optional`. | ||
* | ||
@@ -61,3 +73,3 @@ * @category converters | ||
/** | ||
* View a `Prism` as a `Traversal` | ||
* View a `Prism` as a `Traversal`. | ||
* | ||
@@ -69,3 +81,3 @@ * @category converters | ||
/** | ||
* Compose a `Prism` with a `Prism` | ||
* Compose a `Prism` with a `Prism`. | ||
* | ||
@@ -77,5 +89,19 @@ * @category compositions | ||
/** | ||
* Compose a `Prism` with a `Lens` | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composePrism: <A, B>(ab: Prism<A, B>) => <S>(sa: Prism<S, A>) => Prism<S, B> | ||
/** | ||
* Compose a `Prism` with a `Iso`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeIso: <A, B>(ab: Iso<A, B>) => <S>(sa: Prism<S, A>) => Prism<S, B> | ||
/** | ||
* Compose a `Prism` with a `Lens`. | ||
* | ||
* @category compositions | ||
* @since 2.3.0 | ||
@@ -85,3 +111,3 @@ */ | ||
/** | ||
* Compose a `Prism` with an `Optional` | ||
* Compose a `Prism` with an `Optional`. | ||
* | ||
@@ -93,2 +119,9 @@ * @category compositions | ||
/** | ||
* Compose a `Prism` with an `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeTraversal: <A, B>(ab: Traversal<A, B>) => <S>(sa: Prism<S, A>) => Traversal<S, B> | ||
/** | ||
* @category combinators | ||
@@ -125,3 +158,3 @@ * @since 2.3.0 | ||
/** | ||
* Return a `Prism` from a `Prism` focused on a nullable value | ||
* Return a `Prism` from a `Prism` focused on a nullable value. | ||
* | ||
@@ -139,3 +172,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` and a prop | ||
* Return a `Optional` from a `Prism` and a prop. | ||
* | ||
@@ -147,3 +180,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` and a list of props | ||
* Return a `Optional` from a `Prism` and a list of props. | ||
* | ||
@@ -159,3 +192,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` and a component | ||
* Return a `Optional` from a `Prism` focused on a component of a tuple. | ||
* | ||
@@ -169,3 +202,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` focused on a `ReadonlyArray` | ||
* Return a `Optional` from a `Prism` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -177,5 +210,12 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` focused on a `ReadonlyRecord` and a key | ||
* Return a `Optional` from a `Prism` focused on an index of a `ReadonlyNonEmptyArray`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const indexNonEmpty: (i: number) => <S, A>(sa: Prism<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* Return a `Optional` from a `Prism` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
@@ -185,3 +225,3 @@ */ | ||
/** | ||
* Return a `Optional` from a `Prism` focused on a `ReadonlyRecord` and a required key | ||
* Return a `Optional` from a `Prism` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -195,3 +235,3 @@ * @category combinators | ||
/** | ||
* Return a `Prism` from a `Prism` focused on the `Some` of a `Option` type | ||
* Return a `Prism` from a `Prism` focused on the `Some` of a `Option` type. | ||
* | ||
@@ -203,3 +243,3 @@ * @category combinators | ||
/** | ||
* Return a `Prism` from a `Prism` focused on the `Right` of a `Either` type | ||
* Return a `Prism` from a `Prism` focused on the `Right` of a `Either` type. | ||
* | ||
@@ -211,3 +251,3 @@ * @category combinators | ||
/** | ||
* Return a `Prism` from a `Prism` focused on the `Left` of a `Either` type | ||
* Return a `Prism` from a `Prism` focused on the `Left` of a `Either` type. | ||
* | ||
@@ -219,3 +259,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Prism` focused on a `Traversable` | ||
* Return a `Traversal` from a `Prism` focused on a `Traversable`. | ||
* | ||
@@ -237,2 +277,12 @@ * @category combinators | ||
/** | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function findFirstNonEmpty<A, B extends A>( | ||
refinement: Refinement<A, B> | ||
): <S>(sa: Prism<S, ReadonlyNonEmptyArray<A>>) => Optional<S, B> | ||
export declare function findFirstNonEmpty<A>( | ||
predicate: Predicate<A> | ||
): <S>(sa: Prism<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* @category Invariant | ||
@@ -261,7 +311,12 @@ * @since 2.3.0 | ||
*/ | ||
export declare const invariantPrism: Invariant2<URI> | ||
export declare const Invariant: Invariant2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
export declare const Semigroupoid: Semigroupoid2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
export declare const categoryPrism: Category2<URI> | ||
export declare const Category: Category2<URI> |
127
es6/Prism.js
@@ -10,8 +10,10 @@ import { flow, identity } from 'fp-ts/es6/function'; | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export var prism = _.prism; | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
*/ | ||
export var id = function () { return ({ | ||
getOption: O.some, | ||
reverseGet: identity | ||
}); }; | ||
export var id = function () { return prism(O.some, identity); }; | ||
/** | ||
@@ -26,3 +28,3 @@ * @category constructors | ||
/** | ||
* View a `Prism` as a `Optional` | ||
* View a `Prism` as a `Optional`. | ||
* | ||
@@ -34,3 +36,3 @@ * @category converters | ||
/** | ||
* View a `Prism` as a `Traversal` | ||
* View a `Prism` as a `Traversal`. | ||
* | ||
@@ -45,3 +47,3 @@ * @category converters | ||
/** | ||
* Compose a `Prism` with a `Prism` | ||
* Compose a `Prism` with a `Prism`. | ||
* | ||
@@ -51,10 +53,23 @@ * @category compositions | ||
*/ | ||
export var compose = function (ab) { return function (sa) { return ({ | ||
getOption: flow(sa.getOption, O.chain(ab.getOption)), | ||
reverseGet: flow(ab.reverseGet, sa.reverseGet) | ||
}); }; }; | ||
export var compose = _.prismComposePrism; | ||
/** | ||
* Compose a `Prism` with a `Lens` | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export var composePrism = compose; | ||
/** | ||
* Compose a `Prism` with a `Iso`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export var composeIso = | ||
/*#__PURE__*/ | ||
flow(_.isoAsPrism, compose); | ||
/** | ||
* Compose a `Prism` with a `Lens`. | ||
* | ||
* @category compositions | ||
* @since 2.3.0 | ||
@@ -64,3 +79,3 @@ */ | ||
/** | ||
* Compose a `Prism` with an `Optional` | ||
* Compose a `Prism` with an `Optional`. | ||
* | ||
@@ -70,5 +85,14 @@ * @category compositions | ||
*/ | ||
export var composeOptional = function (ab) { return function (sa) { | ||
return _.optionalComposeOptional(ab)(asOptional(sa)); | ||
}; }; | ||
export var composeOptional = function (ab) { | ||
return flow(asOptional, _.optionalComposeOptional(ab)); | ||
}; | ||
/** | ||
* Compose a `Prism` with an `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export var composeTraversal = function (ab) { | ||
return flow(asTraversal, _.traversalComposeTraversal(ab)); | ||
}; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -98,3 +122,3 @@ // combinators | ||
/** | ||
* Return a `Prism` from a `Prism` focused on a nullable value | ||
* Return a `Prism` from a `Prism` focused on a nullable value. | ||
* | ||
@@ -111,3 +135,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` and a prop | ||
* Return a `Optional` from a `Prism` and a prop. | ||
* | ||
@@ -119,5 +143,5 @@ * @category combinators | ||
return composeLens(pipe(_.lensId(), _.lensProp(prop))); | ||
}; | ||
}; // TODO: simplify? | ||
/** | ||
* Return a `Optional` from a `Prism` and a list of props | ||
* Return a `Optional` from a `Prism` and a list of props. | ||
* | ||
@@ -135,3 +159,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` and a component | ||
* Return a `Optional` from a `Prism` focused on a component of a tuple. | ||
* | ||
@@ -143,3 +167,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` focused on a `ReadonlyArray` | ||
* Return a `Optional` from a `Prism` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -149,16 +173,25 @@ * @category combinators | ||
*/ | ||
export var index = function (i) { return function (sa) { | ||
return pipe(sa, asOptional, _.optionalComposeOptional(_.indexReadonlyArray().index(i))); | ||
}; }; | ||
export var index = function (i) { | ||
return flow(asOptional, _.optionalIndex(i)); | ||
}; | ||
/** | ||
* Return a `Optional` from a `Prism` focused on a `ReadonlyRecord` and a key | ||
* Return a `Optional` from a `Prism` 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 `Prism` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
*/ | ||
export var key = function (key) { return function (sa) { | ||
return pipe(sa, asOptional, _.optionalComposeOptional(_.indexReadonlyRecord().index(key))); | ||
}; }; | ||
export var key = function (key) { | ||
return flow(asOptional, _.optionalKey(key)); | ||
}; | ||
/** | ||
* Return a `Optional` from a `Prism` focused on a `ReadonlyRecord` and a required key | ||
* Return a `Optional` from a `Prism` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -172,3 +205,3 @@ * @category combinators | ||
/** | ||
* Return a `Prism` from a `Prism` focused on the `Some` of a `Option` type | ||
* Return a `Prism` from a `Prism` focused on the `Some` of a `Option` type. | ||
* | ||
@@ -182,3 +215,3 @@ * @category combinators | ||
/** | ||
* Return a `Prism` from a `Prism` focused on the `Right` of a `Either` type | ||
* Return a `Prism` from a `Prism` focused on the `Right` of a `Either` type. | ||
* | ||
@@ -192,3 +225,3 @@ * @category combinators | ||
/** | ||
* Return a `Prism` from a `Prism` focused on the `Left` of a `Either` type | ||
* Return a `Prism` from a `Prism` focused on the `Left` of a `Either` type. | ||
* | ||
@@ -202,3 +235,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Prism` focused on a `Traversable` | ||
* Return a `Traversal` from a `Prism` focused on a `Traversable`. | ||
* | ||
@@ -209,7 +242,10 @@ * @category combinators | ||
export function traverse(T) { | ||
return flow(asTraversal, _.traversalComposeTraversal(_.fromTraversable(T)())); | ||
return flow(asTraversal, _.traversalTraverse(T)); | ||
} | ||
export function findFirst(predicate) { | ||
return composeOptional(_.findFirst(predicate)); | ||
return composeOptional(_.optionalFindFirst(predicate)); | ||
} | ||
export function findFirstNonEmpty(predicate) { | ||
return composeOptional(_.optionalFindFirstNonEmpty(predicate)); | ||
} | ||
// ------------------------------------------------------------------------------------- | ||
@@ -228,6 +264,3 @@ // pipeables | ||
// ------------------------------------------------------------------------------------- | ||
var imap_ = function (ea, ab, ba) { return ({ | ||
getOption: flow(ea.getOption, O.map(ab)), | ||
reverseGet: flow(ba, ea.reverseGet) | ||
}); }; | ||
var imap_ = function (ea, ab, ba) { return prism(flow(ea.getOption, O.map(ab)), flow(ba, ea.reverseGet)); }; | ||
/** | ||
@@ -242,3 +275,3 @@ * @category instances | ||
*/ | ||
export var invariantPrism = { | ||
export var Invariant = { | ||
URI: URI, | ||
@@ -249,8 +282,16 @@ 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 categoryPrism = { | ||
export var Category = { | ||
URI: URI, | ||
compose: function (ab, ea) { return compose(ab)(ea); }, | ||
compose: Semigroupoid.compose, | ||
id: id | ||
}; |
@@ -23,4 +23,10 @@ /** | ||
import { Option } from 'fp-ts/es6/Option' | ||
import { Traversable1 } from 'fp-ts/es6/Traversable' | ||
import { ReadonlyNonEmptyArray } from 'fp-ts/es6/ReadonlyNonEmptyArray' | ||
import { ReadonlyRecord } from 'fp-ts/es6/ReadonlyRecord' | ||
import { Semigroupoid2 } from 'fp-ts/es6/Semigroupoid' | ||
import * as _ from './internal' | ||
import { Iso } from './Iso' | ||
import { Lens } from './Lens' | ||
import { Optional } from './Optional' | ||
import { Prism } from './Prism' | ||
/** | ||
@@ -46,2 +52,7 @@ * @category model | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const traversal: <S, A>(modifyF: Traversal<S, A>['modifyF']) => Traversal<S, A> | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
@@ -51,3 +62,3 @@ */ | ||
/** | ||
* Create a `Traversal` from a `Traversable` | ||
* Create a `Traversal` from a `Traversable`. | ||
* | ||
@@ -59,3 +70,3 @@ * @category constructor | ||
/** | ||
* Compose a `Traversal` with a `Traversal` | ||
* Compose a `Traversal` with a `Traversal`. | ||
* | ||
@@ -67,2 +78,37 @@ * @category compositions | ||
/** | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeTraversal: <A, B>(ab: Traversal<A, B>) => <S>(sa: Traversal<S, A>) => Traversal<S, B> | ||
/** | ||
* Compose a `Traversal` with a `Iso`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeIso: <A, B>(ab: Iso<A, B>) => <S>(sa: Traversal<S, A>) => Traversal<S, B> | ||
/** | ||
* Compose a `Traversal` with a `Lens`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeLens: <A, B>(ab: Lens<A, B>) => <S>(sa: Traversal<S, A>) => Traversal<S, B> | ||
/** | ||
* Compose a `Traversal` with a `Prism`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composePrism: <A, B>(ab: Prism<A, B>) => <S>(sa: Traversal<S, A>) => Traversal<S, B> | ||
/** | ||
* Compose a `Traversal` with a `Optional`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeOptional: <A, B>(ab: Optional<A, B>) => <S>(sa: Traversal<S, A>) => Traversal<S, B> | ||
/** | ||
* @category combinators | ||
@@ -78,5 +124,12 @@ * @since 2.3.0 | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a nullable value. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
*/ | ||
export declare const fromNullable: <S, A>(sa: Traversal<S, A>) => Traversal<S, NonNullable<A>> | ||
/** | ||
* @category combinators | ||
* @since 2.3.0 | ||
*/ | ||
export declare function filter<A, B extends A>( | ||
@@ -87,3 +140,3 @@ refinement: Refinement<A, B> | ||
/** | ||
* Return a `Traversal` from a `Traversal` and a prop | ||
* Return a `Traversal` from a `Traversal` and a prop. | ||
* | ||
@@ -95,3 +148,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` and a list of props | ||
* Return a `Traversal` from a `Traversal` and a list of props. | ||
* | ||
@@ -107,3 +160,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` and a component | ||
* Return a `Traversal` from a `Traversal` focused on a component of a tuple. | ||
* | ||
@@ -117,3 +170,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `ReadonlyArray` | ||
* Return a `Traversal` from a `Traversal` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -125,5 +178,12 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `ReadonlyRecord` and a key | ||
* Alias of `index`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const indexNonEmpty: (i: number) => <S, A>(sa: Traversal<S, ReadonlyNonEmptyArray<A>>) => Traversal<S, A> | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
@@ -133,3 +193,3 @@ */ | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `ReadonlyRecord` and a required key | ||
* Return a `Traversal` from a `Traversal` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -143,3 +203,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on the `Some` of a `Option` type | ||
* Return a `Traversal` from a `Traversal` focused on the `Some` of a `Option` type. | ||
* | ||
@@ -151,3 +211,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on the `Right` of a `Either` type | ||
* Return a `Traversal` from a `Traversal` focused on the `Right` of a `Either` type. | ||
* | ||
@@ -159,3 +219,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on the `Left` of a `Either` type | ||
* Return a `Traversal` from a `Traversal` focused on the `Left` of a `Either` type. | ||
* | ||
@@ -167,3 +227,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `Traversable` | ||
* Return a `Traversal` from a `Traversal` focused on a `Traversable`. | ||
* | ||
@@ -173,6 +233,26 @@ * @category combinators | ||
*/ | ||
export declare function traverse<T extends URIS>( | ||
T: Traversable1<T> | ||
): <S, A>(sta: Traversal<S, Kind<T, A>>) => Traversal<S, A> | ||
export declare const traverse: typeof _.traversalTraverse | ||
/** | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function findFirst<A, B extends A>( | ||
refinement: Refinement<A, B> | ||
): <S>(sa: Traversal<S, ReadonlyArray<A>>) => Traversal<S, B> | ||
export declare function findFirst<A>( | ||
predicate: Predicate<A> | ||
): <S>(sa: Traversal<S, ReadonlyArray<A>>) => Traversal<S, A> | ||
/** | ||
* Alias of `findFirst`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function findFirstNonEmpty<A, B extends A>( | ||
refinement: Refinement<A, B> | ||
): <S>(sa: Traversal<S, ReadonlyNonEmptyArray<A>>) => Traversal<S, B> | ||
export declare function findFirstNonEmpty<A>( | ||
predicate: Predicate<A> | ||
): <S>(sa: Traversal<S, ReadonlyNonEmptyArray<A>>) => Traversal<S, A> | ||
/** | ||
* Map each target to a `Monoid` and combine the results. | ||
@@ -215,4 +295,9 @@ * | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
export declare const Semigroupoid: Semigroupoid2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
export declare const categoryTraversal: Category2<URI> | ||
export declare const Category: Category2<URI> |
import * as C from 'fp-ts/es6/Const'; | ||
import { identity } from 'fp-ts/es6/function'; | ||
import { flow, identity } from 'fp-ts/es6/function'; | ||
import * as I from 'fp-ts/es6/Identity'; | ||
@@ -12,9 +12,12 @@ import { pipe } from 'fp-ts/es6/pipeable'; | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export var traversal = _.traversal; | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
*/ | ||
export var id = function () { return ({ | ||
modifyF: function (_) { return function (f) { return f; }; } | ||
}); }; | ||
export var id = function () { return traversal(function (_) { return function (f) { return f; }; }); }; | ||
/** | ||
* Create a `Traversal` from a `Traversable` | ||
* Create a `Traversal` from a `Traversable`. | ||
* | ||
@@ -29,3 +32,3 @@ * @category constructor | ||
/** | ||
* Compose a `Traversal` with a `Traversal` | ||
* Compose a `Traversal` with a `Traversal`. | ||
* | ||
@@ -36,2 +39,45 @@ * @category compositions | ||
export var compose = _.traversalComposeTraversal; | ||
/** | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export var composeTraversal = compose; | ||
/** | ||
* Compose a `Traversal` with a `Iso`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export var composeIso = | ||
/*#__PURE__*/ | ||
flow(_.isoAsTraversal, compose); | ||
/** | ||
* Compose a `Traversal` with a `Lens`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export var composeLens = | ||
/*#__PURE__*/ | ||
flow(_.lensAsTraversal, _.traversalComposeTraversal); | ||
/** | ||
* Compose a `Traversal` with a `Prism`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export var composePrism = | ||
/*#__PURE__*/ | ||
flow(_.prismAsTraversal, _.traversalComposeTraversal); | ||
/** | ||
* Compose a `Traversal` with a `Optional`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export var composeOptional = | ||
/*#__PURE__*/ | ||
flow(_.optionalAsTraversal, _.traversalComposeTraversal); | ||
// ------------------------------------------------------------------------------------- | ||
@@ -54,2 +100,11 @@ // combinators | ||
}; | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a nullable value. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
*/ | ||
export var fromNullable = function (sa) { | ||
return composePrism(_.prismFromNullable())(sa); | ||
}; | ||
export function filter(predicate) { | ||
@@ -59,3 +114,3 @@ return compose(_.prismAsTraversal(_.prismFromPredicate(predicate))); | ||
/** | ||
* Return a `Traversal` from a `Traversal` and a prop | ||
* Return a `Traversal` from a `Traversal` and a prop. | ||
* | ||
@@ -69,3 +124,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` and a list of props | ||
* Return a `Traversal` from a `Traversal` and a list of props. | ||
* | ||
@@ -83,3 +138,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` and a component | ||
* Return a `Traversal` from a `Traversal` focused on a component of a tuple. | ||
* | ||
@@ -93,3 +148,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `ReadonlyArray` | ||
* Return a `Traversal` from a `Traversal` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -103,5 +158,12 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `ReadonlyRecord` and a key | ||
* Alias of `index`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export var indexNonEmpty = index; | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
@@ -113,3 +175,3 @@ */ | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `ReadonlyRecord` and a required key | ||
* Return a `Traversal` from a `Traversal` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -123,3 +185,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on the `Some` of a `Option` type | ||
* Return a `Traversal` from a `Traversal` focused on the `Some` of a `Option` type. | ||
* | ||
@@ -133,3 +195,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on the `Right` of a `Either` type | ||
* Return a `Traversal` from a `Traversal` focused on the `Right` of a `Either` type. | ||
* | ||
@@ -143,3 +205,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on the `Left` of a `Either` type | ||
* Return a `Traversal` from a `Traversal` focused on the `Left` of a `Either` type. | ||
* | ||
@@ -153,3 +215,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `Traversable` | ||
* Return a `Traversal` from a `Traversal` focused on a `Traversable`. | ||
* | ||
@@ -159,5 +221,9 @@ * @category combinators | ||
*/ | ||
export function traverse(T) { | ||
return compose(fromTraversable(T)()); | ||
export var traverse = _.traversalTraverse; | ||
export function findFirst(predicate) { | ||
return composeOptional(_.optionalFindFirst(predicate)); | ||
} | ||
export function findFirstNonEmpty(predicate) { | ||
return findFirst(predicate); | ||
} | ||
/** | ||
@@ -198,8 +264,16 @@ * Map each target to a `Monoid` and combine the results. | ||
* @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 categoryTraversal = { | ||
export var Category = { | ||
URI: URI, | ||
compose: function (ab, ea) { return compose(ab)(ea); }, | ||
compose: Semigroupoid.compose, | ||
id: id | ||
}; |
@@ -12,2 +12,3 @@ /** | ||
import * as O from 'fp-ts/lib/Option' | ||
import { ReadonlyRecord } from 'fp-ts/lib/ReadonlyRecord' | ||
import { Iso } from './Iso' | ||
@@ -24,3 +25,8 @@ import { Lens } from './Lens' | ||
/** | ||
* Lift an instance of `At` using an `Iso` | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const at: <S, I, A>(at: At<S, I, A>['at']) => At<S, I, A> | ||
/** | ||
* Lift an instance of `At` using an `Iso`. | ||
* | ||
@@ -35,3 +41,3 @@ * @category constructors | ||
*/ | ||
export declare const atReadonlyRecord: <A = never>() => At<Readonly<Record<string, A>>, string, Option<A>> | ||
export declare const atReadonlyRecord: <A = never>() => At<ReadonlyRecord<string, A>, string, Option<A>> | ||
/** | ||
@@ -54,2 +60,2 @@ * @category constructors | ||
*/ | ||
export declare const atRecord: <A = never>() => At<Readonly<Record<string, A>>, string, Option<A>> | ||
export declare const atRecord: <A = never>() => At<ReadonlyRecord<string, A>, string, Option<A>> |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.atRecord = exports.atReadonlySet = exports.atReadonlyMap = exports.atReadonlyRecord = exports.fromIso = void 0; | ||
exports.atRecord = exports.atReadonlySet = exports.atReadonlyMap = exports.atReadonlyRecord = exports.fromIso = exports.at = void 0; | ||
var O = require("fp-ts/lib/Option"); | ||
@@ -13,3 +13,8 @@ var pipeable_1 = require("fp-ts/lib/pipeable"); | ||
/** | ||
* Lift an instance of `At` using an `Iso` | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
exports.at = _.at; | ||
/** | ||
* Lift an instance of `At` using an `Iso`. | ||
* | ||
@@ -19,5 +24,5 @@ * @category constructors | ||
*/ | ||
var fromIso = function (iso) { return function (sia) { return ({ | ||
at: function (i) { return pipeable_1.pipe(iso, _.isoAsLens, _.lensComposeLens(sia.at(i))); } | ||
}); }; }; | ||
var fromIso = function (iso) { return function (sia) { | ||
return exports.at(function (i) { return pipeable_1.pipe(iso, _.isoAsLens, _.lensComposeLens(sia.at(i))); }); | ||
}; }; | ||
exports.fromIso = fromIso; | ||
@@ -33,13 +38,12 @@ /** | ||
*/ | ||
var atReadonlyMap = function (E) { return function () { | ||
var atReadonlyMap = function (E) { | ||
var lookupE = RM.lookup(E); | ||
var deleteAtE = RM.deleteAt(E); | ||
var insertAtE = RM.insertAt(E); | ||
return { | ||
at: function (key) { return ({ | ||
get: lookupE(key), | ||
set: O.fold(function () { return deleteAtE(key); }, function (a) { return insertAtE(key, a); }) | ||
}); } | ||
return function () { | ||
return exports.at(function (key) { | ||
return _.lens(function (s) { return lookupE(key, s); }, O.fold(function () { return deleteAtE(key); }, function (a) { return insertAtE(key, a); })); | ||
}); | ||
}; | ||
}; }; | ||
}; | ||
exports.atReadonlyMap = atReadonlyMap; | ||
@@ -54,12 +58,7 @@ /** | ||
var removeE = RS.remove(E); | ||
return { | ||
at: function (a) { | ||
var insert = insertE(a); | ||
var remove = removeE(a); | ||
return { | ||
get: elemE(a), | ||
set: function (b) { return function (s) { return (b ? insert(s) : remove(s)); }; } | ||
}; | ||
} | ||
}; | ||
return exports.at(function (a) { | ||
var insert = insertE(a); | ||
var remove = removeE(a); | ||
return _.lens(function (s) { return elemE(a, s); }, function (b) { return function (s) { return (b ? insert(s) : remove(s)); }; }); | ||
}); | ||
}; | ||
@@ -66,0 +65,0 @@ exports.atReadonlySet = atReadonlySet; |
@@ -5,2 +5,3 @@ /** | ||
import { Option } from 'fp-ts/lib/Option' | ||
import { ReadonlyRecord } from 'fp-ts/lib/ReadonlyRecord' | ||
import { At } from '..' | ||
@@ -11,2 +12,2 @@ /** | ||
*/ | ||
export declare const atReadonlyRecord: <A = never>() => At<Readonly<Record<string, A>>, string, Option<A>> | ||
export declare const atReadonlyRecord: <A = never>() => At<ReadonlyRecord<string, A>, string, Option<A>> |
@@ -989,3 +989,3 @@ import { Foldable, Foldable1, Foldable2, Foldable3 } from 'fp-ts/lib/Foldable' | ||
readonly all: (p: Predicate<A>) => Predicate<S> | ||
private foldMapFirst | ||
private readonly foldMapFirst | ||
constructor(foldMap: <M>(M: Monoid<M>) => (f: (a: A) => M) => (s: S) => M) | ||
@@ -992,0 +992,0 @@ /** |
/** | ||
* @since 2.2.0 | ||
*/ | ||
import { ReadonlyRecord } from 'fp-ts/lib/ReadonlyRecord' | ||
import { Index } from '..' | ||
@@ -9,2 +10,2 @@ /** | ||
*/ | ||
export declare const indexReadonlyRecord: <A = never>() => Index<Readonly<Record<string, A>>, string, A> | ||
export declare const indexReadonlyRecord: <A = never>() => Index<ReadonlyRecord<string, A>, string, A> |
"use strict"; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.atReadonlyRecord = exports.indexReadonlyRecord = exports.indexReadonlyArray = exports.fromTraversable = exports.traversalComposeTraversal = exports.findFirst = exports.optionalComposeOptional = exports.optionalModify = exports.optionalModifyOption = exports.optionalAsTraversal = exports.prismLeft = exports.prismRight = exports.prismSome = exports.prismFromPredicate = exports.prismFromNullable = exports.prismComposeLens = exports.prismSet = exports.prismModify = exports.prismModifyOption = exports.prismAsTraversal = exports.prismAsOptional = exports.lensComponent = exports.lensProps = exports.lensProp = exports.lensId = exports.lensComposePrism = exports.lensComposeLens = exports.lensAsTraversal = exports.lensAsOptional = exports.isoAsOptional = exports.isoAsLens = void 0; | ||
var A = require("fp-ts/lib/Array"); // TODO: replace with ReadonlyArray in v3 | ||
var R = require("fp-ts/lib/Record"); // TODO: replace with ReadonlyRecord in v3 | ||
exports.atReadonlyRecord = exports.at = exports.indexReadonlyRecord = exports.indexReadonlyNonEmptyArray = exports.indexReadonlyArray = exports.index = exports.traversalTraverse = exports.fromTraversable = exports.traversalComposeTraversal = exports.traversal = exports.optionalFindFirstNonEmpty = exports.optionalFindFirst = exports.optionalKey = exports.optionalIndexNonEmpty = exports.optionalIndex = exports.optionalComposeOptional = exports.optionalModify = exports.optionalModifyOption = exports.optionalAsTraversal = exports.optional = exports.prismLeft = exports.prismRight = exports.prismSome = exports.prismFromPredicate = exports.prismFromNullable = exports.prismComposeLens = exports.prismSet = exports.prismModify = exports.prismModifyOption = exports.prismAsTraversal = exports.prismAsOptional = exports.prism = exports.lensAtKey = exports.lensComponent = exports.lensProps = exports.lensProp = exports.lensId = exports.lensComposePrism = exports.prismComposePrism = exports.lensComposeLens = exports.lensAsTraversal = exports.lensAsOptional = exports.lens = exports.isoAsTraversal = exports.isoAsOptional = exports.isoAsPrism = exports.isoAsLens = exports.iso = void 0; | ||
var RA = require("fp-ts/lib/ReadonlyArray"); | ||
var RR = require("fp-ts/lib/ReadonlyRecord"); | ||
var function_1 = require("fp-ts/lib/function"); | ||
@@ -14,13 +21,25 @@ var O = require("fp-ts/lib/Option"); | ||
/** @internal */ | ||
var isoAsLens = function (sa) { return ({ | ||
get: sa.get, | ||
set: function_1.flow(sa.reverseGet, function_1.constant) | ||
var iso = function (get, reverseGet) { return ({ | ||
get: get, | ||
reverseGet: reverseGet | ||
}); }; | ||
exports.iso = iso; | ||
/** @internal */ | ||
var isoAsLens = function (sa) { return exports.lens(sa.get, function_1.flow(sa.reverseGet, function_1.constant)); }; | ||
exports.isoAsLens = isoAsLens; | ||
/** @internal */ | ||
var isoAsOptional = function (sa) { return ({ | ||
getOption: function_1.flow(sa.get, O.some), | ||
set: function_1.flow(sa.reverseGet, function_1.constant) | ||
}); }; | ||
var isoAsPrism = function (sa) { return exports.prism(function_1.flow(sa.get, O.some), sa.reverseGet); }; | ||
exports.isoAsPrism = isoAsPrism; | ||
/** @internal */ | ||
var isoAsOptional = function (sa) { | ||
return exports.optional(function_1.flow(sa.get, O.some), function_1.flow(sa.reverseGet, function_1.constant)); | ||
}; | ||
exports.isoAsOptional = isoAsOptional; | ||
/** @internal */ | ||
var isoAsTraversal = function (sa) { | ||
return exports.traversal(function (F) { return function (f) { return function (s) { | ||
return F.map(f(sa.get(s)), function (a) { return sa.reverseGet(a); }); | ||
}; }; }); | ||
}; | ||
exports.isoAsTraversal = isoAsTraversal; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -30,19 +49,23 @@ // Lens | ||
/** @internal */ | ||
var lensAsOptional = function (sa) { return ({ | ||
getOption: function_1.flow(sa.get, O.some), | ||
set: sa.set | ||
}); }; | ||
var lens = function (get, set) { return ({ get: get, set: set }); }; | ||
exports.lens = lens; | ||
/** @internal */ | ||
var lensAsOptional = function (sa) { return exports.optional(function_1.flow(sa.get, O.some), sa.set); }; | ||
exports.lensAsOptional = lensAsOptional; | ||
/** @internal */ | ||
var lensAsTraversal = function (sa) { return ({ | ||
modifyF: function (F) { return function (f) { return function (s) { return F.map(f(sa.get(s)), function (a) { return sa.set(a)(s); }); }; }; } | ||
}); }; | ||
var lensAsTraversal = function (sa) { | ||
return exports.traversal(function (F) { return function (f) { return function (s) { return F.map(f(sa.get(s)), function (a) { return sa.set(a)(s); }); }; }; }); | ||
}; | ||
exports.lensAsTraversal = lensAsTraversal; | ||
/** @internal */ | ||
var lensComposeLens = function (ab) { return function (sa) { return ({ | ||
get: function (s) { return ab.get(sa.get(s)); }, | ||
set: function (b) { return function (s) { return sa.set(ab.set(b)(sa.get(s)))(s); }; } | ||
}); }; }; | ||
var lensComposeLens = function (ab) { return function (sa) { | ||
return exports.lens(function (s) { return ab.get(sa.get(s)); }, function (b) { return function (s) { return sa.set(ab.set(b)(sa.get(s)))(s); }; }); | ||
}; }; | ||
exports.lensComposeLens = lensComposeLens; | ||
/** @internal */ | ||
var prismComposePrism = function (ab) { return function (sa) { | ||
return exports.prism(function_1.flow(sa.getOption, O.chain(ab.getOption)), function_1.flow(ab.reverseGet, sa.reverseGet)); | ||
}; }; | ||
exports.prismComposePrism = prismComposePrism; | ||
/** @internal */ | ||
var lensComposePrism = function (ab) { return function (sa) { | ||
@@ -53,19 +76,15 @@ return exports.optionalComposeOptional(exports.prismAsOptional(ab))(exports.lensAsOptional(sa)); | ||
/** @internal */ | ||
var lensId = function () { return ({ | ||
get: function_1.identity, | ||
set: function_1.constant | ||
}); }; | ||
var lensId = function () { return exports.lens(function_1.identity, function_1.constant); }; | ||
exports.lensId = lensId; | ||
/** @internal */ | ||
var lensProp = function (prop) { return function (lens) { return ({ | ||
get: function (s) { return lens.get(s)[prop]; }, | ||
set: function (ap) { return function (s) { | ||
var lensProp = function (prop) { return function (sa) { | ||
return exports.lens(function (s) { return sa.get(s)[prop]; }, function (ap) { return function (s) { | ||
var _a; | ||
var oa = lens.get(s); | ||
var oa = sa.get(s); | ||
if (ap === oa[prop]) { | ||
return s; | ||
} | ||
return lens.set(Object.assign({}, oa, (_a = {}, _a[prop] = ap, _a)))(s); | ||
}; } | ||
}); }; }; | ||
return sa.set(Object.assign({}, oa, (_a = {}, _a[prop] = ap, _a)))(s); | ||
}; }); | ||
}; }; | ||
exports.lensProp = lensProp; | ||
@@ -78,5 +97,5 @@ /** @internal */ | ||
} | ||
return function (lens) { return ({ | ||
get: function (s) { | ||
var a = lens.get(s); | ||
return function (sa) { | ||
return exports.lens(function (s) { | ||
var a = sa.get(s); | ||
var r = {}; | ||
@@ -88,21 +107,19 @@ for (var _i = 0, props_1 = props; _i < props_1.length; _i++) { | ||
return r; | ||
}, | ||
set: function (a) { return function (s) { | ||
var oa = lens.get(s); | ||
}, function (a) { return function (s) { | ||
var oa = sa.get(s); | ||
for (var _i = 0, props_2 = props; _i < props_2.length; _i++) { | ||
var k = props_2[_i]; | ||
if (a[k] !== oa[k]) { | ||
return lens.set(Object.assign({}, oa, a))(s); | ||
return sa.set(Object.assign({}, oa, a))(s); | ||
} | ||
} | ||
return s; | ||
}; } | ||
}); }; | ||
}; }); | ||
}; | ||
}; | ||
exports.lensProps = lensProps; | ||
/** @internal */ | ||
var lensComponent = function (prop) { return function (lens) { return ({ | ||
get: function (s) { return lens.get(s)[prop]; }, | ||
set: function (ap) { return function (s) { | ||
var oa = lens.get(s); | ||
var lensComponent = function (prop) { return function (sa) { | ||
return exports.lens(function (s) { return sa.get(s)[prop]; }, function (ap) { return function (s) { | ||
var oa = sa.get(s); | ||
if (ap === oa[prop]) { | ||
@@ -113,6 +130,11 @@ return s; | ||
copy[prop] = ap; | ||
return lens.set(copy)(s); | ||
}; } | ||
}); }; }; | ||
return sa.set(copy)(s); | ||
}; }); | ||
}; }; | ||
exports.lensComponent = lensComponent; | ||
/** @internal */ | ||
var lensAtKey = function (key) { return function (sa) { | ||
return pipeable_1.pipe(sa, exports.lensComposeLens(atReadonlyRecord().at(key))); | ||
}; }; | ||
exports.lensAtKey = lensAtKey; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -122,13 +144,13 @@ // Prism | ||
/** @internal */ | ||
var prismAsOptional = function (sa) { return ({ | ||
getOption: sa.getOption, | ||
set: function (a) { return exports.prismSet(a)(sa); } | ||
}); }; | ||
var prism = function (getOption, reverseGet) { return ({ getOption: getOption, reverseGet: reverseGet }); }; | ||
exports.prism = prism; | ||
/** @internal */ | ||
var prismAsOptional = function (sa) { return exports.optional(sa.getOption, function (a) { return exports.prismSet(a)(sa); }); }; | ||
exports.prismAsOptional = prismAsOptional; | ||
/** @internal */ | ||
var prismAsTraversal = function (sa) { return ({ | ||
modifyF: function (F) { return function (f) { return function (s) { | ||
var prismAsTraversal = function (sa) { | ||
return exports.traversal(function (F) { return function (f) { return function (s) { | ||
return pipeable_1.pipe(sa.getOption(s), O.fold(function () { return F.of(s); }, function (a) { return F.map(f(a), function (a) { return exports.prismSet(a)(sa)(s); }); })); | ||
}; }; } | ||
}); }; | ||
}; }; }); | ||
}; | ||
exports.prismAsTraversal = prismAsTraversal; | ||
@@ -160,32 +182,20 @@ /** @internal */ | ||
/** @internal */ | ||
var prismFromNullable = function () { return ({ | ||
getOption: O.fromNullable, | ||
reverseGet: function_1.identity | ||
}); }; | ||
var prismFromNullable = function () { return exports.prism(O.fromNullable, function_1.identity); }; | ||
exports.prismFromNullable = prismFromNullable; | ||
/** @internal */ | ||
function prismFromPredicate(predicate) { | ||
return { | ||
getOption: O.fromPredicate(predicate), | ||
reverseGet: function_1.identity | ||
}; | ||
} | ||
var prismFromPredicate = function (predicate) { | ||
return exports.prism(O.fromPredicate(predicate), function_1.identity); | ||
}; | ||
exports.prismFromPredicate = prismFromPredicate; | ||
/** @internal */ | ||
var prismSome = function () { return ({ | ||
getOption: function_1.identity, | ||
reverseGet: O.some | ||
}); }; | ||
var prismSome = function () { return exports.prism(function_1.identity, O.some); }; | ||
exports.prismSome = prismSome; | ||
/** @internal */ | ||
var prismRight = function () { return ({ | ||
getOption: O.fromEither, | ||
reverseGet: E.right | ||
}); }; | ||
var prismRight = function () { return exports.prism(O.fromEither, E.right); }; | ||
exports.prismRight = prismRight; | ||
/** @internal */ | ||
var prismLeft = function () { return ({ | ||
getOption: function (s) { return (E.isLeft(s) ? O.some(s.left) : O.none); }, | ||
reverseGet: E.left | ||
}); }; | ||
var prismLeft = function () { | ||
return exports.prism(function (s) { return (E.isLeft(s) ? O.some(s.left) : O.none); }, // TODO: replace with E.getLeft in v3 | ||
E.left); | ||
}; | ||
exports.prismLeft = prismLeft; | ||
@@ -196,7 +206,13 @@ // ------------------------------------------------------------------------------------- | ||
/** @internal */ | ||
var optionalAsTraversal = function (sa) { return ({ | ||
modifyF: function (F) { return function (f) { return function (s) { | ||
var optional = function (getOption, set) { return ({ | ||
getOption: getOption, | ||
set: set | ||
}); }; | ||
exports.optional = optional; | ||
/** @internal */ | ||
var optionalAsTraversal = function (sa) { | ||
return exports.traversal(function (F) { return function (f) { return function (s) { | ||
return pipeable_1.pipe(sa.getOption(s), O.fold(function () { return F.of(s); }, function (a) { return F.map(f(a), function (a) { return sa.set(a)(s); }); })); | ||
}; }; } | ||
}); }; | ||
}; }; }); | ||
}; | ||
exports.optionalAsTraversal = optionalAsTraversal; | ||
@@ -220,15 +236,43 @@ /** @internal */ | ||
/** @internal */ | ||
var optionalComposeOptional = function (ab) { return function (sa) { return ({ | ||
getOption: function_1.flow(sa.getOption, O.chain(ab.getOption)), | ||
set: function (b) { return exports.optionalModify(ab.set(b))(sa); } | ||
}); }; }; | ||
var optionalComposeOptional = function (ab) { return function (sa) { | ||
return exports.optional(function_1.flow(sa.getOption, O.chain(ab.getOption)), function (b) { return exports.optionalModify(ab.set(b))(sa); }); | ||
}; }; | ||
exports.optionalComposeOptional = optionalComposeOptional; | ||
var findFirstMutable = function (predicate) { return ({ | ||
getOption: A.findFirst(predicate), | ||
set: function (a) { return function (s) { | ||
return pipeable_1.pipe(A.findIndex(predicate)(s), O.fold(function () { return s; }, function (i) { return A.unsafeUpdateAt(i, a, s); })); | ||
}; } | ||
}); }; | ||
/** @internal */ | ||
exports.findFirst = findFirstMutable; | ||
var optionalIndex = function (i) { return function (sa) { | ||
return pipeable_1.pipe(sa, exports.optionalComposeOptional(exports.indexReadonlyArray().index(i))); | ||
}; }; | ||
exports.optionalIndex = optionalIndex; | ||
/** @internal */ | ||
var optionalIndexNonEmpty = function (i) { return function (sa) { return pipeable_1.pipe(sa, exports.optionalComposeOptional(exports.indexReadonlyNonEmptyArray().index(i))); }; }; | ||
exports.optionalIndexNonEmpty = optionalIndexNonEmpty; | ||
/** @internal */ | ||
var optionalKey = function (key) { return function (sa) { | ||
return pipeable_1.pipe(sa, exports.optionalComposeOptional(exports.indexReadonlyRecord().index(key))); | ||
}; }; | ||
exports.optionalKey = optionalKey; | ||
/** @internal */ | ||
var optionalFindFirst = function (predicate) { | ||
return exports.optional(RA.findFirst(predicate), function (a) { return function (s) { | ||
return pipeable_1.pipe(RA.findIndex(predicate)(s), O.fold(function () { return s; }, function (i) { return RA.unsafeUpdateAt(i, a, s); })); | ||
}; }); | ||
}; | ||
exports.optionalFindFirst = optionalFindFirst; | ||
var unsafeUpdateAt = function (i, a, as) { | ||
if (as[i] === a) { | ||
return as; | ||
} | ||
else { | ||
var xs = __spreadArrays([as[0]], as.slice(1)); | ||
xs[i] = a; | ||
return xs; | ||
} | ||
}; | ||
/** @internal */ | ||
var optionalFindFirstNonEmpty = function (predicate) { | ||
return exports.optional(RA.findFirst(predicate), function (a) { return function (as) { | ||
return pipeable_1.pipe(RA.findIndex(predicate)(as), O.fold(function () { return as; }, function (i) { return unsafeUpdateAt(i, a, as); })); | ||
}; }); | ||
}; | ||
exports.optionalFindFirstNonEmpty = optionalFindFirstNonEmpty; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -238,6 +282,9 @@ // Traversal | ||
/** @internal */ | ||
var traversal = function (modifyF) { return ({ | ||
modifyF: modifyF | ||
}); }; | ||
exports.traversal = traversal; | ||
/** @internal */ | ||
function traversalComposeTraversal(ab) { | ||
return function (sa) { return ({ | ||
modifyF: function (F) { return function (f) { return sa.modifyF(F)(ab.modifyF(F)(f)); }; } | ||
}); }; | ||
return function (sa) { return exports.traversal(function (F) { return function (f) { return sa.modifyF(F)(ab.modifyF(F)(f)); }; }); }; | ||
} | ||
@@ -247,39 +294,50 @@ exports.traversalComposeTraversal = traversalComposeTraversal; | ||
function fromTraversable(T) { | ||
return function () { return ({ | ||
modifyF: function (F) { | ||
return function () { | ||
return exports.traversal(function (F) { | ||
var traverseF = T.traverse(F); | ||
return function (f) { return function (s) { return traverseF(s, f); }; }; | ||
} | ||
}); }; | ||
}); | ||
}; | ||
} | ||
exports.fromTraversable = fromTraversable; | ||
/** @internal */ | ||
function traversalTraverse(T) { | ||
return traversalComposeTraversal(fromTraversable(T)()); | ||
} | ||
exports.traversalTraverse = traversalTraverse; | ||
// ------------------------------------------------------------------------------------- | ||
// Ix | ||
// ------------------------------------------------------------------------------------- | ||
function indexMutableArray() { | ||
return { | ||
index: function (i) { return ({ | ||
getOption: function (as) { return A.lookup(i, as); }, | ||
set: function (a) { return function (as) { | ||
return pipeable_1.pipe(A.updateAt(i, a)(as), O.getOrElse(function () { return as; })); | ||
}; } | ||
}); } | ||
}; | ||
} | ||
/** @internal */ | ||
exports.indexReadonlyArray = indexMutableArray; | ||
var index = function (index) { return ({ index: index }); }; | ||
exports.index = index; | ||
/** @internal */ | ||
function indexReadonlyRecord() { | ||
return { | ||
index: function (k) { return ({ | ||
getOption: function (r) { return R.lookup(k, r); }, | ||
set: function (a) { return function (r) { | ||
if (r[k] === a || O.isNone(R.lookup(k, r))) { | ||
return r; | ||
} | ||
return R.insertAt(k, a)(r); | ||
}; } | ||
}); } | ||
}; | ||
} | ||
var indexReadonlyArray = function () { | ||
return exports.index(function (i) { | ||
return exports.optional(function (as) { return RA.lookup(i, as); }, function (a) { return function (as) { | ||
return pipeable_1.pipe(RA.lookup(i, as), O.fold(function () { return as; }, function () { return RA.unsafeUpdateAt(i, a, as); })); | ||
}; }); | ||
}); | ||
}; | ||
exports.indexReadonlyArray = indexReadonlyArray; | ||
/** @internal */ | ||
var indexReadonlyNonEmptyArray = function () { | ||
return exports.index(function (i) { | ||
return exports.optional(function (as) { return RA.lookup(i, as); }, function (a) { return function (as) { | ||
return pipeable_1.pipe(RA.lookup(i, as), O.fold(function () { return as; }, function () { return unsafeUpdateAt(i, a, as); })); | ||
}; }); | ||
}); | ||
}; | ||
exports.indexReadonlyNonEmptyArray = indexReadonlyNonEmptyArray; | ||
/** @internal */ | ||
var indexReadonlyRecord = function () { | ||
return exports.index(function (k) { | ||
return exports.optional(function (r) { return RR.lookup(k, r); }, function (a) { return function (r) { | ||
if (r[k] === a || O.isNone(RR.lookup(k, r))) { | ||
return r; | ||
} | ||
return RR.insertAt(k, a)(r); | ||
}; }); | ||
}); | ||
}; | ||
exports.indexReadonlyRecord = indexReadonlyRecord; | ||
@@ -290,10 +348,10 @@ // ------------------------------------------------------------------------------------- | ||
/** @internal */ | ||
var at = function (at) { return ({ at: at }); }; | ||
exports.at = at; | ||
/** @internal */ | ||
function atReadonlyRecord() { | ||
return { | ||
at: function (key) { return ({ | ||
get: function (r) { return R.lookup(key, r); }, | ||
set: O.fold(function () { return R.deleteAt(key); }, function (a) { return R.insertAt(key, a); }) | ||
}); } | ||
}; | ||
return exports.at(function (key) { | ||
return exports.lens(function (r) { return RR.lookup(key, r); }, O.fold(function () { return RR.deleteAt(key); }, function (a) { return RR.insertAt(key, a); })); | ||
}); | ||
} | ||
exports.atReadonlyRecord = atReadonlyRecord; |
196
lib/Iso.d.ts
@@ -0,5 +1,28 @@ | ||
/** | ||
* **This module is experimental** | ||
* | ||
* Experimental features are published in order to get early feedback from the community. | ||
* | ||
* A feature tagged as _Experimental_ is in a high state of flux, you're at risk of it changing without notice. | ||
* | ||
* An `Iso` is an optic which converts elements of type `S` into elements of type `A` without loss. | ||
* | ||
* Laws: | ||
* | ||
* 1. `reverseGet(get(s)) = s` | ||
* 2. `get(reversetGet(a)) = a` | ||
* | ||
* @since 2.3.0 | ||
*/ | ||
import { Category2 } from 'fp-ts/lib/Category' | ||
import { Either } from 'fp-ts/lib/Either' | ||
import { Predicate, Refinement } from 'fp-ts/lib/function' | ||
import { Functor, Functor1, Functor2, Functor3 } from 'fp-ts/lib/Functor' | ||
import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from 'fp-ts/lib/HKT' | ||
import { Invariant2 } from 'fp-ts/lib/Invariant' | ||
import { Option } from 'fp-ts/lib/Option' | ||
import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray' | ||
import { ReadonlyRecord } from 'fp-ts/lib/ReadonlyRecord' | ||
import { Semigroupoid2 } from 'fp-ts/lib/Semigroupoid' | ||
import { Traversable1 } from 'fp-ts/lib/Traversable' | ||
import { Lens } from './Lens' | ||
@@ -19,2 +42,7 @@ import { Optional } from './Optional' | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const iso: <S, A>(get: Iso<S, A>['get'], reverseGet: Iso<S, A>['reverseGet']) => Iso<S, A> | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
@@ -24,3 +52,3 @@ */ | ||
/** | ||
* View an `Iso` as a `Lens` | ||
* View an `Iso` as a `Lens`. | ||
* | ||
@@ -32,3 +60,3 @@ * @category converters | ||
/** | ||
* View an `Iso` as a `Prism` | ||
* View an `Iso` as a `Prism`. | ||
* | ||
@@ -40,3 +68,3 @@ * @category converters | ||
/** | ||
* View an `Iso` as a `Optional` | ||
* View an `Iso` as a `Optional`. | ||
* | ||
@@ -48,3 +76,3 @@ * @category converters | ||
/** | ||
* View an `Iso` as a `Traversal` | ||
* View an `Iso` as a `Traversal`. | ||
* | ||
@@ -56,3 +84,3 @@ * @category converters | ||
/** | ||
* Compose an `Iso` with an `Iso` | ||
* Compose an `Iso` with an `Iso`. | ||
* | ||
@@ -64,2 +92,37 @@ * @category compositions | ||
/** | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeIso: <A, B>(ab: Iso<A, B>) => <S>(sa: Iso<S, A>) => Iso<S, B> | ||
/** | ||
* Compose an `Iso` with a `Lens`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeLens: <A, B>(ab: Lens<A, B>) => <S>(sa: Iso<S, A>) => Lens<S, B> | ||
/** | ||
* Compose an `Iso` with a `Prism`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composePrism: <A, B>(ab: Prism<A, B>) => <S>(sa: Iso<S, A>) => Prism<S, B> | ||
/** | ||
* Compose an `Iso` with a `Optional`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeOptional: <A, B>(ab: Optional<A, B>) => <S>(sa: Iso<S, A>) => Optional<S, B> | ||
/** | ||
* Compose an `Iso` with a `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeTraversal: <A, B>(ab: Traversal<A, B>) => <S>(sa: Iso<S, A>) => Traversal<S, B> | ||
/** | ||
* @category constructors | ||
@@ -91,2 +154,116 @@ * @since 2.3.0 | ||
/** | ||
* Return a `Prism` from a `Iso` focused on a nullable value. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const fromNullable: <S, A>(sa: Iso<S, A>) => Prism<S, NonNullable<A>> | ||
/** | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function filter<A, B extends A>(refinement: Refinement<A, B>): <S>(sa: Iso<S, A>) => Prism<S, B> | ||
export declare function filter<A>(predicate: Predicate<A>): <S>(sa: Iso<S, A>) => Prism<S, A> | ||
/** | ||
* Return a `Lens` from a `Iso` and a prop. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const prop: <A, P extends keyof A>(prop: P) => <S>(sa: Iso<S, A>) => Lens<S, A[P]> | ||
/** | ||
* Return a `Lens` from a `Iso` and a list of props. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const props: <A, P extends keyof A>( | ||
props_0: P, | ||
props_1: P, | ||
...props_2: P[] | ||
) => <S>(sa: Iso<S, A>) => Lens<S, { [K in P]: A[K] }> | ||
/** | ||
* Return a `Lens` from a `Iso` focused on a component of a tuple. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const component: <A extends readonly unknown[], P extends keyof A>( | ||
prop: P | ||
) => <S>(sa: Iso<S, A>) => Lens<S, A[P]> | ||
/** | ||
* Return a `Optional` from a `Iso` focused on an index of a `ReadonlyArray`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const index: (i: number) => <S, A>(sa: Iso<S, readonly A[]>) => Optional<S, A> | ||
/** | ||
* Return a `Optional` from a `Iso` focused on an index of a `ReadonlyNonEmptyArray`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const indexNonEmpty: (i: number) => <S, A>(sa: Iso<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* Return a `Optional` from a `Iso` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const key: (key: string) => <S, A>(sa: Iso<S, Readonly<Record<string, A>>>) => Optional<S, A> | ||
/** | ||
* Return a `Lens` from a `Iso` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const atKey: (key: string) => <S, A>(sa: Iso<S, Readonly<Record<string, A>>>) => Lens<S, Option<A>> | ||
/** | ||
* Return a `Prism` from a `Iso` focused on the `Some` of a `Option` type. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const some: <S, A>(soa: Iso<S, Option<A>>) => Prism<S, A> | ||
/** | ||
* Return a `Prism` from a `Iso` focused on the `Right` of a `Either` type. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const right: <S, E, A>(sea: Iso<S, Either<E, A>>) => Prism<S, A> | ||
/** | ||
* Return a `Prism` from a `Iso` focused on the `Left` of a `Either` type. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const left: <S, E, A>(sea: Iso<S, Either<E, A>>) => Prism<S, E> | ||
/** | ||
* Return a `Traversal` from a `Iso` focused on a `Traversable`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function traverse<T extends URIS>(T: Traversable1<T>): <S, A>(sta: Iso<S, Kind<T, A>>) => Traversal<S, A> | ||
/** | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function findFirst<A, B extends A>( | ||
refinement: Refinement<A, B> | ||
): <S>(sa: Iso<S, ReadonlyArray<A>>) => Optional<S, B> | ||
export declare function findFirst<A>(predicate: Predicate<A>): <S>(sa: Iso<S, ReadonlyArray<A>>) => Optional<S, A> | ||
/** | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function findFirstNonEmpty<A, B extends A>( | ||
refinement: Refinement<A, B> | ||
): <S>(sa: Iso<S, ReadonlyNonEmptyArray<A>>) => Optional<S, B> | ||
export declare function findFirstNonEmpty<A>( | ||
predicate: Predicate<A> | ||
): <S>(sa: Iso<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* @category Invariant | ||
@@ -115,7 +292,12 @@ * @since 2.3.0 | ||
*/ | ||
export declare const invariantIso: Invariant2<URI> | ||
export declare const Invariant: Invariant2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
export declare const Semigroupoid: Semigroupoid2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
export declare const categoryIso: Category2<URI> | ||
export declare const Category: Category2<URI> |
246
lib/Iso.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.categoryIso = exports.invariantIso = exports.URI = exports.imap = exports.modifyF = exports.modify = exports.reverse = exports.compose = exports.asTraversal = exports.asOptional = exports.asPrism = exports.asLens = exports.id = void 0; | ||
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; | ||
var function_1 = require("fp-ts/lib/function"); | ||
var O = require("fp-ts/lib/Option"); | ||
var pipeable_1 = require("fp-ts/lib/pipeable"); | ||
var _ = require("./internal"); | ||
@@ -12,8 +12,10 @@ // ------------------------------------------------------------------------------------- | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
exports.iso = _.iso; | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
*/ | ||
var id = function () { return ({ | ||
get: function_1.identity, | ||
reverseGet: function_1.identity | ||
}); }; | ||
var id = function () { return exports.iso(function_1.identity, function_1.identity); }; | ||
exports.id = id; | ||
@@ -24,3 +26,3 @@ // ------------------------------------------------------------------------------------- | ||
/** | ||
* View an `Iso` as a `Lens` | ||
* View an `Iso` as a `Lens`. | ||
* | ||
@@ -32,3 +34,3 @@ * @category converters | ||
/** | ||
* View an `Iso` as a `Prism` | ||
* View an `Iso` as a `Prism`. | ||
* | ||
@@ -38,9 +40,5 @@ * @category converters | ||
*/ | ||
var asPrism = function (sa) { return ({ | ||
getOption: function_1.flow(sa.get, O.some), | ||
reverseGet: sa.reverseGet | ||
}); }; | ||
exports.asPrism = asPrism; | ||
exports.asPrism = _.isoAsPrism; | ||
/** | ||
* View an `Iso` as a `Optional` | ||
* View an `Iso` as a `Optional`. | ||
* | ||
@@ -52,3 +50,3 @@ * @category converters | ||
/** | ||
* View an `Iso` as a `Traversal` | ||
* View an `Iso` as a `Traversal`. | ||
* | ||
@@ -58,6 +56,3 @@ * @category converters | ||
*/ | ||
var asTraversal = function (sa) { return ({ | ||
modifyF: function (F) { return function (f) { return function (s) { return F.map(f(sa.get(s)), function (a) { return sa.reverseGet(a); }); }; }; } | ||
}); }; | ||
exports.asTraversal = asTraversal; | ||
exports.asTraversal = _.isoAsTraversal; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -67,3 +62,3 @@ // compositions | ||
/** | ||
* Compose an `Iso` with an `Iso` | ||
* Compose an `Iso` with an `Iso`. | ||
* | ||
@@ -73,7 +68,53 @@ * @category compositions | ||
*/ | ||
var compose = function (ab) { return function (sa) { return ({ | ||
get: function_1.flow(sa.get, ab.get), | ||
reverseGet: function_1.flow(ab.reverseGet, sa.reverseGet) | ||
}); }; }; | ||
var compose = function (ab) { return function (sa) { | ||
return exports.iso(function_1.flow(sa.get, ab.get), 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 | ||
*/ | ||
var composeLens = function (ab) { | ||
return function_1.flow(exports.asLens, _.lensComposeLens(ab)); | ||
}; | ||
exports.composeLens = composeLens; | ||
/** | ||
* Compose an `Iso` with a `Prism`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
var composePrism = function (ab) { | ||
return function_1.flow(exports.asPrism, _.prismComposePrism(ab)); | ||
}; | ||
exports.composePrism = composePrism; | ||
/** | ||
* Compose an `Iso` with a `Optional`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
var composeOptional = function (ab) { | ||
return function_1.flow(exports.asOptional, _.optionalComposeOptional(ab)); | ||
}; | ||
exports.composeOptional = composeOptional; | ||
/** | ||
* Compose an `Iso` with a `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
var composeTraversal = function (ab) { | ||
return function_1.flow(exports.asTraversal, _.traversalComposeTraversal(ab)); | ||
}; | ||
exports.composeTraversal = composeTraversal; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -86,6 +127,3 @@ // combinators | ||
*/ | ||
var reverse = function (sa) { return ({ | ||
get: sa.reverseGet, | ||
reverseGet: sa.get | ||
}); }; | ||
var reverse = function (sa) { return exports.iso(sa.reverseGet, sa.get); }; | ||
exports.reverse = reverse; | ||
@@ -99,5 +137,136 @@ /** | ||
function modifyF(F) { | ||
return function (f) { return function (sa) { return function (s) { return function_1.pipe(sa.get(s), f, function (fa) { return F.map(fa, sa.reverseGet); }); }; }; }; | ||
return function (f) { return function (sa) { return function (s) { return pipeable_1.pipe(sa.get(s), f, function (fa) { return 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 | ||
*/ | ||
var fromNullable = function (sa) { | ||
return exports.composePrism(_.prismFromNullable())(sa); | ||
}; | ||
exports.fromNullable = fromNullable; | ||
function filter(predicate) { | ||
return exports.composePrism(_.prismFromPredicate(predicate)); | ||
} | ||
exports.filter = filter; | ||
/** | ||
* Return a `Lens` from a `Iso` and a prop. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
var prop = function (prop) { | ||
return 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 | ||
*/ | ||
var props = function () { | ||
var props = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
props[_i] = arguments[_i]; | ||
} | ||
return function_1.flow(exports.asLens, _.lensProps.apply(_, props)); | ||
}; | ||
exports.props = props; | ||
/** | ||
* Return a `Lens` from a `Iso` focused on a component of a tuple. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
var component = function (prop) { return 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 | ||
*/ | ||
var index = function (i) { | ||
return 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 | ||
*/ | ||
var indexNonEmpty = function (i) { | ||
return 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 | ||
*/ | ||
var key = function (key) { | ||
return 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 | ||
*/ | ||
var atKey = function (key) { | ||
return 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__*/ | ||
exports.composePrism(_.prismSome()); | ||
/** | ||
* Return a `Prism` from a `Iso` focused on the `Right` of a `Either` type. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
exports.right = | ||
/*#__PURE__*/ | ||
exports.composePrism(_.prismRight()); | ||
/** | ||
* Return a `Prism` from a `Iso` focused on the `Left` of a `Either` type. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
exports.left = | ||
/*#__PURE__*/ | ||
exports.composePrism(_.prismLeft()); | ||
/** | ||
* Return a `Traversal` from a `Iso` focused on a `Traversable`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
function traverse(T) { | ||
return function_1.flow(exports.asTraversal, _.traversalTraverse(T)); | ||
} | ||
exports.traverse = traverse; | ||
function findFirst(predicate) { | ||
return exports.composeOptional(_.optionalFindFirst(predicate)); | ||
} | ||
exports.findFirst = findFirst; | ||
function findFirstNonEmpty(predicate) { | ||
return exports.composeOptional(_.optionalFindFirstNonEmpty(predicate)); | ||
} | ||
exports.findFirstNonEmpty = findFirstNonEmpty; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -117,6 +286,3 @@ // pipeables | ||
// ------------------------------------------------------------------------------------- | ||
var imap_ = function (ea, ab, ba) { return ({ | ||
get: function_1.flow(ea.get, ab), | ||
reverseGet: function_1.flow(ba, ea.reverseGet) | ||
}); }; | ||
var imap_ = function (ea, ab, ba) { return exports.iso(function_1.flow(ea.get, ab), function_1.flow(ba, ea.reverseGet)); }; | ||
/** | ||
@@ -131,3 +297,3 @@ * @category instances | ||
*/ | ||
exports.invariantIso = { | ||
exports.Invariant = { | ||
URI: exports.URI, | ||
@@ -138,8 +304,16 @@ imap: imap_ | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
exports.Semigroupoid = { | ||
URI: exports.URI, | ||
compose: function (ab, ea) { return exports.compose(ab)(ea); } | ||
}; | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
exports.categoryIso = { | ||
exports.Category = { | ||
URI: exports.URI, | ||
compose: function (ab, ea) { return exports.compose(ab)(ea); }, | ||
compose: exports.Semigroupoid.compose, | ||
id: exports.id | ||
}; |
@@ -16,2 +16,4 @@ /** | ||
import Option = O.Option | ||
import { ReadonlyRecord } from 'fp-ts/lib/ReadonlyRecord' | ||
import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray' | ||
/** | ||
@@ -26,2 +28,7 @@ * @category model | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const index: <S, I, A>(index: Index<S, I, A>['index']) => Index<S, I, A> | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
@@ -31,3 +38,3 @@ */ | ||
/** | ||
* Lift an instance of `Index` using an `Iso` | ||
* Lift an instance of `Index` using an `Iso`. | ||
* | ||
@@ -45,5 +52,10 @@ * @category constructors | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const indexReadonlyNonEmptyArray: <A = never>() => Index<ReadonlyNonEmptyArray<A>, number, A> | ||
/** | ||
* @category constructors | ||
* @since 2.3.7 | ||
*/ | ||
export declare const indexReadonlyRecord: <A = never>() => Index<Readonly<Record<string, A>>, string, A> | ||
export declare const indexReadonlyRecord: <A = never>() => Index<ReadonlyRecord<string, A>, string, A> | ||
/** | ||
@@ -69,2 +81,2 @@ * @category constructors | ||
*/ | ||
export declare const indexRecord: <A = never>() => Index<Readonly<Record<string, A>>, string, A> | ||
export declare const indexRecord: <A = never>() => Index<ReadonlyRecord<string, A>, string, A> |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.indexRecord = exports.indexArray = exports.indexReadonlyMap = exports.indexReadonlyRecord = exports.indexReadonlyArray = exports.fromIso = exports.fromAt = void 0; | ||
exports.indexRecord = exports.indexArray = exports.indexReadonlyMap = exports.indexReadonlyRecord = exports.indexReadonlyNonEmptyArray = exports.indexReadonlyArray = exports.fromIso = exports.fromAt = exports.index = void 0; | ||
/** | ||
@@ -22,10 +22,15 @@ * **This module is experimental** | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
exports.index = _.index; | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
*/ | ||
var fromAt = function (at) { return ({ | ||
index: function (i) { return _.lensComposePrism(_.prismSome())(at.at(i)); } | ||
}); }; | ||
var fromAt = function (at) { | ||
return exports.index(function (i) { return _.lensComposePrism(_.prismSome())(at.at(i)); }); | ||
}; | ||
exports.fromAt = fromAt; | ||
/** | ||
* Lift an instance of `Index` using an `Iso` | ||
* Lift an instance of `Index` using an `Iso`. | ||
* | ||
@@ -35,5 +40,5 @@ * @category constructors | ||
*/ | ||
var fromIso = function (iso) { return function (sia) { return ({ | ||
index: function (i) { return pipeable_1.pipe(iso, _.isoAsOptional, _.optionalComposeOptional(sia.index(i))); } | ||
}); }; }; | ||
var fromIso = function (iso) { return function (sia) { | ||
return exports.index(function (i) { return pipeable_1.pipe(iso, _.isoAsOptional, _.optionalComposeOptional(sia.index(i))); }); | ||
}; }; | ||
exports.fromIso = fromIso; | ||
@@ -47,2 +52,7 @@ /** | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
exports.indexReadonlyNonEmptyArray = _.indexReadonlyNonEmptyArray; | ||
/** | ||
* @category constructors | ||
* @since 2.3.7 | ||
@@ -55,20 +65,16 @@ */ | ||
*/ | ||
var indexReadonlyMap = function (E) { return function () { | ||
var indexReadonlyMap = function (E) { | ||
var lookupE = RM.lookup(E); | ||
var insertAtE = RM.insertAt(E); | ||
return { | ||
index: function (key) { | ||
var lookup = lookupE(key); | ||
return { | ||
getOption: lookup, | ||
set: function (next) { | ||
var insert = insertAtE(key, next); | ||
return function (s) { | ||
return pipeable_1.pipe(lookup(s), O.fold(function () { return s; }, function (prev) { return (next === prev ? s : insert(s)); })); | ||
}; | ||
} | ||
}; | ||
} | ||
return function () { | ||
return exports.index(function (key) { | ||
return _.optional(function (s) { return lookupE(key, s); }, function (next) { | ||
var insert = insertAtE(key, next); | ||
return function (s) { | ||
return pipeable_1.pipe(lookupE(key, s), O.fold(function () { return s; }, function (prev) { return (next === prev ? s : insert(s)); })); | ||
}; | ||
}); | ||
}); | ||
}; | ||
}; }; | ||
}; | ||
exports.indexReadonlyMap = indexReadonlyMap; | ||
@@ -75,0 +81,0 @@ // ------------------------------------------------------------------------------------- |
@@ -28,3 +28,7 @@ /** | ||
import { Option } from 'fp-ts/lib/Option' | ||
import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray' | ||
import { ReadonlyRecord } from 'fp-ts/lib/ReadonlyRecord' | ||
import { Semigroupoid2 } from 'fp-ts/lib/Semigroupoid' | ||
import { Traversable1 } from 'fp-ts/lib/Traversable' | ||
import { Iso } from './Iso' | ||
import { Optional } from './Optional' | ||
@@ -43,2 +47,7 @@ import { Prism } from './Prism' | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const lens: <S, A>(get: Lens<S, A>['get'], set: Lens<S, A>['set']) => Lens<S, A> | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
@@ -48,3 +57,3 @@ */ | ||
/** | ||
* View a `Lens` as a `Optional` | ||
* View a `Lens` as a `Optional`. | ||
* | ||
@@ -56,3 +65,3 @@ * @category converters | ||
/** | ||
* View a `Lens` as a `Traversal` | ||
* View a `Lens` as a `Traversal`. | ||
* | ||
@@ -64,3 +73,3 @@ * @category converters | ||
/** | ||
* Compose a `Lens` with a `Lens` | ||
* Compose a `Lens` with a `Lens`. | ||
* | ||
@@ -72,5 +81,19 @@ * @category compositions | ||
/** | ||
* Compose a `Lens` with a `Prism` | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeLens: <A, B>(ab: Lens<A, B>) => <S>(sa: Lens<S, A>) => Lens<S, B> | ||
/** | ||
* Compose a `Lens` with a `Iso`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeIso: <A, B>(ab: Iso<A, B>) => <S>(sa: Lens<S, A>) => Lens<S, B> | ||
/** | ||
* Compose a `Lens` with a `Prism`. | ||
* | ||
* @category compositions | ||
* @since 2.3.0 | ||
@@ -80,3 +103,3 @@ */ | ||
/** | ||
* Compose a `Lens` with an `Optional` | ||
* Compose a `Lens` with an `Optional`. | ||
* | ||
@@ -88,2 +111,9 @@ * @category compositions | ||
/** | ||
* Compose a `Lens` with an `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeTraversal: <A, B>(ab: Traversal<A, B>) => <S>(sa: Lens<S, A>) => Traversal<S, B> | ||
/** | ||
* @category combinators | ||
@@ -110,3 +140,3 @@ * @since 2.3.0 | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a nullable value | ||
* Return a `Optional` from a `Lens` focused on a nullable value. | ||
* | ||
@@ -124,3 +154,3 @@ * @category combinators | ||
/** | ||
* Return a `Lens` from a `Lens` and a prop | ||
* Return a `Lens` from a `Lens` and a prop. | ||
* | ||
@@ -132,3 +162,3 @@ * @category combinators | ||
/** | ||
* Return a `Lens` from a `Lens` and a list of props | ||
* Return a `Lens` from a `Lens` and a list of props. | ||
* | ||
@@ -139,3 +169,3 @@ * @category combinators | ||
export declare const props: <A, P extends keyof A>( | ||
...props: [P, P, ...Array<P>] | ||
...props: readonly [P, P, ...ReadonlyArray<P>] | ||
) => <S>( | ||
@@ -150,3 +180,3 @@ sa: Lens<S, A> | ||
/** | ||
* Return a `Lens` from a `Lens` and a component | ||
* Return a `Lens` from a `Lens` focused on a component of a tuple. | ||
* | ||
@@ -160,3 +190,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a `ReadonlyArray` | ||
* Return a `Optional` from a `Lens` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -168,5 +198,12 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a `ReadonlyRecord` and a key | ||
* Return a `Optional` from a `Lens` focused on an index of a `ReadonlyNonEmptyArray`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const indexNonEmpty: (i: number) => <S, A>(sa: Lens<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
@@ -176,3 +213,3 @@ */ | ||
/** | ||
* Return a `Lens` from a `Lens` focused on a `ReadonlyRecord` and a required key | ||
* Return a `Lens` from a `Lens` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -182,5 +219,5 @@ * @category combinators | ||
*/ | ||
export declare const atKey: (key: string) => <S, A>(sa: Lens<S, Readonly<Record<string, A>>>) => Lens<S, Option<A>> | ||
export declare const atKey: (key: string) => <S, A>(sa: Lens<S, ReadonlyRecord<string, A>>) => Lens<S, Option<A>> | ||
/** | ||
* Return a `Optional` from a `Lens` focused on the `Some` of a `Option` type | ||
* Return a `Optional` from a `Lens` focused on the `Some` of a `Option` type. | ||
* | ||
@@ -192,3 +229,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on the `Right` of a `Either` type | ||
* Return a `Optional` from a `Lens` focused on the `Right` of a `Either` type. | ||
* | ||
@@ -200,3 +237,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on the `Left` of a `Either` type | ||
* Return a `Optional` from a `Lens` focused on the `Left` of a `Either` type. | ||
* | ||
@@ -208,3 +245,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Lens` focused on a `Traversable` | ||
* Return a `Traversal` from a `Lens` focused on a `Traversable`. | ||
* | ||
@@ -226,2 +263,12 @@ * @category combinators | ||
/** | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function findFirstNonEmpty<A, B extends A>( | ||
refinement: Refinement<A, B> | ||
): <S>(sa: Lens<S, ReadonlyNonEmptyArray<A>>) => Optional<S, B> | ||
export declare function findFirstNonEmpty<A>( | ||
predicate: Predicate<A> | ||
): <S>(sa: Lens<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* @category Invariant | ||
@@ -250,7 +297,12 @@ * @since 2.3.0 | ||
*/ | ||
export declare const invariantLens: Invariant2<URI> | ||
export declare const Invariant: Invariant2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
export declare const Semigroupoid: Semigroupoid2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
export declare const categoryLens: Category2<URI> | ||
export declare const Category: Category2<URI> |
127
lib/Lens.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.categoryLens = exports.invariantLens = exports.URI = exports.imap = exports.findFirst = exports.traverse = exports.left = exports.right = exports.some = exports.atKey = exports.key = exports.index = exports.component = exports.props = exports.prop = exports.filter = exports.fromNullable = exports.modifyF = exports.modify = exports.composeOptional = exports.composePrism = exports.compose = exports.asTraversal = exports.asOptional = exports.id = void 0; | ||
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.composeTraversal = exports.composeOptional = exports.composePrism = exports.composeIso = exports.composeLens = exports.compose = exports.asTraversal = exports.asOptional = exports.id = exports.lens = void 0; | ||
var function_1 = require("fp-ts/lib/function"); | ||
@@ -12,2 +12,7 @@ var pipeable_1 = require("fp-ts/lib/pipeable"); | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
exports.lens = _.lens; | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
@@ -20,3 +25,3 @@ */ | ||
/** | ||
* View a `Lens` as a `Optional` | ||
* View a `Lens` as a `Optional`. | ||
* | ||
@@ -28,3 +33,3 @@ * @category converters | ||
/** | ||
* View a `Lens` as a `Traversal` | ||
* View a `Lens` as a `Traversal`. | ||
* | ||
@@ -39,3 +44,3 @@ * @category converters | ||
/** | ||
* Compose a `Lens` with a `Lens` | ||
* Compose a `Lens` with a `Lens`. | ||
* | ||
@@ -47,5 +52,21 @@ * @category compositions | ||
/** | ||
* Compose a `Lens` with a `Prism` | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
exports.composeLens = exports.compose; | ||
/** | ||
* Compose a `Lens` with a `Iso`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
exports.composeIso = | ||
/*#__PURE__*/ | ||
function_1.flow(_.isoAsLens, exports.compose); | ||
/** | ||
* Compose a `Lens` with a `Prism`. | ||
* | ||
* @category compositions | ||
* @since 2.3.0 | ||
@@ -55,3 +76,3 @@ */ | ||
/** | ||
* Compose a `Lens` with an `Optional` | ||
* Compose a `Lens` with an `Optional`. | ||
* | ||
@@ -61,6 +82,16 @@ * @category compositions | ||
*/ | ||
var composeOptional = function (ab) { return function (sa) { | ||
return _.optionalComposeOptional(ab)(exports.asOptional(sa)); | ||
}; }; | ||
var composeOptional = function (ab) { | ||
return function_1.flow(exports.asOptional, _.optionalComposeOptional(ab)); | ||
}; | ||
exports.composeOptional = composeOptional; | ||
/** | ||
* Compose a `Lens` with an `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
var composeTraversal = function (ab) { | ||
return function_1.flow(exports.asTraversal, _.traversalComposeTraversal(ab)); | ||
}; | ||
exports.composeTraversal = composeTraversal; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -84,3 +115,3 @@ // combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a nullable value | ||
* Return a `Optional` from a `Lens` focused on a nullable value. | ||
* | ||
@@ -91,3 +122,3 @@ * @category combinators | ||
var fromNullable = function (sa) { | ||
return _.lensComposePrism(_.prismFromNullable())(sa); | ||
return exports.composePrism(_.prismFromNullable())(sa); | ||
}; | ||
@@ -100,3 +131,3 @@ exports.fromNullable = fromNullable; | ||
/** | ||
* Return a `Lens` from a `Lens` and a prop | ||
* Return a `Lens` from a `Lens` and a prop. | ||
* | ||
@@ -108,3 +139,3 @@ * @category combinators | ||
/** | ||
* Return a `Lens` from a `Lens` and a list of props | ||
* Return a `Lens` from a `Lens` and a list of props. | ||
* | ||
@@ -116,3 +147,3 @@ * @category combinators | ||
/** | ||
* Return a `Lens` from a `Lens` and a component | ||
* Return a `Lens` from a `Lens` focused on a component of a tuple. | ||
* | ||
@@ -124,3 +155,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a `ReadonlyArray` | ||
* Return a `Optional` from a `Lens` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -130,18 +161,28 @@ * @category combinators | ||
*/ | ||
var index = function (i) { return function (sa) { | ||
return pipeable_1.pipe(sa, exports.asOptional, _.optionalComposeOptional(_.indexReadonlyArray().index(i))); | ||
}; }; | ||
var index = function (i) { | ||
return function_1.flow(exports.asOptional, _.optionalIndex(i)); | ||
}; | ||
exports.index = index; | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a `ReadonlyRecord` and a key | ||
* Return a `Optional` from a `Lens` focused on an index of a `ReadonlyNonEmptyArray`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
var indexNonEmpty = function (i) { | ||
return function_1.flow(exports.asOptional, _.optionalIndexNonEmpty(i)); | ||
}; | ||
exports.indexNonEmpty = indexNonEmpty; | ||
/** | ||
* Return a `Optional` from a `Lens` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
*/ | ||
var key = function (key) { return function (sa) { | ||
return pipeable_1.pipe(sa, exports.asOptional, _.optionalComposeOptional(_.indexReadonlyRecord().index(key))); | ||
}; }; | ||
var key = function (key) { | ||
return function_1.flow(exports.asOptional, _.optionalKey(key)); | ||
}; | ||
exports.key = key; | ||
/** | ||
* Return a `Lens` from a `Lens` focused on a `ReadonlyRecord` and a required key | ||
* Return a `Lens` from a `Lens` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -151,8 +192,5 @@ * @category combinators | ||
*/ | ||
var atKey = function (key) { return function (sa) { | ||
return pipeable_1.pipe(sa, exports.compose(_.atReadonlyRecord().at(key))); | ||
}; }; | ||
exports.atKey = atKey; | ||
exports.atKey = _.lensAtKey; | ||
/** | ||
* Return a `Optional` from a `Lens` focused on the `Some` of a `Option` type | ||
* Return a `Optional` from a `Lens` focused on the `Some` of a `Option` type. | ||
* | ||
@@ -166,3 +204,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on the `Right` of a `Either` type | ||
* Return a `Optional` from a `Lens` focused on the `Right` of a `Either` type. | ||
* | ||
@@ -176,3 +214,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Lens` focused on the `Left` of a `Either` type | ||
* Return a `Optional` from a `Lens` focused on the `Left` of a `Either` type. | ||
* | ||
@@ -186,3 +224,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Lens` focused on a `Traversable` | ||
* Return a `Traversal` from a `Lens` focused on a `Traversable`. | ||
* | ||
@@ -193,9 +231,13 @@ * @category combinators | ||
function traverse(T) { | ||
return function_1.flow(exports.asTraversal, _.traversalComposeTraversal(_.fromTraversable(T)())); | ||
return function_1.flow(exports.asTraversal, _.traversalTraverse(T)); | ||
} | ||
exports.traverse = traverse; | ||
function findFirst(predicate) { | ||
return exports.composeOptional(_.findFirst(predicate)); | ||
return exports.composeOptional(_.optionalFindFirst(predicate)); | ||
} | ||
exports.findFirst = findFirst; | ||
function findFirstNonEmpty(predicate) { | ||
return exports.composeOptional(_.optionalFindFirstNonEmpty(predicate)); | ||
} | ||
exports.findFirstNonEmpty = findFirstNonEmpty; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -215,6 +257,3 @@ // pipeables | ||
// ------------------------------------------------------------------------------------- | ||
var imap_ = function (ea, ab, ba) { return ({ | ||
get: function_1.flow(ea.get, ab), | ||
set: function_1.flow(ba, ea.set) | ||
}); }; | ||
var imap_ = function (ea, ab, ba) { return exports.lens(function_1.flow(ea.get, ab), function_1.flow(ba, ea.set)); }; | ||
/** | ||
@@ -229,3 +268,3 @@ * @category instances | ||
*/ | ||
exports.invariantLens = { | ||
exports.Invariant = { | ||
URI: exports.URI, | ||
@@ -236,8 +275,16 @@ imap: imap_ | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
exports.Semigroupoid = { | ||
URI: exports.URI, | ||
compose: function (ab, ea) { return exports.compose(ab)(ea); } | ||
}; | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
exports.categoryLens = { | ||
exports.Category = { | ||
URI: exports.URI, | ||
compose: function (ab, ea) { return exports.compose(ab)(ea); }, | ||
compose: exports.Semigroupoid.compose, | ||
id: exports.id | ||
}; |
@@ -29,3 +29,7 @@ /** | ||
import * as O from 'fp-ts/lib/Option' | ||
import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray' | ||
import { ReadonlyRecord } from 'fp-ts/lib/ReadonlyRecord' | ||
import { Semigroupoid2 } from 'fp-ts/lib/Semigroupoid' | ||
import { Traversable1 } from 'fp-ts/lib/Traversable' | ||
import { Iso } from './Iso' | ||
import { Lens } from './Lens' | ||
@@ -45,2 +49,10 @@ import { Prism } from './Prism' | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const optional: <S, A>( | ||
getOption: Optional<S, A>['getOption'], | ||
set: Optional<S, A>['set'] | ||
) => Optional<S, A> | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
@@ -64,2 +76,16 @@ */ | ||
/** | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeOptional: <A, B>(ab: Optional<A, B>) => <S>(sa: Optional<S, A>) => Optional<S, B> | ||
/** | ||
* Compose a `Optional` with a `Iso`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeIso: <A, B>(ab: Iso<A, B>) => <S>(sa: Optional<S, A>) => Optional<S, B> | ||
/** | ||
* Compose a `Optional` with a `Lens`. | ||
@@ -79,2 +105,9 @@ * | ||
/** | ||
* Compose a `Optional` with an `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeTraversal: <A, B>(ab: Traversal<A, B>) => <S>(sa: Optional<S, A>) => Traversal<S, B> | ||
/** | ||
* @category combinators | ||
@@ -142,3 +175,3 @@ * @since 2.3.0 | ||
/** | ||
* Return a `Optional` from a `Optional` and a component. | ||
* Return a `Optional` from a `Optional` focused on a component of a tuple. | ||
* | ||
@@ -152,3 +185,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a `ReadonlyArray`. | ||
* Return a `Optional` from a `Optional` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -158,12 +191,19 @@ * @category combinators | ||
*/ | ||
export declare const index: (i: number) => <S, A>(sa: Optional<S, readonly A[]>) => Optional<S, A> | ||
export declare const index: (i: number) => <S, A>(sa: Optional<S, ReadonlyArray<A>>) => Optional<S, A> | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a `ReadonlyRecord` and a key. | ||
* Return a `Optional` from a `Optional` focused on an index of a `ReadonlyNonEmptyArray`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const indexNonEmpty: (i: number) => <S, A>(sa: Optional<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
*/ | ||
export declare const key: (key: string) => <S, A>(sa: Optional<S, Readonly<Record<string, A>>>) => Optional<S, A> | ||
export declare const key: (key: string) => <S, A>(sa: Optional<S, ReadonlyRecord<string, A>>) => Optional<S, A> | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a `ReadonlyRecord` and a required key. | ||
* Return a `Optional` from a `Optional` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -215,2 +255,12 @@ * @category combinators | ||
/** | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function findFirstNonEmpty<A, B extends A>( | ||
refinement: Refinement<A, B> | ||
): <S>(sa: Optional<S, ReadonlyNonEmptyArray<A>>) => Optional<S, B> | ||
export declare function findFirstNonEmpty<A>( | ||
predicate: Predicate<A> | ||
): <S>(sa: Optional<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* @category Invariant | ||
@@ -239,7 +289,12 @@ * @since 2.3.0 | ||
*/ | ||
export declare const invariantOptional: Invariant2<URI> | ||
export declare const Invariant: Invariant2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
export declare const Semigroupoid: Semigroupoid2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
export declare const categoryOptional: Category2<URI> | ||
export declare const Category: Category2<URI> |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.categoryOptional = exports.invariantOptional = exports.URI = exports.imap = exports.findFirst = exports.traverse = exports.left = exports.right = exports.some = exports.atKey = exports.key = exports.index = exports.component = exports.props = exports.prop = exports.filter = exports.fromNullable = exports.modifyF = exports.modify = exports.setOption = exports.modifyOption = exports.composePrism = exports.composeLens = exports.compose = exports.asTraversal = exports.id = void 0; | ||
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; | ||
var function_1 = require("fp-ts/lib/function"); | ||
@@ -13,8 +13,10 @@ var O = require("fp-ts/lib/Option"); | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
exports.optional = _.optional; | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
*/ | ||
var id = function () { return ({ | ||
getOption: O.some, | ||
set: function_1.constant | ||
}); }; | ||
var id = function () { return exports.optional(O.some, function_1.constant); }; | ||
exports.id = id; | ||
@@ -42,2 +44,18 @@ // ------------------------------------------------------------------------------------- | ||
/** | ||
* 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__*/ | ||
function_1.flow(_.isoAsOptional, exports.compose); | ||
/** | ||
* Compose a `Optional` with a `Lens`. | ||
@@ -48,6 +66,5 @@ * | ||
*/ | ||
var composeLens = function (ab) { return function (sa) { | ||
return _.optionalComposeOptional(_.lensAsOptional(ab))(sa); | ||
}; }; | ||
exports.composeLens = composeLens; | ||
exports.composeLens = | ||
/*#__PURE__*/ | ||
function_1.flow(_.lensAsOptional, _.optionalComposeOptional); | ||
/** | ||
@@ -59,6 +76,15 @@ * Compose a `Optional` with a `Prism`. | ||
*/ | ||
var composePrism = function (ab) { return function (sa) { | ||
return _.optionalComposeOptional(_.prismAsOptional(ab))(sa); | ||
}; }; | ||
exports.composePrism = composePrism; | ||
exports.composePrism = | ||
/*#__PURE__*/ | ||
function_1.flow(_.prismAsOptional, _.optionalComposeOptional); | ||
/** | ||
* Compose a `Optional` with an `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
var composeTraversal = function (ab) { | ||
return function_1.flow(exports.asTraversal, _.traversalComposeTraversal(ab)); | ||
}; | ||
exports.composeTraversal = composeTraversal; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -127,3 +153,3 @@ // combinators | ||
/** | ||
* Return a `Optional` from a `Optional` and a component. | ||
* Return a `Optional` from a `Optional` focused on a component of a tuple. | ||
* | ||
@@ -138,3 +164,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a `ReadonlyArray`. | ||
* Return a `Optional` from a `Optional` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -144,18 +170,19 @@ * @category combinators | ||
*/ | ||
var index = function (i) { return function (sa) { | ||
return pipeable_1.pipe(sa, _.optionalComposeOptional(_.indexReadonlyArray().index(i))); | ||
}; }; | ||
exports.index = index; | ||
exports.index = _.optionalIndex; | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a `ReadonlyRecord` and a key. | ||
* 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 | ||
*/ | ||
var key = function (key) { return function (sa) { | ||
return pipeable_1.pipe(sa, _.optionalComposeOptional(_.indexReadonlyRecord().index(key))); | ||
}; }; | ||
exports.key = key; | ||
exports.key = _.optionalKey; | ||
/** | ||
* Return a `Optional` from a `Optional` focused on a `ReadonlyRecord` and a required key. | ||
* Return a `Optional` from a `Optional` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -203,9 +230,13 @@ * @category combinators | ||
function traverse(T) { | ||
return function_1.flow(exports.asTraversal, _.traversalComposeTraversal(_.fromTraversable(T)())); | ||
return function_1.flow(exports.asTraversal, _.traversalTraverse(T)); | ||
} | ||
exports.traverse = traverse; | ||
function findFirst(predicate) { | ||
return exports.compose(_.findFirst(predicate)); | ||
return exports.compose(_.optionalFindFirst(predicate)); | ||
} | ||
exports.findFirst = findFirst; | ||
function findFirstNonEmpty(predicate) { | ||
return exports.compose(_.optionalFindFirstNonEmpty(predicate)); | ||
} | ||
exports.findFirstNonEmpty = findFirstNonEmpty; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -223,6 +254,3 @@ // pipeables | ||
// ------------------------------------------------------------------------------------- | ||
var imap_ = function (ea, ab, ba) { return ({ | ||
getOption: function_1.flow(ea.getOption, O.map(ab)), | ||
set: function_1.flow(ba, ea.set) | ||
}); }; | ||
var imap_ = function (ea, ab, ba) { return exports.optional(function_1.flow(ea.getOption, O.map(ab)), function_1.flow(ba, ea.set)); }; | ||
/** | ||
@@ -237,3 +265,3 @@ * @category instances | ||
*/ | ||
exports.invariantOptional = { | ||
exports.Invariant = { | ||
URI: exports.URI, | ||
@@ -244,8 +272,16 @@ imap: imap_ | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
exports.Semigroupoid = { | ||
URI: exports.URI, | ||
compose: function (ab, ea) { return exports.compose(ab)(ea); } | ||
}; | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
exports.categoryOptional = { | ||
exports.Category = { | ||
URI: exports.URI, | ||
compose: function (ab, ea) { return exports.compose(ab)(ea); }, | ||
compose: exports.Semigroupoid.compose, | ||
id: exports.id | ||
}; |
@@ -24,3 +24,7 @@ /** | ||
import * as O from 'fp-ts/lib/Option' | ||
import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray' | ||
import { ReadonlyRecord } from 'fp-ts/lib/ReadonlyRecord' | ||
import { Semigroupoid2 } from 'fp-ts/lib/Semigroupoid' | ||
import { Traversable1 } from 'fp-ts/lib/Traversable' | ||
import { Iso } from './Iso' | ||
import { Lens } from './Lens' | ||
@@ -40,2 +44,10 @@ import { Optional } from './Optional' | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const prism: <S, A>( | ||
getOption: Prism<S, A>['getOption'], | ||
reverseGet: Prism<S, A>['reverseGet'] | ||
) => Prism<S, A> | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
@@ -53,3 +65,3 @@ */ | ||
/** | ||
* View a `Prism` as a `Optional` | ||
* View a `Prism` as a `Optional`. | ||
* | ||
@@ -61,3 +73,3 @@ * @category converters | ||
/** | ||
* View a `Prism` as a `Traversal` | ||
* View a `Prism` as a `Traversal`. | ||
* | ||
@@ -69,3 +81,3 @@ * @category converters | ||
/** | ||
* Compose a `Prism` with a `Prism` | ||
* Compose a `Prism` with a `Prism`. | ||
* | ||
@@ -77,5 +89,19 @@ * @category compositions | ||
/** | ||
* Compose a `Prism` with a `Lens` | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composePrism: <A, B>(ab: Prism<A, B>) => <S>(sa: Prism<S, A>) => Prism<S, B> | ||
/** | ||
* Compose a `Prism` with a `Iso`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeIso: <A, B>(ab: Iso<A, B>) => <S>(sa: Prism<S, A>) => Prism<S, B> | ||
/** | ||
* Compose a `Prism` with a `Lens`. | ||
* | ||
* @category compositions | ||
* @since 2.3.0 | ||
@@ -85,3 +111,3 @@ */ | ||
/** | ||
* Compose a `Prism` with an `Optional` | ||
* Compose a `Prism` with an `Optional`. | ||
* | ||
@@ -93,2 +119,9 @@ * @category compositions | ||
/** | ||
* Compose a `Prism` with an `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeTraversal: <A, B>(ab: Traversal<A, B>) => <S>(sa: Prism<S, A>) => Traversal<S, B> | ||
/** | ||
* @category combinators | ||
@@ -125,3 +158,3 @@ * @since 2.3.0 | ||
/** | ||
* Return a `Prism` from a `Prism` focused on a nullable value | ||
* Return a `Prism` from a `Prism` focused on a nullable value. | ||
* | ||
@@ -139,3 +172,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` and a prop | ||
* Return a `Optional` from a `Prism` and a prop. | ||
* | ||
@@ -147,3 +180,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` and a list of props | ||
* Return a `Optional` from a `Prism` and a list of props. | ||
* | ||
@@ -159,3 +192,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` and a component | ||
* Return a `Optional` from a `Prism` focused on a component of a tuple. | ||
* | ||
@@ -169,3 +202,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` focused on a `ReadonlyArray` | ||
* Return a `Optional` from a `Prism` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -177,5 +210,12 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` focused on a `ReadonlyRecord` and a key | ||
* Return a `Optional` from a `Prism` focused on an index of a `ReadonlyNonEmptyArray`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const indexNonEmpty: (i: number) => <S, A>(sa: Prism<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* Return a `Optional` from a `Prism` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
@@ -185,3 +225,3 @@ */ | ||
/** | ||
* Return a `Optional` from a `Prism` focused on a `ReadonlyRecord` and a required key | ||
* Return a `Optional` from a `Prism` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -195,3 +235,3 @@ * @category combinators | ||
/** | ||
* Return a `Prism` from a `Prism` focused on the `Some` of a `Option` type | ||
* Return a `Prism` from a `Prism` focused on the `Some` of a `Option` type. | ||
* | ||
@@ -203,3 +243,3 @@ * @category combinators | ||
/** | ||
* Return a `Prism` from a `Prism` focused on the `Right` of a `Either` type | ||
* Return a `Prism` from a `Prism` focused on the `Right` of a `Either` type. | ||
* | ||
@@ -211,3 +251,3 @@ * @category combinators | ||
/** | ||
* Return a `Prism` from a `Prism` focused on the `Left` of a `Either` type | ||
* Return a `Prism` from a `Prism` focused on the `Left` of a `Either` type. | ||
* | ||
@@ -219,3 +259,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Prism` focused on a `Traversable` | ||
* Return a `Traversal` from a `Prism` focused on a `Traversable`. | ||
* | ||
@@ -237,2 +277,12 @@ * @category combinators | ||
/** | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function findFirstNonEmpty<A, B extends A>( | ||
refinement: Refinement<A, B> | ||
): <S>(sa: Prism<S, ReadonlyNonEmptyArray<A>>) => Optional<S, B> | ||
export declare function findFirstNonEmpty<A>( | ||
predicate: Predicate<A> | ||
): <S>(sa: Prism<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> | ||
/** | ||
* @category Invariant | ||
@@ -261,7 +311,12 @@ * @since 2.3.0 | ||
*/ | ||
export declare const invariantPrism: Invariant2<URI> | ||
export declare const Invariant: Invariant2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
export declare const Semigroupoid: Semigroupoid2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
export declare const categoryPrism: Category2<URI> | ||
export declare const Category: Category2<URI> |
133
lib/Prism.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.categoryPrism = exports.invariantPrism = exports.URI = exports.imap = exports.findFirst = exports.traverse = exports.left = exports.right = exports.some = exports.atKey = exports.key = exports.index = exports.component = exports.props = exports.prop = exports.filter = exports.fromNullable = exports.modifyF = exports.modify = exports.modifyOption = exports.set = exports.composeOptional = exports.composeLens = exports.compose = exports.asTraversal = exports.asOptional = exports.fromPredicate = exports.id = void 0; | ||
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; | ||
var function_1 = require("fp-ts/lib/function"); | ||
@@ -13,8 +13,10 @@ var O = require("fp-ts/lib/Option"); | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
exports.prism = _.prism; | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
*/ | ||
var id = function () { return ({ | ||
getOption: O.some, | ||
reverseGet: function_1.identity | ||
}); }; | ||
var id = function () { return exports.prism(O.some, function_1.identity); }; | ||
exports.id = id; | ||
@@ -30,3 +32,3 @@ /** | ||
/** | ||
* View a `Prism` as a `Optional` | ||
* View a `Prism` as a `Optional`. | ||
* | ||
@@ -38,3 +40,3 @@ * @category converters | ||
/** | ||
* View a `Prism` as a `Traversal` | ||
* View a `Prism` as a `Traversal`. | ||
* | ||
@@ -49,3 +51,3 @@ * @category converters | ||
/** | ||
* Compose a `Prism` with a `Prism` | ||
* Compose a `Prism` with a `Prism`. | ||
* | ||
@@ -55,11 +57,23 @@ * @category compositions | ||
*/ | ||
var compose = function (ab) { return function (sa) { return ({ | ||
getOption: function_1.flow(sa.getOption, O.chain(ab.getOption)), | ||
reverseGet: function_1.flow(ab.reverseGet, sa.reverseGet) | ||
}); }; }; | ||
exports.compose = compose; | ||
exports.compose = _.prismComposePrism; | ||
/** | ||
* Compose a `Prism` with a `Lens` | ||
* 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__*/ | ||
function_1.flow(_.isoAsPrism, exports.compose); | ||
/** | ||
* Compose a `Prism` with a `Lens`. | ||
* | ||
* @category compositions | ||
* @since 2.3.0 | ||
@@ -69,3 +83,3 @@ */ | ||
/** | ||
* Compose a `Prism` with an `Optional` | ||
* Compose a `Prism` with an `Optional`. | ||
* | ||
@@ -75,6 +89,16 @@ * @category compositions | ||
*/ | ||
var composeOptional = function (ab) { return function (sa) { | ||
return _.optionalComposeOptional(ab)(exports.asOptional(sa)); | ||
}; }; | ||
var composeOptional = function (ab) { | ||
return function_1.flow(exports.asOptional, _.optionalComposeOptional(ab)); | ||
}; | ||
exports.composeOptional = composeOptional; | ||
/** | ||
* Compose a `Prism` with an `Traversal`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
var composeTraversal = function (ab) { | ||
return function_1.flow(exports.asTraversal, _.traversalComposeTraversal(ab)); | ||
}; | ||
exports.composeTraversal = composeTraversal; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -105,3 +129,3 @@ // combinators | ||
/** | ||
* Return a `Prism` from a `Prism` focused on a nullable value | ||
* Return a `Prism` from a `Prism` focused on a nullable value. | ||
* | ||
@@ -119,3 +143,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` and a prop | ||
* Return a `Optional` from a `Prism` and a prop. | ||
* | ||
@@ -127,6 +151,6 @@ * @category combinators | ||
return exports.composeLens(pipeable_1.pipe(_.lensId(), _.lensProp(prop))); | ||
}; | ||
}; // TODO: simplify? | ||
exports.prop = prop; | ||
/** | ||
* Return a `Optional` from a `Prism` and a list of props | ||
* Return a `Optional` from a `Prism` and a list of props. | ||
* | ||
@@ -145,3 +169,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` and a component | ||
* Return a `Optional` from a `Prism` focused on a component of a tuple. | ||
* | ||
@@ -154,3 +178,3 @@ * @category combinators | ||
/** | ||
* Return a `Optional` from a `Prism` focused on a `ReadonlyArray` | ||
* Return a `Optional` from a `Prism` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -160,18 +184,28 @@ * @category combinators | ||
*/ | ||
var index = function (i) { return function (sa) { | ||
return pipeable_1.pipe(sa, exports.asOptional, _.optionalComposeOptional(_.indexReadonlyArray().index(i))); | ||
}; }; | ||
var index = function (i) { | ||
return function_1.flow(exports.asOptional, _.optionalIndex(i)); | ||
}; | ||
exports.index = index; | ||
/** | ||
* Return a `Optional` from a `Prism` focused on a `ReadonlyRecord` and a key | ||
* Return a `Optional` from a `Prism` focused on an index of a `ReadonlyNonEmptyArray`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
var indexNonEmpty = function (i) { | ||
return 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 | ||
*/ | ||
var key = function (key) { return function (sa) { | ||
return pipeable_1.pipe(sa, exports.asOptional, _.optionalComposeOptional(_.indexReadonlyRecord().index(key))); | ||
}; }; | ||
var key = function (key) { | ||
return function_1.flow(exports.asOptional, _.optionalKey(key)); | ||
}; | ||
exports.key = key; | ||
/** | ||
* Return a `Optional` from a `Prism` focused on a `ReadonlyRecord` and a required key | ||
* Return a `Optional` from a `Prism` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -186,3 +220,3 @@ * @category combinators | ||
/** | ||
* Return a `Prism` from a `Prism` focused on the `Some` of a `Option` type | ||
* Return a `Prism` from a `Prism` focused on the `Some` of a `Option` type. | ||
* | ||
@@ -196,3 +230,3 @@ * @category combinators | ||
/** | ||
* Return a `Prism` from a `Prism` focused on the `Right` of a `Either` type | ||
* Return a `Prism` from a `Prism` focused on the `Right` of a `Either` type. | ||
* | ||
@@ -206,3 +240,3 @@ * @category combinators | ||
/** | ||
* Return a `Prism` from a `Prism` focused on the `Left` of a `Either` type | ||
* Return a `Prism` from a `Prism` focused on the `Left` of a `Either` type. | ||
* | ||
@@ -216,3 +250,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Prism` focused on a `Traversable` | ||
* Return a `Traversal` from a `Prism` focused on a `Traversable`. | ||
* | ||
@@ -223,9 +257,13 @@ * @category combinators | ||
function traverse(T) { | ||
return function_1.flow(exports.asTraversal, _.traversalComposeTraversal(_.fromTraversable(T)())); | ||
return function_1.flow(exports.asTraversal, _.traversalTraverse(T)); | ||
} | ||
exports.traverse = traverse; | ||
function findFirst(predicate) { | ||
return exports.composeOptional(_.findFirst(predicate)); | ||
return exports.composeOptional(_.optionalFindFirst(predicate)); | ||
} | ||
exports.findFirst = findFirst; | ||
function findFirstNonEmpty(predicate) { | ||
return exports.composeOptional(_.optionalFindFirstNonEmpty(predicate)); | ||
} | ||
exports.findFirstNonEmpty = findFirstNonEmpty; | ||
// ------------------------------------------------------------------------------------- | ||
@@ -245,6 +283,3 @@ // pipeables | ||
// ------------------------------------------------------------------------------------- | ||
var imap_ = function (ea, ab, ba) { return ({ | ||
getOption: function_1.flow(ea.getOption, O.map(ab)), | ||
reverseGet: function_1.flow(ba, ea.reverseGet) | ||
}); }; | ||
var imap_ = function (ea, ab, ba) { return exports.prism(function_1.flow(ea.getOption, O.map(ab)), function_1.flow(ba, ea.reverseGet)); }; | ||
/** | ||
@@ -259,3 +294,3 @@ * @category instances | ||
*/ | ||
exports.invariantPrism = { | ||
exports.Invariant = { | ||
URI: exports.URI, | ||
@@ -266,8 +301,16 @@ imap: imap_ | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
exports.Semigroupoid = { | ||
URI: exports.URI, | ||
compose: function (ab, ea) { return exports.compose(ab)(ea); } | ||
}; | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
exports.categoryPrism = { | ||
exports.Category = { | ||
URI: exports.URI, | ||
compose: function (ab, ea) { return exports.compose(ab)(ea); }, | ||
compose: exports.Semigroupoid.compose, | ||
id: exports.id | ||
}; |
@@ -23,4 +23,10 @@ /** | ||
import { Option } from 'fp-ts/lib/Option' | ||
import { Traversable1 } from 'fp-ts/lib/Traversable' | ||
import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray' | ||
import { ReadonlyRecord } from 'fp-ts/lib/ReadonlyRecord' | ||
import { Semigroupoid2 } from 'fp-ts/lib/Semigroupoid' | ||
import * as _ from './internal' | ||
import { Iso } from './Iso' | ||
import { Lens } from './Lens' | ||
import { Optional } from './Optional' | ||
import { Prism } from './Prism' | ||
/** | ||
@@ -46,2 +52,7 @@ * @category model | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
export declare const traversal: <S, A>(modifyF: Traversal<S, A>['modifyF']) => Traversal<S, A> | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
@@ -51,3 +62,3 @@ */ | ||
/** | ||
* Create a `Traversal` from a `Traversable` | ||
* Create a `Traversal` from a `Traversable`. | ||
* | ||
@@ -59,3 +70,3 @@ * @category constructor | ||
/** | ||
* Compose a `Traversal` with a `Traversal` | ||
* Compose a `Traversal` with a `Traversal`. | ||
* | ||
@@ -67,2 +78,37 @@ * @category compositions | ||
/** | ||
* Alias of `compose`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeTraversal: <A, B>(ab: Traversal<A, B>) => <S>(sa: Traversal<S, A>) => Traversal<S, B> | ||
/** | ||
* Compose a `Traversal` with a `Iso`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeIso: <A, B>(ab: Iso<A, B>) => <S>(sa: Traversal<S, A>) => Traversal<S, B> | ||
/** | ||
* Compose a `Traversal` with a `Lens`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeLens: <A, B>(ab: Lens<A, B>) => <S>(sa: Traversal<S, A>) => Traversal<S, B> | ||
/** | ||
* Compose a `Traversal` with a `Prism`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composePrism: <A, B>(ab: Prism<A, B>) => <S>(sa: Traversal<S, A>) => Traversal<S, B> | ||
/** | ||
* Compose a `Traversal` with a `Optional`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
export declare const composeOptional: <A, B>(ab: Optional<A, B>) => <S>(sa: Traversal<S, A>) => Traversal<S, B> | ||
/** | ||
* @category combinators | ||
@@ -78,5 +124,12 @@ * @since 2.3.0 | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a nullable value. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
*/ | ||
export declare const fromNullable: <S, A>(sa: Traversal<S, A>) => Traversal<S, NonNullable<A>> | ||
/** | ||
* @category combinators | ||
* @since 2.3.0 | ||
*/ | ||
export declare function filter<A, B extends A>( | ||
@@ -87,3 +140,3 @@ refinement: Refinement<A, B> | ||
/** | ||
* Return a `Traversal` from a `Traversal` and a prop | ||
* Return a `Traversal` from a `Traversal` and a prop. | ||
* | ||
@@ -95,3 +148,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` and a list of props | ||
* Return a `Traversal` from a `Traversal` and a list of props. | ||
* | ||
@@ -107,3 +160,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` and a component | ||
* Return a `Traversal` from a `Traversal` focused on a component of a tuple. | ||
* | ||
@@ -117,3 +170,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `ReadonlyArray` | ||
* Return a `Traversal` from a `Traversal` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -125,5 +178,12 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `ReadonlyRecord` and a key | ||
* Alias of `index`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare const indexNonEmpty: (i: number) => <S, A>(sa: Traversal<S, ReadonlyNonEmptyArray<A>>) => Traversal<S, A> | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
@@ -133,3 +193,3 @@ */ | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `ReadonlyRecord` and a required key | ||
* Return a `Traversal` from a `Traversal` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -143,3 +203,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on the `Some` of a `Option` type | ||
* Return a `Traversal` from a `Traversal` focused on the `Some` of a `Option` type. | ||
* | ||
@@ -151,3 +211,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on the `Right` of a `Either` type | ||
* Return a `Traversal` from a `Traversal` focused on the `Right` of a `Either` type. | ||
* | ||
@@ -159,3 +219,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on the `Left` of a `Either` type | ||
* Return a `Traversal` from a `Traversal` focused on the `Left` of a `Either` type. | ||
* | ||
@@ -167,3 +227,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `Traversable` | ||
* Return a `Traversal` from a `Traversal` focused on a `Traversable`. | ||
* | ||
@@ -173,6 +233,26 @@ * @category combinators | ||
*/ | ||
export declare function traverse<T extends URIS>( | ||
T: Traversable1<T> | ||
): <S, A>(sta: Traversal<S, Kind<T, A>>) => Traversal<S, A> | ||
export declare const traverse: typeof _.traversalTraverse | ||
/** | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function findFirst<A, B extends A>( | ||
refinement: Refinement<A, B> | ||
): <S>(sa: Traversal<S, ReadonlyArray<A>>) => Traversal<S, B> | ||
export declare function findFirst<A>( | ||
predicate: Predicate<A> | ||
): <S>(sa: Traversal<S, ReadonlyArray<A>>) => Traversal<S, A> | ||
/** | ||
* Alias of `findFirst`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
export declare function findFirstNonEmpty<A, B extends A>( | ||
refinement: Refinement<A, B> | ||
): <S>(sa: Traversal<S, ReadonlyNonEmptyArray<A>>) => Traversal<S, B> | ||
export declare function findFirstNonEmpty<A>( | ||
predicate: Predicate<A> | ||
): <S>(sa: Traversal<S, ReadonlyNonEmptyArray<A>>) => Traversal<S, A> | ||
/** | ||
* Map each target to a `Monoid` and combine the results. | ||
@@ -215,4 +295,9 @@ * | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
export declare const Semigroupoid: Semigroupoid2<URI> | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
export declare const categoryTraversal: Category2<URI> | ||
export declare const Category: Category2<URI> |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.categoryTraversal = exports.URI = exports.getAll = exports.fold = exports.foldMap = exports.traverse = exports.left = exports.right = exports.some = exports.atKey = exports.key = exports.index = exports.component = exports.props = exports.prop = exports.filter = exports.set = exports.modify = exports.compose = exports.fromTraversable = exports.id = void 0; | ||
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; | ||
var C = require("fp-ts/lib/Const"); | ||
@@ -15,10 +15,13 @@ var function_1 = require("fp-ts/lib/function"); | ||
* @category constructors | ||
* @since 2.3.8 | ||
*/ | ||
exports.traversal = _.traversal; | ||
/** | ||
* @category constructors | ||
* @since 2.3.0 | ||
*/ | ||
var id = function () { return ({ | ||
modifyF: function (_) { return function (f) { return f; }; } | ||
}); }; | ||
var id = function () { return exports.traversal(function (_) { return function (f) { return f; }; }); }; | ||
exports.id = id; | ||
/** | ||
* Create a `Traversal` from a `Traversable` | ||
* Create a `Traversal` from a `Traversable`. | ||
* | ||
@@ -33,3 +36,3 @@ * @category constructor | ||
/** | ||
* Compose a `Traversal` with a `Traversal` | ||
* Compose a `Traversal` with a `Traversal`. | ||
* | ||
@@ -40,2 +43,45 @@ * @category compositions | ||
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__*/ | ||
function_1.flow(_.isoAsTraversal, exports.compose); | ||
/** | ||
* Compose a `Traversal` with a `Lens`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
exports.composeLens = | ||
/*#__PURE__*/ | ||
function_1.flow(_.lensAsTraversal, _.traversalComposeTraversal); | ||
/** | ||
* Compose a `Traversal` with a `Prism`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
exports.composePrism = | ||
/*#__PURE__*/ | ||
function_1.flow(_.prismAsTraversal, _.traversalComposeTraversal); | ||
/** | ||
* Compose a `Traversal` with a `Optional`. | ||
* | ||
* @category compositions | ||
* @since 2.3.8 | ||
*/ | ||
exports.composeOptional = | ||
/*#__PURE__*/ | ||
function_1.flow(_.optionalAsTraversal, _.traversalComposeTraversal); | ||
// ------------------------------------------------------------------------------------- | ||
@@ -60,2 +106,12 @@ // combinators | ||
exports.set = set; | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a nullable value. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
*/ | ||
var fromNullable = function (sa) { | ||
return exports.composePrism(_.prismFromNullable())(sa); | ||
}; | ||
exports.fromNullable = fromNullable; | ||
function filter(predicate) { | ||
@@ -66,3 +122,3 @@ return exports.compose(_.prismAsTraversal(_.prismFromPredicate(predicate))); | ||
/** | ||
* Return a `Traversal` from a `Traversal` and a prop | ||
* Return a `Traversal` from a `Traversal` and a prop. | ||
* | ||
@@ -77,3 +133,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` and a list of props | ||
* Return a `Traversal` from a `Traversal` and a list of props. | ||
* | ||
@@ -92,3 +148,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` and a component | ||
* Return a `Traversal` from a `Traversal` focused on a component of a tuple. | ||
* | ||
@@ -103,3 +159,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `ReadonlyArray` | ||
* Return a `Traversal` from a `Traversal` focused on an index of a `ReadonlyArray`. | ||
* | ||
@@ -114,5 +170,12 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `ReadonlyRecord` and a key | ||
* Alias of `index`. | ||
* | ||
* @category combinators | ||
* @since 2.3.8 | ||
*/ | ||
exports.indexNonEmpty = exports.index; | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a key of a `ReadonlyRecord`. | ||
* | ||
* @category combinators | ||
* @since 2.3.0 | ||
@@ -125,3 +188,3 @@ */ | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `ReadonlyRecord` and a required key | ||
* Return a `Traversal` from a `Traversal` focused on a required key of a `ReadonlyRecord`. | ||
* | ||
@@ -136,3 +199,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on the `Some` of a `Option` type | ||
* Return a `Traversal` from a `Traversal` focused on the `Some` of a `Option` type. | ||
* | ||
@@ -146,3 +209,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on the `Right` of a `Either` type | ||
* Return a `Traversal` from a `Traversal` focused on the `Right` of a `Either` type. | ||
* | ||
@@ -156,3 +219,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on the `Left` of a `Either` type | ||
* Return a `Traversal` from a `Traversal` focused on the `Left` of a `Either` type. | ||
* | ||
@@ -166,3 +229,3 @@ * @category combinators | ||
/** | ||
* Return a `Traversal` from a `Traversal` focused on a `Traversable` | ||
* Return a `Traversal` from a `Traversal` focused on a `Traversable`. | ||
* | ||
@@ -172,6 +235,11 @@ * @category combinators | ||
*/ | ||
function traverse(T) { | ||
return exports.compose(exports.fromTraversable(T)()); | ||
exports.traverse = _.traversalTraverse; | ||
function findFirst(predicate) { | ||
return exports.composeOptional(_.optionalFindFirst(predicate)); | ||
} | ||
exports.traverse = traverse; | ||
exports.findFirst = findFirst; | ||
function findFirstNonEmpty(predicate) { | ||
return findFirst(predicate); | ||
} | ||
exports.findFirstNonEmpty = findFirstNonEmpty; | ||
/** | ||
@@ -215,8 +283,16 @@ * Map each target to a `Monoid` and combine the results. | ||
* @category instances | ||
* @since 2.3.8 | ||
*/ | ||
exports.Semigroupoid = { | ||
URI: exports.URI, | ||
compose: function (ab, ea) { return exports.compose(ab)(ea); } | ||
}; | ||
/** | ||
* @category instances | ||
* @since 2.3.0 | ||
*/ | ||
exports.categoryTraversal = { | ||
exports.Category = { | ||
URI: exports.URI, | ||
compose: function (ab, ea) { return exports.compose(ab)(ea); }, | ||
compose: exports.Semigroupoid.compose, | ||
id: exports.id | ||
}; |
{ | ||
"name": "monocle-ts", | ||
"version": "2.3.7", | ||
"version": "2.3.8", | ||
"description": "A porting of scala monocle library to TypeScript", | ||
@@ -20,3 +20,3 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"fp-ts": "^2.0.0" | ||
"fp-ts": "^2.5.0" | ||
}, | ||
@@ -23,0 +23,0 @@ "tags": [ |
364516
12470