monocle-ts
Advanced tools
Comparing version 1.7.2 to 2.0.0-rc.1
@@ -16,2 +16,15 @@ # Changelog | ||
# 2.0.0-rc.1 | ||
- **Breaking Change** | ||
- upgrade to `fp-ts@2.x` (@gcanti) | ||
- remove deprecated APIs (@gcanti) | ||
- uncurried `Lens.fromProp` | ||
- uncurried `Lens.fromProps` | ||
- uncurried `Lens.fromNullableProp` | ||
- uncurried `Optional.fromNullableProp` | ||
- uncurried `Optional.fromOptionProp` | ||
- remove `At/StrMap` (@gcanti) | ||
- remove `Index/StrMap` (@gcanti) | ||
# 1.7.2 | ||
@@ -18,0 +31,0 @@ |
import { At, Lens } from '../index'; | ||
import { isNone } from 'fp-ts/lib/Option'; | ||
import * as R from 'fp-ts/lib/Record'; | ||
export function atRecord() { | ||
return new At(function (at) { return new Lens(function (s) { return R.lookup(at, s); }, function (a) { return function (s) { return a.fold(R.remove(at, s), function (x) { return R.insert(at, x, s); }); }; }); }); | ||
return new At(function (k) { | ||
return new Lens(function (r) { return R.lookup(k, r); }, function (oa) { return function (r) { | ||
if (isNone(oa)) { | ||
return R.deleteAt(k)(r); | ||
} | ||
else { | ||
return R.insertAt(k, oa.value)(r); | ||
} | ||
}; }); | ||
}); | ||
} |
import { At } from '../index'; | ||
import { Setoid } from 'fp-ts/lib/Setoid'; | ||
export declare function atSet<A = never>(setoid: Setoid<A>): At<Set<A>, A, boolean>; | ||
import { Eq } from 'fp-ts/lib/Eq'; | ||
export declare function atSet<A = never>(E: Eq<A>): At<Set<A>, A, boolean>; |
import { At, Lens } from '../index'; | ||
import * as S from 'fp-ts/lib/Set'; | ||
export function atSet(setoid) { | ||
// tslint:disable-next-line: deprecation | ||
var member = S.member(setoid); | ||
var insert = S.insert(setoid); | ||
var remove = S.remove(setoid); | ||
return new At(function (at) { return new Lens(function (s) { return member(s)(at); }, function (a) { return function (s) { return (a ? insert(at, s) : remove(at, s)); }; }); }); | ||
export function atSet(E) { | ||
var elemE = S.elem(E); | ||
var insertE = S.insert(E); | ||
var removeE = S.remove(E); | ||
return new At(function (at) { | ||
var insertEAt = insertE(at); | ||
var removeEAt = removeE(at); | ||
return new Lens(function (s) { return elemE(at, s); }, function (a) { return function (s) { return (a ? insertEAt(s) : removeEAt(s)); }; }); | ||
}); | ||
} |
@@ -1,2 +0,2 @@ | ||
import { right, left } from 'fp-ts/lib/Either'; | ||
import { right, left, fold } from 'fp-ts/lib/Either'; | ||
import { fromEither, none, some } from 'fp-ts/lib/Option'; | ||
@@ -6,3 +6,3 @@ import { Prism } from '.'; | ||
export var _right = function () { return r; }; | ||
var l = new Prism(function (e) { return e.fold(some, function () { return none; }); }, left); | ||
var l = new Prism(fold(some, function () { return none; }), left); | ||
export var _left = function () { return l; }; |
@@ -1,4 +0,4 @@ | ||
import { HKT, URIS, URIS2, URIS3, Type, Type2, Type3 } from 'fp-ts/lib/HKT'; | ||
import { HKT, URIS, URIS2, URIS3, Kind3, Kind2, Kind } from 'fp-ts/lib/HKT'; | ||
import { Monoid } from 'fp-ts/lib/Monoid'; | ||
import { Applicative, Applicative1, Applicative2, Applicative3, Applicative2C, Applicative3C } from 'fp-ts/lib/Applicative'; | ||
import { Applicative, Applicative1, Applicative2, Applicative3, Applicative2C } from 'fp-ts/lib/Applicative'; | ||
import { Foldable, Foldable1, Foldable2, Foldable3 } from 'fp-ts/lib/Foldable'; | ||
@@ -36,3 +36,3 @@ import { Traversable, Traversable1, Traversable2, Traversable3 } from 'fp-ts/lib/Traversable'; | ||
compose<B>(ab: Iso<A, B>): Iso<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composeIso<B>(ab: Iso<A, B>): Iso<S, B>; | ||
@@ -86,7 +86,2 @@ /** compose an Iso with a Lens */ | ||
static fromPath<S>(): LensFromPath<S>; | ||
static fromPath<S, K1 extends keyof S, K2 extends keyof S[K1], K3 extends keyof S[K1][K2], K4 extends keyof S[K1][K2][K3], K5 extends keyof S[K1][K2][K3][K4]>(path: [K1, K2, K3, K4, K5]): Lens<S, S[K1][K2][K3][K4][K5]>; | ||
static fromPath<S, K1 extends keyof S, K2 extends keyof S[K1], K3 extends keyof S[K1][K2], K4 extends keyof S[K1][K2][K3]>(path: [K1, K2, K3, K4]): Lens<S, S[K1][K2][K3][K4]>; | ||
static fromPath<S, K1 extends keyof S, K2 extends keyof S[K1], K3 extends keyof S[K1][K2]>(path: [K1, K2, K3]): Lens<S, S[K1][K2][K3]>; | ||
static fromPath<S, K1 extends keyof S, K2 extends keyof S[K1]>(path: [K1, K2]): Lens<S, S[K1][K2]>; | ||
static fromPath<S, K1 extends keyof S>(path: [K1]): Lens<S, S[K1]>; | ||
/** | ||
@@ -104,4 +99,2 @@ * generate a lens from a type and a prop | ||
* const age = Lens.fromProp<Person>()('age') | ||
* // or (deprecated) | ||
* // const age = Lens.fromProp<Person, 'age'>('age') | ||
* | ||
@@ -114,3 +107,2 @@ * const person: Person = { name: 'Giulio', age: 43 } | ||
static fromProp<S>(): <P extends keyof S>(prop: P) => Lens<S, S[P]>; | ||
static fromProp<S, P extends keyof S>(prop: P): Lens<S, S[P]>; | ||
/** | ||
@@ -163,3 +155,2 @@ * generate a lens from a type and an array of props | ||
static fromNullableProp<S>(): <A extends S[K], K extends keyof S>(k: K, defaultValue: A) => Lens<S, NonNullable<S[K]>>; | ||
static fromNullableProp<S, A extends S[K], K extends keyof S>(k: K, defaultValue: A): Lens<S, NonNullable<S[K]>>; | ||
modify(f: (a: A) => A): (s: S) => S; | ||
@@ -178,3 +169,3 @@ /** view a Lens as a Optional */ | ||
compose<B>(ab: Lens<A, B>): Lens<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composeLens<B>(ab: Lens<A, B>): Lens<S, B>; | ||
@@ -203,7 +194,2 @@ /** compose a Lens with a Getter */ | ||
static fromPredicate<A>(predicate: Predicate<A>): Prism<A, A>; | ||
/** | ||
* Use `fromPredicate` instead | ||
* @deprecated | ||
*/ | ||
static fromRefinement<S, A extends S>(refinement: Refinement<S, A>): Prism<S, A>; | ||
static some<A>(): Prism<Option<A>, A>; | ||
@@ -224,3 +210,3 @@ modify(f: (a: A) => A): (s: S) => S; | ||
compose<B>(ab: Prism<A, B>): Prism<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composePrism<B>(ab: Prism<A, B>): Prism<S, B>; | ||
@@ -296,3 +282,2 @@ /** compose a Prism with a Optional */ | ||
static fromNullableProp<S>(): <K extends keyof S>(k: K) => Optional<S, NonNullable<S[K]>>; | ||
static fromNullableProp<S, A extends S[K], K extends keyof S>(k: K): Optional<S, NonNullable<S[K]>>; | ||
/** | ||
@@ -316,5 +301,5 @@ * @example | ||
* | ||
* const info = Optional.fromOptionProp<Response>('info') | ||
* const employment = Optional.fromOptionProp<Info>('employment') | ||
* const phone = Optional.fromOptionProp<Employment>('phone') | ||
* const info = Optional.fromOptionProp<Response>()('info') | ||
* const employment = Optional.fromOptionProp<Info>()('employment') | ||
* const phone = Optional.fromOptionProp<Employment>()('phone') | ||
* const number = Lens.fromProp<Phone>()('number') | ||
@@ -327,3 +312,2 @@ * export const numberFromResponse = info | ||
static fromOptionProp<S>(): <P extends OptionPropertyNames<S>>(prop: P) => Optional<S, OptionPropertyType<S, P>>; | ||
static fromOptionProp<S>(prop: OptionPropertyNames<S>): Optional<S, OptionPropertyType<S, typeof prop>>; | ||
modify(f: (a: A) => A): (s: S) => S; | ||
@@ -339,3 +323,3 @@ modifyOption(f: (a: A) => A): (s: S) => Option<S>; | ||
compose<B>(ab: Optional<A, B>): Optional<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composeOptional<B>(ab: Optional<A, B>): Optional<S, B>; | ||
@@ -358,7 +342,6 @@ /** compose an Optional with a Traversal */ | ||
export interface ModifyF<S, A> { | ||
<F extends URIS3>(F: Applicative3<F>): <U, L>(f: (a: A) => Type3<F, U, L, A>) => (s: S) => Type3<F, U, L, S>; | ||
<F extends URIS3, U, L>(F: Applicative3C<F, U, L>): (f: (a: A) => Type3<F, U, L, A>) => (s: S) => Type3<F, U, L, S>; | ||
<F extends URIS2>(F: Applicative2<F>): <L>(f: (a: A) => Type2<F, L, A>) => (s: S) => Type2<F, L, S>; | ||
<F extends URIS2, L>(F: Applicative2C<F, L>): (f: (a: A) => Type2<F, L, A>) => (s: S) => Type2<F, L, S>; | ||
<F extends URIS>(F: Applicative1<F>): (f: (a: A) => Type<F, A>) => (s: S) => Type<F, S>; | ||
<F extends URIS3>(F: Applicative3<F>): <U, L>(f: (a: A) => Kind3<F, U, L, A>) => (s: S) => Kind3<F, U, L, S>; | ||
<F extends URIS2>(F: Applicative2<F>): <L>(f: (a: A) => Kind2<F, L, A>) => (s: S) => Kind2<F, L, S>; | ||
<F extends URIS2, L>(F: Applicative2C<F, L>): (f: (a: A) => Kind2<F, L, A>) => (s: S) => Kind2<F, L, S>; | ||
<F extends URIS>(F: Applicative1<F>): (f: (a: A) => Kind<F, A>) => (s: S) => Kind<F, S>; | ||
<F>(F: Applicative<F>): (f: (a: A) => HKT<F, A>) => (s: S) => HKT<F, S>; | ||
@@ -401,3 +384,3 @@ } | ||
compose<B>(ab: Traversal<A, B>): Traversal<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B>; | ||
@@ -442,3 +425,3 @@ /** compose a Traversal with a Fold */ | ||
compose<B>(ab: Getter<A, B>): Getter<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composeGetter<B>(ab: Getter<A, B>): Getter<S, B>; | ||
@@ -471,3 +454,3 @@ /** compose a Getter with a Fold */ | ||
compose<B>(ab: Fold<A, B>): Fold<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composeFold<B>(ab: Fold<A, B>): Fold<S, B>; | ||
@@ -499,3 +482,3 @@ /** compose a Fold with a Getter */ | ||
compose<B>(ab: Setter<A, B>): Setter<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composeSetter<B>(ab: Setter<A, B>): Setter<S, B>; | ||
@@ -546,11 +529,11 @@ /** compose a Setter with a Traversal */ | ||
*/ | ||
export declare function fromTraversable<T extends URIS3>(T: Traversable3<T>): <U, L, A>() => Traversal<Type3<T, U, L, A>, A>; | ||
export declare function fromTraversable<T extends URIS2>(T: Traversable2<T>): <L, A>() => Traversal<Type2<T, L, A>, A>; | ||
export declare function fromTraversable<T extends URIS>(T: Traversable1<T>): <A>() => Traversal<Type<T, A>, A>; | ||
export declare function fromTraversable<T extends URIS3>(T: Traversable3<T>): <U, L, A>() => Traversal<Kind3<T, U, L, A>, A>; | ||
export declare function fromTraversable<T extends URIS2>(T: Traversable2<T>): <L, A>() => Traversal<Kind2<T, L, A>, A>; | ||
export declare function fromTraversable<T extends URIS>(T: Traversable1<T>): <A>() => Traversal<Kind<T, A>, A>; | ||
export declare function fromTraversable<T>(T: Traversable<T>): <A>() => Traversal<HKT<T, A>, A>; | ||
/** create a Fold from a Foldable */ | ||
export declare function fromFoldable<F extends URIS3>(F: Foldable3<F>): <U, L, A>() => Fold<Type3<F, U, L, A>, A>; | ||
export declare function fromFoldable<F extends URIS2>(F: Foldable2<F>): <L, A>() => Fold<Type2<F, L, A>, A>; | ||
export declare function fromFoldable<F extends URIS>(F: Foldable1<F>): <A>() => Fold<Type<F, A>, A>; | ||
export declare function fromFoldable<F extends URIS3>(F: Foldable3<F>): <U, L, A>() => Fold<Kind3<F, U, L, A>, A>; | ||
export declare function fromFoldable<F extends URIS2>(F: Foldable2<F>): <L, A>() => Fold<Kind2<F, L, A>, A>; | ||
export declare function fromFoldable<F extends URIS>(F: Foldable1<F>): <A>() => Fold<Kind<F, A>, A>; | ||
export declare function fromFoldable<F>(F: Foldable<F>): <A>() => Fold<HKT<F, A>, A>; | ||
export {}; |
287
es6/index.js
@@ -1,11 +0,8 @@ | ||
import { getArrayMonoid, monoidAll, monoidAny } from 'fp-ts/lib/Monoid'; | ||
import { foldMap } from 'fp-ts/lib/Foldable'; | ||
import { none, some, fromNullable, getFirstMonoid, fromPredicate } from 'fp-ts/lib/Option'; | ||
import { monoidAll, monoidAny } from 'fp-ts/lib/Monoid'; | ||
import { none, some, fromNullable, getFirstMonoid, fromPredicate, isNone, option } from 'fp-ts/lib/Option'; | ||
import { identity, constant } from 'fp-ts/lib/function'; | ||
import { identity as id } from 'fp-ts/lib/Identity'; | ||
import { Const, getApplicative } from 'fp-ts/lib/Const'; | ||
/** | ||
* @internal | ||
*/ | ||
export var update = function (o, k, a) { | ||
import { getApplicative, make } from 'fp-ts/lib/Const'; | ||
import { getMonoid } from 'fp-ts/lib/Array'; | ||
var update = function (o, k, a) { | ||
var _a; | ||
@@ -79,3 +76,3 @@ return a === o[k] ? o : Object.assign({}, o, (_a = {}, _a[k] = a, _a)); | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Iso.prototype.composeIso = function (ab) { | ||
@@ -115,12 +112,2 @@ return this.compose(ab); | ||
export { Iso }; | ||
function lensFromPath(path) { | ||
var lens = Lens.fromProp(path[0]); | ||
return path.slice(1).reduce(function (acc, prop) { return acc.compose(Lens.fromProp(prop)); }, lens); | ||
} | ||
function lensFromProp(prop) { | ||
return new Lens(function (s) { return s[prop]; }, function (a) { return function (s) { return update(s, prop, a); }; }); | ||
} | ||
function lensFromNullableProp(k, defaultValue) { | ||
return new Lens(function (s) { return fromNullable(s[k]).getOrElse(defaultValue); }, function (a) { return function (s) { return update(s, k, a); }; }); | ||
} | ||
/* | ||
@@ -138,7 +125,48 @@ Laws: | ||
} | ||
/** | ||
* @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' } }) | ||
*/ | ||
Lens.fromPath = function () { | ||
return arguments.length === 0 ? lensFromPath : lensFromPath(arguments[0]); | ||
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); | ||
}; | ||
}; | ||
/** | ||
* generate 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 }) | ||
*/ | ||
Lens.fromProp = function () { | ||
return arguments.length === 0 ? lensFromProp : lensFromProp(arguments[0]); | ||
return function (prop) { return new Lens(function (s) { return s[prop]; }, function (a) { return function (s) { return update(s, prop, a); }; }); }; | ||
}; | ||
@@ -185,6 +213,38 @@ /** | ||
}; | ||
/** | ||
* generate a lens from a type and a prop whose type is nullable | ||
* | ||
* @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) | ||
*/ | ||
Lens.fromNullableProp = function () { | ||
return arguments.length === 0 | ||
? lensFromNullableProp | ||
: lensFromNullableProp(arguments[0], arguments[1]); | ||
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); }; }); | ||
}; | ||
}; | ||
@@ -231,3 +291,3 @@ Lens.prototype.modify = function (f) { | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Lens.prototype.composeLens = function (ab) { | ||
@@ -281,9 +341,2 @@ return this.compose(ab); | ||
}; | ||
/** | ||
* Use `fromPredicate` instead | ||
* @deprecated | ||
*/ | ||
Prism.fromRefinement = function (refinement) { | ||
return new Prism(function (s) { return (refinement(s) ? some(s) : none); }, identity); | ||
}; | ||
Prism.some = function () { | ||
@@ -294,3 +347,11 @@ return somePrism; | ||
var _this = this; | ||
return function (s) { return _this.modifyOption(f)(s).getOrElse(s); }; | ||
return function (s) { | ||
var os = _this.modifyOption(f)(s); | ||
if (isNone(os)) { | ||
return s; | ||
} | ||
else { | ||
return os.value; | ||
} | ||
}; | ||
}; | ||
@@ -300,3 +361,3 @@ Prism.prototype.modifyOption = function (f) { | ||
return function (s) { | ||
return _this.getOption(s).map(function (v) { | ||
return option.map(_this.getOption(s), function (v) { | ||
var n = f(v); | ||
@@ -320,3 +381,9 @@ return n === v ? s : _this.reverseGet(n); | ||
return new Traversal(function (F) { return function (f) { return function (s) { | ||
return _this.getOption(s).foldL(function () { return F.of(s); }, function (a) { return F.map(f(a), function (a) { return _this.set(a)(s); }); }); | ||
var oa = _this.getOption(s); | ||
if (isNone(oa)) { | ||
return F.of(s); | ||
} | ||
else { | ||
return F.map(f(oa.value), function (a) { return _this.set(a)(s); }); | ||
} | ||
}; }; }); | ||
@@ -332,3 +399,11 @@ }; | ||
var _this = this; | ||
return new Fold(function (M) { return function (f) { return function (s) { return _this.getOption(s).fold(M.empty, f); }; }; }); | ||
return new Fold(function (M) { return function (f) { return function (s) { | ||
var oa = _this.getOption(s); | ||
if (isNone(oa)) { | ||
return M.empty; | ||
} | ||
else { | ||
return f(oa.value); | ||
} | ||
}; }; }); | ||
}; | ||
@@ -338,5 +413,5 @@ /** compose a Prism with a Prism */ | ||
var _this = this; | ||
return new Prism(function (s) { return _this.getOption(s).chain(function (a) { return ab.getOption(a); }); }, function (b) { return _this.reverseGet(ab.reverseGet(b)); }); | ||
return new Prism(function (s) { return option.chain(_this.getOption(s), function (a) { return ab.getOption(a); }); }, function (b) { return _this.reverseGet(ab.reverseGet(b)); }); | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Prism.prototype.composePrism = function (ab) { | ||
@@ -377,8 +452,2 @@ return this.compose(ab); | ||
var somePrism = new Prism(identity, some); | ||
function optionalFromNullableProp(k) { | ||
return new Optional(function (s) { return fromNullable(s[k]); }, function (a) { return function (s) { return update(s, k, a); }; }); | ||
} | ||
function optionalFromOptionProp(k) { | ||
return lensFromProp(k).composePrism(somePrism); | ||
} | ||
/* | ||
@@ -396,11 +465,91 @@ Laws: | ||
} | ||
/** | ||
* @example | ||
* import { Optional, Lens } from 'monocle-ts' | ||
* | ||
* interface Phone { | ||
* number: string | ||
* } | ||
* interface Employment { | ||
* phone?: Phone | ||
* } | ||
* interface Info { | ||
* employment?: Employment | ||
* } | ||
* interface Response { | ||
* info?: Info | ||
* } | ||
* | ||
* const info = Optional.fromNullableProp<Response>()('info') | ||
* const employment = Optional.fromNullableProp<Info>()('employment') | ||
* const phone = Optional.fromNullableProp<Employment>()('phone') | ||
* const number = Lens.fromProp<Phone>()('number') | ||
* const numberFromResponse = info | ||
* .compose(employment) | ||
* .compose(phone) | ||
* .composeLens(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 | ||
*/ | ||
Optional.fromNullableProp = function () { | ||
return arguments.length === 0 ? optionalFromNullableProp : optionalFromNullableProp(arguments[0]); | ||
return function (k) { return new Optional(function (s) { return fromNullable(s[k]); }, function (a) { return function (s) { return update(s, k, a); }; }); }; | ||
}; | ||
/** | ||
* @example | ||
* import { Optional, Lens } from 'monocle-ts' | ||
* import { Option } from 'fp-ts/lib/Option' | ||
* | ||
* interface Phone { | ||
* number: string | ||
* } | ||
* interface Employment { | ||
* phone: Option<Phone> | ||
* } | ||
* interface Info { | ||
* employment: Option<Employment> | ||
* } | ||
* interface Response { | ||
* info: Option<Info> | ||
* } | ||
* | ||
* const info = Optional.fromOptionProp<Response>()('info') | ||
* const employment = Optional.fromOptionProp<Info>()('employment') | ||
* const phone = Optional.fromOptionProp<Employment>()('phone') | ||
* const number = Lens.fromProp<Phone>()('number') | ||
* export const numberFromResponse = info | ||
* .compose(employment) | ||
* .compose(phone) | ||
* .composeLens(number) | ||
*/ | ||
Optional.fromOptionProp = function () { | ||
return arguments.length === 0 ? optionalFromOptionProp : optionalFromOptionProp(arguments[0]); | ||
var formProp = Lens.fromProp(); | ||
return function (prop) { return formProp(prop).composePrism(somePrism); }; | ||
}; | ||
Optional.prototype.modify = function (f) { | ||
var _this = this; | ||
return function (s) { return _this.modifyOption(f)(s).getOrElse(s); }; | ||
return function (s) { | ||
var os = _this.modifyOption(f)(s); | ||
if (isNone(os)) { | ||
return s; | ||
} | ||
else { | ||
return os.value; | ||
} | ||
}; | ||
}; | ||
@@ -410,3 +559,3 @@ Optional.prototype.modifyOption = function (f) { | ||
return function (s) { | ||
return _this.getOption(s).map(function (a) { | ||
return option.map(_this.getOption(s), function (a) { | ||
var n = f(a); | ||
@@ -421,3 +570,9 @@ return n === a ? s : _this.set(n)(s); | ||
return new Traversal(function (F) { return function (f) { return function (s) { | ||
return _this.getOption(s).foldL(function () { return F.of(s); }, function (a) { return F.map(f(a), function (a) { return _this.set(a)(s); }); }); | ||
var oa = _this.getOption(s); | ||
if (isNone(oa)) { | ||
return F.of(s); | ||
} | ||
else { | ||
return F.map(f(oa.value), function (a) { return _this.set(a)(s); }); | ||
} | ||
}; }; }); | ||
@@ -428,3 +583,11 @@ }; | ||
var _this = this; | ||
return new Fold(function (M) { return function (f) { return function (s) { return _this.getOption(s).fold(M.empty, f); }; }; }); | ||
return new Fold(function (M) { return function (f) { return function (s) { | ||
var oa = _this.getOption(s); | ||
if (isNone(oa)) { | ||
return M.empty; | ||
} | ||
else { | ||
return f(oa.value); | ||
} | ||
}; }; }); | ||
}; | ||
@@ -439,5 +602,5 @@ /** view an Optional as a Setter */ | ||
var _this = this; | ||
return new Optional(function (s) { return _this.getOption(s).chain(function (a) { return ab.getOption(a); }); }, function (b) { return _this.modify(ab.set(b)); }); | ||
return new Optional(function (s) { return option.chain(_this.getOption(s), function (a) { return ab.getOption(a); }); }, function (b) { return _this.modify(ab.set(b)); }); | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Optional.prototype.composeOptional = function (ab) { | ||
@@ -486,3 +649,3 @@ return this.compose(ab); | ||
var _this = this; | ||
return function (s) { return _this.modifyF(id)(function (a) { return id.of(f(a)); })(s).extract(); }; | ||
return function (s) { return _this.modifyF(id)(f)(s); }; | ||
}; | ||
@@ -498,5 +661,3 @@ Traversal.prototype.set = function (a) { | ||
var _this = this; | ||
return new Fold(function (M) { return function (f) { return function (s) { | ||
return _this.modifyF(getApplicative(M))(function (a) { return new Const(f(a)); })(s).fold(identity); | ||
}; }; }); | ||
return new Fold(function (M) { return function (f) { return function (s) { return _this.modifyF(getApplicative(M))(function (a) { return make(f(a)); })(s); }; }; }); | ||
}; | ||
@@ -513,3 +674,3 @@ /** view a Traversal as a Setter */ | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Traversal.prototype.composeTraversal = function (ab) { | ||
@@ -593,3 +754,3 @@ return this.compose(ab); | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Getter.prototype.composeGetter = function (ab) { | ||
@@ -629,3 +790,3 @@ return this.compose(ab); | ||
this._tag = 'Fold'; | ||
this.getAll = foldMap(getArrayMonoid())(function (a) { return [a]; }); | ||
this.getAll = foldMap(getMonoid())(function (a) { return [a]; }); | ||
this.exist = foldMap(monoidAny); | ||
@@ -640,3 +801,3 @@ this.all = foldMap(monoidAll); | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Fold.prototype.composeFold = function (ab) { | ||
@@ -692,3 +853,3 @@ return this.compose(ab); | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Setter.prototype.composeSetter = function (ab) { | ||
@@ -720,3 +881,2 @@ return this.compose(ab); | ||
export { Setter }; | ||
// tslint:disable-next-line: deprecation | ||
export function fromTraversable(T) { | ||
@@ -730,7 +890,6 @@ return function () { | ||
} | ||
// tslint:disable-next-line: deprecation | ||
export function fromFoldable(F) { | ||
return function () { | ||
return new Fold(function (M) { | ||
var foldMapFM = foldMap(F, M); | ||
var foldMapFM = F.foldMap(M); | ||
return function (f) { return function (s) { return foldMapFM(s, f); }; }; | ||
@@ -737,0 +896,0 @@ }); |
import { Index, Optional } from '../index'; | ||
import { index, updateAt } from 'fp-ts/lib/Array'; | ||
import { lookup, updateAt } from 'fp-ts/lib/Array'; | ||
import { isNone } from 'fp-ts/lib/Option'; | ||
export function indexArray() { | ||
// tslint:disable-next-line: deprecation | ||
return new Index(function (i) { return new Optional(function (s) { return index(i, s); }, function (a) { return function (s) { return updateAt(i, a, s).getOrElse(s); }; }); }); | ||
return new Index(function (i) { | ||
return new Optional(function (as) { return lookup(i, as); }, function (a) { return function (as) { | ||
var oas = updateAt(i, a)(as); | ||
if (isNone(oas)) { | ||
return as; | ||
} | ||
else { | ||
return oas.value; | ||
} | ||
}; }); | ||
}); | ||
} |
import { Index, Optional } from '../index'; | ||
import { updateAt } from 'fp-ts/lib/NonEmptyArray'; | ||
import { lookup } from 'fp-ts/lib/Array'; | ||
import { isNone } from 'fp-ts/lib/Option'; | ||
export function indexNonEmptyArray() { | ||
// tslint:disable-next-line: deprecation | ||
return new Index(function (i) { return new Optional(function (s) { return s.index(i); }, function (a) { return function (s) { return s.updateAt(i, a).getOrElse(s); }; }); }); | ||
return new Index(function (i) { | ||
return new Optional(function (s) { return lookup(i, s); }, function (a) { return function (nea) { | ||
var onea = updateAt(i, a)(nea); | ||
if (isNone(onea)) { | ||
return nea; | ||
} | ||
else { | ||
return onea.value; | ||
} | ||
}; }); | ||
}); | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var index_1 = require("../index"); | ||
var Option_1 = require("fp-ts/lib/Option"); | ||
var R = require("fp-ts/lib/Record"); | ||
function atRecord() { | ||
return new index_1.At(function (at) { return new index_1.Lens(function (s) { return R.lookup(at, s); }, function (a) { return function (s) { return a.fold(R.remove(at, s), function (x) { return R.insert(at, x, s); }); }; }); }); | ||
return new index_1.At(function (k) { | ||
return new index_1.Lens(function (r) { return R.lookup(k, r); }, function (oa) { return function (r) { | ||
if (Option_1.isNone(oa)) { | ||
return R.deleteAt(k)(r); | ||
} | ||
else { | ||
return R.insertAt(k, oa.value)(r); | ||
} | ||
}; }); | ||
}); | ||
} | ||
exports.atRecord = atRecord; |
import { At } from '../index'; | ||
import { Setoid } from 'fp-ts/lib/Setoid'; | ||
export declare function atSet<A = never>(setoid: Setoid<A>): At<Set<A>, A, boolean>; | ||
import { Eq } from 'fp-ts/lib/Eq'; | ||
export declare function atSet<A = never>(E: Eq<A>): At<Set<A>, A, boolean>; |
@@ -5,9 +5,12 @@ "use strict"; | ||
var S = require("fp-ts/lib/Set"); | ||
function atSet(setoid) { | ||
// tslint:disable-next-line: deprecation | ||
var member = S.member(setoid); | ||
var insert = S.insert(setoid); | ||
var remove = S.remove(setoid); | ||
return new index_1.At(function (at) { return new index_1.Lens(function (s) { return member(s)(at); }, function (a) { return function (s) { return (a ? insert(at, s) : remove(at, s)); }; }); }); | ||
function atSet(E) { | ||
var elemE = S.elem(E); | ||
var insertE = S.insert(E); | ||
var removeE = S.remove(E); | ||
return new index_1.At(function (at) { | ||
var insertEAt = insertE(at); | ||
var removeEAt = removeE(at); | ||
return new index_1.Lens(function (s) { return elemE(at, s); }, function (a) { return function (s) { return (a ? insertEAt(s) : removeEAt(s)); }; }); | ||
}); | ||
} | ||
exports.atSet = atSet; |
@@ -8,3 +8,3 @@ "use strict"; | ||
exports._right = function () { return r; }; | ||
var l = new _1.Prism(function (e) { return e.fold(Option_1.some, function () { return Option_1.none; }); }, Either_1.left); | ||
var l = new _1.Prism(Either_1.fold(Option_1.some, function () { return Option_1.none; }), Either_1.left); | ||
exports._left = function () { return l; }; |
@@ -1,4 +0,4 @@ | ||
import { HKT, URIS, URIS2, URIS3, Type, Type2, Type3 } from 'fp-ts/lib/HKT'; | ||
import { HKT, URIS, URIS2, URIS3, Kind3, Kind2, Kind } from 'fp-ts/lib/HKT'; | ||
import { Monoid } from 'fp-ts/lib/Monoid'; | ||
import { Applicative, Applicative1, Applicative2, Applicative3, Applicative2C, Applicative3C } from 'fp-ts/lib/Applicative'; | ||
import { Applicative, Applicative1, Applicative2, Applicative3, Applicative2C } from 'fp-ts/lib/Applicative'; | ||
import { Foldable, Foldable1, Foldable2, Foldable3 } from 'fp-ts/lib/Foldable'; | ||
@@ -36,3 +36,3 @@ import { Traversable, Traversable1, Traversable2, Traversable3 } from 'fp-ts/lib/Traversable'; | ||
compose<B>(ab: Iso<A, B>): Iso<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composeIso<B>(ab: Iso<A, B>): Iso<S, B>; | ||
@@ -86,7 +86,2 @@ /** compose an Iso with a Lens */ | ||
static fromPath<S>(): LensFromPath<S>; | ||
static fromPath<S, K1 extends keyof S, K2 extends keyof S[K1], K3 extends keyof S[K1][K2], K4 extends keyof S[K1][K2][K3], K5 extends keyof S[K1][K2][K3][K4]>(path: [K1, K2, K3, K4, K5]): Lens<S, S[K1][K2][K3][K4][K5]>; | ||
static fromPath<S, K1 extends keyof S, K2 extends keyof S[K1], K3 extends keyof S[K1][K2], K4 extends keyof S[K1][K2][K3]>(path: [K1, K2, K3, K4]): Lens<S, S[K1][K2][K3][K4]>; | ||
static fromPath<S, K1 extends keyof S, K2 extends keyof S[K1], K3 extends keyof S[K1][K2]>(path: [K1, K2, K3]): Lens<S, S[K1][K2][K3]>; | ||
static fromPath<S, K1 extends keyof S, K2 extends keyof S[K1]>(path: [K1, K2]): Lens<S, S[K1][K2]>; | ||
static fromPath<S, K1 extends keyof S>(path: [K1]): Lens<S, S[K1]>; | ||
/** | ||
@@ -104,4 +99,2 @@ * generate a lens from a type and a prop | ||
* const age = Lens.fromProp<Person>()('age') | ||
* // or (deprecated) | ||
* // const age = Lens.fromProp<Person, 'age'>('age') | ||
* | ||
@@ -114,3 +107,2 @@ * const person: Person = { name: 'Giulio', age: 43 } | ||
static fromProp<S>(): <P extends keyof S>(prop: P) => Lens<S, S[P]>; | ||
static fromProp<S, P extends keyof S>(prop: P): Lens<S, S[P]>; | ||
/** | ||
@@ -163,3 +155,2 @@ * generate a lens from a type and an array of props | ||
static fromNullableProp<S>(): <A extends S[K], K extends keyof S>(k: K, defaultValue: A) => Lens<S, NonNullable<S[K]>>; | ||
static fromNullableProp<S, A extends S[K], K extends keyof S>(k: K, defaultValue: A): Lens<S, NonNullable<S[K]>>; | ||
modify(f: (a: A) => A): (s: S) => S; | ||
@@ -178,3 +169,3 @@ /** view a Lens as a Optional */ | ||
compose<B>(ab: Lens<A, B>): Lens<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composeLens<B>(ab: Lens<A, B>): Lens<S, B>; | ||
@@ -203,7 +194,2 @@ /** compose a Lens with a Getter */ | ||
static fromPredicate<A>(predicate: Predicate<A>): Prism<A, A>; | ||
/** | ||
* Use `fromPredicate` instead | ||
* @deprecated | ||
*/ | ||
static fromRefinement<S, A extends S>(refinement: Refinement<S, A>): Prism<S, A>; | ||
static some<A>(): Prism<Option<A>, A>; | ||
@@ -224,3 +210,3 @@ modify(f: (a: A) => A): (s: S) => S; | ||
compose<B>(ab: Prism<A, B>): Prism<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composePrism<B>(ab: Prism<A, B>): Prism<S, B>; | ||
@@ -296,3 +282,2 @@ /** compose a Prism with a Optional */ | ||
static fromNullableProp<S>(): <K extends keyof S>(k: K) => Optional<S, NonNullable<S[K]>>; | ||
static fromNullableProp<S, A extends S[K], K extends keyof S>(k: K): Optional<S, NonNullable<S[K]>>; | ||
/** | ||
@@ -316,5 +301,5 @@ * @example | ||
* | ||
* const info = Optional.fromOptionProp<Response>('info') | ||
* const employment = Optional.fromOptionProp<Info>('employment') | ||
* const phone = Optional.fromOptionProp<Employment>('phone') | ||
* const info = Optional.fromOptionProp<Response>()('info') | ||
* const employment = Optional.fromOptionProp<Info>()('employment') | ||
* const phone = Optional.fromOptionProp<Employment>()('phone') | ||
* const number = Lens.fromProp<Phone>()('number') | ||
@@ -327,3 +312,2 @@ * export const numberFromResponse = info | ||
static fromOptionProp<S>(): <P extends OptionPropertyNames<S>>(prop: P) => Optional<S, OptionPropertyType<S, P>>; | ||
static fromOptionProp<S>(prop: OptionPropertyNames<S>): Optional<S, OptionPropertyType<S, typeof prop>>; | ||
modify(f: (a: A) => A): (s: S) => S; | ||
@@ -339,3 +323,3 @@ modifyOption(f: (a: A) => A): (s: S) => Option<S>; | ||
compose<B>(ab: Optional<A, B>): Optional<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composeOptional<B>(ab: Optional<A, B>): Optional<S, B>; | ||
@@ -358,7 +342,6 @@ /** compose an Optional with a Traversal */ | ||
export interface ModifyF<S, A> { | ||
<F extends URIS3>(F: Applicative3<F>): <U, L>(f: (a: A) => Type3<F, U, L, A>) => (s: S) => Type3<F, U, L, S>; | ||
<F extends URIS3, U, L>(F: Applicative3C<F, U, L>): (f: (a: A) => Type3<F, U, L, A>) => (s: S) => Type3<F, U, L, S>; | ||
<F extends URIS2>(F: Applicative2<F>): <L>(f: (a: A) => Type2<F, L, A>) => (s: S) => Type2<F, L, S>; | ||
<F extends URIS2, L>(F: Applicative2C<F, L>): (f: (a: A) => Type2<F, L, A>) => (s: S) => Type2<F, L, S>; | ||
<F extends URIS>(F: Applicative1<F>): (f: (a: A) => Type<F, A>) => (s: S) => Type<F, S>; | ||
<F extends URIS3>(F: Applicative3<F>): <U, L>(f: (a: A) => Kind3<F, U, L, A>) => (s: S) => Kind3<F, U, L, S>; | ||
<F extends URIS2>(F: Applicative2<F>): <L>(f: (a: A) => Kind2<F, L, A>) => (s: S) => Kind2<F, L, S>; | ||
<F extends URIS2, L>(F: Applicative2C<F, L>): (f: (a: A) => Kind2<F, L, A>) => (s: S) => Kind2<F, L, S>; | ||
<F extends URIS>(F: Applicative1<F>): (f: (a: A) => Kind<F, A>) => (s: S) => Kind<F, S>; | ||
<F>(F: Applicative<F>): (f: (a: A) => HKT<F, A>) => (s: S) => HKT<F, S>; | ||
@@ -401,3 +384,3 @@ } | ||
compose<B>(ab: Traversal<A, B>): Traversal<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B>; | ||
@@ -442,3 +425,3 @@ /** compose a Traversal with a Fold */ | ||
compose<B>(ab: Getter<A, B>): Getter<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composeGetter<B>(ab: Getter<A, B>): Getter<S, B>; | ||
@@ -471,3 +454,3 @@ /** compose a Getter with a Fold */ | ||
compose<B>(ab: Fold<A, B>): Fold<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composeFold<B>(ab: Fold<A, B>): Fold<S, B>; | ||
@@ -499,3 +482,3 @@ /** compose a Fold with a Getter */ | ||
compose<B>(ab: Setter<A, B>): Setter<S, B>; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
composeSetter<B>(ab: Setter<A, B>): Setter<S, B>; | ||
@@ -546,11 +529,11 @@ /** compose a Setter with a Traversal */ | ||
*/ | ||
export declare function fromTraversable<T extends URIS3>(T: Traversable3<T>): <U, L, A>() => Traversal<Type3<T, U, L, A>, A>; | ||
export declare function fromTraversable<T extends URIS2>(T: Traversable2<T>): <L, A>() => Traversal<Type2<T, L, A>, A>; | ||
export declare function fromTraversable<T extends URIS>(T: Traversable1<T>): <A>() => Traversal<Type<T, A>, A>; | ||
export declare function fromTraversable<T extends URIS3>(T: Traversable3<T>): <U, L, A>() => Traversal<Kind3<T, U, L, A>, A>; | ||
export declare function fromTraversable<T extends URIS2>(T: Traversable2<T>): <L, A>() => Traversal<Kind2<T, L, A>, A>; | ||
export declare function fromTraversable<T extends URIS>(T: Traversable1<T>): <A>() => Traversal<Kind<T, A>, A>; | ||
export declare function fromTraversable<T>(T: Traversable<T>): <A>() => Traversal<HKT<T, A>, A>; | ||
/** create a Fold from a Foldable */ | ||
export declare function fromFoldable<F extends URIS3>(F: Foldable3<F>): <U, L, A>() => Fold<Type3<F, U, L, A>, A>; | ||
export declare function fromFoldable<F extends URIS2>(F: Foldable2<F>): <L, A>() => Fold<Type2<F, L, A>, A>; | ||
export declare function fromFoldable<F extends URIS>(F: Foldable1<F>): <A>() => Fold<Type<F, A>, A>; | ||
export declare function fromFoldable<F extends URIS3>(F: Foldable3<F>): <U, L, A>() => Fold<Kind3<F, U, L, A>, A>; | ||
export declare function fromFoldable<F extends URIS2>(F: Foldable2<F>): <L, A>() => Fold<Kind2<F, L, A>, A>; | ||
export declare function fromFoldable<F extends URIS>(F: Foldable1<F>): <A>() => Fold<Kind<F, A>, A>; | ||
export declare function fromFoldable<F>(F: Foldable<F>): <A>() => Fold<HKT<F, A>, A>; | ||
export {}; |
281
lib/index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Monoid_1 = require("fp-ts/lib/Monoid"); | ||
var Foldable_1 = require("fp-ts/lib/Foldable"); | ||
var Option_1 = require("fp-ts/lib/Option"); | ||
@@ -9,6 +8,4 @@ var function_1 = require("fp-ts/lib/function"); | ||
var Const_1 = require("fp-ts/lib/Const"); | ||
/** | ||
* @internal | ||
*/ | ||
exports.update = function (o, k, a) { | ||
var Array_1 = require("fp-ts/lib/Array"); | ||
var update = function (o, k, a) { | ||
var _a; | ||
@@ -82,3 +79,3 @@ return a === o[k] ? o : Object.assign({}, o, (_a = {}, _a[k] = a, _a)); | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Iso.prototype.composeIso = function (ab) { | ||
@@ -118,12 +115,2 @@ return this.compose(ab); | ||
exports.Iso = Iso; | ||
function lensFromPath(path) { | ||
var lens = Lens.fromProp(path[0]); | ||
return path.slice(1).reduce(function (acc, prop) { return acc.compose(Lens.fromProp(prop)); }, lens); | ||
} | ||
function lensFromProp(prop) { | ||
return new Lens(function (s) { return s[prop]; }, function (a) { return function (s) { return exports.update(s, prop, a); }; }); | ||
} | ||
function lensFromNullableProp(k, defaultValue) { | ||
return new Lens(function (s) { return Option_1.fromNullable(s[k]).getOrElse(defaultValue); }, function (a) { return function (s) { return exports.update(s, k, a); }; }); | ||
} | ||
/* | ||
@@ -141,7 +128,48 @@ Laws: | ||
} | ||
/** | ||
* @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' } }) | ||
*/ | ||
Lens.fromPath = function () { | ||
return arguments.length === 0 ? lensFromPath : lensFromPath(arguments[0]); | ||
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); | ||
}; | ||
}; | ||
/** | ||
* generate 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 }) | ||
*/ | ||
Lens.fromProp = function () { | ||
return arguments.length === 0 ? lensFromProp : lensFromProp(arguments[0]); | ||
return function (prop) { return new Lens(function (s) { return s[prop]; }, function (a) { return function (s) { return update(s, prop, a); }; }); }; | ||
}; | ||
@@ -188,6 +216,38 @@ /** | ||
}; | ||
/** | ||
* generate a lens from a type and a prop whose type is nullable | ||
* | ||
* @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) | ||
*/ | ||
Lens.fromNullableProp = function () { | ||
return arguments.length === 0 | ||
? lensFromNullableProp | ||
: lensFromNullableProp(arguments[0], arguments[1]); | ||
return function (k, defaultValue) { | ||
return new Lens(function (s) { | ||
var osk = Option_1.fromNullable(s[k]); | ||
if (Option_1.isNone(osk)) { | ||
return defaultValue; | ||
} | ||
else { | ||
return osk.value; | ||
} | ||
}, function (a) { return function (s) { return update(s, k, a); }; }); | ||
}; | ||
}; | ||
@@ -234,3 +294,3 @@ Lens.prototype.modify = function (f) { | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Lens.prototype.composeLens = function (ab) { | ||
@@ -284,9 +344,2 @@ return this.compose(ab); | ||
}; | ||
/** | ||
* Use `fromPredicate` instead | ||
* @deprecated | ||
*/ | ||
Prism.fromRefinement = function (refinement) { | ||
return new Prism(function (s) { return (refinement(s) ? Option_1.some(s) : Option_1.none); }, function_1.identity); | ||
}; | ||
Prism.some = function () { | ||
@@ -297,3 +350,11 @@ return somePrism; | ||
var _this = this; | ||
return function (s) { return _this.modifyOption(f)(s).getOrElse(s); }; | ||
return function (s) { | ||
var os = _this.modifyOption(f)(s); | ||
if (Option_1.isNone(os)) { | ||
return s; | ||
} | ||
else { | ||
return os.value; | ||
} | ||
}; | ||
}; | ||
@@ -303,3 +364,3 @@ Prism.prototype.modifyOption = function (f) { | ||
return function (s) { | ||
return _this.getOption(s).map(function (v) { | ||
return Option_1.option.map(_this.getOption(s), function (v) { | ||
var n = f(v); | ||
@@ -323,3 +384,9 @@ return n === v ? s : _this.reverseGet(n); | ||
return new Traversal(function (F) { return function (f) { return function (s) { | ||
return _this.getOption(s).foldL(function () { return F.of(s); }, function (a) { return F.map(f(a), function (a) { return _this.set(a)(s); }); }); | ||
var oa = _this.getOption(s); | ||
if (Option_1.isNone(oa)) { | ||
return F.of(s); | ||
} | ||
else { | ||
return F.map(f(oa.value), function (a) { return _this.set(a)(s); }); | ||
} | ||
}; }; }); | ||
@@ -335,3 +402,11 @@ }; | ||
var _this = this; | ||
return new Fold(function (M) { return function (f) { return function (s) { return _this.getOption(s).fold(M.empty, f); }; }; }); | ||
return new Fold(function (M) { return function (f) { return function (s) { | ||
var oa = _this.getOption(s); | ||
if (Option_1.isNone(oa)) { | ||
return M.empty; | ||
} | ||
else { | ||
return f(oa.value); | ||
} | ||
}; }; }); | ||
}; | ||
@@ -341,5 +416,5 @@ /** compose a Prism with a Prism */ | ||
var _this = this; | ||
return new Prism(function (s) { return _this.getOption(s).chain(function (a) { return ab.getOption(a); }); }, function (b) { return _this.reverseGet(ab.reverseGet(b)); }); | ||
return new Prism(function (s) { return Option_1.option.chain(_this.getOption(s), function (a) { return ab.getOption(a); }); }, function (b) { return _this.reverseGet(ab.reverseGet(b)); }); | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Prism.prototype.composePrism = function (ab) { | ||
@@ -380,8 +455,2 @@ return this.compose(ab); | ||
var somePrism = new Prism(function_1.identity, Option_1.some); | ||
function optionalFromNullableProp(k) { | ||
return new Optional(function (s) { return Option_1.fromNullable(s[k]); }, function (a) { return function (s) { return exports.update(s, k, a); }; }); | ||
} | ||
function optionalFromOptionProp(k) { | ||
return lensFromProp(k).composePrism(somePrism); | ||
} | ||
/* | ||
@@ -399,11 +468,91 @@ Laws: | ||
} | ||
/** | ||
* @example | ||
* import { Optional, Lens } from 'monocle-ts' | ||
* | ||
* interface Phone { | ||
* number: string | ||
* } | ||
* interface Employment { | ||
* phone?: Phone | ||
* } | ||
* interface Info { | ||
* employment?: Employment | ||
* } | ||
* interface Response { | ||
* info?: Info | ||
* } | ||
* | ||
* const info = Optional.fromNullableProp<Response>()('info') | ||
* const employment = Optional.fromNullableProp<Info>()('employment') | ||
* const phone = Optional.fromNullableProp<Employment>()('phone') | ||
* const number = Lens.fromProp<Phone>()('number') | ||
* const numberFromResponse = info | ||
* .compose(employment) | ||
* .compose(phone) | ||
* .composeLens(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 | ||
*/ | ||
Optional.fromNullableProp = function () { | ||
return arguments.length === 0 ? optionalFromNullableProp : optionalFromNullableProp(arguments[0]); | ||
return function (k) { return new Optional(function (s) { return Option_1.fromNullable(s[k]); }, function (a) { return function (s) { return update(s, k, a); }; }); }; | ||
}; | ||
/** | ||
* @example | ||
* import { Optional, Lens } from 'monocle-ts' | ||
* import { Option } from 'fp-ts/lib/Option' | ||
* | ||
* interface Phone { | ||
* number: string | ||
* } | ||
* interface Employment { | ||
* phone: Option<Phone> | ||
* } | ||
* interface Info { | ||
* employment: Option<Employment> | ||
* } | ||
* interface Response { | ||
* info: Option<Info> | ||
* } | ||
* | ||
* const info = Optional.fromOptionProp<Response>()('info') | ||
* const employment = Optional.fromOptionProp<Info>()('employment') | ||
* const phone = Optional.fromOptionProp<Employment>()('phone') | ||
* const number = Lens.fromProp<Phone>()('number') | ||
* export const numberFromResponse = info | ||
* .compose(employment) | ||
* .compose(phone) | ||
* .composeLens(number) | ||
*/ | ||
Optional.fromOptionProp = function () { | ||
return arguments.length === 0 ? optionalFromOptionProp : optionalFromOptionProp(arguments[0]); | ||
var formProp = Lens.fromProp(); | ||
return function (prop) { return formProp(prop).composePrism(somePrism); }; | ||
}; | ||
Optional.prototype.modify = function (f) { | ||
var _this = this; | ||
return function (s) { return _this.modifyOption(f)(s).getOrElse(s); }; | ||
return function (s) { | ||
var os = _this.modifyOption(f)(s); | ||
if (Option_1.isNone(os)) { | ||
return s; | ||
} | ||
else { | ||
return os.value; | ||
} | ||
}; | ||
}; | ||
@@ -413,3 +562,3 @@ Optional.prototype.modifyOption = function (f) { | ||
return function (s) { | ||
return _this.getOption(s).map(function (a) { | ||
return Option_1.option.map(_this.getOption(s), function (a) { | ||
var n = f(a); | ||
@@ -424,3 +573,9 @@ return n === a ? s : _this.set(n)(s); | ||
return new Traversal(function (F) { return function (f) { return function (s) { | ||
return _this.getOption(s).foldL(function () { return F.of(s); }, function (a) { return F.map(f(a), function (a) { return _this.set(a)(s); }); }); | ||
var oa = _this.getOption(s); | ||
if (Option_1.isNone(oa)) { | ||
return F.of(s); | ||
} | ||
else { | ||
return F.map(f(oa.value), function (a) { return _this.set(a)(s); }); | ||
} | ||
}; }; }); | ||
@@ -431,3 +586,11 @@ }; | ||
var _this = this; | ||
return new Fold(function (M) { return function (f) { return function (s) { return _this.getOption(s).fold(M.empty, f); }; }; }); | ||
return new Fold(function (M) { return function (f) { return function (s) { | ||
var oa = _this.getOption(s); | ||
if (Option_1.isNone(oa)) { | ||
return M.empty; | ||
} | ||
else { | ||
return f(oa.value); | ||
} | ||
}; }; }); | ||
}; | ||
@@ -442,5 +605,5 @@ /** view an Optional as a Setter */ | ||
var _this = this; | ||
return new Optional(function (s) { return _this.getOption(s).chain(function (a) { return ab.getOption(a); }); }, function (b) { return _this.modify(ab.set(b)); }); | ||
return new Optional(function (s) { return Option_1.option.chain(_this.getOption(s), function (a) { return ab.getOption(a); }); }, function (b) { return _this.modify(ab.set(b)); }); | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Optional.prototype.composeOptional = function (ab) { | ||
@@ -489,3 +652,3 @@ return this.compose(ab); | ||
var _this = this; | ||
return function (s) { return _this.modifyF(Identity_1.identity)(function (a) { return Identity_1.identity.of(f(a)); })(s).extract(); }; | ||
return function (s) { return _this.modifyF(Identity_1.identity)(f)(s); }; | ||
}; | ||
@@ -501,5 +664,3 @@ Traversal.prototype.set = function (a) { | ||
var _this = this; | ||
return new Fold(function (M) { return function (f) { return function (s) { | ||
return _this.modifyF(Const_1.getApplicative(M))(function (a) { return new Const_1.Const(f(a)); })(s).fold(function_1.identity); | ||
}; }; }); | ||
return new Fold(function (M) { return function (f) { return function (s) { return _this.modifyF(Const_1.getApplicative(M))(function (a) { return Const_1.make(f(a)); })(s); }; }; }); | ||
}; | ||
@@ -516,3 +677,3 @@ /** view a Traversal as a Setter */ | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Traversal.prototype.composeTraversal = function (ab) { | ||
@@ -596,3 +757,3 @@ return this.compose(ab); | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Getter.prototype.composeGetter = function (ab) { | ||
@@ -632,3 +793,3 @@ return this.compose(ab); | ||
this._tag = 'Fold'; | ||
this.getAll = foldMap(Monoid_1.getArrayMonoid())(function (a) { return [a]; }); | ||
this.getAll = foldMap(Array_1.getMonoid())(function (a) { return [a]; }); | ||
this.exist = foldMap(Monoid_1.monoidAny); | ||
@@ -643,3 +804,3 @@ this.all = foldMap(Monoid_1.monoidAll); | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Fold.prototype.composeFold = function (ab) { | ||
@@ -695,3 +856,3 @@ return this.compose(ab); | ||
}; | ||
/** @alias of `compose` */ | ||
/** Alias of `compose` */ | ||
Setter.prototype.composeSetter = function (ab) { | ||
@@ -723,3 +884,2 @@ return this.compose(ab); | ||
exports.Setter = Setter; | ||
// tslint:disable-next-line: deprecation | ||
function fromTraversable(T) { | ||
@@ -734,7 +894,6 @@ return function () { | ||
exports.fromTraversable = fromTraversable; | ||
// tslint:disable-next-line: deprecation | ||
function fromFoldable(F) { | ||
return function () { | ||
return new Fold(function (M) { | ||
var foldMapFM = Foldable_1.foldMap(F, M); | ||
var foldMapFM = F.foldMap(M); | ||
return function (f) { return function (s) { return foldMapFM(s, f); }; }; | ||
@@ -741,0 +900,0 @@ }); |
@@ -5,6 +5,16 @@ "use strict"; | ||
var Array_1 = require("fp-ts/lib/Array"); | ||
var Option_1 = require("fp-ts/lib/Option"); | ||
function indexArray() { | ||
// tslint:disable-next-line: deprecation | ||
return new index_1.Index(function (i) { return new index_1.Optional(function (s) { return Array_1.index(i, s); }, function (a) { return function (s) { return Array_1.updateAt(i, a, s).getOrElse(s); }; }); }); | ||
return new index_1.Index(function (i) { | ||
return new index_1.Optional(function (as) { return Array_1.lookup(i, as); }, function (a) { return function (as) { | ||
var oas = Array_1.updateAt(i, a)(as); | ||
if (Option_1.isNone(oas)) { | ||
return as; | ||
} | ||
else { | ||
return oas.value; | ||
} | ||
}; }); | ||
}); | ||
} | ||
exports.indexArray = indexArray; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var index_1 = require("../index"); | ||
var NonEmptyArray_1 = require("fp-ts/lib/NonEmptyArray"); | ||
var Array_1 = require("fp-ts/lib/Array"); | ||
var Option_1 = require("fp-ts/lib/Option"); | ||
function indexNonEmptyArray() { | ||
// tslint:disable-next-line: deprecation | ||
return new index_1.Index(function (i) { return new index_1.Optional(function (s) { return s.index(i); }, function (a) { return function (s) { return s.updateAt(i, a).getOrElse(s); }; }); }); | ||
return new index_1.Index(function (i) { | ||
return new index_1.Optional(function (s) { return Array_1.lookup(i, s); }, function (a) { return function (nea) { | ||
var onea = NonEmptyArray_1.updateAt(i, a)(nea); | ||
if (Option_1.isNone(onea)) { | ||
return nea; | ||
} | ||
else { | ||
return onea.value; | ||
} | ||
}; }); | ||
}); | ||
} | ||
exports.indexNonEmptyArray = indexNonEmptyArray; |
{ | ||
"name": "monocle-ts", | ||
"version": "1.7.2", | ||
"version": "2.0.0-rc.1", | ||
"description": "A porting of scala monocle library to TypeScript", | ||
@@ -39,3 +39,3 @@ "files": [ | ||
"peerDependencies": { | ||
"fp-ts": "^1.11.0" | ||
"fp-ts": "^2.0.0-rc.6" | ||
}, | ||
@@ -47,2 +47,3 @@ "devDependencies": { | ||
"dtslint": "github:gcanti/dtslint", | ||
"fp-ts": "^2.0.0-rc.6", | ||
"jest": "^24.3.0", | ||
@@ -56,4 +57,3 @@ "mocha": "^5.2.0", | ||
"tslint-config-standard": "^8.0.1", | ||
"typescript": "^3.3.1", | ||
"fp-ts": "^1.11.0" | ||
"typescript": "^3.5.1" | ||
}, | ||
@@ -60,0 +60,0 @@ "tags": [ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
121486
2985
32
1