console-table-printer 2.14.5 → 2.15.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.
package/README.md CHANGED
@@ -19,8 +19,8 @@ npm install console-table-printer --save
19
19
 
20
20
  ## Basic Example
21
21
 
22
- ```javascript
23
- const { printTable } = require('console-table-printer');
22
+ ```typescript
23
+ import { printTable } from 'console-table-printer';
24
24
 
25
25
  // Create a simple task list
26
26
  const tasks = [
@@ -39,8 +39,8 @@ printTable(tasks);
39
39
 
40
40
  You can also create a Table instance and print it:
41
41
 
42
- ```javascript
43
- const { Table } = require('console-table-printer');
42
+ ```typescript
43
+ import { Table } from 'console-table-printer';
44
44
 
45
45
  // Create a game leaderboard
46
46
  const leaderboard = new Table();
@@ -61,7 +61,9 @@ leaderboard.printTable();
61
61
 
62
62
  You can also put some color to your table like this:
63
63
 
64
- ```javascript
64
+ ```typescript
65
+ import { Table } from 'console-table-printer';
66
+
65
67
  const p = new Table();
66
68
  p.addRow({ item: 'Pizza', price: 12.99, rating: '5/5' }, { color: 'red' });
67
69
  p.addRow({ item: 'Burger', price: 8.99, rating: '4/5' }, { color: 'green' });
@@ -74,7 +76,9 @@ p.printTable();
74
76
 
75
77
  You can also put properties based on columns (color/alignment/title)
76
78
 
77
- ```javascript
79
+ ```typescript
80
+ import { Table } from 'console-table-printer';
81
+
78
82
  const p = new Table({
79
83
  title: 'Project Status',
80
84
  columns: [
@@ -115,13 +119,16 @@ Official documentation has been moved here: [console-table-documentation](https:
115
119
 
116
120
  3. Detailed way of creating table instance
117
121
 
118
- ```javascript
122
+ ```typescript
123
+ import { Table } from 'console-table-printer';
124
+
119
125
  new Table({
120
126
  title: '📊 Sales Report Q4 2024', // A text showsup on top of table (optional)
121
127
  columns: [
122
128
  { name: 'region', alignment: 'left', color: 'blue' }, // with alignment and color
123
129
  { name: 'sales', alignment: 'right', maxLen: 30 }, // lines bigger than this will be splitted in multiple lines
124
130
  { name: 'growth', title: 'Growth %' }, // Title is what will be shown while printing, by default title = name
131
+ { name: 'price', transform: (value) => `$${Number(value).toFixed(2)}` }, // Transform function to format cell values before display
125
132
  ],
126
133
  rows: [
127
134
  { region: 'North America', sales: '$2.5M', growth: '+15%' },
@@ -162,13 +169,13 @@ Check Docs: [color-vals](https://console-table.netlify.app/docs/doc-color)
162
169
 
163
170
  Example usage: To Create a row of color blue
164
171
 
165
- ```js
172
+ ```typescript
166
173
  table.addRow(rowObject, { color: 'blue' });
167
174
  ```
168
175
 
169
176
  Example usage: To apply blue for all rows
170
177
 
171
- ```js
178
+ ```typescript
172
179
  table.addRows(rowsArray, { color: 'blue' });
173
180
  ```
174
181
 
@@ -8,7 +8,7 @@ export default class Table {
8
8
  addColumn(column: string | ColumnOptionsRaw): this;
9
9
  addColumns(columns: string[] | ColumnOptionsRaw[]): this;
10
10
  addRow(text: Dictionary, rowOptions?: RowOptionsRaw): this;
11
- addRows(toBeInsertedRows: any, rowOptions?: RowOptionsRaw): this;
11
+ addRows(toBeInsertedRows: Dictionary[], rowOptions?: RowOptionsRaw): this;
12
12
  printTable(): void;
13
13
  render(): string;
14
14
  }
@@ -15,6 +15,6 @@ const rawColumnToInternalColumn = (column, defaultColumnStyles) => {
15
15
  var _a;
16
16
  return (Object.assign(Object.assign(Object.assign(Object.assign({ name: column.name, title: (_a = column.title) !== null && _a !== void 0 ? _a : column.name }, (0, exports.objIfExists)('color', (column.color || (defaultColumnStyles === null || defaultColumnStyles === void 0 ? void 0 : defaultColumnStyles.color)))), (0, exports.objIfExists)('maxLen', (column.maxLen || (defaultColumnStyles === null || defaultColumnStyles === void 0 ? void 0 : defaultColumnStyles.maxLen)))), (0, exports.objIfExists)('minLen', (column.minLen || (defaultColumnStyles === null || defaultColumnStyles === void 0 ? void 0 : defaultColumnStyles.minLen)))), { alignment: (column.alignment ||
17
17
  (defaultColumnStyles === null || defaultColumnStyles === void 0 ? void 0 : defaultColumnStyles.alignment) ||
18
- table_constants_1.DEFAULT_ROW_ALIGNMENT) }));
18
+ table_constants_1.DEFAULT_ROW_ALIGNMENT), transform: column.transform }));
19
19
  };
20
20
  exports.rawColumnToInternalColumn = rawColumnToInternalColumn;
@@ -1,5 +1,6 @@
1
+ import { Dictionary } from '../models/common';
1
2
  import { ComplexOptions } from '../models/external-table';
2
3
  import TableInternal from './internal-table';
3
4
  export declare const renderTable: (table: TableInternal) => string;
4
- export declare const renderSimpleTable: (rows: any[], tableOptions?: ComplexOptions) => string;
5
- export declare const printSimpleTable: (rows: any[], tableOptions?: ComplexOptions) => void;
5
+ export declare const renderSimpleTable: (rows: Dictionary[], tableOptions?: ComplexOptions) => string;
6
+ export declare const printSimpleTable: (rows: Dictionary[], tableOptions?: ComplexOptions) => void;
@@ -38,10 +38,26 @@ const renderWidthLimitedLines = (tableStyle, columns, row, colorMap, isHeader, c
38
38
  }
39
39
  return ret;
40
40
  };
41
+ const transformRow = (row, columns) => {
42
+ const transformedRow = JSON.parse(JSON.stringify(row));
43
+ const transforms = {};
44
+ columns
45
+ .filter((c) => {
46
+ return !!c.transform;
47
+ })
48
+ .forEach((c) => {
49
+ transforms[c.name] = c.transform;
50
+ });
51
+ Object.keys(transforms).forEach((t) => {
52
+ transformedRow.text[t] = transforms[t](transformedRow.text[t]);
53
+ });
54
+ return transformedRow;
55
+ };
41
56
  // ║ 1 ║ I would like some red wine please ║ 10.212 ║
42
57
  const renderRow = (table, row) => {
43
58
  let ret = [];
44
- ret = ret.concat(renderWidthLimitedLines(table.tableStyle, table.columns, row, table.colorMap, undefined, table.charLength));
59
+ const transformedRow = transformRow(row, table.columns);
60
+ ret = ret.concat(renderWidthLimitedLines(table.tableStyle, table.columns, transformedRow, table.colorMap, undefined, table.charLength));
45
61
  return ret;
46
62
  };
47
63
  /*
@@ -1,5 +1,5 @@
1
1
  import { CharLengthDict, Dictionary, Row } from '../models/common';
2
- import { ColumnOptionsRaw, ComplexOptions, ComputedColumn, DefaultColumnOptions, RowFilterFunction, RowSortFunction } from '../models/external-table';
2
+ import { ColumnOptionsRaw, ComplexOptions, ComputedColumn, DefaultColumnOptions, RowFilterFunction, RowSortFunction, Valuetransform } from '../models/external-table';
3
3
  import { Column, TableStyleDetails } from '../models/internal-table';
4
4
  import { ColorMap } from '../utils/colored-console-line';
5
5
  import { RowOptions } from '../utils/table-helpers';
@@ -12,11 +12,12 @@ declare class TableInternal {
12
12
  sortFunction: RowSortFunction;
13
13
  enabledColumns: string[];
14
14
  disabledColumns: string[];
15
- computedColumns: any[];
15
+ computedColumns: ComputedColumn[];
16
16
  rowSeparator: boolean;
17
17
  colorMap: ColorMap;
18
18
  charLength: CharLengthDict;
19
19
  defaultColumnOptions?: DefaultColumnOptions;
20
+ transforms?: Record<string, Valuetransform>;
20
21
  initSimple(columns: string[]): void;
21
22
  initDetailed(options: ComplexOptions): void;
22
23
  constructor(options?: ComplexOptions | string[]);
@@ -18,7 +18,7 @@ class TableInternal {
18
18
  initDetailed(options) {
19
19
  var _a;
20
20
  this.title = (options === null || options === void 0 ? void 0 : options.title) || this.title;
21
- this.tableStyle = (options === null || options === void 0 ? void 0 : options.style) || this.tableStyle;
21
+ this.tableStyle = Object.assign(Object.assign({}, this.tableStyle), options === null || options === void 0 ? void 0 : options.style);
22
22
  this.sortFunction = (options === null || options === void 0 ? void 0 : options.sort) || this.sortFunction;
23
23
  this.filterFunction = (options === null || options === void 0 ? void 0 : options.filter) || this.filterFunction;
24
24
  this.enabledColumns = (options === null || options === void 0 ? void 0 : options.enabledColumns) || this.enabledColumns;
@@ -55,6 +55,7 @@ class TableInternal {
55
55
  this.colorMap = colored_console_line_1.DEFAULT_COLOR_MAP;
56
56
  this.charLength = {};
57
57
  this.defaultColumnOptions = undefined;
58
+ this.transforms = {};
58
59
  if (options instanceof Array) {
59
60
  this.initSimple(options);
60
61
  }
@@ -1,8 +1,10 @@
1
1
  import { ColorMap } from '../utils/colored-console-line';
2
2
  import { ALIGNMENT, CharLengthDict, COLOR, Dictionary } from './common';
3
- import { TableStyleDetails } from './internal-table';
3
+ import { TableStyleDetails as InternalTableStyleDetails } from './internal-table';
4
4
  export { ALIGNMENT, COLOR };
5
- export type CellValue = string | number | undefined;
5
+ export type TableStyleDetails = Partial<InternalTableStyleDetails>;
6
+ export type CellValue = string | number | boolean | undefined | null;
7
+ export type Valuetransform = (cellValue: CellValue) => CellValue;
6
8
  /**
7
9
  * Configuration options for a table column
8
10
  */
@@ -19,6 +21,8 @@ export interface ColumnOptionsRaw {
19
21
  maxLen?: number;
20
22
  /** Minimum length of text in the column. Shorter text will be padded with spaces */
21
23
  minLen?: number;
24
+ /** Value transform, For example 2.00000 => 2.0 */
25
+ transform?: Valuetransform;
22
26
  }
23
27
  /**
24
28
  * Configuration for a computed column that generates values dynamically
@@ -1,4 +1,5 @@
1
1
  import { ALIGNMENT, COLOR } from './common';
2
+ import { Valuetransform } from './external-table';
2
3
  export interface Column {
3
4
  name: string;
4
5
  title: string;
@@ -7,6 +8,7 @@ export interface Column {
7
8
  length?: number;
8
9
  minLen?: number;
9
10
  maxLen?: number;
11
+ transform?: Valuetransform;
10
12
  }
11
13
  type TableLineDetailsKeys = 'left' | 'right' | 'mid' | 'other';
12
14
  export type TableLineDetails = {
@@ -17,6 +19,6 @@ export type TableStyleDetails = {
17
19
  headerBottom: TableLineDetails;
18
20
  tableBottom: TableLineDetails;
19
21
  vertical: string;
20
- rowSeparator?: TableLineDetails;
22
+ rowSeparator: TableLineDetails;
21
23
  };
22
24
  export {};
@@ -1,6 +1,8 @@
1
1
  import { CharLengthDict, COLOR, Dictionary, Row } from '../models/common';
2
- import { Column } from '../models/internal-table';
3
- export declare const cellText: (text: string | number) => string;
2
+ import { CellValue, Valuetransform } from '../models/external-table';
3
+ import { Column, TableLineDetails } from '../models/internal-table';
4
+ export declare const cellText: (text: CellValue) => string;
5
+ export declare const evaluateCellText: (text: CellValue, transform?: Valuetransform) => string;
4
6
  export interface RowOptionsRaw {
5
7
  color?: string;
6
8
  separator?: boolean;
@@ -9,21 +11,19 @@ export interface RowOptions {
9
11
  color: COLOR;
10
12
  separator: boolean;
11
13
  }
14
+ export interface CreateRowFunction {
15
+ (color: COLOR, text: Dictionary, separator: boolean): Row;
16
+ }
12
17
  export declare const convertRawRowOptionsToStandard: (options?: RowOptionsRaw) => RowOptions | undefined;
13
- export declare const createTableHorizontalBorders: ({ left, mid, right, other, }: {
14
- left: string;
15
- mid: string;
16
- right: string;
17
- other: string;
18
- }, column_lengths: number[]) => string;
18
+ export declare const createTableHorizontalBorders: ({ left, mid, right, other }: TableLineDetails, column_lengths: number[]) => string;
19
19
  export declare const createColumFromOnlyName: (name: string) => {
20
20
  name: string;
21
21
  title: string;
22
22
  };
23
- export declare const createRow: (color: COLOR, text: Dictionary, separator: boolean) => Row;
23
+ export declare const createRow: CreateRowFunction;
24
24
  export declare const findLenOfColumn: (column: Column, rows: Row[], charLength?: CharLengthDict) => number;
25
- export declare const renderTableHorizontalBorders: (style: any, column_lengths: number[]) => string;
26
- export declare const createHeaderAsRow: (createRowFn: any, columns: Column[]) => Row;
25
+ export declare const renderTableHorizontalBorders: (style: TableLineDetails, column_lengths: number[]) => string;
26
+ export declare const createHeaderAsRow: (createRowFn: CreateRowFunction, columns: Column[]) => Row;
27
27
  export declare const getWidthLimitedColumnsArray: (columns: Column[], row: Row, charLength?: CharLengthDict) => {
28
28
  [key: string]: string[];
29
29
  };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getWidthLimitedColumnsArray = exports.createHeaderAsRow = exports.renderTableHorizontalBorders = exports.findLenOfColumn = exports.createRow = exports.createColumFromOnlyName = exports.createTableHorizontalBorders = exports.convertRawRowOptionsToStandard = exports.cellText = void 0;
3
+ exports.getWidthLimitedColumnsArray = exports.createHeaderAsRow = exports.renderTableHorizontalBorders = exports.findLenOfColumn = exports.createRow = exports.createColumFromOnlyName = exports.createTableHorizontalBorders = exports.convertRawRowOptionsToStandard = exports.evaluateCellText = exports.cellText = void 0;
4
4
  const console_utils_1 = require("./console-utils");
5
5
  const string_utils_1 = require("./string-utils");
6
6
  const table_constants_1 = require("./table-constants");
@@ -8,6 +8,9 @@ const max = (a, b) => Math.max(a, b);
8
8
  // takes any input that is given by user and converts to string
9
9
  const cellText = (text) => text === undefined || text === null ? '' : `${text}`;
10
10
  exports.cellText = cellText;
11
+ // evaluate cell text with defined transform
12
+ const evaluateCellText = (text, transform) => (transform ? `${transform(text)}` : (0, exports.cellText)(text));
13
+ exports.evaluateCellText = evaluateCellText;
11
14
  const convertRawRowOptionsToStandard = (options) => {
12
15
  if (options) {
13
16
  return {
@@ -18,7 +21,8 @@ const convertRawRowOptionsToStandard = (options) => {
18
21
  return undefined;
19
22
  };
20
23
  exports.convertRawRowOptionsToStandard = convertRawRowOptionsToStandard;
21
- const createTableHorizontalBorders = ({ left, mid, right, other, }, column_lengths) => {
24
+ // ({ left: "╚", mid: "╩", right: "╝", other: "═" }, [5, 10, 7]) => "╚═══════╩════════════╩═════════╝"
25
+ const createTableHorizontalBorders = ({ left, mid, right, other }, column_lengths) => {
22
26
  // ╚
23
27
  let ret = left;
24
28
  // ╚═══════╩═══════════════════════════════════════╩════════╩
@@ -33,40 +37,49 @@ const createTableHorizontalBorders = ({ left, mid, right, other, }, column_lengt
33
37
  return ret;
34
38
  };
35
39
  exports.createTableHorizontalBorders = createTableHorizontalBorders;
40
+ // ("id") => { name: "id", title: "id" }
36
41
  const createColumFromOnlyName = (name) => ({
37
42
  name,
38
43
  title: name,
39
44
  });
40
45
  exports.createColumFromOnlyName = createColumFromOnlyName;
46
+ // ("green", { id: 1, name: "John" }, true) => { color: "green", separator: true, text: { id: 1, name: "John" } }
41
47
  const createRow = (color, text, separator) => ({
42
48
  color,
43
49
  separator,
44
50
  text,
45
51
  });
46
52
  exports.createRow = createRow;
53
+ // ({ name: "id", title: "ID", minLen: 2 }, [{ text: { id: 1 } }, { text: { id: 100 } }]) => 3
54
+ // Calculates optimal column width based on content and constraints
47
55
  const findLenOfColumn = (column, rows, charLength) => {
48
56
  const columnId = column.name;
49
57
  const columnTitle = column.title;
58
+ const datatransform = column.transform;
50
59
  let length = max(0, (column === null || column === void 0 ? void 0 : column.minLen) || 0);
51
60
  if (column.maxLen) {
52
61
  // if customer input is mentioned a max width, lets see if all other can fit here
53
62
  // if others cant fit find the max word length so that at least the table can be printed
54
63
  length = max(length, max(column.maxLen, (0, string_utils_1.biggestWordInSentence)(columnTitle, charLength)));
55
- length = rows.reduce((acc, row) => max(acc, (0, string_utils_1.biggestWordInSentence)((0, exports.cellText)(row.text[columnId]), charLength)), length);
64
+ length = rows.reduce((acc, row) => max(acc, (0, string_utils_1.biggestWordInSentence)((0, exports.evaluateCellText)(row.text[columnId], datatransform), charLength)), length);
56
65
  return length;
57
66
  }
58
67
  length = max(length, (0, console_utils_1.findWidthInConsole)(columnTitle, charLength));
59
68
  rows.forEach((row) => {
60
- length = max(length, (0, console_utils_1.findWidthInConsole)((0, exports.cellText)(row.text[columnId]), charLength));
69
+ length = max(length, (0, console_utils_1.findWidthInConsole)((0, exports.evaluateCellText)(row.text[columnId], datatransform), charLength));
61
70
  });
62
71
  return length;
63
72
  };
64
73
  exports.findLenOfColumn = findLenOfColumn;
74
+ // ({ left: "╚", mid: "╩", right: "╝", other: "═" }, [5, 10, 7]) => "╚═══════╩════════════╩═════════╝"
75
+ // (undefined, [5, 10, 7]) => ""
65
76
  const renderTableHorizontalBorders = (style, column_lengths) => {
66
77
  const str = (0, exports.createTableHorizontalBorders)(style, column_lengths);
67
78
  return str;
68
79
  };
69
80
  exports.renderTableHorizontalBorders = renderTableHorizontalBorders;
81
+ // (createRow, [{ name: "id", title: "ID" }, { name: "name", title: "Name" }]) =>
82
+ // { color: "white_bold", separator: false, text: { id: "ID", name: "Name" } }
70
83
  const createHeaderAsRow = (createRowFn, columns) => {
71
84
  const headerColor = table_constants_1.DEFAULT_HEADER_FONT_COLOR;
72
85
  const row = createRowFn(headerColor, {}, false);
@@ -76,7 +89,8 @@ const createHeaderAsRow = (createRowFn, columns) => {
76
89
  return row;
77
90
  };
78
91
  exports.createHeaderAsRow = createHeaderAsRow;
79
- // { col1: ['How', 'Is', 'Going'], col2: ['I am', 'Tom'], }
92
+ // ([{ name: "desc", length: 10 }], { text: { desc: "This is a long description" } })
93
+ // => { desc: ["This is a", "long", "description"] }
80
94
  const getWidthLimitedColumnsArray = (columns, row, charLength) => {
81
95
  const ret = {};
82
96
  columns.forEach((column) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "console-table-printer",
3
- "version": "2.14.5",
3
+ "version": "2.15.0",
4
4
  "repository": "github:console-table-printer/console-table-printer",
5
5
  "description": "Printing pretty tables on console log",
6
6
  "main": "dist/index.js",
@@ -26,25 +26,25 @@
26
26
  "author": "Nahiyan Kamal",
27
27
  "license": "MIT",
28
28
  "devDependencies": {
29
- "@eslint/js": "^9.28.0",
29
+ "@eslint/js": "^9.38.0",
30
30
  "@semantic-release/changelog": "^6.0.3",
31
31
  "@semantic-release/git": "^10.0.1",
32
- "@types/jest": "^29.5.14",
33
- "@types/node": "^24.0.3",
34
- "eslint": "^9.28.0",
35
- "eslint-config-prettier": "^10.1.5",
36
- "eslint-plugin-prettier": "^5.4.1",
32
+ "@types/jest": "^30.0.0",
33
+ "@types/node": "^24.8.1",
34
+ "eslint": "^9.38.0",
35
+ "eslint-config-prettier": "^10.1.8",
36
+ "eslint-plugin-prettier": "^5.5.4",
37
37
  "husky": "^9.1.7",
38
- "jest": "^29.7.0",
39
- "prettier": "^3.5.3",
38
+ "jest": "^30.2.0",
39
+ "prettier": "^3.6.2",
40
40
  "pretty-quick": "^4.2.2",
41
- "semantic-release": "^24.2.5",
42
- "ts-jest": "^29.3.4",
43
- "typescript": "^5.8.3",
44
- "typescript-eslint": "^8.33.1"
41
+ "semantic-release": "^25.0.1",
42
+ "ts-jest": "^29.4.5",
43
+ "typescript": "^5.9.3",
44
+ "typescript-eslint": "^8.46.1"
45
45
  },
46
46
  "homepage": "https://console-table.netlify.app",
47
47
  "dependencies": {
48
- "simple-wcswidth": "^1.0.1"
48
+ "simple-wcswidth": "^1.1.2"
49
49
  }
50
50
  }