console-table-printer 2.11.0 β†’ 2.11.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.
package/README.md CHANGED
@@ -45,10 +45,10 @@ const testCases = [
45
45
  printTable(testCases);
46
46
  ```
47
47
 
48
- Output:
49
-
50
48
  ![Screenshot](https://cdn.jsdelivr.net/gh/ayonious/console-table-printer@master/static-resources/quick-print.png)
51
49
 
50
+ ## 🚨🚨Announcement🚨🚨 Official Documentation is moved [Here](https://console-table.netlify.app/docs)
51
+
52
52
  You can also create a Table instance and print it:
53
53
 
54
54
  ```javascript
@@ -147,12 +147,19 @@ new Table({
147
147
  filter: (row) => row.column1 < 3, // filtering rows (optional)
148
148
  enabledColumns: ['column1'], // array of columns that you want to see, all other will be ignored (optional)
149
149
  disabledColumns: ['column2'], // array of columns that you DONT want to see, these will always be hidden
150
+ colorMap: {
151
+ custom_green: '\x1b[32m', // define customized color
152
+ },
153
+ charLength: {
154
+ 'πŸ‘‹': 2,
155
+ 'πŸ˜…': 2,
156
+ }, // custom len of chars in console
150
157
  });
151
158
  ```
152
159
 
153
160
  ### Functions
154
161
 
155
- - `addRow(rowObjet, options)` adding single row.
162
+ - `addRow(rowObjet, options)` adding single row. This can be chained
156
163
  - `addRows(rowObjects, options)` adding multiple rows. array of row object. This case options will be applied to all the objects in row
157
164
  - `addColumn(columnObject)` adding single column
158
165
  - `addColumns(columnObjects)` adding multiple columns
@@ -11,29 +11,29 @@ const table_helpers_1 = require("../utils/table-helpers");
11
11
  const internal_table_1 = __importDefault(require("./internal-table"));
12
12
  const table_pre_processors_1 = require("./table-pre-processors");
13
13
  // β•‘ Index β•‘ β•‘ β•‘
14
- const renderOneLine = (tableStyle, columns, currentLineIndex, widthLimitedColumnsArray, isHeader, row, colorMap) => {
14
+ const renderOneLine = (tableStyle, columns, currentLineIndex, widthLimitedColumnsArray, isHeader, row, colorMap, charLength) => {
15
15
  const line = new colored_console_line_1.default(colorMap);
16
- line.addCharsWithColor(table_constants_1.DEFAULT_ROW_FONT_COLOR, tableStyle.vertical);
16
+ line.addCharsWithColor('', tableStyle.vertical); // dont Color the Column borders
17
17
  columns.forEach((column) => {
18
18
  const thisLineHasText = currentLineIndex < widthLimitedColumnsArray[column.name].length;
19
19
  const textForThisLine = thisLineHasText
20
20
  ? (0, table_helpers_1.cellText)(widthLimitedColumnsArray[column.name][currentLineIndex])
21
21
  : '';
22
22
  line.addCharsWithColor(table_constants_1.DEFAULT_ROW_FONT_COLOR, ' ');
23
- line.addCharsWithColor((isHeader && table_constants_1.DEFAULT_HEADER_FONT_COLOR) || column.color || row.color, (0, string_utils_1.textWithPadding)(textForThisLine, column.alignment || table_constants_1.DEFAULT_ROW_ALIGNMENT, column.length || table_constants_1.DEFAULT_COLUMN_LEN));
24
- line.addCharsWithColor(table_constants_1.DEFAULT_ROW_FONT_COLOR, ` ${tableStyle.vertical}`);
23
+ line.addCharsWithColor((isHeader && table_constants_1.DEFAULT_HEADER_FONT_COLOR) || column.color || row.color, (0, string_utils_1.textWithPadding)(textForThisLine, column.alignment || table_constants_1.DEFAULT_ROW_ALIGNMENT, column.length || table_constants_1.DEFAULT_COLUMN_LEN, charLength));
24
+ line.addCharsWithColor('', ` ${tableStyle.vertical}`); // dont Color the Column borders
25
25
  });
26
26
  return line.renderConsole();
27
27
  };
28
28
  // β•‘ Bold β•‘ text β•‘ value β•‘
29
29
  // β•‘ Index β•‘ β•‘ β•‘
30
- const renderWidthLimitedLines = (tableStyle, columns, row, colorMap, isHeader) => {
30
+ const renderWidthLimitedLines = (tableStyle, columns, row, colorMap, isHeader, charLength) => {
31
31
  // { col1: ['How', 'Is', 'Going'], col2: ['I am', 'Tom'], }
32
- const widthLimitedColumnsArray = (0, table_helpers_1.getWidthLimitedColumnsArray)(columns, row);
32
+ const widthLimitedColumnsArray = (0, table_helpers_1.getWidthLimitedColumnsArray)(columns, row, charLength);
33
33
  const totalLines = Object.values(widthLimitedColumnsArray).reduce((a, b) => Math.max(a, b.length), 0);
34
34
  const ret = [];
35
35
  for (let currentLineIndex = 0; currentLineIndex < totalLines; currentLineIndex += 1) {
36
- const singleLine = renderOneLine(tableStyle, columns, currentLineIndex, widthLimitedColumnsArray, isHeader, row, colorMap);
36
+ const singleLine = renderOneLine(tableStyle, columns, currentLineIndex, widthLimitedColumnsArray, isHeader, row, colorMap, charLength);
37
37
  ret.push(singleLine);
38
38
  }
39
39
  return ret;
@@ -41,7 +41,7 @@ const renderWidthLimitedLines = (tableStyle, columns, row, colorMap, isHeader) =
41
41
  // β•‘ 1 β•‘ I would like some red wine please β•‘ 10.212 β•‘
42
42
  const renderRow = (table, row) => {
43
43
  let ret = [];
44
- ret = ret.concat(renderWidthLimitedLines(table.tableStyle, table.columns, row, table.colorMap));
44
+ ret = ret.concat(renderWidthLimitedLines(table.tableStyle, table.columns, row, table.colorMap, undefined, table.charLength));
45
45
  return ret;
46
46
  };
47
47
  /*
@@ -1,4 +1,4 @@
1
- import { Dictionary, Row } from '../models/common';
1
+ import { CharLengthDict, Dictionary, Row } from '../models/common';
2
2
  import { 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';
@@ -15,6 +15,7 @@ declare class TableInternal {
15
15
  computedColumns: any[];
16
16
  rowSeparator: boolean;
17
17
  colorMap: ColorMap;
18
+ charLength: CharLengthDict;
18
19
  initSimple(columns: string[]): void;
19
20
  initDetailed(options: ComplexOptions): void;
20
21
  constructor(options?: ComplexOptions | string[]);
@@ -21,6 +21,7 @@ class TableInternal {
21
21
  this.computedColumns = [];
22
22
  this.rowSeparator = table_constants_1.DEFAULT_ROW_SEPARATOR;
23
23
  this.colorMap = colored_console_line_1.DEFAULT_COLOR_MAP;
24
+ this.charLength = {};
24
25
  if (options instanceof Array) {
25
26
  this.initSimple(options);
26
27
  }
@@ -47,6 +48,7 @@ class TableInternal {
47
48
  this.columns =
48
49
  ((_a = options === null || options === void 0 ? void 0 : options.columns) === null || _a === void 0 ? void 0 : _a.map(input_converter_1.rawColumnToInternalColumn)) || this.columns;
49
50
  this.rowSeparator = (options === null || options === void 0 ? void 0 : options.rowSeparator) || this.rowSeparator;
51
+ this.charLength = (options === null || options === void 0 ? void 0 : options.charLength) || this.charLength;
50
52
  if (options === null || options === void 0 ? void 0 : options.colorMap) {
51
53
  this.colorMap = Object.assign(Object.assign({}, this.colorMap), options.colorMap);
52
54
  }
@@ -24,7 +24,7 @@ const enableColumnsIfNecessary = (table) => {
24
24
  };
25
25
  const findColumnWidth = (table) => {
26
26
  table.columns.forEach((column) => {
27
- column.length = (0, table_helpers_1.findLenOfColumn)(column, table.rows);
27
+ column.length = (0, table_helpers_1.findLenOfColumn)(column, table.rows, table.charLength);
28
28
  });
29
29
  };
30
30
  const preProcessColumns = (table) => {
@@ -4,6 +4,9 @@ export declare type COLOR = typeof COLORS[number];
4
4
  export interface Dictionary {
5
5
  [key: string]: any;
6
6
  }
7
+ export interface CharLengthDict {
8
+ [key: string]: number;
9
+ }
7
10
  export interface Row {
8
11
  color: COLOR;
9
12
  separator: boolean;
@@ -1,5 +1,5 @@
1
1
  import { ColorMap } from '../utils/colored-console-line';
2
- import { ALIGNMENT, COLOR, Dictionary } from './common';
2
+ import { ALIGNMENT, CharLengthDict, COLOR, Dictionary } from './common';
3
3
  import { TableStyleDetails } from './internal-table';
4
4
  export { ALIGNMENT, COLOR };
5
5
  export interface ColumnOptionsRaw {
@@ -27,4 +27,5 @@ export interface ComplexOptions {
27
27
  computedColumns?: ComputedColumn[];
28
28
  rowSeparator?: boolean;
29
29
  colorMap?: ColorMap;
30
+ charLength?: CharLengthDict;
30
31
  }
@@ -1,2 +1,3 @@
1
- declare const findWidthInConsole: (str: string) => number;
1
+ import { CharLengthDict } from '../models/common';
2
+ export declare const findWidthInConsole: (str: string, charLength?: CharLengthDict) => number;
2
3
  export default findWidthInConsole;
@@ -1,8 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.findWidthInConsole = void 0;
3
4
  const simple_wcswidth_1 = require("simple-wcswidth");
4
5
  /* eslint-disable no-control-regex */
5
6
  const colorRegex = /\x1b\[\d{1,3}m/g; // \x1b[30m \x1b[305m
6
7
  const stripAnsi = (str) => str.replace(colorRegex, '');
7
- const findWidthInConsole = (str) => (0, simple_wcswidth_1.wcswidth)(stripAnsi(str));
8
- exports.default = findWidthInConsole;
8
+ const findWidthInConsole = (str, charLength) => {
9
+ let strLen = 0;
10
+ str = stripAnsi(str);
11
+ if (charLength) {
12
+ Object.entries(charLength).forEach(([key, value]) => {
13
+ // count appearance of the key in the string and remove from original string
14
+ let regex = new RegExp(key, 'g');
15
+ strLen += (str.match(regex) || []).length * value;
16
+ str = str.replace(key, '');
17
+ });
18
+ }
19
+ strLen += (0, simple_wcswidth_1.wcswidth)(str);
20
+ return strLen;
21
+ };
22
+ exports.findWidthInConsole = findWidthInConsole;
23
+ exports.default = exports.findWidthInConsole;
@@ -1,4 +1,4 @@
1
- import { ALIGNMENT } from '../models/common';
2
- export declare const textWithPadding: (text: string, alignment: ALIGNMENT, columnLen: number) => string;
3
- export declare const limitWidth: (inpStr: string, width: number) => string[];
4
- export declare const biggestWordInSentence: (inpStr: string) => number;
1
+ import { ALIGNMENT, CharLengthDict } from '../models/common';
2
+ export declare const textWithPadding: (text: string, alignment: ALIGNMENT, columnLen: number, charLength?: CharLengthDict) => string;
3
+ export declare const limitWidth: (inpStr: string, width: number, charLength?: CharLengthDict) => string[];
4
+ export declare const biggestWordInSentence: (inpStr: string, charLength?: CharLengthDict) => number;
@@ -7,11 +7,15 @@ exports.biggestWordInSentence = exports.limitWidth = exports.textWithPadding = v
7
7
  const console_utils_1 = __importDefault(require("./console-utils"));
8
8
  // ("How are you?",center, 20) => " How are you? "
9
9
  // ("How are you?",right, 20) => " How are you?"
10
- const textWithPadding = (text, alignment, columnLen) => {
11
- const curTextSize = (0, console_utils_1.default)(text);
10
+ const textWithPadding = (text, alignment, columnLen, charLength) => {
11
+ const curTextSize = (0, console_utils_1.default)(text, charLength);
12
12
  // alignments for center padding case
13
13
  const leftPadding = Math.floor((columnLen - curTextSize) / 2);
14
14
  const rightPadding = columnLen - leftPadding - curTextSize;
15
+ // handle edge cases where the text size is larger than the column length
16
+ if (columnLen < curTextSize) {
17
+ return text;
18
+ }
15
19
  // console.log(text, columnLen, curTextSize);
16
20
  switch (alignment) {
17
21
  case 'left':
@@ -28,13 +32,13 @@ const textWithPadding = (text, alignment, columnLen) => {
28
32
  };
29
33
  exports.textWithPadding = textWithPadding;
30
34
  // ("How are you?",10) => ["How are ", "you?"]
31
- const limitWidth = (inpStr, width) => {
35
+ const limitWidth = (inpStr, width, charLength) => {
32
36
  const ret = [];
33
37
  const spaceSeparatedStrings = inpStr.split(' ');
34
38
  let now = [];
35
39
  let cnt = 0;
36
40
  spaceSeparatedStrings.forEach((strWithoutSpace) => {
37
- const consoleWidth = (0, console_utils_1.default)(strWithoutSpace);
41
+ const consoleWidth = (0, console_utils_1.default)(strWithoutSpace, charLength);
38
42
  if (cnt + consoleWidth <= width) {
39
43
  cnt += consoleWidth + 1; // 1 for the space
40
44
  now.push(strWithoutSpace);
@@ -50,5 +54,7 @@ const limitWidth = (inpStr, width) => {
50
54
  };
51
55
  exports.limitWidth = limitWidth;
52
56
  // ("How are you?",10) => ["How are ", "you?"]
53
- const biggestWordInSentence = (inpStr) => inpStr.split(' ').reduce((a, b) => Math.max(a, (0, console_utils_1.default)(b)), 0);
57
+ const biggestWordInSentence = (inpStr, charLength) => inpStr
58
+ .split(' ')
59
+ .reduce((a, b) => Math.max(a, (0, console_utils_1.default)(b, charLength)), 0);
54
60
  exports.biggestWordInSentence = biggestWordInSentence;
@@ -1,4 +1,4 @@
1
- import { COLOR, Dictionary, Row } from '../models/common';
1
+ import { CharLengthDict, COLOR, Dictionary, Row } from '../models/common';
2
2
  import { ComputedColumn } from '../models/external-table';
3
3
  import { Column } from '../models/internal-table';
4
4
  export declare const cellText: (text: string | number) => string;
@@ -10,7 +10,7 @@ export interface RowOptions {
10
10
  color: COLOR;
11
11
  separator: boolean;
12
12
  }
13
- export declare const convertRawRowOptionsToStandard: (options?: RowOptionsRaw | undefined) => RowOptions | undefined;
13
+ export declare const convertRawRowOptionsToStandard: (options?: RowOptionsRaw) => RowOptions | undefined;
14
14
  export declare const createTableHorizontalBorders: ({ left, mid, right, other, }: {
15
15
  left: string;
16
16
  mid: string;
@@ -20,9 +20,9 @@ export declare const createTableHorizontalBorders: ({ left, mid, right, other, }
20
20
  export declare const createColumFromOnlyName: (name: string) => Column;
21
21
  export declare const createColumFromComputedColumn: (column: ComputedColumn) => Column;
22
22
  export declare const createRow: (color: COLOR, text: Dictionary, separator: boolean) => Row;
23
- export declare const findLenOfColumn: (column: Column, rows: Row[]) => number;
23
+ export declare const findLenOfColumn: (column: Column, rows: Row[], charLength?: CharLengthDict) => number;
24
24
  export declare const renderTableHorizontalBorders: (style: any, column_lengths: number[]) => string;
25
25
  export declare const createHeaderAsRow: (createRowFn: any, columns: Column[]) => Row;
26
- export declare const getWidthLimitedColumnsArray: (columns: Column[], row: Row) => {
26
+ export declare const getWidthLimitedColumnsArray: (columns: Column[], row: Row, charLength?: CharLengthDict) => {
27
27
  [key: string]: string[];
28
28
  };
@@ -50,20 +50,20 @@ const createRow = (color, text, separator) => ({
50
50
  text,
51
51
  });
52
52
  exports.createRow = createRow;
53
- const findLenOfColumn = (column, rows) => {
53
+ const findLenOfColumn = (column, rows, charLength) => {
54
54
  const columnId = column.name;
55
55
  const columnTitle = column.title;
56
56
  let length = max(0, (column === null || column === void 0 ? void 0 : column.minLen) || 0);
57
57
  if (column.maxLen) {
58
58
  // if customer input is mentioned a max width, lets see if all other can fit here
59
59
  // if others cant fit find the max word length so that at least the table can be printed
60
- length = max(length, max(column.maxLen, (0, string_utils_1.biggestWordInSentence)(columnTitle)));
61
- length = rows.reduce((acc, row) => max(acc, (0, string_utils_1.biggestWordInSentence)((0, exports.cellText)(row.text[columnId]))), length);
60
+ length = max(length, max(column.maxLen, (0, string_utils_1.biggestWordInSentence)(columnTitle, charLength)));
61
+ length = rows.reduce((acc, row) => max(acc, (0, string_utils_1.biggestWordInSentence)((0, exports.cellText)(row.text[columnId]), charLength)), length);
62
62
  return length;
63
63
  }
64
- length = max(length, (0, console_utils_1.default)(columnTitle));
64
+ length = max(length, (0, console_utils_1.default)(columnTitle, charLength));
65
65
  rows.forEach((row) => {
66
- length = max(length, (0, console_utils_1.default)((0, exports.cellText)(row.text[columnId])));
66
+ length = max(length, (0, console_utils_1.default)((0, exports.cellText)(row.text[columnId]), charLength));
67
67
  });
68
68
  return length;
69
69
  };
@@ -83,10 +83,10 @@ const createHeaderAsRow = (createRowFn, columns) => {
83
83
  };
84
84
  exports.createHeaderAsRow = createHeaderAsRow;
85
85
  // { col1: ['How', 'Is', 'Going'], col2: ['I am', 'Tom'], }
86
- const getWidthLimitedColumnsArray = (columns, row) => {
86
+ const getWidthLimitedColumnsArray = (columns, row, charLength) => {
87
87
  const ret = {};
88
88
  columns.forEach((column) => {
89
- ret[column.name] = (0, string_utils_1.limitWidth)((0, exports.cellText)(row.text[column.name]), column.length || table_constants_1.DEFAULT_COLUMN_LEN);
89
+ ret[column.name] = (0, string_utils_1.limitWidth)((0, exports.cellText)(row.text[column.name]), column.length || table_constants_1.DEFAULT_COLUMN_LEN, charLength);
90
90
  });
91
91
  return ret;
92
92
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "console-table-printer",
3
- "version": "2.11.0",
3
+ "version": "2.11.1",
4
4
  "repository": "github:ayonious/console-table-printer",
5
5
  "description": "Printing pretty tables on console log",
6
6
  "main": "dist/index.js",
@@ -27,22 +27,22 @@
27
27
  "devDependencies": {
28
28
  "@semantic-release/changelog": "^6.0.1",
29
29
  "@semantic-release/git": "^10.0.1",
30
- "@types/jest": "^27.0.2",
31
- "@typescript-eslint/eslint-plugin": "^5.2.0",
32
- "@typescript-eslint/parser": "^5.2.0",
30
+ "@types/jest": "^28.1.8",
31
+ "@typescript-eslint/eslint-plugin": "^5.34.0",
32
+ "@typescript-eslint/parser": "^5.34.0",
33
33
  "chalk": "^4.1.2",
34
- "eslint": "^8.1.0",
35
- "eslint-config-airbnb-base": "^14.2.1",
36
- "eslint-config-prettier": "^8.3.0",
37
- "eslint-plugin-import": "^2.25.2",
38
- "eslint-plugin-prettier": "^4.0.0",
39
- "husky": "^7.0.4",
40
- "jest": "^27.3.1",
41
- "prettier": "^2.4.1",
42
- "pretty-quick": "^3.1.1",
43
- "semantic-release": "^18.0.0",
44
- "ts-jest": "^27.0.7",
45
- "typescript": "^4.4.4"
34
+ "eslint": "^8.22.0",
35
+ "eslint-config-airbnb-base": "^15.0.0",
36
+ "eslint-config-prettier": "^8.5.0",
37
+ "eslint-plugin-import": "^2.26.0",
38
+ "eslint-plugin-prettier": "^4.2.1",
39
+ "husky": "^8.0.1",
40
+ "jest": "^28.1.3",
41
+ "prettier": "^2.7.1",
42
+ "pretty-quick": "^3.1.3",
43
+ "semantic-release": "^19.0.5",
44
+ "ts-jest": "^28.0.8",
45
+ "typescript": "^4.7.4"
46
46
  },
47
47
  "homepage": "https://console-table.netlify.app",
48
48
  "dependencies": {