1 import alignTableData from './alignTableData';
2 import calculateCellWidthIndex from './calculateCellWidthIndex';
3 import calculateRowHeightIndex from './calculateRowHeightIndex';
4 import drawTable from './drawTable';
5 import makeConfig from './makeConfig';
6 import mapDataUsingRowHeightIndex from './mapDataUsingRowHeightIndex';
7 import padTableData from './padTableData';
8 import stringifyTableData from './stringifyTableData';
9 import truncateTableData from './truncateTableData';
10 import validateTableData from './validateTableData';
13 * @typedef {string} table~cell
17 * @typedef {table~cell[]} table~row
21 * @typedef {object} table~columns
22 * @property {string} alignment Cell content alignment (enum: left, center, right) (default: left).
23 * @property {number} width Column width (default: auto).
24 * @property {number} truncate Number of characters are which the content will be truncated (default: Infinity).
25 * @property {boolean} wrapWord When true the text is broken at the nearest space or one of the special characters
26 * @property {number} paddingLeft Cell content padding width left (default: 1).
27 * @property {number} paddingRight Cell content padding width right (default: 1).
31 * @typedef {object} table~border
32 * @property {string} topBody
33 * @property {string} topJoin
34 * @property {string} topLeft
35 * @property {string} topRight
36 * @property {string} bottomBody
37 * @property {string} bottomJoin
38 * @property {string} bottomLeft
39 * @property {string} bottomRight
40 * @property {string} bodyLeft
41 * @property {string} bodyRight
42 * @property {string} bodyJoin
43 * @property {string} joinBody
44 * @property {string} joinLeft
45 * @property {string} joinRight
46 * @property {string} joinJoin
50 * Used to tell whether to draw a horizontal line.
51 * This callback is called for each non-content line of the table.
52 * The default behavior is to always return true.
54 * @typedef {Function} drawHorizontalLine
55 * @param {number} index
56 * @param {number} size
61 * @typedef {object} table~config
62 * @property {table~border} border
63 * @property {table~columns[]} columns Column specific configuration.
64 * @property {table~columns} columnDefault Default values for all columns. Column specific settings overwrite the default values.
65 * @property {table~drawHorizontalLine} drawHorizontalLine
66 * @property {table~singleLine} singleLine Horizontal lines inside the table are not drawn.
70 * Generates a text table.
72 * @param {table~row[]} data
73 * @param {table~config} userConfig
76 export default (data, userConfig = {}) => {
79 validateTableData(data);
81 rows = stringifyTableData(data);
83 const config = makeConfig(rows, userConfig);
85 rows = truncateTableData(data, config);
87 const rowHeightIndex = calculateRowHeightIndex(rows, config);
89 rows = mapDataUsingRowHeightIndex(rows, rowHeightIndex, config);
90 rows = alignTableData(rows, config);
91 rows = padTableData(rows, config);
93 const cellWidthIndex = calculateCellWidthIndex(rows[0]);
95 return drawTable(rows, config.border, cellWidthIndex, rowHeightIndex, config.drawHorizontalLine, config.singleLine);