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.

131 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 _mapValues2 = _interopRequireDefault(require("lodash/mapValues"));
  7. var _values2 = _interopRequireDefault(require("lodash/values"));
  8. var _trimEnd2 = _interopRequireDefault(require("lodash/trimEnd"));
  9. var _makeStreamConfig = _interopRequireDefault(require("./makeStreamConfig"));
  10. var _drawRow = _interopRequireDefault(require("./drawRow"));
  11. var _drawBorder = require("./drawBorder");
  12. var _stringifyTableData = _interopRequireDefault(require("./stringifyTableData"));
  13. var _truncateTableData = _interopRequireDefault(require("./truncateTableData"));
  14. var _mapDataUsingRowHeightIndex = _interopRequireDefault(require("./mapDataUsingRowHeightIndex"));
  15. var _alignTableData = _interopRequireDefault(require("./alignTableData"));
  16. var _padTableData = _interopRequireDefault(require("./padTableData"));
  17. var _calculateRowHeightIndex = _interopRequireDefault(require("./calculateRowHeightIndex"));
  18. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  19. /**
  20. * @param {Array} data
  21. * @param {Object} config
  22. * @returns {Array}
  23. */
  24. const prepareData = (data, config) => {
  25. let rows;
  26. rows = (0, _stringifyTableData.default)(data);
  27. rows = (0, _truncateTableData.default)(data, config);
  28. const rowHeightIndex = (0, _calculateRowHeightIndex.default)(rows, config);
  29. rows = (0, _mapDataUsingRowHeightIndex.default)(rows, rowHeightIndex, config);
  30. rows = (0, _alignTableData.default)(rows, config);
  31. rows = (0, _padTableData.default)(rows, config);
  32. return rows;
  33. };
  34. /**
  35. * @param {string[]} row
  36. * @param {number[]} columnWidthIndex
  37. * @param {Object} config
  38. * @returns {undefined}
  39. */
  40. const create = (row, columnWidthIndex, config) => {
  41. const rows = prepareData([row], config);
  42. const body = rows.map(literalRow => {
  43. return (0, _drawRow.default)(literalRow, config.border);
  44. }).join('');
  45. let output;
  46. output = '';
  47. output += (0, _drawBorder.drawBorderTop)(columnWidthIndex, config.border);
  48. output += body;
  49. output += (0, _drawBorder.drawBorderBottom)(columnWidthIndex, config.border);
  50. output = (0, _trimEnd2.default)(output);
  51. process.stdout.write(output);
  52. };
  53. /**
  54. * @param {string[]} row
  55. * @param {number[]} columnWidthIndex
  56. * @param {Object} config
  57. * @returns {undefined}
  58. */
  59. const append = (row, columnWidthIndex, config) => {
  60. const rows = prepareData([row], config);
  61. const body = rows.map(literalRow => {
  62. return (0, _drawRow.default)(literalRow, config.border);
  63. }).join('');
  64. let output = '';
  65. const bottom = (0, _drawBorder.drawBorderBottom)(columnWidthIndex, config.border);
  66. if (bottom !== '\n') {
  67. output = '\r\u001B[K';
  68. }
  69. output += (0, _drawBorder.drawBorderJoin)(columnWidthIndex, config.border);
  70. output += body;
  71. output += bottom;
  72. output = (0, _trimEnd2.default)(output);
  73. process.stdout.write(output);
  74. };
  75. /**
  76. * @param {Object} userConfig
  77. * @returns {Object}
  78. */
  79. const createStream = (userConfig = {}) => {
  80. const config = (0, _makeStreamConfig.default)(userConfig); // @todo Use 'Object.values' when Node.js v6 support is dropped.
  81. const columnWidthIndex = (0, _values2.default)((0, _mapValues2.default)(config.columns, column => {
  82. return column.width + column.paddingLeft + column.paddingRight;
  83. }));
  84. let empty;
  85. empty = true;
  86. return {
  87. /**
  88. * @param {string[]} row
  89. * @returns {undefined}
  90. */
  91. write: row => {
  92. if (row.length !== config.columnCount) {
  93. throw new Error('Row cell count does not match the config.columnCount.');
  94. }
  95. if (empty) {
  96. empty = false;
  97. return create(row, columnWidthIndex, config);
  98. } else {
  99. return append(row, columnWidthIndex, config);
  100. }
  101. }
  102. };
  103. };
  104. var _default = createStream;
  105. exports.default = _default;
  106. //# sourceMappingURL=createStream.js.map