-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsamples.ts
68 lines (60 loc) · 1.82 KB
/
samples.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { Entity } from "./lenses"
export const run = () => {
interface Person {
name:string,
surname:string,
age:number
}
const p1 = Entity<Person>({ name:"John", surname:"Doe", age:27 })
const q1 = p1.set("age", a => a+1).set("name", _ => "Jane").commit()
const q2 = p1.set("age", a => a+1).commit()
const q3 = p1.rename("age", "birthday", x => new Date("1-1-2001")).commit()
interface NestedState {
nesting1:{
nesting2:{
nesting3:{
nesting4:{
nesting5:{
obscenelyNestedValueWeNeedToUpdate:number
},
slightlyLessObscenelyNestedValueWeNeedToUpdate:number
}
}
}
}
}
const p2 = Entity<NestedState>({ nesting1:{ nesting2:{ nesting3:{ nesting4:{ slightlyLessObscenelyNestedValueWeNeedToUpdate:0, nesting5:{ obscenelyNestedValueWeNeedToUpdate:0 } } } } } })
const q21 = p2.setIn("nesting1", e =>
e.setIn("nesting2", e =>
e.setIn("nesting3", e =>
e.setIn("nesting4", e =>
e.set("slightlyLessObscenelyNestedValueWeNeedToUpdate", v => v + 2)
.rename("slightlyLessObscenelyNestedValueWeNeedToUpdate", "counter", x => x)
.setIn("nesting5", e =>
e.set("obscenelyNestedValueWeNeedToUpdate", v => v+1)
.rename("obscenelyNestedValueWeNeedToUpdate", "counter", x => x)
)
)
)
)
).commit()
interface AppState {
loginForm:{
firstPage:{
userName:string
password:string
},
secondPage:{
email:string
accountType:number
}
}
}
const setUserName = (newUserName:string) => (s0:AppState) : AppState =>
Entity(s0)
.setIn("loginForm", e => e
.setIn("firstPage", e => e
.set("userName", _ => newUserName)))
.commit()
console.log("Done")
}