table 6.6.0 → 6.7.3

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.
@@ -13,15 +13,7 @@ const alignRight = (subject, width) => {
13
13
  return ' '.repeat(width) + subject;
14
14
  };
15
15
  const alignCenter = (subject, width) => {
16
- let halfWidth;
17
- halfWidth = width / 2;
18
- if (width % 2 === 0) {
19
- return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth);
20
- }
21
- else {
22
- halfWidth = Math.floor(halfWidth);
23
- return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth + 1);
24
- }
16
+ return ' '.repeat(Math.floor(width / 2)) + subject + ' '.repeat(Math.ceil(width / 2));
25
17
  };
26
18
  const alignJustify = (subject, width) => {
27
19
  const spaceSequenceCount = utils_1.countSpaceSequence(subject);
@@ -43,6 +35,9 @@ const alignJustify = (subject, width) => {
43
35
  */
44
36
  const alignString = (subject, containerWidth, alignment) => {
45
37
  const subjectWidth = string_width_1.default(subject);
38
+ if (subjectWidth === containerWidth) {
39
+ return subject;
40
+ }
46
41
  if (subjectWidth > containerWidth) {
47
42
  throw new Error('Subject parameter value width cannot be greater than the container width.');
48
43
  }
@@ -1,21 +1,12 @@
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.alignTableData = void 0;
7
- const string_width_1 = __importDefault(require("string-width"));
8
4
  const alignString_1 = require("./alignString");
9
5
  const alignTableData = (rows, config) => {
10
6
  return rows.map((row) => {
11
7
  return row.map((cell, cellIndex) => {
12
- const column = config.columns[cellIndex];
13
- if (string_width_1.default(cell) === column.width) {
14
- return cell;
15
- }
16
- else {
17
- return alignString_1.alignString(cell, column.width, column.alignment);
18
- }
8
+ const { width, alignment } = config.columns[cellIndex];
9
+ return alignString_1.alignString(cell, width, alignment);
19
10
  });
20
11
  });
21
12
  };
@@ -1,5 +1,5 @@
1
1
  import type { DrawVerticalLine } from './types/api';
2
- import type { BottomBorderConfig, JoinBorderConfig, TopBorderConfig } from './types/internal';
2
+ import type { TableConfig, SeparatorGetter, TopBorderConfig, JoinBorderConfig, BottomBorderConfig } from './types/internal';
3
3
  declare type Separator = {
4
4
  readonly left: string;
5
5
  readonly right: string;
@@ -22,4 +22,5 @@ declare const drawBorderBottom: (columnWidths: number[], config: {
22
22
  border: BottomBorderConfig;
23
23
  drawVerticalLine: DrawVerticalLine;
24
24
  }) => string;
25
+ export declare const createTableBorderGetter: (columnWidths: number[], config: TableConfig) => SeparatorGetter;
25
26
  export { drawBorder, drawBorderBottom, drawBorderJoin, drawBorderTop, };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.drawBorderTop = exports.drawBorderJoin = exports.drawBorderBottom = exports.drawBorder = void 0;
3
+ exports.drawBorderTop = exports.drawBorderJoin = exports.drawBorderBottom = exports.drawBorder = exports.createTableBorderGetter = void 0;
4
4
  const drawContent_1 = require("./drawContent");
5
5
  const drawBorder = (columnWidths, config) => {
6
6
  const { separator, drawVerticalLine } = config;
@@ -9,9 +9,15 @@ const drawBorder = (columnWidths, config) => {
9
9
  });
10
10
  return drawContent_1.drawContent(columns, {
11
11
  drawSeparator: drawVerticalLine,
12
- endSeparator: separator.right,
13
- middleSeparator: separator.join,
14
- startSeparator: separator.left,
12
+ separatorGetter: (index, columnCount) => {
13
+ if (index === 0) {
14
+ return separator.left;
15
+ }
16
+ if (index === columnCount) {
17
+ return separator.right;
18
+ }
19
+ return separator.join;
20
+ },
15
21
  }) + '\n';
16
22
  };
17
23
  exports.drawBorder = drawBorder;
@@ -55,3 +61,40 @@ const drawBorderBottom = (columnWidths, config) => {
55
61
  });
56
62
  };
57
63
  exports.drawBorderBottom = drawBorderBottom;
64
+ const createTableBorderGetter = (columnWidths, config) => {
65
+ return (index, size) => {
66
+ if (!config.header) {
67
+ if (index === 0) {
68
+ return drawBorderTop(columnWidths, config);
69
+ }
70
+ if (index === size) {
71
+ return drawBorderBottom(columnWidths, config);
72
+ }
73
+ return drawBorderJoin(columnWidths, config);
74
+ }
75
+ // Deal with the header
76
+ if (index === 0) {
77
+ return drawBorderTop(columnWidths, {
78
+ ...config,
79
+ border: {
80
+ ...config.border,
81
+ topJoin: config.border.topBody,
82
+ },
83
+ });
84
+ }
85
+ if (index === 1) {
86
+ return drawBorderJoin(columnWidths, {
87
+ ...config,
88
+ border: {
89
+ ...config.border,
90
+ joinJoin: config.border.headerJoin,
91
+ },
92
+ });
93
+ }
94
+ if (index === size) {
95
+ return drawBorderBottom(columnWidths, config);
96
+ }
97
+ return drawBorderJoin(columnWidths, config);
98
+ };
99
+ };
100
+ exports.createTableBorderGetter = createTableBorderGetter;
@@ -1,8 +1,6 @@
1
1
  declare type SeparatorConfig = {
2
2
  drawSeparator: (index: number, size: number) => boolean;
3
- startSeparator: string;
4
- middleSeparator: string;
5
- endSeparator: string;
3
+ separatorGetter: (index: number, size: number) => string;
6
4
  };
7
5
  /**
8
6
  * Shared function to draw horizontal borders, rows or the entire table
@@ -5,21 +5,21 @@ exports.drawContent = void 0;
5
5
  * Shared function to draw horizontal borders, rows or the entire table
6
6
  */
7
7
  const drawContent = (contents, separatorConfig) => {
8
- const { startSeparator, middleSeparator, endSeparator, drawSeparator } = separatorConfig;
8
+ const { separatorGetter, drawSeparator } = separatorConfig;
9
9
  const contentSize = contents.length;
10
10
  const result = [];
11
11
  if (drawSeparator(0, contentSize)) {
12
- result.push(startSeparator);
12
+ result.push(separatorGetter(0, contentSize));
13
13
  }
14
14
  contents.forEach((content, contentIndex) => {
15
15
  result.push(content);
16
16
  // Only append the middle separator if the content is not the last
17
17
  if (contentIndex + 1 < contentSize && drawSeparator(contentIndex + 1, contentSize)) {
18
- result.push(middleSeparator);
18
+ result.push(separatorGetter(contentIndex + 1, contentSize));
19
19
  }
20
20
  });
21
21
  if (drawSeparator(contentSize, contentSize)) {
22
- result.push(endSeparator);
22
+ result.push(separatorGetter(contentSize, contentSize));
23
23
  }
24
24
  return result.join('');
25
25
  };
@@ -0,0 +1,2 @@
1
+ import type { TableConfig } from './types/internal';
2
+ export declare const drawHeader: (width: number, config: TableConfig) => string;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.drawHeader = void 0;
4
+ const alignString_1 = require("./alignString");
5
+ const drawRow_1 = require("./drawRow");
6
+ const padTableData_1 = require("./padTableData");
7
+ const truncateTableData_1 = require("./truncateTableData");
8
+ const wrapCell_1 = require("./wrapCell");
9
+ const drawHeader = (width, config) => {
10
+ if (!config.header) {
11
+ throw new Error('Can not draw header without header configuration');
12
+ }
13
+ const { alignment, paddingRight, paddingLeft, wrapWord } = config.header;
14
+ let content = config.header.content;
15
+ content = truncateTableData_1.truncateString(content, config.header.truncate);
16
+ const headerLines = wrapCell_1.wrapCell(content, width, wrapWord);
17
+ return headerLines.map((headerLine) => {
18
+ let line = alignString_1.alignString(headerLine, width, alignment);
19
+ line = padTableData_1.padString(line, paddingLeft, paddingRight);
20
+ return drawRow_1.drawRow([line], {
21
+ ...config,
22
+ drawVerticalLine: (index) => {
23
+ const columnCount = config.columns.length;
24
+ return config.drawVerticalLine(index === 0 ? 0 : columnCount, columnCount);
25
+ },
26
+ });
27
+ }).join('');
28
+ };
29
+ exports.drawHeader = drawHeader;
package/dist/drawRow.js CHANGED
@@ -6,9 +6,15 @@ const drawRow = (row, config) => {
6
6
  const { border, drawVerticalLine } = config;
7
7
  return drawContent_1.drawContent(row, {
8
8
  drawSeparator: drawVerticalLine,
9
- endSeparator: border.bodyRight,
10
- middleSeparator: border.bodyJoin,
11
- startSeparator: border.bodyLeft,
9
+ separatorGetter: (index, columnCount) => {
10
+ if (index === 0) {
11
+ return border.bodyLeft;
12
+ }
13
+ if (index === columnCount) {
14
+ return border.bodyRight;
15
+ }
16
+ return border.bodyJoin;
17
+ },
12
18
  }) + '\n';
13
19
  };
14
20
  exports.drawRow = drawRow;
package/dist/drawTable.js CHANGED
@@ -1,8 +1,13 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.drawTable = void 0;
7
+ const string_width_1 = __importDefault(require("string-width"));
4
8
  const drawBorder_1 = require("./drawBorder");
5
9
  const drawContent_1 = require("./drawContent");
10
+ const drawHeader_1 = require("./drawHeader");
6
11
  const drawRow_1 = require("./drawRow");
7
12
  const utils_1 = require("./utils");
8
13
  const drawTable = (rows, columnWidths, rowHeights, config) => {
@@ -12,6 +17,13 @@ const drawTable = (rows, columnWidths, rowHeights, config) => {
12
17
  return drawRow_1.drawRow(row, config);
13
18
  }).join('');
14
19
  });
20
+ if (config.header) {
21
+ // assume that topLeft/right border have width = 1
22
+ const headerWidth = string_width_1.default(drawRow_1.drawRow(rows[0], config)) - 2 -
23
+ config.header.paddingLeft - config.header.paddingRight;
24
+ const header = drawHeader_1.drawHeader(headerWidth, config);
25
+ contents.unshift(header);
26
+ }
15
27
  return drawContent_1.drawContent(contents, {
16
28
  drawSeparator: (index, size) => {
17
29
  // Top/bottom border
@@ -20,9 +32,7 @@ const drawTable = (rows, columnWidths, rowHeights, config) => {
20
32
  }
21
33
  return !singleLine && drawHorizontalLine(index, size);
22
34
  },
23
- endSeparator: drawBorder_1.drawBorderBottom(columnWidths, config),
24
- middleSeparator: drawBorder_1.drawBorderJoin(columnWidths, config),
25
- startSeparator: drawBorder_1.drawBorderTop(columnWidths, config),
35
+ separatorGetter: drawBorder_1.createTableBorderGetter(columnWidths, config),
26
36
  });
27
37
  };
28
38
  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 validate73(data: any, { instancePath, parentData, parentDataProperty, rootData }?: {
7
+ declare function validate76(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, validate73 as _streamConfig_json };
13
+ export { validate43 as _config_json, validate76 as _streamConfig_json };