-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (48 loc) · 1.18 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
function omitEmpty(o) {
let newObj;
if (Array.isArray(o)) {
if (o.length > 0) {
newObj = o.filter(v => v && v.length > 0)
if (newObj.length === 0) {
newObj = null;
}
}
} else if (typeof o === 'object') {
newObj = {};
Object.keys(o).forEach((key) => {
if (o[key]) {
let nw = null;
let isDate = false;
if (Array.isArray(o[key]) || typeof o[key] === 'object') {
const dt = new Date(o[key]);
if (dt != "Invalid Date") {
nw = o[key];
isDate = true;
} else {
nw = omitEmpty(o[key]);
}
} else if (o[key].length > 0) {
nw = omitEmpty(o[key]);
} else {
nw = o[key];
}
if (isDate) {
newObj[key] = nw;
} else if (nw) {
newObj[key] = omitEmpty(nw);
}
} else if (o[key] === false || o[key] === 0) {
newObj[key] = o[key];
}
});
if (Object.keys(newObj).length === 0) {
newObj = null;
}
} else if (o && (o.length > 0 || o > 0)) {
newObj = o;
} else if (typeof o === 'date') {
newObj = o;
}
return newObj;
};
module.exports = omitEmpty;