console-table-printer 2.12.1 → 2.14.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
@@ -1,24 +1,12 @@
1
1
  <h1 align="center">console-table-printer</h1>
2
- <h3 align="center">🖥️🍭Printing Pretty Tables on your console</h3>
3
- <p align="center">
4
- <a href="https://codecov.io/gh/ayonious/console-table-printer">
5
- <img alt="codecov" src="https://codecov.io/gh/ayonious/console-table-printer/branch/master/graph/badge.svg">
6
- </a>
7
- <a href="https://badge.fury.io/js/console-table-printer">
8
- <img alt="npm version" src="https://badge.fury.io/js/console-table-printer.svg">
9
- </a>
10
- <a href="https://packagephobia.now.sh/result?p=console-table-printer">
11
- <img alt="install size" src="https://packagephobia.now.sh/badge?p=console-table-printer@latest">
12
- </a>
13
- </p>
14
- <p align="center">
15
- <a href="https://github.com/prettier/prettier">
16
- <img alt="code style: prettier" src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=plastic">
17
- </a>
18
- <a href="https://github.com/semantic-release/semantic-release">
19
- <img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg">
20
- </a>
21
- </p>
2
+
3
+ > 🖥️🍭Printing Pretty Tables on your console</h3>
4
+
5
+ ![NPM Downloads](https://img.shields.io/npm/dw/console-table-printer)
6
+ [![install size](https://packagephobia.com/badge?p=console-table-printer)](https://packagephobia.com/result?p=console-table-printer)
7
+ [![npm version](https://badge.fury.io/js/console-table-printer.svg)](https://badge.fury.io/js/console-table-printer)
8
+ [![codecov](https://codecov.io/gh/console-table-printer/console-table-printer/graph/badge.svg?token=SWX9VBuYUs)](https://codecov.io/gh/console-table-printer/console-table-printer)
9
+
22
10
 
23
11
  ## Synopsis
24
12
 
@@ -45,7 +33,7 @@ const testCases = [
45
33
  printTable(testCases);
46
34
  ```
47
35
 
48
- ![Screenshot](https://cdn.jsdelivr.net/gh/ayonious/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/readme-quick-1.png)
49
37
 
50
38
  ## 🚨🚨Announcement🚨🚨 Official Documentation is moved [Here](https://console-table.netlify.app/docs)
51
39
 
@@ -70,7 +58,7 @@ p.addRows([
70
58
  p.printTable();
71
59
  ```
72
60
 
73
- ![Screenshot](https://cdn.jsdelivr.net/gh/ayonious/console-table-printer@master/static-resources/readme-instance-1.png)
61
+ ![Screenshot](https://cdn.jsdelivr.net/gh/console-table-printer/console-table-printer@master/static-resources/readme-instance-1.png)
74
62
 
75
63
  You can also put some color to your table like this:
76
64
 
@@ -82,7 +70,7 @@ p.addRow({ description: 'gelb bananen', value: 100 }, { color: 'yellow' });
82
70
  p.printTable();
83
71
  ```
84
72
 
85
- ![Screenshot](https://cdn.jsdelivr.net/gh/ayonious/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/readme-color-1.png)
86
74
 
87
75
  You can also put properties based on columns (color/alignment/title)
88
76
 
@@ -112,7 +100,7 @@ p.addRow({ id: 3, text: 'rosa hemd wie immer', value: 100 }, { color: 'cyan' });
112
100
  p.printTable();
113
101
  ```
114
102
 
115
- ![Screenshot](https://cdn.jsdelivr.net/gh/ayonious/console-table-printer@master/static-resources/readme-columns-1.png)
103
+ ![Screenshot](https://cdn.jsdelivr.net/gh/console-table-printer/console-table-printer@master/static-resources/readme-columns-1.png)
116
104
 
117
105
  ## CLI
118
106
 
@@ -152,6 +140,12 @@ new Table({
152
140
  '👋': 2,
153
141
  '😅': 2,
154
142
  }, // custom len of chars in console
143
+ defaultColumnOptions: {
144
+ alignment: 'center',
145
+ color: 'red',
146
+ maxLen: 40,
147
+ minLen: 20,
148
+ },
155
149
  });
156
150
  ```
157
151
 
@@ -189,4 +183,4 @@ You can get color / alignment as types. Check Docs: [types-docs](https://console
189
183
 
190
184
  ## License
191
185
 
192
- [MIT](https://github.com/ayonious/console-table-printer/blob/master/LICENSE)
186
+ [MIT](https://github.com/console-table-printer/console-table-printer/blob/master/LICENSE)
@@ -1,12 +1,12 @@
1
1
  import TableInternal from './internalTable/internal-table';
2
2
  import { Dictionary } from './models/common';
3
- import { ComplexOptions } from './models/external-table';
3
+ import { ColumnOptionsRaw, ComplexOptions } from './models/external-table';
4
4
  import { RowOptionsRaw } from './utils/table-helpers';
5
5
  export default class Table {
6
6
  table: TableInternal;
7
7
  constructor(options?: ComplexOptions | string[]);
8
- addColumn(column: string): this;
9
- addColumns(columns: string[]): this;
8
+ addColumn(column: string | ColumnOptionsRaw): this;
9
+ addColumns(columns: string[] | ColumnOptionsRaw[]): this;
10
10
  addRow(text: Dictionary, rowOptions?: RowOptionsRaw): this;
11
11
  addRows(toBeInsertedRows: any, rowOptions?: RowOptionsRaw): this;
12
12
  printTable(): void;
@@ -1,6 +1,12 @@
1
+ import { ALIGNMENT, COLOR } from '../models/common';
1
2
  import { ColumnOptionsRaw } from '../models/external-table';
2
3
  import { Column } from '../models/internal-table';
3
4
  export declare const objIfExists: (key: string, val: any) => {
4
5
  [x: string]: any;
5
6
  };
6
- export declare const rawColumnToInternalColumn: (column: ColumnOptionsRaw) => Column;
7
+ export declare const rawColumnToInternalColumn: (column: ColumnOptionsRaw, defaultColumnStyles?: {
8
+ alignment?: ALIGNMENT;
9
+ color?: COLOR;
10
+ maxLen?: number;
11
+ minLen?: number;
12
+ }) => Column;
@@ -11,8 +11,8 @@ const objIfExists = (key, val) => {
11
11
  };
12
12
  };
13
13
  exports.objIfExists = objIfExists;
14
- const rawColumnToInternalColumn = (column) => {
14
+ const rawColumnToInternalColumn = (column, defaultColumnStyles) => {
15
15
  var _a;
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)), (0, exports.objIfExists)('maxLen', column.maxLen)), (0, exports.objIfExists)('minLen', column.minLen)), { alignment: column.alignment || table_constants_1.DEFAULT_ROW_ALIGNMENT }));
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 || (defaultColumnStyles === null || defaultColumnStyles === void 0 ? void 0 : defaultColumnStyles.alignment) || table_constants_1.DEFAULT_ROW_ALIGNMENT) }));
17
17
  };
18
18
  exports.rawColumnToInternalColumn = rawColumnToInternalColumn;
@@ -1,4 +1,5 @@
1
+ import { ComplexOptions } from '../models/external-table';
1
2
  import TableInternal from './internal-table';
2
3
  export declare const renderTable: (table: TableInternal) => string;
3
- export declare const renderSimpleTable: (rows: any[]) => string;
4
- export declare const printSimpleTable: (rows: any[]) => void;
4
+ export declare const renderSimpleTable: (rows: any[], tableOptions?: ComplexOptions) => string;
5
+ export declare const printSimpleTable: (rows: any[], tableOptions?: ComplexOptions) => void;
@@ -114,13 +114,13 @@ const renderTable = (table) => {
114
114
  return ret.join('\n');
115
115
  };
116
116
  exports.renderTable = renderTable;
117
- const renderSimpleTable = (rows) => {
118
- const table = new internal_table_1.default();
117
+ const renderSimpleTable = (rows, tableOptions) => {
118
+ const table = new internal_table_1.default(tableOptions);
119
119
  table.addRows(rows);
120
120
  return (0, exports.renderTable)(table);
121
121
  };
122
122
  exports.renderSimpleTable = renderSimpleTable;
123
- const printSimpleTable = (rows) => {
124
- console.log((0, exports.renderSimpleTable)(rows));
123
+ const printSimpleTable = (rows, tableOptions) => {
124
+ console.log((0, exports.renderSimpleTable)(rows, tableOptions));
125
125
  };
126
126
  exports.printSimpleTable = printSimpleTable;
@@ -1,5 +1,5 @@
1
1
  import { CharLengthDict, Dictionary, Row } from '../models/common';
2
- import { ComplexOptions, ComputedColumn, RowFilterFunction, RowSortFunction } from '../models/external-table';
2
+ import { ColumnOptionsRaw, ComplexOptions, ComputedColumn, RowFilterFunction, RowSortFunction } 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';
@@ -20,8 +20,8 @@ declare class TableInternal {
20
20
  initDetailed(options: ComplexOptions): void;
21
21
  constructor(options?: ComplexOptions | string[]);
22
22
  createColumnFromRow(text: Dictionary): void;
23
- addColumn(textOrObj: string | ComputedColumn): void;
24
- addColumns(toBeInsertedColumns: string[]): void;
23
+ addColumn(textOrObj: string | ComputedColumn | ColumnOptionsRaw): void;
24
+ addColumns(toBeInsertedColumns: string[] | ColumnOptionsRaw[]): void;
25
25
  addRow(text: Dictionary, options?: RowOptions): void;
26
26
  addRows(toBeInsertedRows: Dictionary[], options?: RowOptions): void;
27
27
  renderTable(): string;
@@ -25,7 +25,7 @@ class TableInternal {
25
25
  this.disabledColumns = (options === null || options === void 0 ? void 0 : options.disabledColumns) || this.disabledColumns;
26
26
  this.computedColumns = (options === null || options === void 0 ? void 0 : options.computedColumns) || this.computedColumns;
27
27
  this.columns =
28
- ((_a = options === null || options === void 0 ? void 0 : options.columns) === null || _a === void 0 ? void 0 : _a.map(input_converter_1.rawColumnToInternalColumn)) || this.columns;
28
+ ((_a = options === null || options === void 0 ? void 0 : options.columns) === null || _a === void 0 ? void 0 : _a.map((column) => (0, input_converter_1.rawColumnToInternalColumn)(column, options === null || options === void 0 ? void 0 : options.defaultColumnOptions))) || this.columns;
29
29
  this.rowSeparator = (options === null || options === void 0 ? void 0 : options.rowSeparator) || this.rowSeparator;
30
30
  this.charLength = (options === null || options === void 0 ? void 0 : options.charLength) || this.charLength;
31
31
  if (options === null || options === void 0 ? void 0 : options.shouldDisableColors) {
@@ -71,9 +71,12 @@ class TableInternal {
71
71
  if (typeof textOrObj === 'string') {
72
72
  this.columns.push((0, table_helpers_1.createColumFromOnlyName)(textOrObj));
73
73
  }
74
- else {
74
+ else if ('function' in textOrObj) { // Only way to know if this is a computed column
75
75
  this.columns.push((0, table_helpers_1.createColumFromComputedColumn)(textOrObj));
76
76
  }
77
+ else {
78
+ this.columns.push((0, input_converter_1.rawColumnToInternalColumn)(textOrObj));
79
+ }
77
80
  }
78
81
  addColumns(toBeInsertedColumns) {
79
82
  toBeInsertedColumns.forEach((toBeInsertedColumn) => {
@@ -5,9 +5,15 @@ const table_helpers_1 = require("../utils/table-helpers");
5
5
  const createComputedColumnsIfNecessary = (table) => {
6
6
  if (table.computedColumns.length) {
7
7
  table.computedColumns.forEach((computedColumn) => {
8
+ // This can happen if renderTable/printTable is called multiple times
9
+ const isColumnAlreadyExists = table.columns.some((col) => col.name === computedColumn.name);
10
+ if (isColumnAlreadyExists) {
11
+ return;
12
+ }
8
13
  table.addColumn(computedColumn);
9
- table.rows.forEach((row) => {
10
- row.text[computedColumn.name] = computedColumn.function(row.text);
14
+ table.rows.forEach((row, index, rowsArray) => {
15
+ const arrayRowText = rowsArray.map((elemInRowsArray) => elemInRowsArray.text);
16
+ row.text[computedColumn.name] = computedColumn.function(row.text, index, arrayRowText);
11
17
  });
12
18
  });
13
19
  }
@@ -11,7 +11,7 @@ export interface ColumnOptionsRaw {
11
11
  minLen?: number;
12
12
  }
13
13
  export interface ComputedColumn extends ColumnOptionsRaw {
14
- function: (arg0: any) => any;
14
+ function: (arg0: any, index: number, array: any[]) => any;
15
15
  }
16
16
  export type RowSortFunction = (row1: any, row2: any) => number;
17
17
  export type RowFilterFunction = (row: any) => Boolean;
@@ -29,4 +29,11 @@ export interface ComplexOptions {
29
29
  shouldDisableColors?: boolean;
30
30
  colorMap?: ColorMap;
31
31
  charLength?: CharLengthDict;
32
+ defaultColumnOptions?: {
33
+ alignment?: ALIGNMENT;
34
+ color?: COLOR;
35
+ title?: string;
36
+ maxLen?: number;
37
+ minLen?: number;
38
+ };
32
39
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "console-table-printer",
3
- "version": "2.12.1",
4
- "repository": "github:ayonious/console-table-printer",
3
+ "version": "2.14.0",
4
+ "repository": "github:console-table-printer/console-table-printer",
5
5
  "description": "Printing pretty tables on console log",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -27,21 +27,21 @@
27
27
  "devDependencies": {
28
28
  "@semantic-release/changelog": "^6.0.3",
29
29
  "@semantic-release/git": "^10.0.1",
30
- "@types/jest": "^29.5.12",
31
- "@typescript-eslint/eslint-plugin": "^7.13.1",
32
- "@typescript-eslint/parser": "^7.13.1",
33
- "eslint": "^9.5.0",
30
+ "@types/jest": "^29.5.14",
31
+ "@typescript-eslint/eslint-plugin": "^8.32.1",
32
+ "@typescript-eslint/parser": "^8.32.1",
33
+ "eslint": "^9.27.0",
34
34
  "eslint-config-airbnb-base": "^15.0.0",
35
- "eslint-config-prettier": "^9.1.0",
36
- "eslint-plugin-import": "^2.29.1",
37
- "eslint-plugin-prettier": "^5.1.3",
38
- "husky": "^9.0.11",
35
+ "eslint-config-prettier": "^10.1.5",
36
+ "eslint-plugin-import": "^2.31.0",
37
+ "eslint-plugin-prettier": "^5.4.0",
38
+ "husky": "^9.1.7",
39
39
  "jest": "^29.7.0",
40
- "prettier": "^3.3.2",
41
- "pretty-quick": "^4.0.0",
42
- "semantic-release": "^24.0.0",
43
- "ts-jest": "^29.1.5",
44
- "typescript": "^5.4.5"
40
+ "prettier": "^3.5.3",
41
+ "pretty-quick": "^4.1.1",
42
+ "semantic-release": "^24.2.5",
43
+ "ts-jest": "^29.3.4",
44
+ "typescript": "^5.8.3"
45
45
  },
46
46
  "homepage": "https://console-table.netlify.app",
47
47
  "dependencies": {