UNPKG

json-table-converter

Version:
202 lines (164 loc) 5.73 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function isObject(obj) { return Object.prototype.toString.call(obj) === '[object Object]'; } var isObject_1 = isObject; function objectToArray(json) { if (isObject_1(json)) { var arr = []; for (var key in json) { arr.push({ $_key: key, $_value: objectToArray(json[key]) }); } return arr; } else if (Array.isArray(json)) { var _arr = []; var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = json[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var item = _step.value; if (isObject_1(json)) { _arr.push(objectToArray(json)); } else { _arr.push(item); } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } return _arr; } return json; } var objectToArray_1 = objectToArray; var defaultTableStyle = 'border-spacing: 0 0; border-color: #808080; border-collapse: collapse;'; var defaultTdStyle = 'border: 1px solid #2d2d2d; padding: 3px;'; var defaultTdKeyStyle = 'background: #F6F4F0; ' + defaultTdStyle; var defaultThStyle = 'background: #F6F4F0;' + defaultTdStyle; var defaultTrStyle = ''; function getTableStyle(tableStyle, tableDepth) { return "".concat(tableDepth > 1 ? 'width: 100%;' : '').concat(tableStyle).replace(/\n\s*/g, ''); } function getTdStyle(tdStyle, tdKeyStyle, isKey) { return "".concat(isKey ? tdKeyStyle : '').concat(tdStyle).replace(/\n\s*/g, ''); } function getThStyle(thStyle) { return "".concat(thStyle).replace(/\n\s*/g, ''); } function tableToHtml(fields, rows, tableDepth) { var _ref = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}, _ref$tableStyle = _ref.tableStyle, tableStyle = _ref$tableStyle === void 0 ? defaultTableStyle : _ref$tableStyle, _ref$trStyle = _ref.trStyle, trStyle = _ref$trStyle === void 0 ? defaultTrStyle : _ref$trStyle, _ref$thStyle = _ref.thStyle, thStyle = _ref$thStyle === void 0 ? defaultThStyle : _ref$thStyle, _ref$tdKeyStyle = _ref.tdKeyStyle, tdKeyStyle = _ref$tdKeyStyle === void 0 ? defaultTdKeyStyle : _ref$tdKeyStyle, _ref$tdStyle = _ref.tdStyle, tdStyle = _ref$tdStyle === void 0 ? defaultTdStyle : _ref$tdStyle; return "<table cellspacing=\"0\" style=\"".concat(getTableStyle(tableStyle, tableDepth), "\">\n <thead>").concat(fields.filter(function (f) { return !f.startsWith('$_'); }).map(function (f) { return "<th style=\"".concat(getThStyle(thStyle), "\">").concat(f, "</th>"); }).join(''), "</thead>\n <tbody>").concat(rows.map(function (row) { var tds = ''; for (var i = 0; i < row.length; i++) { var v = row[i] || ''; if (isObject_1(v)) { tds += "<td style=\"".concat(getTdStyle(tdStyle, tdKeyStyle, v.isKey), "\">").concat(v.value, "</td>"); } else { tds += "<td style=\"".concat(getTdStyle(tdStyle), "\">").concat(v, "</td>"); } } return "<tr style=\"".concat(trStyle, "\">").concat(tds, "</tr>"); }).join(''), "</tbody>\n</table>"); } var tableToHtml_1 = tableToHtml; function arrayToTable(array, options) { function _recur(arr) { var tableDepth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1; var fieldSet = new Set(); for (var i = 0; i < arr.length; i++) { var item = arr[i]; if (isObject_1(item)) { Object.keys(item).forEach(function (i) { return fieldSet.add(i); }); } else if (Array.isArray(item)) { return _recur(item, tableDepth + 1); } } var fields = Array.from(fieldSet); var rows = arr.map(function () { return []; }); for (var _i = 0; _i < arr.length; _i++) { var _item = arr[_i]; if (isObject_1(_item)) { for (var j = 0; j < fields.length; j++) { var 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: 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) { var arr = isObject_1(json) ? objectToArray_1(json) : json; if (!Array.isArray(arr)) { arr = []; } return arrayToTable_1(arr, options); } var src = { jsonToTableHtmlString: jsonToTableHtmlString }; var src_1 = src.jsonToTableHtmlString; exports.default = src; exports.jsonToTableHtmlString = src_1; //# sourceMappingURL=index.cjs.js.map