-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform-dictator.js
147 lines (147 loc) · 3.86 KB
/
form-dictator.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
;
class FormDictator {
constructor(data) {
this.data = data;
this._results = [];
}
;
pick(keys) {
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(newData);
}
;
clone(data = this.data) {
var fd = new FormDictator(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, changer) {
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) {
this._results.push({
key,
value: this.data[key],
checker: function require() { return false; },
result: key in this.data,
});
return this.clone();
}
check(key, checker) {
this._results.push({
key,
value: this.data[key],
checker,
result: checker(this.data[key]),
});
return this.clone();
}
;
checkIfExist(key, 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() {
return this._results.some(r => r.result === false);
}
;
witchFail() {
var target = this._results.find(r => r.result === false);
return target ? target.key : null;
}
static diff(origin, update) {
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;
}
}
exports.default = FormDictator;