|
| 1 | +/** |
| 2 | + * @typedef {Object} TableOptions |
| 3 | + * @prop {(value: (string | number | boolean)) => (string | number | boolean)} tableWrap |
| 4 | + * @prop {(value: (string | number | boolean), row) => (string | number | boolean)} rowWrap |
| 5 | + * @prop {(value: (string | number | boolean), row, col) => (string | number | boolean)} colWrap |
| 6 | + * @prop {string} colDelim |
| 7 | + * @prop {string} rowDelim |
| 8 | + */ |
| 9 | + |
| 10 | +const { timestamp } = require("./createLogger"); |
| 11 | + |
| 12 | +const sanitize = (str) => |
| 13 | + (typeof str === "string" ? str : JSON.stringify(str, null, 2)) |
| 14 | + .replace(/\|/gm, "|") |
| 15 | + .replace(/\r?\n/gm, "<br />") |
| 16 | + .replace(/"username": ".+?"/, '"username": "***"') |
| 17 | + .replace(/"password": ".+?"/, '"password": "***"'); |
| 18 | +const sanitizeTableValues = (table) => table.map((columns) => columns.map(sanitize)); |
| 19 | + |
| 20 | +const arraysToHTMLTable = (arrays, hasHeader) => |
| 21 | + arraysToTable(arrays, { |
| 22 | + tableWrap: (value) => `<table>\n${value}\n</table>`, |
| 23 | + rowWrap: (value, row) => (hasHeader && !row ? `<th>${value}</th>` : `<tr>${value}</tr>`), |
| 24 | + colWrap: (value, row, col) => `<td>${value}</td>`, |
| 25 | + colDelim: " ", |
| 26 | + rowDelim: "\n", |
| 27 | + }); |
| 28 | + |
| 29 | +/** @param {(string | number | boolean)[][]} rows */ |
| 30 | +const arraysToMarkdownTable = ([...rows]) => { |
| 31 | + rows.splice(1, 0, Array(rows[0].length).fill("-")); |
| 32 | + |
| 33 | + return arraysToTable(rows, { |
| 34 | + tableWrap: (value) => value, |
| 35 | + rowWrap: (value, row) => `|${value}|`, |
| 36 | + colWrap: (value, row, col) => value, |
| 37 | + colDelim: "|", |
| 38 | + rowDelim: "\n", |
| 39 | + }); |
| 40 | +}; |
| 41 | + |
| 42 | +/** @param {(string | number | boolean)[][]} rows */ |
| 43 | +const arraysToTerminalTable = (rows) => { |
| 44 | + // determine max length of each column |
| 45 | + const colLenghts = Array(rows[0].length).fill(0); |
| 46 | + rows.forEach((row) => |
| 47 | + row.forEach((col, index) => (colLenghts[index] = Math.max(col.toString().length, colLenghts[index]))) |
| 48 | + ); |
| 49 | + |
| 50 | + return arraysToTable(rows, { |
| 51 | + tableWrap: (value) => value, |
| 52 | + rowWrap: (value, row) => value, |
| 53 | + colWrap: (value, row, col) => value.toString().padEnd(colLenghts[col]), |
| 54 | + colDelim: "|", |
| 55 | + rowDelim: "\n", |
| 56 | + }); |
| 57 | +}; |
| 58 | + |
| 59 | +/** |
| 60 | + * |
| 61 | + * @param {(string | number | boolean)[][]} arrays |
| 62 | + * @param {TableOptions} options |
| 63 | + */ |
| 64 | +const arraysToTable = (arrays, options) => { |
| 65 | + arrays = sanitizeTableValues(arrays); |
| 66 | + const { tableWrap, rowWrap, colWrap, rowDelim, colDelim } = options; |
| 67 | + const formattedColumns = arrays.map((array, row) => |
| 68 | + array.map((value, column) => colWrap(value, row, column)).join(colDelim) |
| 69 | + ); |
| 70 | + const formattedRows = formattedColumns.map((columnStr, row) => rowWrap(columnStr, row)).join(rowDelim); |
| 71 | + |
| 72 | + return tableWrap(formattedRows); |
| 73 | +}; |
| 74 | + |
| 75 | +const unBuffer = (str) => |
| 76 | + str instanceof Buffer ? str.toString() : typeof str === "string" ? str : JSON.stringify(str, null, 2); |
| 77 | + |
| 78 | +/** |
| 79 | + * @param {Device} device |
| 80 | + */ |
| 81 | +const adapterHistoryTable = (device) => { |
| 82 | + const fields = ["function", "args", "time", "result", "queued at", "finished at", "failed"]; |
| 83 | + console.log("proxymeta", device.adapter.__proxyMeta); |
| 84 | + const rows = device.adapter.__proxyMeta.history.map((entry) => [ |
| 85 | + entry.field.toString(), |
| 86 | + entry.args.length ? `<details> <pre>${unBuffer(entry.args)}</pre> </details>` : " ", |
| 87 | + entry.runDuration, |
| 88 | + `<details> <pre>${entry.error || unBuffer(entry.result)}</pre> </details>`, |
| 89 | + entry.queuedAt && timestamp(entry.queuedAt), |
| 90 | + entry.finishedAt && timestamp(entry.finishedAt), |
| 91 | + !!entry.error, |
| 92 | + ]); |
| 93 | + return [fields, ...rows.reverse()]; |
| 94 | +}; |
| 95 | + |
| 96 | +module.exports = { arraysToHTMLTable, arraysToMarkdownTable, arraysToTerminalTable, adapterHistoryTable }; |
0 commit comments