1 | const _ = require('lodash');
|
2 | const MAX_SIZE = 10;
|
3 | const MAX_DEEP = 10;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const processToLog = (data, fromCollection = false, isCollection = false, deep = 0) => {
|
12 | const isArray = _.isArray(data);
|
13 | const isObject = _.isPlainObject(data);
|
14 | deep++;
|
15 |
|
16 |
|
17 | if (deep > MAX_DEEP) {
|
18 | return '{...}'
|
19 | }
|
20 |
|
21 | if (isArray && !fromCollection) {
|
22 | const total = data.length;
|
23 | const result = _.chain(data).take(MAX_SIZE).map(i => processToLog(i, fromCollection, true, deep)).value();
|
24 | if (total > MAX_SIZE) {
|
25 | result.push(`[${total - MAX_SIZE} more results]`)
|
26 | }
|
27 |
|
28 | return result;
|
29 | } else if (isObject && !fromCollection) {
|
30 | return Object.keys(data).reduce((obj, key) => {
|
31 | obj[key] = processToLog(data[key], isCollection, isCollection, deep);
|
32 | return obj;
|
33 | }, {});
|
34 | } else if (isArray) {
|
35 | return '[...]';
|
36 | } else if (isObject) {
|
37 | return '{...}';
|
38 | } else {
|
39 | return data;
|
40 | }
|
41 | };
|
42 |
|
43 | const defaultResponseProcessor = data => processToLog(data);
|
44 |
|
45 | module.exports = defaultResponseProcessor; |
\ | No newline at end of file |