-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtableGenerator.js
More file actions
197 lines (169 loc) · 5.86 KB
/
tableGenerator.js
File metadata and controls
197 lines (169 loc) · 5.86 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Table Generator Module
* Handles conversion of JSON to HTML tables
*/
// Shared state for current JSON data
let currentJsonData = null;
/**
* Format primitive values with appropriate styling
* @param {any} value - The primitive value to format
* @returns {string} HTML string with formatted value
*/
function formatPrimitiveValue(value) {
if (value === null) {
return '<span class="primitive-value null-value">null</span>';
}
if (typeof value === 'string') {
return `<span class="primitive-value string-value">${value}</span>`;
}
if (typeof value === 'number') {
return `<span class="primitive-value number-value">${value}</span>`;
}
if (typeof value === 'boolean') {
return `<span class="primitive-value boolean-value">${value}</span>`;
}
return `<span class="primitive-value">${value}</span>`;
}
/**
* Create a nested table for object values
* @param {object} obj - The object to display as a table
* @returns {string} HTML string with nested table
*/
function createNestedTable(obj) {
let html = '<table class="nested-table">';
for (const [key, value] of Object.entries(obj)) {
html += '<tr>';
html += `<td class="nested-key">${key}</td>`;
html += '<td class="nested-value">';
if (value === null || typeof value !== 'object') {
html += formatPrimitiveValue(value);
} else if (Array.isArray(value)) {
html += createArrayDisplay(value);
} else {
html += createNestedTable(value);
}
html += '</td>';
html += '</tr>';
}
html += '</table>';
return html;
}
/**
* Create display for array values
* @param {array} arr - The array to display
* @returns {string} HTML string with array display
*/
function createArrayDisplay(arr) {
if (arr.length === 0) {
return '<span class="primitive-value null-value">empty array</span>';
}
const allPrimitives = arr.every(item =>
item === null || typeof item !== 'object'
);
if (allPrimitives) {
let html = '<div class="array-container">';
arr.forEach(item => {
html += `<div class="array-item">${formatPrimitiveValue(item)}</div>`;
});
html += '</div>';
return html;
}
let html = '<div class="array-container">';
arr.forEach((item) => {
html += '<div class="array-item">';
if (typeof item === 'object' && item !== null) {
if (Array.isArray(item)) {
html += createArrayDisplay(item);
} else {
html += createNestedTable(item);
}
} else {
html += formatPrimitiveValue(item);
}
html += '</div>';
});
html += '</div>';
return html;
}
/**
* Create the main table for the top level object
* @param {object} data - The JSON data object
* @returns {string} HTML string with main table
*/
function createMainTable(data) {
let html = '<table class="main-table">';
for (const [key, value] of Object.entries(data)) {
html += '<tr>';
html += `<td class="key-column">${key}</td>`;
html += '<td class="value-column">';
if (value === null || typeof value !== 'object') {
html += formatPrimitiveValue(value);
} else if (Array.isArray(value)) {
html += createArrayDisplay(value);
} else {
html += createNestedTable(value);
}
html += '</td>';
html += '</tr>';
}
html += '</table>';
return html;
}
/**
* Create simplified table for PDF export
* @param {any} data - The JSON data to convert to a table
* @returns {string} HTML string with simplified table for PDF export
*/
function createMinimalPDFTable(data) {
if (data === null || data === undefined) {
return '<div>No data</div>';
}
// For primitive values
if (typeof data !== 'object') {
return `<table style="width:100%; border-collapse:collapse; border:1px solid #000;">
<tr>
<td style="border:1px solid #000; padding:8px; width:30%;">Value</td>
<td style="border:1px solid #000; padding:8px;">${data}</td>
</tr>
</table>`;
}
// For arrays
if (Array.isArray(data)) {
let html = '<table style="width:100%; border-collapse:collapse; border:1px solid #000;">';
html += '<tr><th style="border:1px solid #000; padding:8px;">Index</th><th style="border:1px solid #000; padding:8px;">Value</th></tr>';
data.forEach((item, index) => {
html += '<tr>';
html += `<td style="border:1px solid #000; padding:8px;">${index}</td>`;
html += '<td style="border:1px solid #000; padding:8px;">';
if (item === null) {
html += 'null';
} else if (typeof item !== 'object') {
html += item;
} else {
html += createMinimalPDFTable(item);
}
html += '</td>';
html += '</tr>';
});
html += '</table>';
return html;
}
// For objects
let html = '<table style="width:100%; border-collapse:collapse; border:1px solid #000;">';
for (const [key, value] of Object.entries(data)) {
html += '<tr>';
html += `<td style="border:1px solid #000; padding:8px; width:30%; font-weight:bold;">${key}</td>`;
html += '<td style="border:1px solid #000; padding:8px;">';
if (value === null) {
html += 'null';
} else if (typeof value !== 'object') {
html += value;
} else {
html += createMinimalPDFTable(value);
}
html += '</td>';
html += '</tr>';
}
html += '</table>';
return html;
}