json-table-converter
Version:
convert any json to table
150 lines (130 loc) • 4.11 kB
JavaScript
function isObject (obj) {
return Object.prototype.toString.call(obj) === '[object Object]'
}
var isObject_1 = isObject;
function objectToArray (json) {
if (isObject_1(json)) {
let arr = [];
for (let key in json) {
arr.push({ $_key: key, $_value: objectToArray(json[key]) });
}
return arr
} else if (Array.isArray(json)) {
let arr = [];
for (let item of json) {
if (isObject_1(json)) {
arr.push(objectToArray(json));
} else {
arr.push(item);
}
}
return arr
}
return json
}
var objectToArray_1 = objectToArray;
const defaultTableStyle = 'border-spacing: 0 0; border-color: #808080; border-collapse: collapse;';
const defaultTdStyle = 'border: 1px solid #2d2d2d; padding: 3px;';
const defaultTdKeyStyle = 'background: #F6F4F0; ' + defaultTdStyle;
const defaultThStyle = 'background: #F6F4F0;' + defaultTdStyle;
const defaultTrStyle = '';
function getTableStyle(tableStyle, tableDepth) {
return `${tableDepth > 1 ? 'width: 100%;' : ''}${tableStyle}`.replace(/\n\s*/g, '')
}
function getTdStyle (tdStyle, tdKeyStyle, isKey) {
return `${isKey ? tdKeyStyle : ''}${tdStyle}`.replace(/\n\s*/g, '')
}
function getThStyle (thStyle) {
return `${thStyle}`.replace(/\n\s*/g, '')
}
function tableToHtml (fields, rows, tableDepth, {
tableStyle = defaultTableStyle,
trStyle = defaultTrStyle,
thStyle = defaultThStyle,
tdKeyStyle = defaultTdKeyStyle,
tdStyle = defaultTdStyle
} = {}) {
return `<table cellspacing="0" style="${getTableStyle(tableStyle, tableDepth)}">
<thead>${fields
.filter(f => !f.startsWith('$_'))
.map(f => `<th style="${getThStyle(thStyle)}">${f}</th>`)
.join('')}</thead>
<tbody>${rows
.map(row => {
let tds = '';
for (let i = 0; i < row.length; i++) {
const v = row[i] || '';
if (isObject_1(v)) {
tds += `<td style="${getTdStyle(tdStyle, tdKeyStyle, v.isKey)}">${
v.value
}</td>`;
} else {
tds += `<td style="${getTdStyle(tdStyle)}">${v}</td>`;
}
}
return `<tr style="${trStyle}">${tds}</tr>`
})
.join('')}</tbody>
</table>`
}
var tableToHtml_1 = tableToHtml;
function arrayToTable (array, options) {
function _recur(arr, tableDepth = 1) {
let fieldSet = new Set();
for (let i = 0; i < arr.length; i++) {
const item = arr[i];
if (isObject_1(item)) {
Object.keys(item).forEach(i => fieldSet.add(i));
} else if (Array.isArray(item)) {
return _recur(item, tableDepth + 1)
}
}
let fields = Array.from(fieldSet);
let rows = arr.map(() => []);
for (let i = 0; i < arr.length; i++) {
const item = arr[i];
if (isObject_1(item)) {
for (let j = 0; j < fields.length; j++) {
let value = item[fields[j]];
if (Array.isArray(value)) {
rows[i][j] = _recur(value, tableDepth + 1);
} else if (isObject_1(value)) {
rows[i][j] = _recur(objectToArray_1(value), tableDepth + 1);
} else {
rows[i][j] = { isKey: fields[j] === '$_key', value };
}
}
} else {
rows[i][fields.length] = { isKey: false, value: item };
}
}
return tableToHtml_1(fields, rows, tableDepth, options)
}
return _recur(array)
}
var arrayToTable_1 = arrayToTable;
/**
* Convert Json to <table />
*
* @param {Object|Array} json
* @param {Object} options
* @param {String} [options.tableStyle] <table/> Style
* @param {String} [options.trStyle] <tr/> Style
* @param {String} [options.thStyle] <th/> Style
* @param {String} [options.tdStyle] <td/> Style
* @param {String} [options.tdKeyStyle] <td/> Key Style
*/
function jsonToTableHtmlString (json, options) {
let arr = isObject_1(json) ? objectToArray_1(json) : json;
if (!Array.isArray(arr)) {
arr = [];
}
return arrayToTable_1(arr, options)
}
var src = {
jsonToTableHtmlString,
};
var src_1 = src.jsonToTableHtmlString;
export default src;
export { src_1 as jsonToTableHtmlString };
//# sourceMappingURL=index.esm.js.map