Ohm-Management - Projektarbeit B-ME
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.

table.js.flow 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. */
  61. /**
  62. * Generates a text table.
  63. *
  64. * @param {table~row[]} data
  65. * @param {table~config} userConfig
  66. * @returns {string}
  67. */
  68. export default (data, userConfig = {}) => {
  69. let rows;
  70. validateTableData(data);
  71. rows = stringifyTableData(data);
  72. const config = makeConfig(rows, userConfig);
  73. rows = truncateTableData(data, config);
  74. const rowHeightIndex = calculateRowHeightIndex(rows, config);
  75. rows = mapDataUsingRowHeightIndex(rows, rowHeightIndex, config);
  76. rows = alignTableData(rows, config);
  77. rows = padTableData(rows, config);
  78. const cellWidthIndex = calculateCellWidthIndex(rows[0]);
  79. return drawTable(rows, config.border, cellWidthIndex, rowHeightIndex, config.drawHorizontalLine);
  80. };