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.

createStream.js 3.6KB

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