-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.js
83 lines (67 loc) · 1.13 KB
/
source.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
export default state =>{
let pointer = state
let path = []
return {
prop( key ){
pointer = pointer[ key ]
path.push( key )
return this
},
path( list ){
list.map( key => this.prop(key) )
return this
},
end(){
pointer = state
path = []
return this
},
set( value ){
let where = path[ path.length-1 ]
let parent = path[ path.length-2 ]
if( !where ){
state = value
pointer = state
}else if(!parent) {
state[ where ] = value
pointer = value
}else{
path.reduce( (acc, key) =>{
if( parent in acc ){
acc[ parent ][ where ] = value
pointer = value
acc = acc[ key ]
}
return acc
}, state)
}
return this
},
then( fn ){
this.set( fn( pointer ) )
return this
},
propset( key, value ){
pointer[key] = value
return this
},
map( fn ){
this.set( pointer.map( fn ) )
return this
},
filter( fn ){
this.set( pointer.filter( fn ) )
return this
},
reduce( fn, option ){
this.set( pointer.reduce( fn, option ) )
return this
},
value(){
return state
},
data(){
return pointer
}
}
}