table 6.5.1 → 6.6.0

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.
Files changed (65) hide show
  1. package/README.md +20 -25
  2. package/dist/alignString.d.ts +2 -3
  3. package/dist/alignString.js +21 -4
  4. package/dist/alignTableData.d.ts +1 -2
  5. package/dist/alignTableData.js +7 -5
  6. package/dist/calculateCellHeight.d.ts +4 -2
  7. package/dist/calculateCellHeight.js +8 -9
  8. package/dist/calculateCellWidths.d.ts +5 -0
  9. package/dist/{calculateCellWidthIndex.js → calculateCellWidths.js} +6 -6
  10. package/dist/{calculateMaximumColumnWidthIndex.d.ts → calculateColumnWidths.d.ts} +0 -0
  11. package/dist/calculateColumnWidths.js +16 -0
  12. package/dist/calculateRowHeights.d.ts +5 -0
  13. package/dist/calculateRowHeights.js +18 -0
  14. package/dist/createStream.d.ts +1 -2
  15. package/dist/createStream.js +29 -30
  16. package/dist/drawBorder.d.ts +4 -4
  17. package/dist/drawBorder.js +16 -13
  18. package/dist/drawContent.d.ts +11 -0
  19. package/dist/drawContent.js +26 -0
  20. package/dist/drawRow.d.ts +1 -2
  21. package/dist/drawRow.js +11 -13
  22. package/dist/drawTable.d.ts +1 -2
  23. package/dist/drawTable.js +20 -40
  24. package/dist/generated/validators.d.ts +2 -2
  25. package/dist/generated/validators.js +282 -274
  26. package/dist/getBorderCharacters.d.ts +1 -2
  27. package/dist/getBorderCharacters.js +3 -1
  28. package/dist/index.d.ts +3 -3
  29. package/dist/index.js +6 -9
  30. package/dist/makeConfig.d.ts +1 -2
  31. package/dist/makeConfig.js +12 -19
  32. package/dist/makeStreamConfig.d.ts +1 -2
  33. package/dist/makeStreamConfig.js +8 -19
  34. package/dist/mapDataUsingRowHeights.d.ts +2 -0
  35. package/dist/mapDataUsingRowHeights.js +29 -0
  36. package/dist/padTableData.d.ts +1 -2
  37. package/dist/padTableData.js +6 -4
  38. package/dist/stringifyTableData.d.ts +1 -2
  39. package/dist/stringifyTableData.js +7 -2
  40. package/dist/table.d.ts +1 -2
  41. package/dist/table.js +23 -24
  42. package/dist/truncateTableData.d.ts +1 -2
  43. package/dist/truncateTableData.js +6 -4
  44. package/dist/types/api.d.ts +2 -1
  45. package/dist/utils.d.ts +1 -0
  46. package/dist/utils.js +93 -0
  47. package/dist/validateConfig.d.ts +1 -2
  48. package/dist/validateConfig.js +3 -1
  49. package/dist/validateTableData.d.ts +1 -2
  50. package/dist/validateTableData.js +5 -2
  51. package/dist/wrapCell.d.ts +1 -2
  52. package/dist/wrapCell.js +9 -24
  53. package/dist/wrapString.d.ts +1 -2
  54. package/dist/wrapString.js +3 -1
  55. package/dist/wrapWord.d.ts +1 -2
  56. package/dist/wrapWord.js +3 -1
  57. package/package.json +1 -1
  58. package/dist/calculateCellWidthIndex.d.ts +0 -6
  59. package/dist/calculateMaximumColumnWidthIndex.js +0 -24
  60. package/dist/calculateRowHeightIndex.d.ts +0 -6
  61. package/dist/calculateRowHeightIndex.js +0 -21
  62. package/dist/drawHorizontalContent.d.ts +0 -11
  63. package/dist/drawHorizontalContent.js +0 -18
  64. package/dist/mapDataUsingRowHeightIndex.d.ts +0 -3
  65. package/dist/mapDataUsingRowHeightIndex.js +0 -26
package/README.md CHANGED
@@ -155,7 +155,7 @@ console.log(output);
155
155
 
156
156
  `{string} config.columns[{number}].alignment` property controls content horizontal alignment within a cell.
157
157
 
158
- Valid values are: "left", "right" and "center".
158
+ Valid values are: "left", "right", "center" and "justify".
159
159
 
160
160
  ```js
161
161
  let config,
@@ -163,26 +163,21 @@ let config,
163
163
  output;
164
164
 
165
165
  data = [
166
- ['0A', '0B', '0C'],
167
- ['1A', '1B', '1C'],
168
- ['2A', '2B', '2C']
166
+ ['0A', '0B', '0C', '0D 0E 0F'],
167
+ ['1A', '1B', '1C', '1D 1E 1F'],
168
+ ['2A', '2B', '2C', '2D 2E 2F'],
169
169
  ];
170
170
 
171
171
  config = {
172
- columns: {
173
- 0: {
174
- alignment: 'left',
175
- width: 10
176
- },
177
- 1: {
178
- alignment: 'center',
179
- width: 10
180
- },
181
- 2: {
182
- alignment: 'right',
183
- width: 10
184
- }
185
- }
172
+ columnDefault: {
173
+ width: 10,
174
+ },
175
+ columns: [
176
+ {alignment: 'left'},
177
+ {alignment: 'center'},
178
+ {alignment: 'right'},
179
+ {alignment: 'justify'},
180
+ ],
186
181
  };
187
182
 
188
183
  output = table(data, config);
@@ -191,13 +186,13 @@ console.log(output);
191
186
  ```
192
187
 
193
188
  ```
194
- ╔════════════╤════════════╤════════════╗
195
- ║ 0A │ 0B │ 0C ║
196
- ╟────────────┼────────────┼────────────╢
197
- ║ 1A │ 1B │ 1C ║
198
- ╟────────────┼────────────┼────────────╢
199
- ║ 2A │ 2B │ 2C ║
200
- ╚════════════╧════════════╧════════════╝
189
+ ╔════════════╤════════════╤════════════╤════════════╗
190
+ ║ 0A │ 0B │ 0C │ 0D 0E 0F ║
191
+ ╟────────────┼────────────┼────────────┼────────────╢
192
+ ║ 1A │ 1B │ 1C │ 1D 1E 1F ║
193
+ ╟────────────┼────────────┼────────────┼────────────╢
194
+ ║ 2A │ 2B │ 2C │ 2D 2E 2F ║
195
+ ╚════════════╧════════════╧════════════╧════════════╝
201
196
  ```
202
197
 
203
198
  <a name="table-usage-column-width"></a>
@@ -1,7 +1,6 @@
1
- import type { ColumnUserConfig } from './types/api';
2
- declare const _default: (subject: string, containerWidth: number, alignment: ColumnUserConfig['alignment']) => string;
1
+ import type { Alignment } from './types/api';
3
2
  /**
4
3
  * Pads a string to the left and/or right to position the subject
5
4
  * text in a desired alignment within a container.
6
5
  */
7
- export default _default;
6
+ export declare const alignString: (subject: string, containerWidth: number, alignment: Alignment) => string;
@@ -3,7 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.alignString = void 0;
6
7
  const string_width_1 = __importDefault(require("string-width"));
8
+ const utils_1 = require("./utils");
7
9
  const alignLeft = (subject, width) => {
8
10
  return subject + ' '.repeat(width);
9
11
  };
@@ -21,14 +23,25 @@ const alignCenter = (subject, width) => {
21
23
  return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth + 1);
22
24
  }
23
25
  };
26
+ const alignJustify = (subject, width) => {
27
+ const spaceSequenceCount = utils_1.countSpaceSequence(subject);
28
+ if (spaceSequenceCount === 0) {
29
+ return alignLeft(subject, width);
30
+ }
31
+ const addingSpaces = utils_1.distributeUnevenly(width, spaceSequenceCount);
32
+ if (Math.max(...addingSpaces) > 3) {
33
+ return alignLeft(subject, width);
34
+ }
35
+ let spaceSequenceIndex = 0;
36
+ return subject.replace(/\s+/g, (groupSpace) => {
37
+ return groupSpace + ' '.repeat(addingSpaces[spaceSequenceIndex++]);
38
+ });
39
+ };
24
40
  /**
25
41
  * Pads a string to the left and/or right to position the subject
26
42
  * text in a desired alignment within a container.
27
43
  */
28
- exports.default = (subject, containerWidth, alignment) => {
29
- if (typeof subject !== 'string') {
30
- throw new TypeError('Subject parameter value must be a string.');
31
- }
44
+ const alignString = (subject, containerWidth, alignment) => {
32
45
  const subjectWidth = string_width_1.default(subject);
33
46
  if (subjectWidth > containerWidth) {
34
47
  throw new Error('Subject parameter value width cannot be greater than the container width.');
@@ -43,5 +56,9 @@ exports.default = (subject, containerWidth, alignment) => {
43
56
  if (alignment === 'right') {
44
57
  return alignRight(subject, availableWidth);
45
58
  }
59
+ if (alignment === 'justify') {
60
+ return alignJustify(subject, availableWidth);
61
+ }
46
62
  return alignCenter(subject, availableWidth);
47
63
  };
64
+ exports.alignString = alignString;
@@ -1,3 +1,2 @@
1
1
  import type { BaseConfig, Row } from './types/internal';
2
- declare const _default: (rows: Row[], config: BaseConfig) => Row[];
3
- export default _default;
2
+ export declare const alignTableData: (rows: Row[], config: BaseConfig) => Row[];
@@ -3,18 +3,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.alignTableData = void 0;
6
7
  const string_width_1 = __importDefault(require("string-width"));
7
- const alignString_1 = __importDefault(require("./alignString"));
8
- exports.default = (rows, config) => {
8
+ const alignString_1 = require("./alignString");
9
+ const alignTableData = (rows, config) => {
9
10
  return rows.map((row) => {
10
- return row.map((cell, index) => {
11
- const column = config.columns[index];
11
+ return row.map((cell, cellIndex) => {
12
+ const column = config.columns[cellIndex];
12
13
  if (string_width_1.default(cell) === column.width) {
13
14
  return cell;
14
15
  }
15
16
  else {
16
- return alignString_1.default(cell, column.width, column.alignment);
17
+ return alignString_1.alignString(cell, column.width, column.alignment);
17
18
  }
18
19
  });
19
20
  });
20
21
  };
22
+ exports.alignTableData = alignTableData;
@@ -1,2 +1,4 @@
1
- declare const _default: (value: string, columnWidth: number, useWrapWord?: boolean) => number;
2
- export default _default;
1
+ /**
2
+ * Calculates height of cell content in regard to its width and word wrapping.
3
+ */
4
+ export declare const calculateCellHeight: (value: string, columnWidth: number, useWrapWord?: boolean) => number;
@@ -1,12 +1,11 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const wrapCell_1 = __importDefault(require("./wrapCell"));
7
- exports.default = (value, columnWidth, useWrapWord = false) => {
8
- if (typeof value !== 'string') {
9
- throw new TypeError('Value must be a string.');
10
- }
11
- return wrapCell_1.default(value, columnWidth, useWrapWord).length;
3
+ exports.calculateCellHeight = void 0;
4
+ const wrapCell_1 = require("./wrapCell");
5
+ /**
6
+ * Calculates height of cell content in regard to its width and word wrapping.
7
+ */
8
+ const calculateCellHeight = (value, columnWidth, useWrapWord = false) => {
9
+ return wrapCell_1.wrapCell(value, columnWidth, useWrapWord).length;
12
10
  };
11
+ exports.calculateCellHeight = calculateCellHeight;
@@ -0,0 +1,5 @@
1
+ import type { Cell } from './types/internal';
2
+ /**
3
+ * Calculates width of each cell contents in a row.
4
+ */
5
+ export declare const calculateCellWidths: (cells: Cell[]) => number[];
@@ -3,14 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.calculateCellWidths = void 0;
6
7
  const string_width_1 = __importDefault(require("string-width"));
7
8
  /**
8
- * Calculates width of each cell contents.
9
+ * Calculates width of each cell contents in a row.
9
10
  */
10
- exports.default = (cells) => {
11
- return cells.map((value) => {
12
- return Math.max(...value.split('\n').map((line) => {
13
- return string_width_1.default(line);
14
- }));
11
+ const calculateCellWidths = (cells) => {
12
+ return cells.map((cell) => {
13
+ return Math.max(...cell.split('\n').map(string_width_1.default));
15
14
  });
16
15
  };
16
+ exports.calculateCellWidths = calculateCellWidths;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const calculateCellWidths_1 = require("./calculateCellWidths");
4
+ /**
5
+ * Produces an array of values that describe the largest value length (width) in every column.
6
+ */
7
+ exports.default = (rows) => {
8
+ const columnWidths = new Array(rows[0].length).fill(0);
9
+ rows.forEach((row) => {
10
+ const cellWidths = calculateCellWidths_1.calculateCellWidths(row);
11
+ cellWidths.forEach((cellWidth, cellIndex) => {
12
+ columnWidths[cellIndex] = Math.max(columnWidths[cellIndex], cellWidth);
13
+ });
14
+ });
15
+ return columnWidths;
16
+ };
@@ -0,0 +1,5 @@
1
+ import type { BaseConfig, Row } from './types/internal';
2
+ /**
3
+ * Produces an array of values that describe the largest value length (height) in every row.
4
+ */
5
+ export declare const calculateRowHeights: (rows: Row[], config: BaseConfig) => number[];
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.calculateRowHeights = void 0;
4
+ const calculateCellHeight_1 = require("./calculateCellHeight");
5
+ /**
6
+ * Produces an array of values that describe the largest value length (height) in every row.
7
+ */
8
+ const calculateRowHeights = (rows, config) => {
9
+ return rows.map((row) => {
10
+ let rowHeight = 1;
11
+ row.forEach((cell, cellIndex) => {
12
+ const cellHeight = calculateCellHeight_1.calculateCellHeight(cell, config.columns[cellIndex].width, config.columns[cellIndex].wrapWord);
13
+ rowHeight = Math.max(rowHeight, cellHeight);
14
+ });
15
+ return rowHeight;
16
+ });
17
+ };
18
+ exports.calculateRowHeights = calculateRowHeights;
@@ -1,3 +1,2 @@
1
1
  import type { StreamUserConfig, WritableStream } from './types/api';
2
- declare const _default: (userConfig: StreamUserConfig) => WritableStream;
3
- export default _default;
2
+ export declare const createStream: (userConfig: StreamUserConfig) => WritableStream;
@@ -1,58 +1,56 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const alignTableData_1 = __importDefault(require("./alignTableData"));
7
- const calculateRowHeightIndex_1 = __importDefault(require("./calculateRowHeightIndex"));
3
+ exports.createStream = void 0;
4
+ const alignTableData_1 = require("./alignTableData");
5
+ const calculateRowHeights_1 = require("./calculateRowHeights");
8
6
  const drawBorder_1 = require("./drawBorder");
9
- const drawRow_1 = __importDefault(require("./drawRow"));
10
- const makeStreamConfig_1 = __importDefault(require("./makeStreamConfig"));
11
- const mapDataUsingRowHeightIndex_1 = __importDefault(require("./mapDataUsingRowHeightIndex"));
12
- const padTableData_1 = __importDefault(require("./padTableData"));
13
- const stringifyTableData_1 = __importDefault(require("./stringifyTableData"));
14
- const truncateTableData_1 = __importDefault(require("./truncateTableData"));
7
+ const drawRow_1 = require("./drawRow");
8
+ const makeStreamConfig_1 = require("./makeStreamConfig");
9
+ const mapDataUsingRowHeights_1 = require("./mapDataUsingRowHeights");
10
+ const padTableData_1 = require("./padTableData");
11
+ const stringifyTableData_1 = require("./stringifyTableData");
12
+ const truncateTableData_1 = require("./truncateTableData");
15
13
  const prepareData = (data, config) => {
16
- let rows = stringifyTableData_1.default(data);
17
- rows = truncateTableData_1.default(rows, config);
18
- const rowHeightIndex = calculateRowHeightIndex_1.default(rows, config);
19
- rows = mapDataUsingRowHeightIndex_1.default(rows, rowHeightIndex, config);
20
- rows = alignTableData_1.default(rows, config);
21
- rows = padTableData_1.default(rows, config);
14
+ let rows = stringifyTableData_1.stringifyTableData(data);
15
+ rows = truncateTableData_1.truncateTableData(rows, config);
16
+ const rowHeights = calculateRowHeights_1.calculateRowHeights(rows, config);
17
+ rows = mapDataUsingRowHeights_1.mapDataUsingRowHeights(rows, rowHeights, config);
18
+ rows = alignTableData_1.alignTableData(rows, config);
19
+ rows = padTableData_1.padTableData(rows, config);
22
20
  return rows;
23
21
  };
24
- const create = (row, columnWidthIndex, config) => {
22
+ const create = (row, columnWidths, config) => {
25
23
  const rows = prepareData([row], config);
26
24
  const body = rows.map((literalRow) => {
27
- return drawRow_1.default(literalRow, config);
25
+ return drawRow_1.drawRow(literalRow, config);
28
26
  }).join('');
29
27
  let output;
30
28
  output = '';
31
- output += drawBorder_1.drawBorderTop(columnWidthIndex, config);
29
+ output += drawBorder_1.drawBorderTop(columnWidths, config);
32
30
  output += body;
33
- output += drawBorder_1.drawBorderBottom(columnWidthIndex, config);
31
+ output += drawBorder_1.drawBorderBottom(columnWidths, config);
34
32
  output = output.trimEnd();
35
33
  process.stdout.write(output);
36
34
  };
37
- const append = (row, columnWidthIndex, config) => {
35
+ const append = (row, columnWidths, config) => {
38
36
  const rows = prepareData([row], config);
39
37
  const body = rows.map((literalRow) => {
40
- return drawRow_1.default(literalRow, config);
38
+ return drawRow_1.drawRow(literalRow, config);
41
39
  }).join('');
42
40
  let output = '';
43
- const bottom = drawBorder_1.drawBorderBottom(columnWidthIndex, config);
41
+ const bottom = drawBorder_1.drawBorderBottom(columnWidths, config);
44
42
  if (bottom !== '\n') {
45
43
  output = '\r\u001B[K';
46
44
  }
47
- output += drawBorder_1.drawBorderJoin(columnWidthIndex, config);
45
+ output += drawBorder_1.drawBorderJoin(columnWidths, config);
48
46
  output += body;
49
47
  output += bottom;
50
48
  output = output.trimEnd();
51
49
  process.stdout.write(output);
52
50
  };
53
- exports.default = (userConfig) => {
54
- const config = makeStreamConfig_1.default(userConfig);
55
- const columnWidthIndex = Object.values(config.columns).map((column) => {
51
+ const createStream = (userConfig) => {
52
+ const config = makeStreamConfig_1.makeStreamConfig(userConfig);
53
+ const columnWidths = Object.values(config.columns).map((column) => {
56
54
  return column.width + column.paddingLeft + column.paddingRight;
57
55
  });
58
56
  let empty = true;
@@ -63,11 +61,12 @@ exports.default = (userConfig) => {
63
61
  }
64
62
  if (empty) {
65
63
  empty = false;
66
- create(row, columnWidthIndex, config);
64
+ create(row, columnWidths, config);
67
65
  }
68
66
  else {
69
- append(row, columnWidthIndex, config);
67
+ append(row, columnWidths, config);
70
68
  }
71
69
  },
72
70
  };
73
71
  };
72
+ exports.createStream = createStream;
@@ -6,19 +6,19 @@ declare type Separator = {
6
6
  readonly body: string;
7
7
  readonly join: string;
8
8
  };
9
- declare const drawBorder: (columnSizeIndex: number[], config: {
9
+ declare const drawBorder: (columnWidths: number[], config: {
10
10
  separator: Separator;
11
11
  drawVerticalLine: DrawVerticalLine;
12
12
  }) => string;
13
- declare const drawBorderTop: (columnSizeIndex: number[], config: {
13
+ declare const drawBorderTop: (columnWidths: number[], config: {
14
14
  border: TopBorderConfig;
15
15
  drawVerticalLine: DrawVerticalLine;
16
16
  }) => string;
17
- declare const drawBorderJoin: (columnSizeIndex: number[], config: {
17
+ declare const drawBorderJoin: (columnWidths: number[], config: {
18
18
  border: JoinBorderConfig;
19
19
  drawVerticalLine: DrawVerticalLine;
20
20
  }) => string;
21
- declare const drawBorderBottom: (columnSizeIndex: number[], config: {
21
+ declare const drawBorderBottom: (columnWidths: number[], config: {
22
22
  border: BottomBorderConfig;
23
23
  drawVerticalLine: DrawVerticalLine;
24
24
  }) => string;
@@ -1,19 +1,22 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.drawBorderTop = exports.drawBorderJoin = exports.drawBorderBottom = exports.drawBorder = void 0;
7
- const drawHorizontalContent_1 = __importDefault(require("./drawHorizontalContent"));
8
- const drawBorder = (columnSizeIndex, config) => {
9
- const columns = columnSizeIndex.map((size) => {
4
+ const drawContent_1 = require("./drawContent");
5
+ const drawBorder = (columnWidths, config) => {
6
+ const { separator, drawVerticalLine } = config;
7
+ const columns = columnWidths.map((size) => {
10
8
  return config.separator.body.repeat(size);
11
9
  });
12
- return drawHorizontalContent_1.default(columns, config);
10
+ return drawContent_1.drawContent(columns, {
11
+ drawSeparator: drawVerticalLine,
12
+ endSeparator: separator.right,
13
+ middleSeparator: separator.join,
14
+ startSeparator: separator.left,
15
+ }) + '\n';
13
16
  };
14
17
  exports.drawBorder = drawBorder;
15
- const drawBorderTop = (columnSizeIndex, config) => {
16
- const result = drawBorder(columnSizeIndex, {
18
+ const drawBorderTop = (columnWidths, config) => {
19
+ const result = drawBorder(columnWidths, {
17
20
  ...config,
18
21
  separator: {
19
22
  body: config.border.topBody,
@@ -28,8 +31,8 @@ const drawBorderTop = (columnSizeIndex, config) => {
28
31
  return result;
29
32
  };
30
33
  exports.drawBorderTop = drawBorderTop;
31
- const drawBorderJoin = (columnSizeIndex, config) => {
32
- return drawBorder(columnSizeIndex, {
34
+ const drawBorderJoin = (columnWidths, config) => {
35
+ return drawBorder(columnWidths, {
33
36
  ...config,
34
37
  separator: {
35
38
  body: config.border.joinBody,
@@ -40,8 +43,8 @@ const drawBorderJoin = (columnSizeIndex, config) => {
40
43
  });
41
44
  };
42
45
  exports.drawBorderJoin = drawBorderJoin;
43
- const drawBorderBottom = (columnSizeIndex, config) => {
44
- return drawBorder(columnSizeIndex, {
46
+ const drawBorderBottom = (columnWidths, config) => {
47
+ return drawBorder(columnWidths, {
45
48
  ...config,
46
49
  separator: {
47
50
  body: config.border.bottomBody,
@@ -0,0 +1,11 @@
1
+ declare type SeparatorConfig = {
2
+ drawSeparator: (index: number, size: number) => boolean;
3
+ startSeparator: string;
4
+ middleSeparator: string;
5
+ endSeparator: string;
6
+ };
7
+ /**
8
+ * Shared function to draw horizontal borders, rows or the entire table
9
+ */
10
+ export declare const drawContent: (contents: string[], separatorConfig: SeparatorConfig) => string;
11
+ export {};
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.drawContent = void 0;
4
+ /**
5
+ * Shared function to draw horizontal borders, rows or the entire table
6
+ */
7
+ const drawContent = (contents, separatorConfig) => {
8
+ const { startSeparator, middleSeparator, endSeparator, drawSeparator } = separatorConfig;
9
+ const contentSize = contents.length;
10
+ const result = [];
11
+ if (drawSeparator(0, contentSize)) {
12
+ result.push(startSeparator);
13
+ }
14
+ contents.forEach((content, contentIndex) => {
15
+ result.push(content);
16
+ // Only append the middle separator if the content is not the last
17
+ if (contentIndex + 1 < contentSize && drawSeparator(contentIndex + 1, contentSize)) {
18
+ result.push(middleSeparator);
19
+ }
20
+ });
21
+ if (drawSeparator(contentSize, contentSize)) {
22
+ result.push(endSeparator);
23
+ }
24
+ return result.join('');
25
+ };
26
+ exports.drawContent = drawContent;
package/dist/drawRow.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import type { DrawVerticalLine } from './types/api';
2
2
  import type { BodyBorderConfig, Row } from './types/internal';
3
- declare const _default: (row: Row, config: {
3
+ export declare const drawRow: (row: Row, config: {
4
4
  border: BodyBorderConfig;
5
5
  drawVerticalLine: DrawVerticalLine;
6
6
  }) => string;
7
- export default _default;
package/dist/drawRow.js CHANGED
@@ -1,16 +1,14 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const drawHorizontalContent_1 = __importDefault(require("./drawHorizontalContent"));
7
- exports.default = (row, config) => {
8
- return drawHorizontalContent_1.default(row, {
9
- ...config,
10
- separator: {
11
- join: config.border.bodyJoin,
12
- left: config.border.bodyLeft,
13
- right: config.border.bodyRight,
14
- },
15
- });
3
+ exports.drawRow = void 0;
4
+ const drawContent_1 = require("./drawContent");
5
+ const drawRow = (row, config) => {
6
+ const { border, drawVerticalLine } = config;
7
+ return drawContent_1.drawContent(row, {
8
+ drawSeparator: drawVerticalLine,
9
+ endSeparator: border.bodyRight,
10
+ middleSeparator: border.bodyJoin,
11
+ startSeparator: border.bodyLeft,
12
+ }) + '\n';
16
13
  };
14
+ exports.drawRow = drawRow;
@@ -1,3 +1,2 @@
1
1
  import type { TableConfig, Row } from './types/internal';
2
- declare const _default: (rows: Row[], columnWidths: number[], rowHeights: number[], config: TableConfig) => string;
3
- export default _default;
2
+ export declare const drawTable: (rows: Row[], columnWidths: number[], rowHeights: number[], config: TableConfig) => string;
package/dist/drawTable.js CHANGED
@@ -1,48 +1,28 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.drawTable = void 0;
6
4
  const drawBorder_1 = require("./drawBorder");
7
- const drawRow_1 = __importDefault(require("./drawRow"));
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) => {
5
+ const drawContent_1 = require("./drawContent");
6
+ const drawRow_1 = require("./drawRow");
7
+ const utils_1 = require("./utils");
8
+ const drawTable = (rows, columnWidths, rowHeights, config) => {
9
+ const { drawHorizontalLine, singleLine, } = config;
10
+ const contents = utils_1.groupBySizes(rows, rowHeights).map((group) => {
29
11
  return group.map((row) => {
30
- return drawRow_1.default(row, config);
12
+ return drawRow_1.drawRow(row, config);
31
13
  }).join('');
32
14
  });
33
- const rowCount = groupedRows.length;
34
- let output = '';
35
- if (drawHorizontalLine(0, rowCount)) {
36
- output += drawBorder_1.drawBorderTop(columnWidths, config);
37
- }
38
- groupedRows.forEach((row, rowIndex) => {
39
- output += row;
40
- if (shouldDrawBorderJoin(rowIndex, rowCount, config)) {
41
- output += drawBorder_1.drawBorderJoin(columnWidths, config);
42
- }
15
+ return drawContent_1.drawContent(contents, {
16
+ drawSeparator: (index, size) => {
17
+ // Top/bottom border
18
+ if (index === 0 || index === size) {
19
+ return drawHorizontalLine(index, size);
20
+ }
21
+ return !singleLine && drawHorizontalLine(index, size);
22
+ },
23
+ endSeparator: drawBorder_1.drawBorderBottom(columnWidths, config),
24
+ middleSeparator: drawBorder_1.drawBorderJoin(columnWidths, config),
25
+ startSeparator: drawBorder_1.drawBorderTop(columnWidths, config),
43
26
  });
44
- if (drawHorizontalLine(rowCount, rowCount)) {
45
- output += drawBorder_1.drawBorderBottom(columnWidths, config);
46
- }
47
- return output;
48
27
  };
28
+ exports.drawTable = drawTable;
@@ -4,10 +4,10 @@ declare function validate43(data: any, { instancePath, parentData, parentDataPro
4
4
  parentDataProperty: any;
5
5
  rootData?: any;
6
6
  }): boolean;
7
- declare function validate70(data: any, { instancePath, parentData, parentDataProperty, rootData }?: {
7
+ declare function validate73(data: any, { instancePath, parentData, parentDataProperty, rootData }?: {
8
8
  instancePath?: string | undefined;
9
9
  parentData: any;
10
10
  parentDataProperty: any;
11
11
  rootData?: any;
12
12
  }): boolean;
13
- export { validate43 as _config_json, validate70 as _streamConfig_json };
13
+ export { validate43 as _config_json, validate73 as _streamConfig_json };