-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
114 lines (105 loc) · 3.43 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* jslint node: true */
'use strict';
function isDefined(x) {
return x !== undefined && x !== null;
}
function tablify(options) {
options = options || {};
var tableData = options.data || [];
var header = options.header;
var border = isDefined(options.border) ? options.border : 1;
var cellspacing = isDefined(options.cellspacing) ? options.cellspacing : 0;
var cellpadding = isDefined(options.cellpadding) ? options.cellpadding : 0;
var tableId = options.table_id || 'tablify';
var tableClass = options.table_class || 'tablify';
var header_mapping = options.header_mapping || {};
var pretty = options.pretty;
if (pretty === undefined) {
pretty = true;
}
var isSingleRow = false;
if (!Array.isArray(tableData)) {
isSingleRow = true;
tableData = [tableData];
}
// If header exists in options use that else create it.
if (!Array.isArray(header)) {
var headerObj = {};
tableData.forEach(function (json) {
var keys = Object.keys(json);
keys.forEach(function (key) {
headerObj[key] = true;
});
});
header = Object.keys(headerObj);
}
if (isSingleRow && tableData.length === 1) {
// Don't create row if value is not defined for the header (key for objects)
header = header.filter(function (h) {
return tableData[0][h];
});
}
// Generate table
var htmlTable = '';
var cellArray = [];
var cellRow = [];
cellArray.push(cellRow);
header.forEach(function (key) {
cellRow.push('<th>' + (header_mapping[key] || key) + '</th>');
});
tableData.forEach(function (json) {
cellRow = [];
cellArray.push(cellRow);
header.forEach(function (key) {
var value = json[key];
if (value === undefined) {
value = '';
} else if (typeof value !== 'string') {
value = JSON.stringify(value);
}
cellRow.push('<td>' + value + '</td>');
});
});
var i, j;
if (isSingleRow && cellArray.length) {
// Transpose the array to show object as key-value pair instead of table
cellArray = cellArray[0].map(function(col, i) {
return cellArray.map(function(row) {
return row[i];
})
});
}
var newLine = '';
var indent = '';
if (pretty) {
newLine = '\n';
indent = ' ';
}
if (options.css) {
htmlTable += `<style>${newLine}${indent}${options.css}${newLine}</style>${newLine}`
}
if (tableData.length) {
htmlTable += '<table id="' + tableId + '" class="' + tableClass + '" border="' + border + '" cellspacing="' + cellspacing + '" cellpadding="' + cellpadding + '">';
for (i = 0; i < cellArray.length; i++) {
htmlTable += newLine;
htmlTable += indent;
htmlTable += '<tr>';
for (j = 0; j < cellArray[i].length; j++) {
htmlTable += newLine;
htmlTable += indent;
htmlTable += indent;
htmlTable += cellArray[i][j];
}
htmlTable += newLine;
htmlTable += indent;
htmlTable += '</tr>';
}
htmlTable += newLine;
htmlTable += '</table>';
}
return htmlTable;
}
var html_tablify = {
tablify: tablify
};
module.exports = html_tablify;