-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (62 loc) · 1.65 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
/**
* Checks the given object (recursively if needed) and
* returns the object with its associated binary value for
* isEmpty evaluation
* @param obj
* @returns {*}
*/
function nestedEmptyCheck(obj) {
if(obj === "null") {
return true;
}
else if(typeof obj === "boolean"){
return false;
}
else if(typeof obj === "number"){
return false;
}
else if((typeof obj === "string") && obj.trim() != ''){
return false;
} else if((typeof obj === "string") && obj.trim() === ''){
return true;
}
else if(Array.isArray(obj)){
if(obj.length === 0) {
return true;
} else {
for(var i = 0; i < obj.length; i++) {
obj[i] = nestedEmptyCheck(obj[i]);
}
return obj;
}
}
else if(typeof obj === "object"){
var primaryKeys = Object.keys(obj);
if(primaryKeys && primaryKeys.length === 0) {
return true;
} else {
primaryKeys.forEach(key => {
var value = nestedEmptyCheck(obj[key]);
obj[key] = value;
});
return obj;
}
}
return obj;
}
/**
* before calling the nested validation method,
* makes a copy of the original object and calls with the copy of the object.
* @param object
* @returns {boolean}
*/
function isEmptyObj(object) {
if(object === undefined)
return true;
var objToSend = JSON.parse(JSON.stringify(object));
var result = nestedEmptyCheck(objToSend);
if(JSON.stringify(result).indexOf('false') > -1)
return false;
return true;
}
module.exports = isEmptyObj