UNPKG

1.51 kBJavaScriptView Raw
1const _ = require('lodash');
2const MAX_SIZE = 10;
3const MAX_DEEP = 10;
4/**
5 * - if is array and is not from collection, reduce the collection to MAX_SIZE;
6 * - if is object and is not from collection, call recursively processtoLog fn;
7 * - if is array and is from collection, returns [...]
8 * - if is object and is from collection, returns {...}
9 * - else, returns the same value.
10 */
11const processToLog = (data, fromCollection = false, isCollection = false, deep = 0) => {
12 const isArray = _.isArray(data);
13 const isObject = _.isPlainObject(data);
14 deep++;
15
16 // protects from infinity recursion. This will interrupt if deep is greater than maxDeep
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
43const defaultResponseProcessor = data => processToLog(data);
44
45module.exports = defaultResponseProcessor;
\No newline at end of file