You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.1 KiB

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