monocle-ts
Advanced tools
Comparing version 0.4.3 to 0.4.4
@@ -15,2 +15,7 @@ # Changelog | ||
# 0.4.4 | ||
- **New Feature** | ||
- add `Lens.fromNullableProp` (@gcanti) | ||
# 0.4.3 | ||
@@ -17,0 +22,0 @@ |
@@ -69,2 +69,4 @@ import { HKT, HKTS, HKTAs, HKT2S, HKT2As } from 'fp-ts/lib/HKT'; | ||
static fromProp<T, P extends keyof T>(prop: P): Lens<T, T[P]>; | ||
/** 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>; | ||
modify(f: (a: A) => A): (s: S) => S; | ||
@@ -71,0 +73,0 @@ /** view a Lens as a Optional */ |
@@ -132,2 +132,9 @@ "use strict"; | ||
}; | ||
/** 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]).getOrElseValue(defaultValue); }, function (a) { return function (s) { | ||
return (__assign({}, s, (_a = {}, _a[k] = a, _a))); | ||
var _a; | ||
}; }); | ||
}; | ||
Lens.prototype.modify = function (f) { | ||
@@ -134,0 +141,0 @@ var _this = this; |
{ | ||
"name": "monocle-ts", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"description": "A porting of scala monocle library to TypeScript", | ||
@@ -5,0 +5,0 @@ "files": ["lib"], |
@@ -129,2 +129,3 @@ # Motivation | ||
- [fromProp](#fromprop) | ||
- [fromNullableProp](#fromnullableprop) | ||
- [Methods](#methods-1) | ||
@@ -165,3 +166,3 @@ - [modify](#modify-1) | ||
- [Optional](#optional) | ||
- [fromNullableProp](#fromnullableprop) | ||
- [fromNullableProp](#fromnullableprop-1) | ||
- [Methods](#methods-3) | ||
@@ -441,2 +442,4 @@ - [modify](#modify-3) | ||
generate a lens from a type and a prop | ||
Example | ||
@@ -458,2 +461,32 @@ | ||
## fromNullableProp | ||
```ts | ||
<S, A extends S[K], K extends keyof S>(k: K, defaultValue: A): Lens<S, A> | ||
``` | ||
generate a lens from a type and a prop whose type is nullable | ||
Example | ||
```ts | ||
interface Outer { | ||
inner?: Inner | ||
} | ||
interface Inner { | ||
value: number | ||
foo: string | ||
} | ||
const inner = Lens.fromNullableProp<Outer, Inner, 'inner'>('inner', { value: 0, foo: 'foo' }) | ||
const value = Lens.fromProp<Inner, 'value'>('value') | ||
const lens = inner.compose(value) | ||
console.log(lens.set(1)({})) // { inner: { value: 1, foo: 'foo' } } | ||
console.log(lens.get({})) // 0 | ||
console.log(lens.set(1)({ inner: { value: 1, foo: 'bar' } })) // { inner: { value: 1, foo: 'bar' } } | ||
console.log(lens.get({ inner: { value: 1, foo: 'bar' } })) // 1 | ||
``` | ||
## Methods | ||
@@ -460,0 +493,0 @@ |
Sorry, the diff of this file is not supported yet
82635
862
1261