console-table-printer 2.14.3 → 2.14.5

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
@@ -22,17 +22,18 @@ npm install console-table-printer --save
22
22
  ```javascript
23
23
  const { printTable } = require('console-table-printer');
24
24
 
25
- //Create a table
26
- const testCases = [
27
- { Rank: 3, text: 'I would like some Yellow', value: 100 },
28
- { Rank: 4, text: 'I hope batch update is working', value: 300 },
25
+ // Create a simple task list
26
+ const tasks = [
27
+ { id: 1, task: 'Fix login bug', priority: 'High', status: 'In Progress' },
28
+ { id: 2, task: 'Update documentation', priority: 'Medium', status: 'Done' },
29
+ { id: 3, task: 'Add unit tests', priority: 'High', status: 'Todo' },
29
30
  ];
30
31
 
31
- //print
32
- printTable(testCases);
32
+ // Print the table
33
+ printTable(tasks);
33
34
  ```
34
35
 
35
- ![Screenshot](https://cdn.jsdelivr.net/gh/console-table-printer/console-table-printer@master/static-resources/readme-quick-1.png)
36
+ ![Screenshot](https://cdn.jsdelivr.net/gh/console-table-printer/console-table-printer@master/static-resources/Example-1-basic.png)
36
37
 
37
38
  ## 🚨🚨Announcement🚨🚨 Official Documentation is moved [Here](https://console-table.netlify.app/docs)
38
39
 
@@ -41,65 +42,60 @@ You can also create a Table instance and print it:
41
42
  ```javascript
42
43
  const { Table } = require('console-table-printer');
43
44
 
44
- //Create a table
45
- const p = new Table();
45
+ // Create a game leaderboard
46
+ const leaderboard = new Table();
46
47
 
47
- // add rows with color
48
- p.addRow({ Record: 'a', text: 'red wine please', value: 10.212 });
49
- p.addRow({ Record: 'b', text: 'green gemuse please', value: 20.0 });
50
- p.addRows([
51
- // adding multiple rows are possible
52
- { Record: 'c', text: 'gelb bananen bitte', value: 100 },
53
- { Record: 'd', text: 'update is working', value: 300 },
48
+ // Add players with their scores
49
+ leaderboard.addRow({ rank: 1, player: 'Alice', score: 1250, level: 'Master' });
50
+ leaderboard.addRow({ rank: 2, player: 'Bob', score: 1180, level: 'Expert' });
51
+ leaderboard.addRows([
52
+ { rank: 3, player: 'Charlie', score: 1050, level: 'Advanced' },
53
+ { rank: 4, player: 'Diana', score: 920, level: 'Intermediate' },
54
54
  ]);
55
55
 
56
- //print
57
- p.printTable();
56
+ // Print the leaderboard
57
+ leaderboard.printTable();
58
58
  ```
59
59
 
60
- ![Screenshot](https://cdn.jsdelivr.net/gh/console-table-printer/console-table-printer@master/static-resources/readme-instance-1.png)
60
+ ![Screenshot](https://cdn.jsdelivr.net/gh/console-table-printer/console-table-printer@master/static-resources/Example-2-instance.png)
61
61
 
62
62
  You can also put some color to your table like this:
63
63
 
64
64
  ```javascript
65
65
  const p = new Table();
66
- p.addRow({ description: 'red wine', value: 10.212 }, { color: 'red' });
67
- p.addRow({ description: 'green gemuse', value: 20.0 }, { color: 'green' });
68
- p.addRow({ description: 'gelb bananen', value: 100 }, { color: 'yellow' });
66
+ p.addRow({ item: 'Pizza', price: 12.99, rating: '5/5' }, { color: 'red' });
67
+ p.addRow({ item: 'Burger', price: 8.99, rating: '4/5' }, { color: 'green' });
68
+ p.addRow({ item: 'Ramen', price: 15.99, rating: '5/5' }, { color: 'yellow' });
69
+ p.addRow({ item: 'Salad', price: 6.99, rating: '3/5' }, { color: 'cyan' });
69
70
  p.printTable();
70
71
  ```
71
72
 
72
- ![Screenshot](https://cdn.jsdelivr.net/gh/console-table-printer/console-table-printer@master/static-resources/readme-color-1.png)
73
+ ![Screenshot](https://cdn.jsdelivr.net/gh/console-table-printer/console-table-printer@master/static-resources/Example-3-color.png)
73
74
 
74
75
  You can also put properties based on columns (color/alignment/title)
75
76
 
76
77
  ```javascript
77
78
  const p = new Table({
79
+ title: 'Project Status',
78
80
  columns: [
79
- { name: 'id', alignment: 'left', color: 'blue' }, // with alignment and color
80
- { name: 'text', alignment: 'right' },
81
- { name: 'is_priority_today', title: 'Is This Priority?' }, // with Title as separate Text
81
+ { name: 'id', alignment: 'left', color: 'blue' },
82
+ { name: 'project', alignment: 'left' },
83
+ { name: 'status', title: 'Current Status' },
82
84
  ],
83
85
  colorMap: {
84
- custom_green: '\x1b[32m', // define customized color
86
+ urgent: '\x1b[31m',
87
+ on_track: '\x1b[32m',
85
88
  },
86
89
  });
87
90
 
88
- p.addRow({ id: 1, text: 'red wine', value: 10.212 }, { color: 'green' });
89
- p.addRow(
90
- { id: 2, text: 'green gemuse', value: 20.0 },
91
- { color: 'custom_green' } // your green
92
- );
93
- p.addRow(
94
- { id: 3, text: 'gelb bananen', value: 100, is_priority_today: 'Y' },
95
- { color: 'yellow' }
96
- );
97
- p.addRow({ id: 3, text: 'rosa hemd wie immer', value: 100 }, { color: 'cyan' });
91
+ p.addRow({ id: 1, project: 'Website Redesign', status: 'On Track' }, { color: 'on_track' });
92
+ p.addRow({ id: 2, project: 'Mobile App', status: 'Behind Schedule' }, { color: 'urgent' });
93
+ p.addRow({ id: 3, project: 'API Integration', status: 'Completed' }, { color: 'green' });
98
94
 
99
95
  p.printTable();
100
96
  ```
101
97
 
102
- ![Screenshot](https://cdn.jsdelivr.net/gh/console-table-printer/console-table-printer@master/static-resources/readme-columns-1.png)
98
+ ![Screenshot](https://cdn.jsdelivr.net/gh/console-table-printer/console-table-printer@master/static-resources/Example-4-columns.png)
103
99
 
104
100
  ## CLI
105
101
 
@@ -121,19 +117,23 @@ Official documentation has been moved here: [console-table-documentation](https:
121
117
 
122
118
  ```javascript
123
119
  new Table({
124
- title: 'Title of the Table', // A text showsup on top of table (optoinal)
120
+ title: '📊 Sales Report Q4 2024', // A text showsup on top of table (optional)
125
121
  columns: [
126
- { name: 'column1', alignment: 'left', color: 'red' }, // with alignment and color
127
- { name: 'column2', alignment: 'right', maxLen: 30 }, // lines bigger than this will be splitted in multiple lines
128
- { name: 'column3', title: 'Column3' }, // Title is what will be shown while printing, by default title = name
122
+ { name: 'region', alignment: 'left', color: 'blue' }, // with alignment and color
123
+ { name: 'sales', alignment: 'right', maxLen: 30 }, // lines bigger than this will be splitted in multiple lines
124
+ { name: 'growth', title: 'Growth %' }, // Title is what will be shown while printing, by default title = name
125
+ ],
126
+ rows: [
127
+ { region: 'North America', sales: '$2.5M', growth: '+15%' },
128
+ { region: 'Europe', sales: '$1.8M', growth: '+8%' },
129
+ { region: 'Asia Pacific', sales: '$3.2M', growth: '+22%' },
129
130
  ],
130
- rows: [{ column1: 'row1' }, { column2: 'row2' }, { column3: 'row3' }],
131
- sort: (row1, row2) => row2.column1 - row1.column1, // sorting order of rows (optional), this is normal js sort function for Array.sort
132
- filter: (row) => row.column1 < 3, // filtering rows (optional)
133
- enabledColumns: ['column1'], // array of columns that you want to see, all other will be ignored (optional)
134
- disabledColumns: ['column2'], // array of columns that you DONT want to see, these will always be hidden
131
+ sort: (row1, row2) => row2.sales - row1.sales, // sorting order of rows (optional), this is normal js sort function for Array.sort
132
+ filter: (row) => row.growth > '+10%', // filtering rows (optional)
133
+ enabledColumns: ['region', 'sales'], // array of columns that you want to see, all other will be ignored (optional)
134
+ disabledColumns: ['growth'], // array of columns that you DONT want to see, these will always be hidden
135
135
  colorMap: {
136
- custom_green: '\x1b[32m', // define customized color
136
+ high_growth: '\x1b[32m', // define customized color
137
137
  },
138
138
  charLength: {
139
139
  '👋': 2,
@@ -2,39 +2,94 @@ import { ColorMap } from '../utils/colored-console-line';
2
2
  import { ALIGNMENT, CharLengthDict, COLOR, Dictionary } from './common';
3
3
  import { TableStyleDetails } from './internal-table';
4
4
  export { ALIGNMENT, COLOR };
5
+ export type CellValue = string | number | undefined;
6
+ /**
7
+ * Configuration options for a table column
8
+ */
5
9
  export interface ColumnOptionsRaw {
10
+ /** Unique identifier for the column */
6
11
  name: string;
12
+ /** Display name for the column header. If not provided, uses the name property */
7
13
  title?: string;
14
+ /** Text alignment within the column: 'left', 'center', or 'right' */
8
15
  alignment?: ALIGNMENT;
16
+ /** Text color for the column content */
9
17
  color?: COLOR;
18
+ /** Maximum length of text in the column. Longer text will be wrapped to multiple lines */
10
19
  maxLen?: number;
20
+ /** Minimum length of text in the column. Shorter text will be padded with spaces */
11
21
  minLen?: number;
12
22
  }
23
+ /**
24
+ * Configuration for a computed column that generates values dynamically
25
+ */
13
26
  export interface ComputedColumn extends ColumnOptionsRaw {
14
- function: (arg0: any, index: number, array: any[]) => any;
27
+ /** Function that computes the column value for each row
28
+ * @param arg0 - The current row data object
29
+ * @param index - The index of the current row in the data array
30
+ * @param array - The complete array of row data
31
+ * @returns The computed value for this column
32
+ */
33
+ function: (arg0: Dictionary, index: number, array: Dictionary[]) => CellValue;
15
34
  }
16
- export type RowSortFunction = (row1: any, row2: any) => number;
17
- export type RowFilterFunction = (row: any) => boolean;
35
+ /**
36
+ * Function type for sorting table rows
37
+ * @param row1 - First row to compare
38
+ * @param row2 - Second row to compare
39
+ * @returns Negative number if row1 should come before row2, positive if row2 should come before row1, 0 if equal
40
+ */
41
+ export type RowSortFunction = (row1: Dictionary, row2: Dictionary) => number;
42
+ /**
43
+ * Function type for filtering table rows
44
+ * @param row - The row data to evaluate
45
+ * @returns True if the row should be included, false if it should be filtered out
46
+ */
47
+ export type RowFilterFunction = (row: Dictionary) => boolean;
48
+ /**
49
+ * Default styling options applied to all columns unless overridden
50
+ */
18
51
  export interface DefaultColumnOptions {
52
+ /** Default text alignment for all columns */
19
53
  alignment?: ALIGNMENT;
54
+ /** Default text color for all columns */
20
55
  color?: COLOR;
56
+ /** Default title prefix for all columns */
21
57
  title?: string;
58
+ /** Default maximum length for all columns */
22
59
  maxLen?: number;
60
+ /** Default minimum length for all columns */
23
61
  minLen?: number;
24
62
  }
63
+ /**
64
+ * Complete configuration options for table creation and styling
65
+ */
25
66
  export interface ComplexOptions {
67
+ /** Table styling configuration including borders and colors */
26
68
  style?: TableStyleDetails;
69
+ /** Title displayed at the top of the table */
27
70
  title?: string;
71
+ /** Array of column configurations */
28
72
  columns?: ColumnOptionsRaw[];
73
+ /** Initial data rows for the table */
29
74
  rows?: Dictionary[];
75
+ /** Function to sort rows before display */
30
76
  sort?: RowSortFunction;
77
+ /** Function to filter rows before display */
31
78
  filter?: RowFilterFunction;
79
+ /** Array of column names to include (all others will be hidden) */
32
80
  enabledColumns?: string[];
81
+ /** Array of column names to exclude (these will always be hidden) */
33
82
  disabledColumns?: string[];
83
+ /** Array of computed columns that generate values dynamically */
34
84
  computedColumns?: ComputedColumn[];
85
+ /** Whether to add separator lines between rows */
35
86
  rowSeparator?: boolean;
87
+ /** Whether to disable color output (useful for non-color terminals) */
36
88
  shouldDisableColors?: boolean;
89
+ /** Custom color mapping for special characters or values */
37
90
  colorMap?: ColorMap;
91
+ /** Custom character length mapping for special characters (e.g., emojis) */
38
92
  charLength?: CharLengthDict;
93
+ /** Default options applied to all columns unless overridden */
39
94
  defaultColumnOptions?: DefaultColumnOptions;
40
95
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "console-table-printer",
3
- "version": "2.14.3",
3
+ "version": "2.14.5",
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",
@@ -30,6 +30,7 @@
30
30
  "@semantic-release/changelog": "^6.0.3",
31
31
  "@semantic-release/git": "^10.0.1",
32
32
  "@types/jest": "^29.5.14",
33
+ "@types/node": "^24.0.3",
33
34
  "eslint": "^9.28.0",
34
35
  "eslint-config-prettier": "^10.1.5",
35
36
  "eslint-plugin-prettier": "^5.4.1",