push
travis-ci
520 of 552 relevant lines covered (94.2%)
180.75 hits per line
1 |
"use strict";
|
|
2 |
|
|
3 |
var _ = require('underscore'); |
1✔ |
4 |
|
|
5 |
exports.deepDelete = function deepDelete(obj, prop) { |
1✔ |
6 |
if (obj && _.isObject(obj)) {
|
13✔ |
7 |
delete obj[prop];
|
7✔ |
8 |
Object.keys(obj).forEach(function(key) {
|
7✔ |
9 |
deepDelete(obj[key], prop); |
11✔ |
10 |
}); |
|
11 |
} |
|
12 |
}; |
|
13 |
|
|
14 |
exports.deepDeleteEmpty = function deepDeleteEmpty(obj) { |
1✔ |
15 |
if (_.isObject(obj)) {
|
7,518✔ |
16 |
Object.keys(obj).forEach(function(key) {
|
7,518✔ |
17 |
if (_.isObject(obj[key])) {
|
17,288✔ |
18 |
deepDeleteEmpty(obj[key]); |
7,302✔ |
19 |
if (_.isEmpty(obj[key])) {
|
7,302✔ |
20 |
if (! (obj[key] instanceof Date)) { |
1,587✔ |
21 |
delete obj[key];
|
1,234✔ |
22 |
} |
|
23 |
} else if (_.isArray(obj[key])) { |
5,715✔ |
24 |
var reduced = obj[key].reduce(function(r, v) { |
1,219✔ |
25 |
if (v !== undefined) { |
2,308✔ |
26 |
r.push(v); |
2,308✔ |
27 |
} |
|
28 |
return r;
|
2,308✔ |
29 |
}, []); |
|
30 |
if (reduced.length !== obj[key].length) {
|
1,219✔ |
31 |
if (reduced.length > 0) { |
4✔ |
32 |
obj[key] = reduced; |
1✔ |
33 |
} else {
|
|
34 |
delete obj[key];
|
3✔ |
35 |
} |
|
36 |
} |
|
37 |
} |
|
38 |
} |
|
39 |
}); |
|
40 |
} |
|
41 |
}; |