monocle-ts
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -16,2 +16,7 @@ # Changelog | ||
# 1.3.0 | ||
- **New Feature** | ||
- add `Lens.{fromProp, fromPath, fromNullableProp}` and `Optional.fromNullableProp` curried overloadings (@gcanti) | ||
# 1.2.0 | ||
@@ -18,0 +23,0 @@ |
@@ -12,2 +12,1 @@ "use strict"; | ||
exports.atSet = atSet; | ||
//# sourceMappingURL=Set.js.map |
@@ -9,2 +9,1 @@ "use strict"; | ||
exports.atStrMap = atStrMap; | ||
//# sourceMappingURL=StrMap.js.map |
@@ -53,20 +53,22 @@ import { HKT, URIS, URIS2, URIS3, Type, Type2, Type3 } from 'fp-ts/lib/HKT'; | ||
} | ||
export declare function lensFromPath<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], K5 extends keyof T[K1][K2][K3][K4], K6 extends keyof T[K1][K2][K3][K4][K5], K7 extends keyof T[K1][K2][K3][K4][K5][K6], K8 extends keyof T[K1][K2][K3][K4][K5][K6][K7], K9 extends keyof T[K1][K2][K3][K4][K5][K6][K7][K8], K10 extends keyof T[K1][K2][K3][K4][K5][K6][K7][K8][K9]>(path: [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10]): Lens<T, T[K1][K2][K3][K4][K5][K6][K7][K8][K9][K10]>; | ||
export declare function lensFromPath<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], K5 extends keyof T[K1][K2][K3][K4], K6 extends keyof T[K1][K2][K3][K4][K5], K7 extends keyof T[K1][K2][K3][K4][K5][K6], K8 extends keyof T[K1][K2][K3][K4][K5][K6][K7], K9 extends keyof T[K1][K2][K3][K4][K5][K6][K7][K8]>(path: [K1, K2, K3, K4, K5, K6, K7, K8, K9]): Lens<T, T[K1][K2][K3][K4][K5][K6][K7][K8][K9]>; | ||
export declare function lensFromPath<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], K5 extends keyof T[K1][K2][K3][K4], K6 extends keyof T[K1][K2][K3][K4][K5], K7 extends keyof T[K1][K2][K3][K4][K5][K6], K8 extends keyof T[K1][K2][K3][K4][K5][K6][K7]>(path: [K1, K2, K3, K4, K5, K6, K7, K8]): Lens<T, T[K1][K2][K3][K4][K5][K6][K7][K8]>; | ||
export declare function lensFromPath<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], K5 extends keyof T[K1][K2][K3][K4], K6 extends keyof T[K1][K2][K3][K4][K5], K7 extends keyof T[K1][K2][K3][K4][K5][K6]>(path: [K1, K2, K3, K4, K5, K6, K7]): Lens<T, T[K1][K2][K3][K4][K5][K6][K7]>; | ||
export declare function lensFromPath<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], K5 extends keyof T[K1][K2][K3][K4], K6 extends keyof T[K1][K2][K3][K4][K5]>(path: [K1, K2, K3, K4, K5, K6]): Lens<T, T[K1][K2][K3][K4][K5][K6]>; | ||
export declare function lensFromPath<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], K5 extends keyof T[K1][K2][K3][K4]>(path: [K1, K2, K3, K4, K5]): Lens<T, T[K1][K2][K3][K4][K5]>; | ||
export declare function lensFromPath<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3]>(path: [K1, K2, K3, K4]): Lens<T, T[K1][K2][K3][K4]>; | ||
export declare function lensFromPath<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(path: [K1, K2, K3]): Lens<T, T[K1][K2][K3]>; | ||
export declare function lensFromPath<T, K1 extends keyof T, K2 extends keyof T[K1]>(path: [K1, K2]): Lens<T, T[K1][K2]>; | ||
export declare function lensFromPath<T, K1 extends keyof T>(path: [K1]): Lens<T, T[K1]>; | ||
export interface LensFromPath<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]>; | ||
<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]>; | ||
<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]>; | ||
<K1 extends keyof S, K2 extends keyof S[K1]>(path: [K1, K2]): Lens<S, S[K1][K2]>; | ||
<K1 extends keyof S>(path: [K1]): Lens<S, S[K1]>; | ||
} | ||
export declare class Lens<S, A> { | ||
readonly get: (s: S) => A; | ||
readonly set: (a: A) => (s: S) => S; | ||
static fromPath: typeof lensFromPath; | ||
readonly _tag: 'Lens'; | ||
constructor(get: (s: S) => A, set: (a: A) => (s: S) => S); | ||
/** generate a lens from a type and a prop */ | ||
static fromProp<T, P extends keyof T>(prop: P): Lens<T, T[P]>; | ||
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]>; | ||
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]>; | ||
/** generate a lens from a type and an array of props */ | ||
@@ -77,3 +79,4 @@ static fromProps<S>(): <P extends keyof S>(props: Array<P>) => Lens<S, { | ||
/** generate a lens from a type and a prop whose type is nullable */ | ||
static fromNullableProp<S, A extends S[K], K extends keyof S>(k: K, defaultValue: A): Lens<S, A>; | ||
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; | ||
@@ -148,3 +151,4 @@ /** view a Lens as a Optional */ | ||
constructor(getOption: (s: S) => Option<A>, set: (a: A) => (s: S) => S); | ||
static fromNullableProp: <S_1, A_1 extends S_1[K], K extends keyof S_1>(k: K) => Optional<S_1, A_1>; | ||
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]>>; | ||
modify(f: (a: A) => A): (s: S) => S; | ||
@@ -151,0 +155,0 @@ modifyOption(f: (a: A) => A): (s: S) => Option<S>; |
"use strict"; | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
@@ -19,4 +22,4 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
Laws: | ||
1. get . reverseGet = identity | ||
2. reversetGet . get = identity | ||
1. reverseGet(get(s)) = s | ||
2. get(reversetGet(a)) = a | ||
*/ | ||
@@ -116,8 +119,19 @@ var Iso = /** @class */ (function () { | ||
} | ||
exports.lensFromPath = lensFromPath; | ||
function lensFromProp(prop) { | ||
return new Lens(function (s) { return s[prop]; }, function (a) { return function (s) { | ||
var _a; | ||
return Object.assign({}, s, (_a = {}, _a[prop] = a, _a)); | ||
}; }); | ||
} | ||
function lensFromNullableProp(k, defaultValue) { | ||
return new Lens(function (s) { return Option_1.fromNullable(s[k]).getOrElse(defaultValue); }, function (a) { return function (s) { | ||
var _a; | ||
return (__assign({}, s, (_a = {}, _a[k] = a, _a))); | ||
}; }); | ||
} | ||
/* | ||
Laws: | ||
1. get(set(a, s)) = a | ||
2. set(get(s), s) = s | ||
3. set(a, set(a, s)) = set(a, s) | ||
1. get(set(a)(s)) = a | ||
2. set(get(s))(s) = s | ||
3. set(a)(set(a)(s)) = set(a)(s) | ||
*/ | ||
@@ -130,9 +144,8 @@ var Lens = /** @class */ (function () { | ||
} | ||
/** generate a lens from a type and a prop */ | ||
Lens.fromProp = function (prop) { | ||
return new Lens(function (s) { return s[prop]; }, function (a) { return function (s) { | ||
var _a; | ||
return Object.assign({}, s, (_a = {}, _a[prop] = a, _a)); | ||
}; }); | ||
Lens.fromPath = function () { | ||
return arguments.length === 0 ? lensFromPath : lensFromPath(arguments[0]); | ||
}; | ||
Lens.fromProp = function () { | ||
return arguments.length === 0 ? lensFromProp : lensFromProp(arguments[0]); | ||
}; | ||
/** generate a lens from a type and an array of props */ | ||
@@ -152,8 +165,6 @@ Lens.fromProps = function () { | ||
}; | ||
/** generate a lens from a type and a prop whose type is nullable */ | ||
Lens.fromNullableProp = function (k, defaultValue) { | ||
return new Lens(function (s) { return Option_1.fromNullable(s[k]).getOrElse(defaultValue); }, function (a) { return function (s) { | ||
var _a; | ||
return (__assign({}, s, (_a = {}, _a[k] = a, _a))); | ||
}; }); | ||
Lens.fromNullableProp = function () { | ||
return arguments.length === 0 | ||
? lensFromNullableProp | ||
: lensFromNullableProp(arguments[0], arguments[1]); | ||
}; | ||
@@ -224,3 +235,2 @@ Lens.prototype.modify = function (f) { | ||
}; | ||
Lens.fromPath = lensFromPath; | ||
return Lens; | ||
@@ -231,3 +241,3 @@ }()); | ||
Laws: | ||
1. getOption(s).fold(() => s, reverseGet) = s | ||
1. getOption(s).fold(s, reverseGet) = s | ||
2. getOption(reverseGet(a)) = Some(a) | ||
@@ -249,3 +259,3 @@ */ | ||
var _this = this; | ||
return function (s) { return _this.modifyOption(f)(s).fold(s, function_1.identity); }; | ||
return function (s) { return _this.modifyOption(f)(s).getOrElse(s); }; | ||
}; | ||
@@ -319,2 +329,8 @@ Prism.prototype.modifyOption = function (f) { | ||
var somePrism = new Prism(function (s) { return s; }, function (a) { return Option_1.some(a); }); | ||
function optionalFromNullableProp(k) { | ||
return new Optional(function (s) { return Option_1.fromNullable(s[k]); }, function (a) { return function (s) { | ||
var _a; | ||
return (__assign({}, s, (_a = {}, _a[k] = a, _a))); | ||
}; }); | ||
} | ||
/* | ||
@@ -332,5 +348,8 @@ Laws: | ||
} | ||
Optional.fromNullableProp = function () { | ||
return arguments.length === 0 ? optionalFromNullableProp : optionalFromNullableProp(arguments[0]); | ||
}; | ||
Optional.prototype.modify = function (f) { | ||
var _this = this; | ||
return function (s) { return _this.modifyOption(f)(s).fold(s, function_1.identity); }; | ||
return function (s) { return _this.modifyOption(f)(s).getOrElse(s); }; | ||
}; | ||
@@ -391,8 +410,2 @@ Optional.prototype.modifyOption = function (f) { | ||
}; | ||
Optional.fromNullableProp = function (k) { | ||
return new Optional(function (s) { return Option_1.fromNullable(s[k]); }, function (a) { return function (s) { | ||
var _a; | ||
return (__assign({}, s, (_a = {}, _a[k] = a, _a))); | ||
}; }); | ||
}; | ||
return Optional; | ||
@@ -625,5 +638,6 @@ }()); | ||
return function () { | ||
return new Traversal(function (F) { return function (f) { return function (s) { | ||
return T.traverse(F)(s, f); | ||
}; }; }); | ||
return new Traversal(function (F) { | ||
var traverseF = T.traverse(F); | ||
return function (f) { return function (s) { return traverseF(s, f); }; }; | ||
}); | ||
}; | ||
@@ -633,5 +647,9 @@ } | ||
function fromFoldable(F) { | ||
return function () { return new Fold(function (M) { return function (f) { return function (s) { return Foldable_1.foldMap(F, M)(s, f); }; }; }); }; | ||
return function () { | ||
return new Fold(function (M) { | ||
var foldMapFM = Foldable_1.foldMap(F, M); | ||
return function (f) { return function (s) { return foldMapFM(s, f); }; }; | ||
}); | ||
}; | ||
} | ||
exports.fromFoldable = fromFoldable; | ||
//# sourceMappingURL=index.js.map |
@@ -9,2 +9,1 @@ "use strict"; | ||
exports.indexArray = indexArray; | ||
//# sourceMappingURL=Array.js.map |
@@ -9,2 +9,1 @@ "use strict"; | ||
exports.indexStrMap = indexStrMap; | ||
//# sourceMappingURL=StrMap.js.map |
{ | ||
"name": "monocle-ts", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "A porting of scala monocle library to TypeScript", | ||
@@ -18,2 +18,3 @@ "files": [ | ||
"build": "npm run clean && tsc", | ||
"prepublish": "npm run build", | ||
"docs-fix-prettier": "prettier --no-semi --single-quote --print-width 120 --parser markdown --write \"README.md\"", | ||
@@ -37,12 +38,12 @@ "docs": "doctoc README.md && npm run docs-fix-prettier", | ||
"devDependencies": { | ||
"@types/jest": "^22.2.2", | ||
"@types/jest": "22.2.2", | ||
"@types/node": "7.0.4", | ||
"doctoc": "1.3.0", | ||
"dtslint": "^0.3.0", | ||
"jest": "^22.4.3", | ||
"jest": "^23.5.0", | ||
"prettier": "^1.13.5", | ||
"ts-jest": "^22.4.2", | ||
"ts-jest": "22.4.2", | ||
"tslint": "5.9.1", | ||
"tslint-config-standard": "7.0.0", | ||
"typescript": "^2.9.1" | ||
"typescript": "^3.1.2" | ||
}, | ||
@@ -49,0 +50,0 @@ "tags": [ |
@@ -0,1 +1,5 @@ | ||
[![build status](https://img.shields.io/travis/gcanti/monocle-ts/master.svg?style=flat-square)](https://travis-ci.org/gcanti/monocle-ts) | ||
[![dependency status](https://img.shields.io/david/gcanti/monocle-ts.svg?style=flat-square)](https://david-dm.org/gcanti/monocle-ts) | ||
![npm downloads](https://img.shields.io/npm/dm/monocle-ts.svg) | ||
# Motivation | ||
@@ -69,6 +73,6 @@ | ||
const company = Lens.fromProp<Employee, 'company'>('company') | ||
const address = Lens.fromProp<Company, 'address'>('address') | ||
const street = Lens.fromProp<Address, 'street'>('street') | ||
const name = Lens.fromProp<Street, 'name'>('name') | ||
const company = Lens.fromProp<Employee>()('company') | ||
const address = Lens.fromProp<Company>()('address') | ||
const street = Lens.fromProp<Address>()('street') | ||
const name = Lens.fromProp<Street>()('name') | ||
@@ -118,2 +122,8 @@ company | ||
# TypeScript compatibility | ||
The stable version is tested against TypeScript 3.1.2, but should run with TypeScript 2.3.0+ too | ||
# API | ||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
@@ -457,4 +467,5 @@ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
```ts | ||
// other 9 overloadings | ||
<T, K1 extends keyof T>(path: [K1]): Lens<T, T[K1]> | ||
<S>(): <K1 extends keyof S>(path: [K1]) => Lens<S, S[K1]> // other 4 overloadings | ||
// or (deprecated) | ||
<S, K1 extends keyof S>(path: [K1]): Lens<S, S[K1]> // other 4 overloadings | ||
``` | ||
@@ -473,3 +484,3 @@ | ||
const city = Lens.fromPath<Person, 'address', 'city'>(['address', 'city']) | ||
const city = Lens.fromPath<Person>()(['address', 'city']) | ||
@@ -485,3 +496,5 @@ const person: Person = { name: 'Giulio', age: 43, address: { city: 'Milan' } } | ||
```ts | ||
<T, P extends keyof T>(prop: P): Lens<T, T[P]> | ||
<S>(): <P extends keyof S>(prop: P) => Lens<S, S[P]> | ||
// or (deprecated) | ||
<S, P extends keyof T>(prop: P): Lens<S, S[P]> | ||
``` | ||
@@ -499,3 +512,5 @@ | ||
const age = Lens.fromProp<Person, 'age'>('age') | ||
const age = Lens.fromProp<Person>()('age') | ||
// or (deprecated) | ||
// const age = Lens.fromProp<Person, 'age'>('age') | ||
@@ -536,2 +551,4 @@ const person: Person = { name: 'Giulio', age: 43 } | ||
```ts | ||
<S>(): <A extends S[K], K extends keyof S>(k: K, defaultValue: A) => Lens<S, A> | ||
// or (deprecated) | ||
<S, A extends S[K], K extends keyof S>(k: K, defaultValue: A): Lens<S, A> | ||
@@ -554,4 +571,4 @@ ``` | ||
const inner = Lens.fromNullableProp<Outer, Inner, 'inner'>('inner', { value: 0, foo: 'foo' }) | ||
const value = Lens.fromProp<Inner, 'value'>('value') | ||
const inner = Lens.fromNullableProp<Outer>()('inner', { value: 0, foo: 'foo' }) | ||
const value = Lens.fromProp<Inner>()('value') | ||
const lens = inner.compose(value) | ||
@@ -826,2 +843,4 @@ | ||
```ts | ||
<S>() <K extends keyof S>(k: K): Optional<S, NonNullable<S[K]>> | ||
// or (deprecated) | ||
<S, A extends S[K], K extends keyof S>(k: K): Optional<S, A> | ||
@@ -846,6 +865,6 @@ ``` | ||
const info = Optional.fromNullableProp<Response, Info, 'info'>('info') | ||
const employment = Optional.fromNullableProp<Info, Employment, 'employment'>('employment') | ||
const phone = Optional.fromNullableProp<Employment, Phone, 'phone'>('phone') | ||
const number = Lens.fromProp<Phone, 'number'>('number') | ||
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 | ||
@@ -985,2 +1004,4 @@ .compose(employment) | ||
an optic that focuses on multiple elements in a data structure. See [fromTraversable](#fromTraversable) | ||
## Methods | ||
@@ -994,2 +1015,4 @@ | ||
modify each element focused by a traversal using the passed function | ||
### set | ||
@@ -1001,2 +1024,4 @@ | ||
set the value of each element focused by the traversal | ||
### asFold | ||
@@ -1024,3 +1049,3 @@ | ||
compose a Traversal with a Traversal | ||
compose a Traversal with another Traversal | ||
@@ -1349,4 +1374,4 @@ ### composeFold | ||
const tweetsLens = Lens.fromProp<Tweets, 'tweets'>('tweets') | ||
const tweetTextLens = Lens.fromProp<Tweet, 'text'>('text') | ||
const tweetsLens = Lens.fromProp<Tweets>()('tweets') | ||
const tweetTextLens = Lens.fromProp<Tweet>()('text') | ||
const tweetTraversal = fromTraversable(array)<Tweet>() | ||
@@ -1353,0 +1378,0 @@ const composedTraversal = tweetsLens.composeTraversal(tweetTraversal).composeLens(tweetTextLens) |
996
1451
71276
14