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.flow 2.9KB

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