table 6.5.0 → 6.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  import type { TableConfig, Row } from './types/internal';
2
- declare const _default: (rows: Row[], columnSizeIndex: number[], rowSpanIndex: number[], config: TableConfig) => string;
2
+ declare const _default: (rows: Row[], columnWidths: number[], rowHeights: number[], config: TableConfig) => string;
3
3
  export default _default;
package/dist/drawTable.js CHANGED
@@ -5,33 +5,44 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const drawBorder_1 = require("./drawBorder");
7
7
  const drawRow_1 = __importDefault(require("./drawRow"));
8
- exports.default = (rows, columnSizeIndex, rowSpanIndex, config) => {
9
- const { drawHorizontalLine, singleLine, } = config;
10
- let output;
11
- let realRowIndex;
12
- let rowHeight;
13
- const rowCount = rows.length;
14
- realRowIndex = 0;
15
- output = '';
16
- if (drawHorizontalLine(realRowIndex, rowCount)) {
17
- output += drawBorder_1.drawBorderTop(columnSizeIndex, config);
8
+ /**
9
+ * Group the array into sub-arrays by sizes.
10
+ *
11
+ * @example
12
+ * chunkBySizes(['a', 'b', 'c', 'd', 'e'], [2, 1, 2]) = [ ['a', 'b'], ['c'], ['d', 'e'] ]
13
+ */
14
+ const groupBySizes = (array, sizes) => {
15
+ let startIndex = 0;
16
+ return sizes.map((rowHeight) => {
17
+ const chunk = array.slice(startIndex, startIndex + rowHeight);
18
+ startIndex += rowHeight;
19
+ return chunk;
20
+ });
21
+ };
22
+ const shouldDrawBorderJoin = (rowIndex, rowCount, config) => {
23
+ const { singleLine, drawHorizontalLine } = config;
24
+ return !singleLine && rowIndex + 1 < rowCount && drawHorizontalLine(rowIndex + 1, rowCount);
25
+ };
26
+ exports.default = (rows, columnWidths, rowHeights, config) => {
27
+ const { drawHorizontalLine, } = config;
28
+ const groupedRows = groupBySizes(rows, rowHeights).map((group) => {
29
+ return group.map((row) => {
30
+ return drawRow_1.default(row, config);
31
+ }).join('');
32
+ });
33
+ const rowCount = groupedRows.length;
34
+ let output = '';
35
+ if (drawHorizontalLine(0, rowCount)) {
36
+ output += drawBorder_1.drawBorderTop(columnWidths, config);
18
37
  }
19
- rows.forEach((row, index0) => {
20
- output += drawRow_1.default(row, config);
21
- if (!rowHeight) {
22
- rowHeight = rowSpanIndex[realRowIndex];
23
- realRowIndex++;
24
- }
25
- rowHeight--;
26
- if (!singleLine &&
27
- rowHeight === 0 &&
28
- index0 !== rowCount - 1 &&
29
- drawHorizontalLine(realRowIndex, rowCount)) {
30
- output += drawBorder_1.drawBorderJoin(columnSizeIndex, config);
38
+ groupedRows.forEach((row, rowIndex) => {
39
+ output += row;
40
+ if (shouldDrawBorderJoin(rowIndex, rowCount, config)) {
41
+ output += drawBorder_1.drawBorderJoin(columnWidths, config);
31
42
  }
32
43
  });
33
- if (drawHorizontalLine(realRowIndex, rowCount)) {
34
- output += drawBorder_1.drawBorderBottom(columnSizeIndex, config);
44
+ if (drawHorizontalLine(rowCount, rowCount)) {
45
+ output += drawBorder_1.drawBorderBottom(columnWidths, config);
35
46
  }
36
47
  return output;
37
48
  };
package/package.json CHANGED
@@ -91,5 +91,5 @@
91
91
  "lint": "eslint --ignore-path .gitignore ./src ./test",
92
92
  "test": "npm run create-validators && mocha --require ts-node/register ./test/**/*.ts"
93
93
  },
94
- "version": "6.5.0"
94
+ "version": "6.5.1"
95
95
  }