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.

109 lines
3.8 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _drawTable = _interopRequireDefault(require("./drawTable"));
  7. var _calculateCellWidthIndex = _interopRequireDefault(require("./calculateCellWidthIndex"));
  8. var _makeConfig = _interopRequireDefault(require("./makeConfig"));
  9. var _calculateRowHeightIndex = _interopRequireDefault(require("./calculateRowHeightIndex"));
  10. var _mapDataUsingRowHeightIndex = _interopRequireDefault(require("./mapDataUsingRowHeightIndex"));
  11. var _alignTableData = _interopRequireDefault(require("./alignTableData"));
  12. var _padTableData = _interopRequireDefault(require("./padTableData"));
  13. var _validateTableData = _interopRequireDefault(require("./validateTableData"));
  14. var _stringifyTableData = _interopRequireDefault(require("./stringifyTableData"));
  15. var _truncateTableData = _interopRequireDefault(require("./truncateTableData"));
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17. /**
  18. * @typedef {string} table~cell
  19. */
  20. /**
  21. * @typedef {table~cell[]} table~row
  22. */
  23. /**
  24. * @typedef {Object} table~columns
  25. * @property {string} alignment Cell content alignment (enum: left, center, right) (default: left).
  26. * @property {number} width Column width (default: auto).
  27. * @property {number} truncate Number of characters are which the content will be truncated (default: Infinity).
  28. * @property {boolean} wrapWord When true the text is broken at the nearest space or one of the special characters
  29. * @property {number} paddingLeft Cell content padding width left (default: 1).
  30. * @property {number} paddingRight Cell content padding width right (default: 1).
  31. */
  32. /**
  33. * @typedef {Object} table~border
  34. * @property {string} topBody
  35. * @property {string} topJoin
  36. * @property {string} topLeft
  37. * @property {string} topRight
  38. * @property {string} bottomBody
  39. * @property {string} bottomJoin
  40. * @property {string} bottomLeft
  41. * @property {string} bottomRight
  42. * @property {string} bodyLeft
  43. * @property {string} bodyRight
  44. * @property {string} bodyJoin
  45. * @property {string} joinBody
  46. * @property {string} joinLeft
  47. * @property {string} joinRight
  48. * @property {string} joinJoin
  49. */
  50. /**
  51. * Used to tell whether to draw a horizontal line.
  52. * This callback is called for each non-content line of the table.
  53. * The default behavior is to always return true.
  54. *
  55. * @typedef {Function} drawHorizontalLine
  56. * @param {number} index
  57. * @param {number} size
  58. * @returns {boolean}
  59. */
  60. /**
  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.
  67. */
  68. /**
  69. * Generates a text table.
  70. *
  71. * @param {table~row[]} data
  72. * @param {table~config} userConfig
  73. * @returns {string}
  74. */
  75. const table = (data, userConfig = {}) => {
  76. let rows;
  77. (0, _validateTableData.default)(data);
  78. rows = (0, _stringifyTableData.default)(data);
  79. const config = (0, _makeConfig.default)(rows, userConfig);
  80. rows = (0, _truncateTableData.default)(data, config);
  81. const rowHeightIndex = (0, _calculateRowHeightIndex.default)(rows, config);
  82. rows = (0, _mapDataUsingRowHeightIndex.default)(rows, rowHeightIndex, config);
  83. rows = (0, _alignTableData.default)(rows, config);
  84. rows = (0, _padTableData.default)(rows, config);
  85. const cellWidthIndex = (0, _calculateCellWidthIndex.default)(rows[0]);
  86. return (0, _drawTable.default)(rows, config.border, cellWidthIndex, rowHeightIndex, config.drawHorizontalLine, config.singleLine);
  87. };
  88. var _default = table;
  89. exports.default = _default;
  90. //# sourceMappingURL=table.js.map