-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.js
executable file
·71 lines (58 loc) · 1.52 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
/**
* Module dependencies.
*/
const isEmpty = require('lodash.isempty');
const isPlainObject = require('lodash.isplainobject');
const transform = require('lodash.transform');
/**
* Export `cleanDeep` function.
*/
module.exports = function cleanDeep(object, {
cleanKeys = [],
cleanValues = [],
emptyArrays = true,
emptyObjects = true,
emptyStrings = true,
nullValues = true,
undefinedValues = true
} = {}) {
return transform(object, (result, value, key) => {
// Exclude specific keys.
if (cleanKeys.includes(key)) {
return;
}
// Recurse into arrays and objects.
if (Array.isArray(value) || isPlainObject(value)) {
value = cleanDeep(value, { cleanKeys, cleanValues, emptyArrays, emptyObjects, emptyStrings, nullValues, undefinedValues });
}
// Exclude specific values.
if (cleanValues.includes(value)) {
return;
}
// Exclude empty objects.
if (emptyObjects && isPlainObject(value) && isEmpty(value)) {
return;
}
// Exclude empty arrays.
if (emptyArrays && Array.isArray(value) && !value.length) {
return;
}
// Exclude empty strings.
if (emptyStrings && value === '') {
return;
}
// Exclude null values.
if (nullValues && value === null) {
return;
}
// Exclude undefined values.
if (undefinedValues && value === undefined) {
return;
}
// Append when recursing arrays.
if (Array.isArray(result)) {
return result.push(value);
}
result[key] = value;
});
}