-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
72 lines (66 loc) · 2.46 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
72
const traverse = require('traverse');
const _ = require('lodash');
module.exports = function transformJSONToTable(docs, options = {}) {
options.defaultVal = _.has(options, 'defaultVal') ? options.defaultVal : '';
if (_.isPlainObject(docs)) {
docs = _.flatten([docs]);
}
// Go through each object, find the deepest path
// Create an array of all of the possible paths
let allHeaders = _.keys(traverse(docs).reduce(
function (headers, value) {
const self = this;
if (this.notRoot && _.isArray(value)) {
if (options.includeCollectionLength) {
headers[`${_.tail(this.path).join('.')}.length`] = true;
}
if (options.excludeSubArrays) {
this.update(value, true);
}
if (options.listSubArrays && !_.isPlainObject(value[0])) {
headers[_.tail(this.path).join('.')] = true;
this.update(value, true)
}
}
if (options.stringifyObjects) {
if (_.size(this.path) === 2) {
headers[_.last(this.path)] = true;
}
return headers;
}
if (this.isLeaf) {
this.path = _.map(this.path, level => {
if (level.indexOf('.') > -1 && self.level > 2) { // If a leaf contains a dot in it, then surround the whole path with ticks
level = `\`${level}\``;
}
return level;
});
if (!(_.isPlainObject(value) && _.keys(value).length === 0)) { // Check against empty objects. Don't treat these paths as a valid header value.
headers[_.tail(this.path).join('.')] = true;
}
}
return headers;
}, {})
);
// Go through each object again, this time, attempt to grab the value
// At each possible path.
let tableData = [allHeaders];
tableData = tableData.concat(_.map(docs, doc => {
return _.map(allHeaders, header => {
if (options.stringifyObjects && doc[header]) {
return _.isObject(doc[header]) ? JSON.stringify(doc[header]) : doc[header];
}
if (options.checkKeyBeforePath && doc[header]) {
return doc[header];
}
if (header.indexOf('.`') > -1) { // One of those special cases where a path is nested, AND has a dot in the name.
const parts = header.split('.`');
const head = parts[0].replace(/\`/g, '');
const tail = parts[1].replace(/\`/g, '');
return _.get(doc, head, {})[tail];
}
return _.get(doc, header, options.defaultVal);
});
}));
return tableData;
};