-
Notifications
You must be signed in to change notification settings - Fork 0
/
form-dictator.ts
129 lines (123 loc) · 3.75 KB
/
form-dictator.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
interface CheckRecord<T>{
key :keyof T;
value:any;
checker:(value:any)=>boolean|Promise<boolean>;
result :boolean|Promise<boolean>;
};
export default class FormDictator<T>{
public _results:CheckRecord<T>[] =[];
constructor(public data:T){};
pick<K extends keyof T>(keys:K[]){
var newData =Object.create(null);
for(let key of keys){
//@ts-ignore
if(!(key in this.data))continue;
//@ts-ignore
newData[key] =this.data[key];
}
return this.clone<Pick<T,K>>(newData);
};
clone<U=T>(data:any=this.data){
var fd =new FormDictator<U>(data!);
//@ts-ignore
fd._results =Array.from(this._results);
return fd;
};
noUndefined(){
var newData =Object.create(null);
for(let key in this.data){
if(this.data[key]===undefined)continue;
newData[key] =this.data[key];
}
return this.clone(newData);
};
noNull(){
var newData =Object.create(null);
for(let key in this.data){
if(this.data[key]===null)continue;
newData[key] =this.data[key];
}
return this.clone(newData);
};
noEmptyStr(){
var newData =Object.create(null);
for(let key in this.data){
//@ts-ignore
if(this.data[key]==='')continue;
newData[key] =this.data[key];
}
return this.clone(newData);
};
noEmpty(){
return this.noNull().noUndefined();
};
changeIfExist(beChangeKey:(keyof T),changer:(value:any)=>any){
if(!(beChangeKey in this.data)){
return this.clone();
};
var newData =Object.create(null);
for(let key in this.data){
newData[key] =this.data[key];
}
if(beChangeKey in this.data) newData[beChangeKey] =changer(newData[beChangeKey]);
return this.clone(newData);
}
require(key:(keyof T)){
this._results.push({
key,
value :this.data[key],
checker:function require(){return false},
result :key in this.data,
});
return this.clone();
}
check(key:(keyof T),checker:CheckRecord<T>['checker']){
this._results.push({
key,
value :this.data[key],
checker,
result :checker(this.data[key]),
});
return this.clone();
};
checkIfExist(key:(keyof T),checker:CheckRecord<T>['checker']){
if(!(key in this.data))this._results.push({
key,
value :undefined,
checker,
result :true,
});
// @ts-ignore
else this.check(...arguments);
return this.clone();
};
async waitResult(){
await Promise.all(this._results.map(r=>r.result));
for(let resultObj of this._results){
resultObj.result =await resultObj.result
};
return this.clone();
};
hasFail():boolean{
return this._results.some(r=>r.result===false)
};
witchFail():(keyof T)|null{
var target =this._results.find(r=>r.result===false);
return target?target.key:null;
}
static diff(origin:any ,update:any){
var diff =Object.create(null);
for(let key in update){
if(typeof update[key]==='object'){
diff[key] =FormDictator.diff(origin[key],update[key]);
if(Object.keys(diff[key]).length===0) delete diff[key];
else if(Array.isArray(update[key])){
diff[key].length =Object.keys(diff[key]).length;
}
}else if(origin[key]!==update[key]){
diff[key] =origin[key];
};
};
return diff;
}
}