1336 lines
33 KiB
JavaScript
1336 lines
33 KiB
JavaScript
/**
|
|
* @since 1.0.0
|
|
*/
|
|
import * as A from 'fp-ts/es6/Array';
|
|
import { getApplicative, make } from 'fp-ts/es6/Const';
|
|
import { constant, identity } from 'fp-ts/es6/function';
|
|
import { monoidAll, monoidAny } from 'fp-ts/es6/Monoid';
|
|
import { fromNullable, fromPredicate, getFirstMonoid, isNone, option, some } from 'fp-ts/es6/Option';
|
|
import { pipe } from 'fp-ts/es6/pipeable';
|
|
import * as at from './At';
|
|
import * as iso from './Iso';
|
|
import * as index from './Ix';
|
|
import * as lens from './Lens';
|
|
import * as optional from './Optional';
|
|
import * as prism from './Prism';
|
|
import * as traversal from './Traversal';
|
|
export {
|
|
/**
|
|
* @since 2.3.0
|
|
*/
|
|
at,
|
|
/**
|
|
* @since 2.3.0
|
|
*/
|
|
iso,
|
|
/**
|
|
* @since 2.3.0
|
|
*/
|
|
index,
|
|
/**
|
|
* @since 2.3.0
|
|
*/
|
|
lens,
|
|
/**
|
|
* @since 2.3.0
|
|
*/
|
|
prism,
|
|
/**
|
|
* @since 2.3.0
|
|
*/
|
|
optional,
|
|
/**
|
|
* @since 2.3.0
|
|
*/
|
|
traversal };
|
|
//
|
|
// compat
|
|
//
|
|
var fromIso = function (iso) { return new Iso(iso.get, iso.reverseGet); };
|
|
var fromLens = function (lens) { return new Lens(lens.get, lens.set); };
|
|
var fromPrism = function (prism) { return new Prism(prism.getOption, prism.reverseGet); };
|
|
var fromOptional = function (optional) {
|
|
return new Optional(optional.getOption, optional.set);
|
|
};
|
|
var fromTraversal = function (traversal) { return new Traversal(traversal.modifyF); };
|
|
var fromAt = function (at) { return new At(function (i) { return fromLens(at.at(i)); }); };
|
|
var fromIndex = function (ix) { return new Index(function (i) { return fromOptional(ix.index(i)); }); };
|
|
//
|
|
// old APIs
|
|
//
|
|
var update = function (o, k, a) {
|
|
var _a;
|
|
return a === o[k] ? o : Object.assign({}, o, (_a = {}, _a[k] = a, _a));
|
|
};
|
|
/**
|
|
* Laws:
|
|
* 1. `reverseGet(get(s)) = s`
|
|
* 2. `get(reversetGet(a)) = a`
|
|
*
|
|
* @category constructor
|
|
* @since 1.0.0
|
|
*/
|
|
var Iso = /** @class */ (function () {
|
|
function Iso(get, reverseGet) {
|
|
this.get = get;
|
|
this.reverseGet = reverseGet;
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
this._tag = 'Iso';
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
this.unwrap = this.get;
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
this.to = this.get;
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
this.wrap = this.reverseGet;
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
this.from = this.reverseGet;
|
|
}
|
|
/**
|
|
* reverse the `Iso`: the source becomes the target and the target becomes the source
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.reverse = function () {
|
|
return fromIso(iso.reverse(this));
|
|
};
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.modify = function (f) {
|
|
return iso.modify(f)(this);
|
|
};
|
|
/**
|
|
* view an `Iso` as a `Lens`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.asLens = function () {
|
|
return fromLens(iso.asLens(this));
|
|
};
|
|
/**
|
|
* view an `Iso` as a `Prism`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.asPrism = function () {
|
|
return fromPrism(iso.asPrism(this));
|
|
};
|
|
/**
|
|
* view an `Iso` as a `Optional`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.asOptional = function () {
|
|
return fromOptional(iso.asOptional(this));
|
|
};
|
|
/**
|
|
* view an `Iso` as a `Traversal`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.asTraversal = function () {
|
|
return fromTraversal(iso.asTraversal(this));
|
|
};
|
|
/**
|
|
* view an `Iso` as a `Fold`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.asFold = function () {
|
|
var _this = this;
|
|
return new Fold(function () { return function (f) { return function (s) { return f(_this.get(s)); }; }; });
|
|
};
|
|
/**
|
|
* view an `Iso` as a `Getter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.asGetter = function () {
|
|
var _this = this;
|
|
return new Getter(function (s) { return _this.get(s); });
|
|
};
|
|
/**
|
|
* view an `Iso` as a `Setter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.asSetter = function () {
|
|
var _this = this;
|
|
return new Setter(function (f) { return _this.modify(f); });
|
|
};
|
|
/**
|
|
* compose an `Iso` with an `Iso`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.compose = function (ab) {
|
|
return fromIso(iso.compose(ab)(this));
|
|
};
|
|
/**
|
|
* Alias of `compose`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.composeIso = function (ab) {
|
|
return this.compose(ab);
|
|
};
|
|
/**
|
|
* compose an `Iso` with a `Lens `
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.composeLens = function (ab) {
|
|
return fromLens(pipe(this, iso.asLens, lens.compose(ab)));
|
|
};
|
|
/**
|
|
* compose an `Iso` with a `Prism`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.composePrism = function (ab) {
|
|
return fromPrism(pipe(this, iso.asPrism, prism.compose(ab)));
|
|
};
|
|
/**
|
|
* compose an `Iso` with an `Optional`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.composeOptional = function (ab) {
|
|
return fromOptional(pipe(this, iso.asOptional, optional.compose(ab)));
|
|
};
|
|
/**
|
|
* compose an `Iso` with a `Traversal`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.composeTraversal = function (ab) {
|
|
return fromTraversal(pipe(this, iso.asTraversal, traversal.compose(ab)));
|
|
};
|
|
/**
|
|
* compose an `Iso` with a `Fold`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.composeFold = function (ab) {
|
|
return this.asFold().compose(ab);
|
|
};
|
|
/**
|
|
* compose an `Iso` with a `Getter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.composeGetter = function (ab) {
|
|
return this.asGetter().compose(ab);
|
|
};
|
|
/**
|
|
* compose an `Iso` with a `Setter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Iso.prototype.composeSetter = function (ab) {
|
|
return this.asSetter().compose(ab);
|
|
};
|
|
return Iso;
|
|
}());
|
|
export { Iso };
|
|
/**
|
|
* Laws:
|
|
* 1. `get(set(a)(s)) = a`
|
|
* 2. `set(get(s))(s) = s`
|
|
* 3. `set(a)(set(a)(s)) = set(a)(s)`
|
|
*
|
|
* @category constructor
|
|
* @since 1.0.0
|
|
*/
|
|
var Lens = /** @class */ (function () {
|
|
function Lens(get, set) {
|
|
this.get = get;
|
|
this.set = set;
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
this._tag = 'Lens';
|
|
}
|
|
/**
|
|
* @example
|
|
* import { Lens } from 'monocle-ts'
|
|
*
|
|
* type Person = {
|
|
* name: string
|
|
* age: number
|
|
* address: {
|
|
* city: string
|
|
* }
|
|
* }
|
|
*
|
|
* const city = Lens.fromPath<Person>()(['address', 'city'])
|
|
*
|
|
* const person: Person = { name: 'Giulio', age: 43, address: { city: 'Milan' } }
|
|
*
|
|
* assert.strictEqual(city.get(person), 'Milan')
|
|
* assert.deepStrictEqual(city.set('London')(person), { name: 'Giulio', age: 43, address: { city: 'London' } })
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.fromPath = function () {
|
|
var fromProp = Lens.fromProp();
|
|
return function (path) {
|
|
var lens = fromProp(path[0]);
|
|
return path.slice(1).reduce(function (acc, prop) { return acc.compose(fromProp(prop)); }, lens);
|
|
};
|
|
};
|
|
/**
|
|
* Returns a `Lens` from a type and a prop
|
|
*
|
|
* @example
|
|
* import { Lens } from 'monocle-ts'
|
|
*
|
|
* type Person = {
|
|
* name: string
|
|
* age: number
|
|
* }
|
|
*
|
|
* const age = Lens.fromProp<Person>()('age')
|
|
*
|
|
* const person: Person = { name: 'Giulio', age: 43 }
|
|
*
|
|
* assert.strictEqual(age.get(person), 43)
|
|
* assert.deepStrictEqual(age.set(44)(person), { name: 'Giulio', age: 44 })
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.fromProp = function () {
|
|
return function (prop) { return fromLens(pipe(lens.id(), lens.prop(prop))); };
|
|
};
|
|
Lens.fromProps = function () {
|
|
return function (props) { return fromLens(pipe(lens.id(), lens.props.apply(lens, props))); };
|
|
};
|
|
/**
|
|
* Returns a `Lens` from a nullable (`A | null | undefined`) prop
|
|
*
|
|
* @example
|
|
* import { Lens } from 'monocle-ts'
|
|
*
|
|
* interface Outer {
|
|
* inner?: Inner
|
|
* }
|
|
*
|
|
* interface Inner {
|
|
* value: number
|
|
* foo: string
|
|
* }
|
|
*
|
|
* const inner = Lens.fromNullableProp<Outer>()('inner', { value: 0, foo: 'foo' })
|
|
* const value = Lens.fromProp<Inner>()('value')
|
|
* const lens = inner.compose(value)
|
|
*
|
|
* assert.deepStrictEqual(lens.set(1)({}), { inner: { value: 1, foo: 'foo' } })
|
|
* assert.strictEqual(lens.get({}), 0)
|
|
* assert.deepStrictEqual(lens.set(1)({ inner: { value: 1, foo: 'bar' } }), { inner: { value: 1, foo: 'bar' } })
|
|
* assert.strictEqual(lens.get({ inner: { value: 1, foo: 'bar' } }), 1)
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.fromNullableProp = function () {
|
|
return function (k, defaultValue) {
|
|
return new Lens(function (s) {
|
|
var osk = fromNullable(s[k]);
|
|
if (isNone(osk)) {
|
|
return defaultValue;
|
|
}
|
|
else {
|
|
return osk.value;
|
|
}
|
|
}, function (a) { return function (s) { return update(s, k, a); }; });
|
|
};
|
|
};
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.modify = function (f) {
|
|
return lens.modify(f)(this);
|
|
};
|
|
/**
|
|
* view a `Lens` as a Optional
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.asOptional = function () {
|
|
return fromOptional(lens.asOptional(this));
|
|
};
|
|
/**
|
|
* view a `Lens` as a `Traversal`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.asTraversal = function () {
|
|
return fromTraversal(lens.asTraversal(this));
|
|
};
|
|
/**
|
|
* view a `Lens` as a `Setter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.asSetter = function () {
|
|
var _this = this;
|
|
return new Setter(function (f) { return _this.modify(f); });
|
|
};
|
|
/**
|
|
* view a `Lens` as a `Getter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.asGetter = function () {
|
|
var _this = this;
|
|
return new Getter(function (s) { return _this.get(s); });
|
|
};
|
|
/**
|
|
* view a `Lens` as a `Fold`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.asFold = function () {
|
|
var _this = this;
|
|
return new Fold(function () { return function (f) { return function (s) { return f(_this.get(s)); }; }; });
|
|
};
|
|
/**
|
|
* compose a `Lens` with a `Lens`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.compose = function (ab) {
|
|
return fromLens(lens.compose(ab)(this));
|
|
};
|
|
/**
|
|
* Alias of `compose`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.composeLens = function (ab) {
|
|
return this.compose(ab);
|
|
};
|
|
/**
|
|
* compose a `Lens` with a `Getter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.composeGetter = function (ab) {
|
|
return this.asGetter().compose(ab);
|
|
};
|
|
/**
|
|
* compose a `Lens` with a `Fold`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.composeFold = function (ab) {
|
|
return this.asFold().compose(ab);
|
|
};
|
|
/**
|
|
* compose a `Lens` with an `Optional`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.composeOptional = function (ab) {
|
|
return fromOptional(pipe(this, lens.asOptional, optional.compose(ab)));
|
|
};
|
|
/**
|
|
* compose a `Lens` with an `Traversal`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.composeTraversal = function (ab) {
|
|
return fromTraversal(pipe(this, lens.asTraversal, traversal.compose(ab)));
|
|
};
|
|
/**
|
|
* compose a `Lens` with an `Setter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.composeSetter = function (ab) {
|
|
return this.asSetter().compose(ab);
|
|
};
|
|
/**
|
|
* compose a `Lens` with an `Iso`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.composeIso = function (ab) {
|
|
return fromLens(pipe(this, lens.compose(pipe(ab, iso.asLens))));
|
|
};
|
|
/**
|
|
* compose a `Lens` with a `Prism`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Lens.prototype.composePrism = function (ab) {
|
|
return fromOptional(lens.composePrism(ab)(this));
|
|
};
|
|
return Lens;
|
|
}());
|
|
export { Lens };
|
|
/**
|
|
* Laws:
|
|
* 1. `pipe(getOption(s), fold(() => s, reverseGet)) = s`
|
|
* 2. `getOption(reverseGet(a)) = some(a)`
|
|
*
|
|
* @category constructor
|
|
* @since 1.0.0
|
|
*/
|
|
var Prism = /** @class */ (function () {
|
|
function Prism(getOption, reverseGet) {
|
|
this.getOption = getOption;
|
|
this.reverseGet = reverseGet;
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
this._tag = 'Prism';
|
|
}
|
|
Prism.fromPredicate = function (predicate) {
|
|
return fromPrism(prism.fromPredicate(predicate));
|
|
};
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.some = function () {
|
|
return somePrism;
|
|
};
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.modify = function (f) {
|
|
var _this = this;
|
|
return function (s) {
|
|
var os = _this.modifyOption(f)(s);
|
|
if (isNone(os)) {
|
|
return s;
|
|
}
|
|
else {
|
|
return os.value;
|
|
}
|
|
};
|
|
};
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.modifyOption = function (f) {
|
|
var _this = this;
|
|
return function (s) {
|
|
return option.map(_this.getOption(s), function (v) {
|
|
var n = f(v);
|
|
return n === v ? s : _this.reverseGet(n);
|
|
});
|
|
};
|
|
};
|
|
/**
|
|
* set the target of a `Prism` with a value
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.set = function (a) {
|
|
return this.modify(function () { return a; });
|
|
};
|
|
/**
|
|
* view a `Prism` as a `Optional`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.asOptional = function () {
|
|
return fromOptional(prism.asOptional(this));
|
|
};
|
|
/**
|
|
* view a `Prism` as a `Traversal`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.asTraversal = function () {
|
|
return fromTraversal(prism.asTraversal(this));
|
|
};
|
|
/**
|
|
* view a `Prism` as a `Setter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.asSetter = function () {
|
|
var _this = this;
|
|
return new Setter(function (f) { return _this.modify(f); });
|
|
};
|
|
/**
|
|
* view a `Prism` as a `Fold`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.asFold = function () {
|
|
var _this = this;
|
|
return new Fold(function (M) { return function (f) { return function (s) {
|
|
var oa = _this.getOption(s);
|
|
return isNone(oa) ? M.empty : f(oa.value);
|
|
}; }; });
|
|
};
|
|
/**
|
|
* compose a `Prism` with a `Prism`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.compose = function (ab) {
|
|
return fromPrism(prism.compose(ab)(this));
|
|
};
|
|
/**
|
|
* Alias of `compose`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.composePrism = function (ab) {
|
|
return this.compose(ab);
|
|
};
|
|
/**
|
|
* compose a `Prism` with a `Optional`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.composeOptional = function (ab) {
|
|
return fromOptional(pipe(this, prism.asOptional, optional.compose(ab)));
|
|
};
|
|
/**
|
|
* compose a `Prism` with a `Traversal`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.composeTraversal = function (ab) {
|
|
return fromTraversal(pipe(this, prism.asTraversal, traversal.compose(ab)));
|
|
};
|
|
/**
|
|
* compose a `Prism` with a `Fold`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.composeFold = function (ab) {
|
|
return this.asFold().compose(ab);
|
|
};
|
|
/**
|
|
* compose a `Prism` with a `Setter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.composeSetter = function (ab) {
|
|
return this.asSetter().compose(ab);
|
|
};
|
|
/**
|
|
* compose a `Prism` with a `Iso`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.composeIso = function (ab) {
|
|
return fromPrism(pipe(this, prism.compose(pipe(ab, iso.asPrism))));
|
|
};
|
|
/**
|
|
* compose a `Prism` with a `Lens`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.composeLens = function (ab) {
|
|
return fromOptional(prism.composeLens(ab)(this));
|
|
};
|
|
/**
|
|
* compose a `Prism` with a `Getter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Prism.prototype.composeGetter = function (ab) {
|
|
return this.asFold().compose(ab.asFold());
|
|
};
|
|
return Prism;
|
|
}());
|
|
export { Prism };
|
|
var somePrism =
|
|
/*#__PURE__*/
|
|
new Prism(identity, some);
|
|
/**
|
|
* Laws:
|
|
* 1. `pipe(getOption(s), fold(() => s, a => set(a)(s))) = s`
|
|
* 2. `getOption(set(a)(s)) = pipe(getOption(s), map(_ => a))`
|
|
* 3. `set(a)(set(a)(s)) = set(a)(s)`
|
|
*
|
|
* @category constructor
|
|
* @since 1.0.0
|
|
*/
|
|
var Optional = /** @class */ (function () {
|
|
function Optional(getOption, set) {
|
|
this.getOption = getOption;
|
|
this.set = set;
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
this._tag = 'Optional';
|
|
}
|
|
/**
|
|
* Returns an `Optional` from a nullable (`A | null | undefined`) prop
|
|
*
|
|
* @example
|
|
* import { Optional } from 'monocle-ts'
|
|
*
|
|
* interface Phone {
|
|
* number: string
|
|
* }
|
|
* interface Employment {
|
|
* phone?: Phone
|
|
* }
|
|
* interface Info {
|
|
* employment?: Employment
|
|
* }
|
|
* interface Response {
|
|
* info?: Info
|
|
* }
|
|
*
|
|
* const numberFromResponse = Optional.fromPath<Response>()(['info', 'employment', 'phone', 'number'])
|
|
*
|
|
* const response1: Response = {
|
|
* info: {
|
|
* employment: {
|
|
* phone: {
|
|
* number: '555-1234'
|
|
* }
|
|
* }
|
|
* }
|
|
* }
|
|
* const response2: Response = {
|
|
* info: {
|
|
* employment: {}
|
|
* }
|
|
* }
|
|
*
|
|
* numberFromResponse.getOption(response1) // some('555-1234')
|
|
* numberFromResponse.getOption(response2) // none
|
|
*
|
|
* @since 2.1.0
|
|
*/
|
|
Optional.fromPath = function () {
|
|
var fromNullableProp = Optional.fromNullableProp();
|
|
return function (path) {
|
|
var optional = fromNullableProp(path[0]);
|
|
return path.slice(1).reduce(function (acc, prop) { return acc.compose(fromNullableProp(prop)); }, optional);
|
|
};
|
|
};
|
|
/**
|
|
* @example
|
|
* import { Optional } from 'monocle-ts'
|
|
*
|
|
* interface S {
|
|
* a: number | undefined | null
|
|
* }
|
|
*
|
|
* const optional = Optional.fromNullableProp<S>()('a')
|
|
*
|
|
* const s1: S = { a: undefined }
|
|
* const s2: S = { a: null }
|
|
* const s3: S = { a: 1 }
|
|
*
|
|
* assert.deepStrictEqual(optional.set(2)(s1), s1)
|
|
* assert.deepStrictEqual(optional.set(2)(s2), s2)
|
|
* assert.deepStrictEqual(optional.set(2)(s3), { a: 2 })
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.fromNullableProp = function () {
|
|
return function (k) {
|
|
return new Optional(function (s) { return fromNullable(s[k]); }, function (a) { return function (s) { return (s[k] == null ? s : update(s, k, a)); }; });
|
|
};
|
|
};
|
|
/**
|
|
* Returns an `Optional` from an option (`Option<A>`) prop
|
|
*
|
|
* @example
|
|
* import { Optional } from 'monocle-ts'
|
|
* import * as O from 'fp-ts/Option'
|
|
*
|
|
* interface S {
|
|
* a: O.Option<number>
|
|
* }
|
|
*
|
|
* const optional = Optional.fromOptionProp<S>()('a')
|
|
* const s1: S = { a: O.none }
|
|
* const s2: S = { a: O.some(1) }
|
|
* assert.deepStrictEqual(optional.set(2)(s1), s1)
|
|
* assert.deepStrictEqual(optional.set(2)(s2), { a: O.some(2) })
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.fromOptionProp = function () {
|
|
var formProp = Lens.fromProp();
|
|
return function (prop) { return formProp(prop).composePrism(somePrism); };
|
|
};
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.prototype.modify = function (f) {
|
|
return optional.modify(f)(this);
|
|
};
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.prototype.modifyOption = function (f) {
|
|
return optional.modifyOption(f)(this);
|
|
};
|
|
/**
|
|
* view a `Optional` as a `Traversal`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.prototype.asTraversal = function () {
|
|
return fromTraversal(optional.asTraversal(this));
|
|
};
|
|
/**
|
|
* view an `Optional` as a `Fold`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.prototype.asFold = function () {
|
|
var _this = this;
|
|
return new Fold(function (M) { return function (f) { return function (s) {
|
|
var oa = _this.getOption(s);
|
|
return isNone(oa) ? M.empty : f(oa.value);
|
|
}; }; });
|
|
};
|
|
/**
|
|
* view an `Optional` as a `Setter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.prototype.asSetter = function () {
|
|
var _this = this;
|
|
return new Setter(function (f) { return _this.modify(f); });
|
|
};
|
|
/**
|
|
* compose a `Optional` with a `Optional`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.prototype.compose = function (ab) {
|
|
return fromOptional(optional.compose(ab)(this));
|
|
};
|
|
/**
|
|
* Alias of `compose`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.prototype.composeOptional = function (ab) {
|
|
return this.compose(ab);
|
|
};
|
|
/**
|
|
* compose an `Optional` with a `Traversal`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.prototype.composeTraversal = function (ab) {
|
|
return fromTraversal(pipe(this, optional.asTraversal, traversal.compose(ab)));
|
|
};
|
|
/**
|
|
* compose an `Optional` with a `Fold`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.prototype.composeFold = function (ab) {
|
|
return this.asFold().compose(ab);
|
|
};
|
|
/**
|
|
* compose an `Optional` with a `Setter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.prototype.composeSetter = function (ab) {
|
|
return this.asSetter().compose(ab);
|
|
};
|
|
/**
|
|
* compose an `Optional` with a `Lens`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.prototype.composeLens = function (ab) {
|
|
return fromOptional(pipe(this, optional.compose(pipe(ab, lens.asOptional))));
|
|
};
|
|
/**
|
|
* compose an `Optional` with a `Prism`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.prototype.composePrism = function (ab) {
|
|
return fromOptional(pipe(this, optional.compose(pipe(ab, prism.asOptional))));
|
|
};
|
|
/**
|
|
* compose an `Optional` with a `Iso`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.prototype.composeIso = function (ab) {
|
|
return fromOptional(pipe(this, optional.compose(pipe(ab, iso.asOptional))));
|
|
};
|
|
/**
|
|
* compose an `Optional` with a `Getter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Optional.prototype.composeGetter = function (ab) {
|
|
return this.asFold().compose(ab.asFold());
|
|
};
|
|
return Optional;
|
|
}());
|
|
export { Optional };
|
|
/**
|
|
* @category constructor
|
|
* @since 1.0.0
|
|
*/
|
|
var Traversal = /** @class */ (function () {
|
|
function Traversal(
|
|
// Van Laarhoven representation
|
|
modifyF) {
|
|
this.modifyF = modifyF;
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
this._tag = 'Traversal';
|
|
}
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
Traversal.prototype.modify = function (f) {
|
|
return traversal.modify(f)(this);
|
|
};
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
Traversal.prototype.set = function (a) {
|
|
return traversal.set(a)(this);
|
|
};
|
|
Traversal.prototype.filter = function (predicate) {
|
|
return fromTraversal(traversal.filter(predicate)(this));
|
|
};
|
|
/**
|
|
* view a `Traversal` as a `Fold`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Traversal.prototype.asFold = function () {
|
|
var _this = this;
|
|
return new Fold(function (M) { return function (f) {
|
|
return _this.modifyF(getApplicative(M))(function (a) { return make(f(a)); });
|
|
}; });
|
|
};
|
|
/**
|
|
* view a `Traversal` as a `Setter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Traversal.prototype.asSetter = function () {
|
|
var _this = this;
|
|
return new Setter(function (f) { return _this.modify(f); });
|
|
};
|
|
/**
|
|
* compose a `Traversal` with a `Traversal`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Traversal.prototype.compose = function (ab) {
|
|
return fromTraversal(traversal.compose(ab)(this));
|
|
};
|
|
/**
|
|
* Alias of `compose`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Traversal.prototype.composeTraversal = function (ab) {
|
|
return this.compose(ab);
|
|
};
|
|
/**
|
|
* compose a `Traversal` with a `Fold`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Traversal.prototype.composeFold = function (ab) {
|
|
return this.asFold().compose(ab);
|
|
};
|
|
/**
|
|
* compose a `Traversal` with a `Setter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Traversal.prototype.composeSetter = function (ab) {
|
|
return this.asSetter().compose(ab);
|
|
};
|
|
/**
|
|
* compose a `Traversal` with a `Optional`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Traversal.prototype.composeOptional = function (ab) {
|
|
return this.compose(ab.asTraversal());
|
|
};
|
|
/**
|
|
* compose a `Traversal` with a `Lens`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Traversal.prototype.composeLens = function (ab) {
|
|
return fromTraversal(pipe(this, traversal.compose(pipe(ab, lens.asTraversal))));
|
|
};
|
|
/**
|
|
* compose a `Traversal` with a `Prism`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Traversal.prototype.composePrism = function (ab) {
|
|
return fromTraversal(pipe(this, traversal.compose(pipe(ab, prism.asTraversal))));
|
|
};
|
|
/**
|
|
* compose a `Traversal` with a `Iso`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Traversal.prototype.composeIso = function (ab) {
|
|
return fromTraversal(pipe(this, traversal.compose(pipe(ab, iso.asTraversal))));
|
|
};
|
|
/**
|
|
* compose a `Traversal` with a `Getter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Traversal.prototype.composeGetter = function (ab) {
|
|
return this.asFold().compose(ab.asFold());
|
|
};
|
|
return Traversal;
|
|
}());
|
|
export { Traversal };
|
|
/**
|
|
* @category constructor
|
|
* @since 1.2.0
|
|
*/
|
|
var At = /** @class */ (function () {
|
|
function At(at) {
|
|
this.at = at;
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
this._tag = 'At';
|
|
}
|
|
/**
|
|
* lift an instance of `At` using an `Iso`
|
|
*
|
|
* @since 1.2.0
|
|
*/
|
|
At.prototype.fromIso = function (iso) {
|
|
return fromAt(at.fromIso(iso)(this));
|
|
};
|
|
return At;
|
|
}());
|
|
export { At };
|
|
/**
|
|
* @category constructor
|
|
* @since 1.2.0
|
|
*/
|
|
var Index = /** @class */ (function () {
|
|
function Index(index) {
|
|
this.index = index;
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
this._tag = 'Index';
|
|
}
|
|
/**
|
|
* @since 1.2.0
|
|
*/
|
|
Index.fromAt = function (at) {
|
|
return fromIndex(index.fromAt(at));
|
|
};
|
|
/**
|
|
* lift an instance of `Index` using an `Iso`
|
|
*
|
|
* @since 1.2.0
|
|
*/
|
|
Index.prototype.fromIso = function (iso) {
|
|
return fromIndex(index.fromIso(iso)(this));
|
|
};
|
|
return Index;
|
|
}());
|
|
export { Index };
|
|
/**
|
|
* @category constructor
|
|
* @since 1.0.0
|
|
*/
|
|
var Getter = /** @class */ (function () {
|
|
function Getter(get) {
|
|
this.get = get;
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
this._tag = 'Getter';
|
|
}
|
|
/**
|
|
* view a `Getter` as a `Fold`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Getter.prototype.asFold = function () {
|
|
var _this = this;
|
|
return new Fold(function () { return function (f) { return function (s) { return f(_this.get(s)); }; }; });
|
|
};
|
|
/**
|
|
* compose a `Getter` with a `Getter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Getter.prototype.compose = function (ab) {
|
|
var _this = this;
|
|
return new Getter(function (s) { return ab.get(_this.get(s)); });
|
|
};
|
|
/**
|
|
* Alias of `compose`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Getter.prototype.composeGetter = function (ab) {
|
|
return this.compose(ab);
|
|
};
|
|
/**
|
|
* compose a `Getter` with a `Fold`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Getter.prototype.composeFold = function (ab) {
|
|
return this.asFold().compose(ab);
|
|
};
|
|
/**
|
|
* compose a `Getter` with a `Lens`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Getter.prototype.composeLens = function (ab) {
|
|
return this.compose(ab.asGetter());
|
|
};
|
|
/**
|
|
* compose a `Getter` with a `Iso`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Getter.prototype.composeIso = function (ab) {
|
|
return this.compose(ab.asGetter());
|
|
};
|
|
/**
|
|
* compose a `Getter` with a `Optional`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Getter.prototype.composeTraversal = function (ab) {
|
|
return this.asFold().compose(ab.asFold());
|
|
};
|
|
/**
|
|
* compose a `Getter` with a `Optional`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Getter.prototype.composeOptional = function (ab) {
|
|
return this.asFold().compose(ab.asFold());
|
|
};
|
|
/**
|
|
* compose a `Getter` with a `Prism`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Getter.prototype.composePrism = function (ab) {
|
|
return this.asFold().compose(ab.asFold());
|
|
};
|
|
return Getter;
|
|
}());
|
|
export { Getter };
|
|
/**
|
|
* @category constructor
|
|
* @since 1.0.0
|
|
*/
|
|
var Fold = /** @class */ (function () {
|
|
function Fold(foldMap) {
|
|
this.foldMap = foldMap;
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
this._tag = 'Fold';
|
|
this.getAll = foldMap(A.getMonoid())(A.of);
|
|
this.exist = foldMap(monoidAny);
|
|
this.all = foldMap(monoidAll);
|
|
this.foldMapFirst = foldMap(getFirstMonoid());
|
|
}
|
|
/**
|
|
* compose a `Fold` with a `Fold`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Fold.prototype.compose = function (ab) {
|
|
var _this = this;
|
|
return new Fold(function (M) { return function (f) { return _this.foldMap(M)(ab.foldMap(M)(f)); }; });
|
|
};
|
|
/**
|
|
* Alias of `compose`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Fold.prototype.composeFold = function (ab) {
|
|
return this.compose(ab);
|
|
};
|
|
/**
|
|
* compose a `Fold` with a `Getter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Fold.prototype.composeGetter = function (ab) {
|
|
return this.compose(ab.asFold());
|
|
};
|
|
/**
|
|
* compose a `Fold` with a `Traversal`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Fold.prototype.composeTraversal = function (ab) {
|
|
return this.compose(ab.asFold());
|
|
};
|
|
/**
|
|
* compose a `Fold` with a `Optional`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Fold.prototype.composeOptional = function (ab) {
|
|
return this.compose(ab.asFold());
|
|
};
|
|
/**
|
|
* compose a `Fold` with a `Lens`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Fold.prototype.composeLens = function (ab) {
|
|
return this.compose(ab.asFold());
|
|
};
|
|
/**
|
|
* compose a `Fold` with a `Prism`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Fold.prototype.composePrism = function (ab) {
|
|
return this.compose(ab.asFold());
|
|
};
|
|
/**
|
|
* compose a `Fold` with a `Iso`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Fold.prototype.composeIso = function (ab) {
|
|
return this.compose(ab.asFold());
|
|
};
|
|
Fold.prototype.find = function (p) {
|
|
return this.foldMapFirst(fromPredicate(p));
|
|
};
|
|
/**
|
|
* get the first target of a `Fold`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Fold.prototype.headOption = function (s) {
|
|
return this.find(function () { return true; })(s);
|
|
};
|
|
return Fold;
|
|
}());
|
|
export { Fold };
|
|
/**
|
|
* @category constructor
|
|
* @since 1.0.0
|
|
*/
|
|
var Setter = /** @class */ (function () {
|
|
function Setter(modify) {
|
|
this.modify = modify;
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
this._tag = 'Setter';
|
|
}
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
Setter.prototype.set = function (a) {
|
|
return this.modify(constant(a));
|
|
};
|
|
/**
|
|
* compose a `Setter` with a `Setter`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Setter.prototype.compose = function (ab) {
|
|
var _this = this;
|
|
return new Setter(function (f) { return _this.modify(ab.modify(f)); });
|
|
};
|
|
/**
|
|
* Alias of `compose`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Setter.prototype.composeSetter = function (ab) {
|
|
return this.compose(ab);
|
|
};
|
|
/**
|
|
* compose a `Setter` with a `Traversal`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Setter.prototype.composeTraversal = function (ab) {
|
|
return this.compose(ab.asSetter());
|
|
};
|
|
/**
|
|
* compose a `Setter` with a `Optional`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Setter.prototype.composeOptional = function (ab) {
|
|
return this.compose(ab.asSetter());
|
|
};
|
|
/**
|
|
* compose a `Setter` with a `Lens`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Setter.prototype.composeLens = function (ab) {
|
|
return this.compose(ab.asSetter());
|
|
};
|
|
/**
|
|
* compose a `Setter` with a `Prism`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Setter.prototype.composePrism = function (ab) {
|
|
return this.compose(ab.asSetter());
|
|
};
|
|
/**
|
|
* compose a `Setter` with a `Iso`
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
Setter.prototype.composeIso = function (ab) {
|
|
return this.compose(ab.asSetter());
|
|
};
|
|
return Setter;
|
|
}());
|
|
export { Setter };
|
|
export function fromTraversable(T) {
|
|
var f = traversal.fromTraversable(T);
|
|
return function () { return fromTraversal(f()); };
|
|
}
|
|
export function fromFoldable(F) {
|
|
return function () {
|
|
return new Fold(function (M) {
|
|
var foldMapFM = F.foldMap(M);
|
|
return function (f) { return function (s) { return foldMapFM(s, f); }; };
|
|
});
|
|
};
|
|
}
|