table 6.5.0 → 6.7.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.
Files changed (67) hide show
  1. package/README.md +407 -434
  2. package/dist/alignString.d.ts +2 -3
  3. package/dist/alignString.js +23 -11
  4. package/dist/alignTableData.d.ts +1 -2
  5. package/dist/alignTableData.js +7 -14
  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 +6 -5
  17. package/dist/drawBorder.js +60 -14
  18. package/dist/drawContent.d.ts +9 -0
  19. package/dist/drawContent.js +26 -0
  20. package/dist/drawHeader.d.ts +2 -0
  21. package/dist/drawHeader.js +29 -0
  22. package/dist/drawRow.d.ts +1 -2
  23. package/dist/drawRow.js +16 -12
  24. package/dist/drawTable.d.ts +1 -2
  25. package/dist/drawTable.js +28 -27
  26. package/dist/generated/validators.d.ts +2 -2
  27. package/dist/generated/validators.js +653 -303
  28. package/dist/getBorderCharacters.d.ts +1 -2
  29. package/dist/getBorderCharacters.js +7 -1
  30. package/dist/index.d.ts +3 -3
  31. package/dist/index.js +6 -9
  32. package/dist/makeStreamConfig.d.ts +1 -2
  33. package/dist/makeStreamConfig.js +14 -26
  34. package/dist/{makeConfig.d.ts → makeTableConfig.d.ts} +1 -2
  35. package/dist/{makeConfig.js → makeTableConfig.js} +29 -21
  36. package/dist/mapDataUsingRowHeights.d.ts +2 -0
  37. package/dist/mapDataUsingRowHeights.js +44 -0
  38. package/dist/padTableData.d.ts +2 -2
  39. package/dist/padTableData.js +10 -4
  40. package/dist/stringifyTableData.d.ts +1 -2
  41. package/dist/stringifyTableData.js +7 -2
  42. package/dist/table.d.ts +1 -2
  43. package/dist/table.js +23 -24
  44. package/dist/truncateTableData.d.ts +2 -2
  45. package/dist/truncateTableData.js +10 -6
  46. package/dist/types/api.d.ts +18 -4
  47. package/dist/utils.d.ts +1 -0
  48. package/dist/utils.js +92 -0
  49. package/dist/validateConfig.d.ts +1 -2
  50. package/dist/validateConfig.js +3 -1
  51. package/dist/validateTableData.d.ts +1 -2
  52. package/dist/validateTableData.js +5 -2
  53. package/dist/wrapCell.d.ts +1 -2
  54. package/dist/wrapCell.js +9 -24
  55. package/dist/wrapString.d.ts +1 -2
  56. package/dist/wrapString.js +3 -1
  57. package/dist/wrapWord.d.ts +1 -2
  58. package/dist/wrapWord.js +3 -1
  59. package/package.json +4 -5
  60. package/dist/calculateCellWidthIndex.d.ts +0 -6
  61. package/dist/calculateMaximumColumnWidthIndex.js +0 -24
  62. package/dist/calculateRowHeightIndex.d.ts +0 -6
  63. package/dist/calculateRowHeightIndex.js +0 -21
  64. package/dist/drawHorizontalContent.d.ts +0 -11
  65. package/dist/drawHorizontalContent.js +0 -18
  66. package/dist/mapDataUsingRowHeightIndex.d.ts +0 -3
  67. package/dist/mapDataUsingRowHeightIndex.js +0 -26
@@ -1,3 +1,2 @@
1
1
  import type { BorderConfig } from './types/api';
2
- declare const _default: (name: string) => BorderConfig;
3
- export default _default;
2
+ export declare const getBorderCharacters: (name: string) => BorderConfig;
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  /* eslint-disable sort-keys-fix/sort-keys-fix */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.default = (name) => {
4
+ exports.getBorderCharacters = void 0;
5
+ const getBorderCharacters = (name) => {
5
6
  if (name === 'honeywell') {
6
7
  return {
7
8
  topBody: '═',
@@ -15,6 +16,7 @@ exports.default = (name) => {
15
16
  bodyLeft: '║',
16
17
  bodyRight: '║',
17
18
  bodyJoin: '│',
19
+ headerJoin: '┬',
18
20
  joinBody: '─',
19
21
  joinLeft: '╟',
20
22
  joinRight: '╢',
@@ -34,6 +36,7 @@ exports.default = (name) => {
34
36
  bodyLeft: '│',
35
37
  bodyRight: '│',
36
38
  bodyJoin: '│',
39
+ headerJoin: '┬',
37
40
  joinBody: '─',
38
41
  joinLeft: '├',
39
42
  joinRight: '┤',
@@ -53,6 +56,7 @@ exports.default = (name) => {
53
56
  bodyLeft: '|',
54
57
  bodyRight: '|',
55
58
  bodyJoin: '|',
59
+ headerJoin: '+',
56
60
  joinBody: '-',
57
61
  joinLeft: '|',
58
62
  joinRight: '|',
@@ -72,6 +76,7 @@ exports.default = (name) => {
72
76
  bodyLeft: '',
73
77
  bodyRight: '',
74
78
  bodyJoin: '',
79
+ headerJoin: '',
75
80
  joinBody: '',
76
81
  joinLeft: '',
77
82
  joinRight: '',
@@ -80,3 +85,4 @@ exports.default = (name) => {
80
85
  }
81
86
  throw new Error('Unknown border template "' + name + '".');
82
87
  };
88
+ exports.getBorderCharacters = getBorderCharacters;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import createStream from './createStream';
2
- import getBorderCharacters from './getBorderCharacters';
3
- import table from './table';
1
+ import { createStream } from './createStream';
2
+ import { getBorderCharacters } from './getBorderCharacters';
3
+ import { table } from './table';
4
4
  export { table, createStream, getBorderCharacters, };
5
5
  export * from './types/api';
package/dist/index.js CHANGED
@@ -9,15 +9,12 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
9
9
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
- var __importDefault = (this && this.__importDefault) || function (mod) {
13
- return (mod && mod.__esModule) ? mod : { "default": mod };
14
- };
15
12
  Object.defineProperty(exports, "__esModule", { value: true });
16
13
  exports.getBorderCharacters = exports.createStream = exports.table = void 0;
17
- const createStream_1 = __importDefault(require("./createStream"));
18
- exports.createStream = createStream_1.default;
19
- const getBorderCharacters_1 = __importDefault(require("./getBorderCharacters"));
20
- exports.getBorderCharacters = getBorderCharacters_1.default;
21
- const table_1 = __importDefault(require("./table"));
22
- exports.table = table_1.default;
14
+ const createStream_1 = require("./createStream");
15
+ Object.defineProperty(exports, "createStream", { enumerable: true, get: function () { return createStream_1.createStream; } });
16
+ const getBorderCharacters_1 = require("./getBorderCharacters");
17
+ Object.defineProperty(exports, "getBorderCharacters", { enumerable: true, get: function () { return getBorderCharacters_1.getBorderCharacters; } });
18
+ const table_1 = require("./table");
19
+ Object.defineProperty(exports, "table", { enumerable: true, get: function () { return table_1.table; } });
23
20
  __exportStar(require("./types/api"), exports);
@@ -1,8 +1,7 @@
1
1
  import type { StreamUserConfig } from './types/api';
2
2
  import type { StreamConfig } from './types/internal';
3
- declare const _default: (userConfig: StreamUserConfig) => StreamConfig;
4
3
  /**
5
4
  * Makes a new configuration object out of the userConfig object
6
5
  * using default values for the missing configuration properties.
7
6
  */
8
- export default _default;
7
+ export declare const makeStreamConfig: (userConfig: StreamUserConfig) => StreamConfig;
@@ -3,30 +3,22 @@ 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.makeStreamConfig = void 0;
6
7
  const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
7
- const getBorderCharacters_1 = __importDefault(require("./getBorderCharacters"));
8
- const validateConfig_1 = __importDefault(require("./validateConfig"));
9
- /**
10
- * Merges user provided border characters with the default border ("honeywell") characters.
11
- *
12
- */
13
- const makeBorder = (border) => {
14
- return {
15
- ...getBorderCharacters_1.default('honeywell'),
16
- ...border,
17
- };
18
- };
8
+ const utils_1 = require("./utils");
9
+ const validateConfig_1 = require("./validateConfig");
19
10
  /**
20
11
  * Creates a configuration for every column using default
21
12
  * values for the missing configuration properties.
22
13
  */
23
- const makeColumns = (columnCount, columns = {}, columnDefault) => {
14
+ const makeColumnsConfig = (columnCount, columns = {}, columnDefault) => {
24
15
  return Array.from({ length: columnCount }).map((_, index) => {
25
16
  return {
26
17
  alignment: 'left',
27
18
  paddingLeft: 1,
28
19
  paddingRight: 1,
29
20
  truncate: Number.POSITIVE_INFINITY,
21
+ verticalAlignment: 'top',
30
22
  wrapWord: false,
31
23
  ...columnDefault,
32
24
  ...columns[index],
@@ -37,23 +29,19 @@ const makeColumns = (columnCount, columns = {}, columnDefault) => {
37
29
  * Makes a new configuration object out of the userConfig object
38
30
  * using default values for the missing configuration properties.
39
31
  */
40
- exports.default = (userConfig) => {
41
- var _a;
42
- validateConfig_1.default('streamConfig.json', userConfig);
32
+ const makeStreamConfig = (userConfig) => {
33
+ validateConfig_1.validateConfig('streamConfig.json', userConfig);
43
34
  const config = lodash_clonedeep_1.default(userConfig);
44
- if (!config.columnDefault || !config.columnDefault.width) {
35
+ if (config.columnDefault.width === undefined) {
45
36
  throw new Error('Must provide config.columnDefault.width when creating a stream.');
46
37
  }
47
- if (!config.columnCount) {
48
- throw new Error('Must provide config.columnCount.');
49
- }
50
38
  return {
51
- ...config,
52
- border: makeBorder(config.border),
53
- columnCount: config.columnCount,
54
- columns: makeColumns(config.columnCount, config.columns, config.columnDefault),
55
- drawVerticalLine: (_a = config.drawVerticalLine) !== null && _a !== void 0 ? _a : (() => {
39
+ drawVerticalLine: () => {
56
40
  return true;
57
- }),
41
+ },
42
+ ...config,
43
+ border: utils_1.makeBorderConfig(config.border),
44
+ columns: makeColumnsConfig(config.columnCount, config.columns, config.columnDefault),
58
45
  };
59
46
  };
47
+ exports.makeStreamConfig = makeStreamConfig;
@@ -1,8 +1,7 @@
1
1
  import type { TableUserConfig } from './types/api';
2
2
  import type { Row, TableConfig } from './types/internal';
3
- declare const _default: (rows: Row[], userConfig?: TableUserConfig) => TableConfig;
4
3
  /**
5
4
  * Makes a new configuration object out of the userConfig object
6
5
  * using default values for the missing configuration properties.
7
6
  */
8
- export default _default;
7
+ export declare const makeTableConfig: (rows: Row[], userConfig?: TableUserConfig) => TableConfig;
@@ -3,56 +3,64 @@ 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.makeTableConfig = void 0;
6
7
  const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
7
- const calculateMaximumColumnWidthIndex_1 = __importDefault(require("./calculateMaximumColumnWidthIndex"));
8
- const getBorderCharacters_1 = __importDefault(require("./getBorderCharacters"));
9
- const validateConfig_1 = __importDefault(require("./validateConfig"));
10
- /**
11
- * Merges user provided border characters with the default border ("honeywell") characters.
12
- */
13
- const makeBorder = (border) => {
14
- return {
15
- ...getBorderCharacters_1.default('honeywell'),
16
- ...border,
17
- };
18
- };
8
+ const calculateColumnWidths_1 = __importDefault(require("./calculateColumnWidths"));
9
+ const utils_1 = require("./utils");
10
+ const validateConfig_1 = require("./validateConfig");
19
11
  /**
20
12
  * Creates a configuration for every column using default
21
13
  * values for the missing configuration properties.
22
14
  */
23
- const makeColumns = (rows, columns, columnDefault) => {
24
- const maximumColumnWidthIndex = calculateMaximumColumnWidthIndex_1.default(rows);
25
- return rows[0].map((_cell, index) => {
15
+ const makeColumnsConfig = (rows, columns, columnDefault) => {
16
+ const columnWidths = calculateColumnWidths_1.default(rows);
17
+ return rows[0].map((_, columnIndex) => {
26
18
  return {
27
19
  alignment: 'left',
28
20
  paddingLeft: 1,
29
21
  paddingRight: 1,
30
22
  truncate: Number.POSITIVE_INFINITY,
31
- width: maximumColumnWidthIndex[index],
23
+ verticalAlignment: 'top',
24
+ width: columnWidths[columnIndex],
32
25
  wrapWord: false,
33
26
  ...columnDefault,
34
- ...columns === null || columns === void 0 ? void 0 : columns[index],
27
+ ...columns === null || columns === void 0 ? void 0 : columns[columnIndex],
35
28
  };
36
29
  });
37
30
  };
31
+ const makeHeaderConfig = (config) => {
32
+ if (!config.header) {
33
+ return undefined;
34
+ }
35
+ return {
36
+ alignment: 'center',
37
+ paddingLeft: 1,
38
+ paddingRight: 1,
39
+ truncate: Number.POSITIVE_INFINITY,
40
+ wrapWord: false,
41
+ ...config.header,
42
+ };
43
+ };
38
44
  /**
39
45
  * Makes a new configuration object out of the userConfig object
40
46
  * using default values for the missing configuration properties.
41
47
  */
42
- exports.default = (rows, userConfig = {}) => {
48
+ const makeTableConfig = (rows, userConfig = {}) => {
43
49
  var _a, _b, _c;
44
- validateConfig_1.default('config.json', userConfig);
50
+ validateConfig_1.validateConfig('config.json', userConfig);
45
51
  const config = lodash_clonedeep_1.default(userConfig);
46
52
  return {
47
53
  ...config,
48
- border: makeBorder(config.border),
49
- columns: makeColumns(rows, config.columns, config.columnDefault),
54
+ border: utils_1.makeBorderConfig(config.border),
55
+ columns: makeColumnsConfig(rows, config.columns, config.columnDefault),
50
56
  drawHorizontalLine: (_a = config.drawHorizontalLine) !== null && _a !== void 0 ? _a : (() => {
51
57
  return true;
52
58
  }),
53
59
  drawVerticalLine: (_b = config.drawVerticalLine) !== null && _b !== void 0 ? _b : (() => {
54
60
  return true;
55
61
  }),
62
+ header: makeHeaderConfig(config),
56
63
  singleLine: (_c = config.singleLine) !== null && _c !== void 0 ? _c : false,
57
64
  };
58
65
  };
66
+ exports.makeTableConfig = makeTableConfig;
@@ -0,0 +1,2 @@
1
+ import type { BaseConfig, Row } from './types/internal';
2
+ export declare const mapDataUsingRowHeights: (unmappedRows: Row[], rowHeights: number[], config: BaseConfig) => Row[];
@@ -0,0 +1,44 @@
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;
@@ -1,3 +1,3 @@
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 padString: (input: string, paddingLeft: number, paddingRight: number) => string;
3
+ export declare const padTableData: (rows: Row[], config: BaseConfig) => Row[];
@@ -1,10 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = (rows, config) => {
3
+ exports.padTableData = exports.padString = void 0;
4
+ const padString = (input, paddingLeft, paddingRight) => {
5
+ return ' '.repeat(paddingLeft) + input + ' '.repeat(paddingRight);
6
+ };
7
+ exports.padString = padString;
8
+ const padTableData = (rows, config) => {
4
9
  return rows.map((cells) => {
5
- return cells.map((value, index1) => {
6
- const column = config.columns[index1];
7
- return ' '.repeat(column.paddingLeft) + value + ' '.repeat(column.paddingRight);
10
+ return cells.map((cell, cellIndex) => {
11
+ const { paddingLeft, paddingRight } = config.columns[cellIndex];
12
+ return exports.padString(cell, paddingLeft, paddingRight);
8
13
  });
9
14
  });
10
15
  };
16
+ exports.padTableData = padTableData;
@@ -1,3 +1,2 @@
1
1
  import type { Row } from './types/internal';
2
- declare const _default: (rows: unknown[][]) => Row[];
3
- export default _default;
2
+ export declare const stringifyTableData: (rows: unknown[][]) => Row[];
@@ -1,7 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = (rows) => {
3
+ exports.stringifyTableData = void 0;
4
+ const utils_1 = require("./utils");
5
+ const stringifyTableData = (rows) => {
4
6
  return rows.map((cells) => {
5
- return cells.map(String);
7
+ return cells.map((cell) => {
8
+ return utils_1.normalizeString(String(cell));
9
+ });
6
10
  });
7
11
  };
12
+ exports.stringifyTableData = stringifyTableData;
package/dist/table.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  import type { TableUserConfig } from './types/api';
2
- declare const _default: (data: unknown[][], userConfig?: TableUserConfig) => string;
3
- export default _default;
2
+ export declare const table: (data: unknown[][], userConfig?: TableUserConfig) => string;
package/dist/table.js CHANGED
@@ -1,27 +1,26 @@
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 calculateCellWidthIndex_1 = __importDefault(require("./calculateCellWidthIndex"));
8
- const calculateRowHeightIndex_1 = __importDefault(require("./calculateRowHeightIndex"));
9
- const drawTable_1 = __importDefault(require("./drawTable"));
10
- const makeConfig_1 = __importDefault(require("./makeConfig"));
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"));
15
- const validateTableData_1 = __importDefault(require("./validateTableData"));
16
- exports.default = (data, userConfig = {}) => {
17
- validateTableData_1.default(data);
18
- let rows = stringifyTableData_1.default(data);
19
- const config = makeConfig_1.default(rows, userConfig);
20
- rows = truncateTableData_1.default(rows, config);
21
- const rowHeightIndex = calculateRowHeightIndex_1.default(rows, config);
22
- rows = mapDataUsingRowHeightIndex_1.default(rows, rowHeightIndex, config);
23
- rows = alignTableData_1.default(rows, config);
24
- rows = padTableData_1.default(rows, config);
25
- const cellWidthIndex = calculateCellWidthIndex_1.default(rows[0]);
26
- return drawTable_1.default(rows, cellWidthIndex, rowHeightIndex, config);
3
+ exports.table = void 0;
4
+ const alignTableData_1 = require("./alignTableData");
5
+ const calculateCellWidths_1 = require("./calculateCellWidths");
6
+ const calculateRowHeights_1 = require("./calculateRowHeights");
7
+ const drawTable_1 = require("./drawTable");
8
+ const makeTableConfig_1 = require("./makeTableConfig");
9
+ const mapDataUsingRowHeights_1 = require("./mapDataUsingRowHeights");
10
+ const padTableData_1 = require("./padTableData");
11
+ const stringifyTableData_1 = require("./stringifyTableData");
12
+ const truncateTableData_1 = require("./truncateTableData");
13
+ const validateTableData_1 = require("./validateTableData");
14
+ const table = (data, userConfig = {}) => {
15
+ validateTableData_1.validateTableData(data);
16
+ let rows = stringifyTableData_1.stringifyTableData(data);
17
+ const config = makeTableConfig_1.makeTableConfig(rows, userConfig);
18
+ rows = truncateTableData_1.truncateTableData(rows, config);
19
+ const rowHeights = calculateRowHeights_1.calculateRowHeights(rows, config);
20
+ rows = mapDataUsingRowHeights_1.mapDataUsingRowHeights(rows, rowHeights, config);
21
+ rows = alignTableData_1.alignTableData(rows, config);
22
+ rows = padTableData_1.padTableData(rows, config);
23
+ const cellWidths = calculateCellWidths_1.calculateCellWidths(rows[0]);
24
+ return drawTable_1.drawTable(rows, cellWidths, rowHeights, config);
27
25
  };
26
+ exports.table = table;
@@ -1,6 +1,6 @@
1
1
  import type { BaseConfig, Row } from './types/internal';
2
- declare const _default: (rows: Row[], config: BaseConfig) => Row[];
2
+ export declare const truncateString: (input: string, length: number) => string;
3
3
  /**
4
4
  * @todo Make it work with ASCII content.
5
5
  */
6
- export default _default;
6
+ export declare const truncateTableData: (rows: Row[], config: BaseConfig) => Row[];
@@ -3,17 +3,21 @@ 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.truncateTableData = exports.truncateString = void 0;
6
7
  const lodash_truncate_1 = __importDefault(require("lodash.truncate"));
8
+ const truncateString = (input, length) => {
9
+ return lodash_truncate_1.default(input, { length,
10
+ omission: '…' });
11
+ };
12
+ exports.truncateString = truncateString;
7
13
  /**
8
14
  * @todo Make it work with ASCII content.
9
15
  */
10
- exports.default = (rows, config) => {
16
+ const truncateTableData = (rows, config) => {
11
17
  return rows.map((cells) => {
12
- return cells.map((content, index) => {
13
- return lodash_truncate_1.default(content, {
14
- length: config.columns[index].truncate,
15
- omission: '…',
16
- });
18
+ return cells.map((cell, cellIndex) => {
19
+ return exports.truncateString(cell, config.columns[cellIndex].truncate);
17
20
  });
18
21
  });
19
22
  };
23
+ exports.truncateTableData = truncateTableData;
@@ -10,20 +10,27 @@ export declare type BorderUserConfig = {
10
10
  readonly bottomRight?: string;
11
11
  readonly bottomBody?: string;
12
12
  readonly bottomJoin?: string;
13
- readonly bodyRight?: string;
14
- readonly bodyLeft?: string;
15
- readonly bodyJoin?: string;
16
13
  readonly joinLeft?: string;
17
14
  readonly joinRight?: string;
18
15
  readonly joinBody?: string;
19
16
  readonly joinJoin?: string;
17
+ readonly headerJoin?: string;
18
+ readonly bodyRight?: string;
19
+ readonly bodyLeft?: string;
20
+ readonly bodyJoin?: string;
20
21
  };
21
22
  export declare type BorderConfig = Required<BorderUserConfig>;
23
+ export declare type Alignment = 'center' | 'justify' | 'left' | 'right';
24
+ export declare type VerticalAlignment = 'bottom' | 'middle' | 'top';
22
25
  export declare type ColumnUserConfig = {
23
26
  /**
24
27
  * Cell content horizontal alignment (default: left)
25
28
  */
26
- readonly alignment?: 'center' | 'left' | 'right';
29
+ readonly alignment?: Alignment;
30
+ /**
31
+ * Cell content vertical alignment (default: top)
32
+ */
33
+ readonly verticalAlignment?: VerticalAlignment;
27
34
  /**
28
35
  * Column width (default: auto calculation based on the cell content)
29
36
  */
@@ -45,6 +52,9 @@ export declare type ColumnUserConfig = {
45
52
  */
46
53
  readonly wrapWord?: boolean;
47
54
  };
55
+ export declare type HeaderUserConfig = Omit<ColumnUserConfig, 'verticalAlignment' | 'width'> & {
56
+ readonly content: string;
57
+ };
48
58
  export declare type BaseUserConfig = {
49
59
  /**
50
60
  * Custom border
@@ -66,6 +76,10 @@ export declare type BaseUserConfig = {
66
76
  readonly drawVerticalLine?: DrawVerticalLine;
67
77
  };
68
78
  export declare type TableUserConfig = BaseUserConfig & {
79
+ /**
80
+ * The header configuration
81
+ */
82
+ readonly header?: HeaderUserConfig;
69
83
  /**
70
84
  * Used to tell whether to draw a horizontal line.
71
85
  * This callback is called for each non-content line of the table.
@@ -0,0 +1 @@
1
+ export {};
package/dist/utils.js ADDED
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.distributeUnevenly = exports.countSpaceSequence = exports.groupBySizes = exports.makeBorderConfig = exports.splitAnsi = exports.normalizeString = void 0;
7
+ const slice_ansi_1 = __importDefault(require("slice-ansi"));
8
+ const string_width_1 = __importDefault(require("string-width"));
9
+ const strip_ansi_1 = __importDefault(require("strip-ansi"));
10
+ const getBorderCharacters_1 = require("./getBorderCharacters");
11
+ /**
12
+ * Converts Windows-style newline to Unix-style
13
+ *
14
+ * @internal
15
+ */
16
+ const normalizeString = (input) => {
17
+ return input.replace(/\r\n/g, '\n');
18
+ };
19
+ exports.normalizeString = normalizeString;
20
+ /**
21
+ * Splits ansi string by newlines
22
+ *
23
+ * @internal
24
+ */
25
+ const splitAnsi = (input) => {
26
+ const lengths = strip_ansi_1.default(input).split('\n').map(string_width_1.default);
27
+ const result = [];
28
+ let startIndex = 0;
29
+ lengths.forEach((length) => {
30
+ result.push(length === 0 ? '' : slice_ansi_1.default(input, startIndex, startIndex + length));
31
+ // Plus 1 for the newline character itself
32
+ startIndex += length + 1;
33
+ });
34
+ return result;
35
+ };
36
+ exports.splitAnsi = splitAnsi;
37
+ /**
38
+ * Merges user provided border characters with the default border ("honeywell") characters.
39
+ *
40
+ * @internal
41
+ */
42
+ const makeBorderConfig = (border) => {
43
+ return {
44
+ ...getBorderCharacters_1.getBorderCharacters('honeywell'),
45
+ ...border,
46
+ };
47
+ };
48
+ exports.makeBorderConfig = makeBorderConfig;
49
+ /**
50
+ * Groups the array into sub-arrays by sizes.
51
+ *
52
+ * @internal
53
+ * @example
54
+ * groupBySizes(['a', 'b', 'c', 'd', 'e'], [2, 1, 2]) = [ ['a', 'b'], ['c'], ['d', 'e'] ]
55
+ */
56
+ const groupBySizes = (array, sizes) => {
57
+ let startIndex = 0;
58
+ return sizes.map((size) => {
59
+ const group = array.slice(startIndex, startIndex + size);
60
+ startIndex += size;
61
+ return group;
62
+ });
63
+ };
64
+ exports.groupBySizes = groupBySizes;
65
+ /**
66
+ * Counts the number of continuous spaces in a string
67
+ *
68
+ * @internal
69
+ * @example
70
+ * countGroupSpaces('a bc de f') = 3
71
+ */
72
+ const countSpaceSequence = (input) => {
73
+ var _a, _b;
74
+ return (_b = (_a = input.match(/\s+/g)) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
75
+ };
76
+ exports.countSpaceSequence = countSpaceSequence;
77
+ /**
78
+ * Creates the non-increasing number array given sum and length
79
+ * whose the difference between maximum and minimum is not greater than 1
80
+ *
81
+ * @internal
82
+ * @example
83
+ * distributeUnevenly(6, 3) = [2, 2, 2]
84
+ * distributeUnevenly(8, 3) = [3, 3, 2]
85
+ */
86
+ const distributeUnevenly = (sum, length) => {
87
+ const result = Array.from({ length }).fill(Math.floor(sum / length));
88
+ return result.map((element, index) => {
89
+ return element + (index < sum % length ? 1 : 0);
90
+ });
91
+ };
92
+ exports.distributeUnevenly = distributeUnevenly;
@@ -1,3 +1,2 @@
1
1
  import type { TableUserConfig } from './types/api';
2
- declare const _default: (schemaId: 'config.json' | 'streamConfig.json', config: TableUserConfig) => void;
3
- export default _default;
2
+ export declare const validateConfig: (schemaId: 'config.json' | 'streamConfig.json', config: TableUserConfig) => void;
@@ -3,8 +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.validateConfig = void 0;
6
7
  const validators_1 = __importDefault(require("./generated/validators"));
7
- exports.default = (schemaId, config) => {
8
+ const validateConfig = (schemaId, config) => {
8
9
  const validate = validators_1.default[schemaId];
9
10
  if (!validate(config) && validate.errors) {
10
11
  const errors = validate.errors.map((error) => {
@@ -21,3 +22,4 @@ exports.default = (schemaId, config) => {
21
22
  throw new Error('Invalid config.');
22
23
  }
23
24
  };
25
+ exports.validateConfig = validateConfig;
@@ -1,2 +1 @@
1
- declare const _default: (rows: unknown[][]) => void;
2
- export default _default;
1
+ export declare const validateTableData: (rows: unknown[][]) => void;