-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
286 lines (236 loc) · 6.19 KB
/
index.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"use strict";
const EXCEPTION_MSGS = {
array: `Doesn't support Array in template yet. The meaning might differ in different context. Please use custom function instead.\nTreating as ${true}`,
mergeTypeChecking: "Using Type Checking in template but object target object doesn't match.\nReturning template Type as result.",
existTypeChecking: "Using Type Checking in template but object target object doesn't match.\nReturning undefined.",
arrayIterFirstArg: "First argument of ArrayIter must be a function of obj-filter",
};
const getExceptionMsg = (type, second = false) => `obj-filter: ${second ? second + ": " : ""}${EXCEPTION_MSGS[type]}`;
function _isType (template) {
if (
(template === String)
|| (template === Number)
|| (template === Boolean)
|| (template === Array)
|| (template === Symbol)
|| (template === Map)
|| (template === Set)
|| (template === WeakMap)
|| (template === WeakSet)
|| (template === Object)
|| (template === Function)
) {
return true;
}
return false;
}
function _sameType (template, obj) {
if (
(template === String && typeof obj === "string")
|| (template === Number && typeof obj === "number")
|| (template === Boolean && typeof obj === "boolean")
|| (template === Array && Array.isArray(obj))
|| (template === Symbol && obj instanceof Symbol)
|| (template === Map && obj instanceof Map)
|| (template === Set && obj instanceof Set)
|| (template === WeakMap && obj instanceof WeakMap)
|| (template === WeakSet && obj instanceof WeakSet)
|| (template === Function && typeof obj === "function")
|| (template === Object && typeof obj === "object")
) {
return true;
}
return false;
}
function filter (template, obj, onException) {
// exclude what's undefined in template
if (typeof template === "undefined") {
return undefined;
}
// only check Type
if ( _isType(template) ) {
if ( _sameType(template, obj) ) {
return obj;
}
// type mismatch
if (onException) {
return onException(template, obj);
}
return undefined;
}
// handle Array as True
if (template instanceof Array) {
if (onException) {
return onException(
template,
obj,
getExceptionMsg("array")
);
}
return obj;
}
// filtering
if ( typeof template === "object" ){
if (typeof obj === "object") {
return Object.keys(template).reduce((result, key) => {
const tmp = filter(template[key], obj[key], onException);
if (typeof tmp !== "undefined") {
result[key] = tmp;
}
return result;
}, {});
}
// type mismatch
if (onException) {
return onException(template, obj);
}
return undefined;
}
if ( typeof template === "function" ) {
return template(obj);
}
return obj;
}
function merge (template, obj, onException) {
// exclude what's undefined in template
if (typeof template === "undefined") {
return undefined;
}
// only check Type
if ( _isType(template) ) {
if ( _sameType(template, obj) ) {
return obj;
}
// type mismatch
if (onException) {
return onException(
template,
obj,
getExceptionMsg("mergeTypeChecking", "merge")
);
}
return template;
}
// handle Array as True
if (template instanceof Array) {
if (onException) {
return onException(
template,
obj,
getExceptionMsg("array", "merge")
);
}
return obj;
}
// obj ? obj : template ;
if ( typeof template === "object" ){
if (typeof obj === "object") {
return Object.keys(template).reduce((result, key) => {
const ret = merge(template[key], obj[key], onException);
if (typeof ret !== "undefined") {
result[key] = ret;
} else if (typeof template[key] !== "undefined") {
result[key] = template[key];
}
return result;
}, {});
} else {
// type mismatch
if (onException) {
return onException(
template,
obj,
getExceptionMsg("mergeTypeChecking", "merge")
);
}
return template;
}
}
// must before "undefined" handling, so user can handle undefined if they wanted
if ( typeof template === "function" ) {
return template(obj);
}
// must after typeof template === "function", so user can handle it if they wanted
if (typeof obj === "undefined") {
return template;
}
return obj;
}
function exist (template, obj, onException) {
// exclude what's undefined in template
if (typeof template === "undefined") {
return undefined;
}
// only check Type
if ( _isType(template) ) {
if ( _sameType(template, obj) ) {
return obj;
}
// type mismatch
if (onException) {
return onException(
template,
obj,
getExceptionMsg("existTypeChecking")
);
}
return undefined;
}
// handle Array as True
if (template instanceof Array) {
if (onException) {
return onException(
template,
obj,
getExceptionMsg("array", "exist")
);
}
return obj;
}
// must before "undefined" handling, so user can handle undefined if they wanted
if (typeof template === "function") {
return template(obj);
}
// must after typeof template === "function", so user can handle it if they wanted
if (typeof obj === "undefined") {
return undefined;
}
// check if all keys exists recursively
if (typeof template === "object") {
return Object.keys(template).reduce((result, key) => {
if (template[key] === undefined) {
// value "undefined" means skip
return result;
}
const tmp = exist(template[key], obj[key], onException);
if (typeof tmp === "undefined") {
return undefined;
}
result[key] = tmp;
return result;
}, {});
}
// return whatever obj has
return obj;
}
function ArrayIter (checker, template, {min = 0, onException} = {}) {
if (typeof(checker) !== "function") {
if (onException) {
return onException(undefined, undefined, getExceptionMsg("arrayIterFirstArg"));
}
throw new Error(getExceptionMsg("arrayIterFirstArg"));
}
return function (array) {
if (!(array instanceof Array)) {
return undefined;
}
const result = array.map((value) => checker(template, value, onException))
.filter((x) => x !== undefined);
return result.length >= min ? result : undefined;
};
}
module.exports = filter;
module.exports.filter = filter;
module.exports.merge = merge;
module.exports.exist = exist;
module.exports.ArrayIter = ArrayIter;