table.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _drawTable = require('./drawTable');
  6. var _drawTable2 = _interopRequireDefault(_drawTable);
  7. var _calculateCellWidthIndex = require('./calculateCellWidthIndex');
  8. var _calculateCellWidthIndex2 = _interopRequireDefault(_calculateCellWidthIndex);
  9. var _makeConfig = require('./makeConfig');
  10. var _makeConfig2 = _interopRequireDefault(_makeConfig);
  11. var _calculateRowHeightIndex = require('./calculateRowHeightIndex');
  12. var _calculateRowHeightIndex2 = _interopRequireDefault(_calculateRowHeightIndex);
  13. var _mapDataUsingRowHeightIndex = require('./mapDataUsingRowHeightIndex');
  14. var _mapDataUsingRowHeightIndex2 = _interopRequireDefault(_mapDataUsingRowHeightIndex);
  15. var _alignTableData = require('./alignTableData');
  16. var _alignTableData2 = _interopRequireDefault(_alignTableData);
  17. var _padTableData = require('./padTableData');
  18. var _padTableData2 = _interopRequireDefault(_padTableData);
  19. var _validateTableData = require('./validateTableData');
  20. var _validateTableData2 = _interopRequireDefault(_validateTableData);
  21. var _stringifyTableData = require('./stringifyTableData');
  22. var _stringifyTableData2 = _interopRequireDefault(_stringifyTableData);
  23. var _truncateTableData = require('./truncateTableData');
  24. var _truncateTableData2 = _interopRequireDefault(_truncateTableData);
  25. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  26. /**
  27. * @typedef {string} table~cell
  28. */
  29. /**
  30. * @typedef {table~cell[]} table~row
  31. */
  32. /**
  33. * @typedef {Object} table~columns
  34. * @property {string} alignment Cell content alignment (enum: left, center, right) (default: left).
  35. * @property {number} width Column width (default: auto).
  36. * @property {number} truncate Number of characters are which the content will be truncated (default: Infinity).
  37. * @property {number} paddingLeft Cell content padding width left (default: 1).
  38. * @property {number} paddingRight Cell content padding width right (default: 1).
  39. */
  40. /**
  41. * @typedef {Object} table~border
  42. * @property {string} topBody
  43. * @property {string} topJoin
  44. * @property {string} topLeft
  45. * @property {string} topRight
  46. * @property {string} bottomBody
  47. * @property {string} bottomJoin
  48. * @property {string} bottomLeft
  49. * @property {string} bottomRight
  50. * @property {string} bodyLeft
  51. * @property {string} bodyRight
  52. * @property {string} bodyJoin
  53. * @property {string} joinBody
  54. * @property {string} joinLeft
  55. * @property {string} joinRight
  56. * @property {string} joinJoin
  57. */
  58. /**
  59. * Used to tell whether to draw a horizontal line.
  60. * This callback is called for each non-content line of the table.
  61. * The default behavior is to always return true.
  62. *
  63. * @typedef {Function} drawHorizontalLine
  64. * @param {number} index
  65. * @param {number} size
  66. * @returns {boolean}
  67. */
  68. /**
  69. * @typedef {Object} table~config
  70. * @property {table~border} border
  71. * @property {table~columns[]} columns Column specific configuration.
  72. * @property {table~columns} columnDefault Default values for all columns. Column specific settings overwrite the default values.
  73. * @property {table~drawHorizontalLine} drawHorizontalLine
  74. */
  75. /**
  76. * Generates a text table.
  77. *
  78. * @param {table~row[]} data
  79. * @param {table~config} userConfig
  80. * @returns {string}
  81. */
  82. exports.default = function (data) {
  83. let userConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  84. let rows;
  85. (0, _validateTableData2.default)(data);
  86. rows = (0, _stringifyTableData2.default)(data);
  87. const config = (0, _makeConfig2.default)(rows, userConfig);
  88. rows = (0, _truncateTableData2.default)(data, config);
  89. const rowHeightIndex = (0, _calculateRowHeightIndex2.default)(rows, config);
  90. rows = (0, _mapDataUsingRowHeightIndex2.default)(rows, rowHeightIndex, config);
  91. rows = (0, _alignTableData2.default)(rows, config);
  92. rows = (0, _padTableData2.default)(rows, config);
  93. const cellWidthIndex = (0, _calculateCellWidthIndex2.default)(rows[0]);
  94. return (0, _drawTable2.default)(rows, config.border, cellWidthIndex, rowHeightIndex, config.drawHorizontalLine);
  95. };