mapDataUsingRowHeights.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.mapDataUsingRowHeights = void 0;
  4. const wrapCell_1 = require("./wrapCell");
  5. const createEmptyStrings = (length) => {
  6. return new Array(length).fill('');
  7. };
  8. const padCellVertically = (lines, rowHeight, columnConfig) => {
  9. const { verticalAlignment } = columnConfig;
  10. const availableLines = rowHeight - lines.length;
  11. if (verticalAlignment === 'top') {
  12. return [...lines, ...createEmptyStrings(availableLines)];
  13. }
  14. if (verticalAlignment === 'bottom') {
  15. return [...createEmptyStrings(availableLines), ...lines];
  16. }
  17. return [
  18. ...createEmptyStrings(Math.floor(availableLines / 2)),
  19. ...lines,
  20. ...createEmptyStrings(Math.ceil(availableLines / 2)),
  21. ];
  22. };
  23. const flatten = (array) => {
  24. return [].concat(...array);
  25. };
  26. const mapDataUsingRowHeights = (unmappedRows, rowHeights, config) => {
  27. const tableWidth = unmappedRows[0].length;
  28. const mappedRows = unmappedRows.map((unmappedRow, unmappedRowIndex) => {
  29. const outputRowHeight = rowHeights[unmappedRowIndex];
  30. const outputRow = Array.from({ length: outputRowHeight }, () => {
  31. return new Array(tableWidth).fill('');
  32. });
  33. unmappedRow.forEach((cell, cellIndex) => {
  34. const cellLines = wrapCell_1.wrapCell(cell, config.columns[cellIndex].width, config.columns[cellIndex].wrapWord);
  35. const paddedCellLines = padCellVertically(cellLines, outputRowHeight, config.columns[cellIndex]);
  36. paddedCellLines.forEach((cellLine, cellLineIndex) => {
  37. outputRow[cellLineIndex][cellIndex] = cellLine;
  38. });
  39. });
  40. return outputRow;
  41. });
  42. return flatten(mappedRows);
  43. };
  44. exports.mapDataUsingRowHeights = mapDataUsingRowHeights;