Skip to content

Commit

Permalink
fix: Calculated Columns Title is shown (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayonious committed Mar 19, 2021
1 parent 7765522 commit 88ae550
Show file tree
Hide file tree
Showing 24 changed files with 306 additions and 137 deletions.
2 changes: 1 addition & 1 deletion index.ts
@@ -1,6 +1,6 @@
import Table from './src/console-table-printer';
import { printSimpleTable as printTable } from './src/internalTable/internal-table-printer';

import { COLOR, ALIGNMENT } from './src/utils/table-constants';
import { COLOR, ALIGNMENT } from './src/models/external-table';

export { Table, printTable, COLOR, ALIGNMENT };
3 changes: 2 additions & 1 deletion src/console-table-printer.ts
@@ -1,5 +1,6 @@
import { ComplexOptions, TableInternal } from './internalTable/internal-table';
import TableInternal from './internalTable/internal-table';
import { Dictionary } from './models/common';
import { ComplexOptions } from './models/external-table';
import {
convertRawRowOptionsToStandard,
RowOptionsRaw,
Expand Down
24 changes: 24 additions & 0 deletions src/internalTable/input-converter.ts
@@ -0,0 +1,24 @@
import { COLOR } from '../models/common';
import { ColumnOptionsRaw } from '../models/external-table';
import { Column } from '../models/internal-table';
import { defaultRowAlignment } from '../utils/table-constants';

export const objIfExists = (key: string, val: any) => {
if (!val) {
return {};
}

return {
[key]: val,
};
};

export const rawColumnToInternalColumn = (
column: ColumnOptionsRaw
): Column => ({
name: column.name,
title: column.title || column.name,
...objIfExists('color', column.color as COLOR),
...objIfExists('maxLen', column.maxLen),
alignment: column.alignment || defaultRowAlignment,
});
13 changes: 7 additions & 6 deletions src/internalTable/internal-table-printer.ts
@@ -1,21 +1,22 @@
import { Column, Row } from '../models/internal-table';
import { Row } from '../models/common';
import { Column, TABLE_STYLE_DETAILS } from '../models/internal-table';
import ColoredConsoleLine from '../utils/colored-console-line';
import { textWithPadding, limitWidth } from '../utils/string-utils';
import { textWithPadding } from '../utils/string-utils';
import {
defaultHeaderAlignment,
defaultHeaderFontColor,
defaultRowAlignment,
defaultRowFontColor,
TABLE_STYLE_DETAILS,
} from '../utils/table-constants';
import {
cellText,
createHeaderAsRow,
createRow,
renderTableHorizontalBorders,
getWidthLimitedColumnsArray,
renderTableHorizontalBorders,
RowOptions,
} from '../utils/table-helpers';
import { TableInternal } from './internal-table';
import TableInternal from './internal-table';
import { preProcessColumns, preProcessRows } from './table-pre-processors';

// β•‘ Index β•‘ β•‘ β•‘
Expand Down Expand Up @@ -139,7 +140,7 @@ const renderTableHeaders = (table: TableInternal): string[] => {
ret.push(
renderTableHorizontalBorders(
table.tableStyle.headerTop,
table.columns.map((m) => m.maxLen || 20)
table.columns.map((m: Column) => m.maxLen || 20)
)
);

Expand Down
79 changes: 26 additions & 53 deletions src/internalTable/internal-table.ts
@@ -1,56 +1,30 @@
import { Dictionary } from '../models/common';
import { Column, Row } from '../models/internal-table';
import { Dictionary, Row } from '../models/common';
import {
ComplexOptions,
ComputedColumn,
RowFilterFunction,
RowSortFunction,
} from '../models/external-table';
import { Column, TABLE_STYLE_DETAILS } from '../models/internal-table';
import {
ALIGNMENT,
COLOR,
DEFAULT_TABLE_STYLE,
defaultRowAlignment,
defaultRowFontColor,
TABLE_STYLE_DETAILS,
} from '../utils/table-constants';
import { createColum, createRow, RowOptions } from '../utils/table-helpers';
import {
createColumFromComputedColumn,
createColumFromOnlyName,
createRow,
RowOptions,
} from '../utils/table-helpers';
import { rawColumnToInternalColumn } from './input-converter';
import { renderTable } from './internal-table-printer';

interface ColumnOptionsRaw {
name: string; // unique id
title?: string; // the value that will be printed, if not present this will be 'name'
alignment?: ALIGNMENT;
color?: COLOR;
maxLen?: number;
}

export interface ComputedColumn extends ColumnOptionsRaw {
function: (arg0: any) => any;
}

export type RowSortFunction = (row1: any, row2: any) => number;
const defaultRowSortFunc = () => 0;

export type RowFilterFunction = (row: any) => Boolean;
const defaultRowFilterFunc = () => true;

export interface ComplexOptions {
style?: TABLE_STYLE_DETAILS;
title?: string;
columns?: ColumnOptionsRaw[];
sort?: RowSortFunction;
filter?: RowFilterFunction;
enabledColumns?: string[];
disabledColumns?: string[];
computedColumns?: ComputedColumn[];
}

function objIfExists(key: string, val: any) {
if (!val) {
return {};
}

return {
[key]: val,
};
}

export class TableInternal {
class TableInternal {
title?: string;

tableStyle: TABLE_STYLE_DETAILS;
Expand Down Expand Up @@ -85,14 +59,7 @@ export class TableInternal {
this.enabledColumns = options?.enabledColumns || [];
this.disabledColumns = options?.disabledColumns || [];
this.computedColumns = options?.computedColumns || [];
this.columns =
options.columns?.map((column: ColumnOptionsRaw) => ({
name: column.name,
title: column.title || column.name,
...objIfExists('color', column.color as COLOR),
...objIfExists('maxLen', column.maxLen),
alignment: column.alignment || defaultRowAlignment,
})) || [];
this.columns = options.columns?.map(rawColumnToInternalColumn) || [];
}

constructor(options?: ComplexOptions | string[]) {
Expand All @@ -118,13 +85,17 @@ export class TableInternal {
const colNames = this.columns.map((col) => col.name);
Object.keys(text).forEach((key) => {
if (!colNames.includes(key)) {
this.columns.push(createColum(key));
this.columns.push(createColumFromOnlyName(key));
}
});
}

addColumn(text: string) {
this.columns.push(createColum(text));
addColumn(textOrObj: string | ComputedColumn) {
if (typeof textOrObj === 'string') {
this.columns.push(createColumFromOnlyName(textOrObj));
} else {
this.columns.push(createColumFromComputedColumn(textOrObj));
}
}

addColumns(toBeInsertedColumns: string[]) {
Expand All @@ -148,3 +119,5 @@ export class TableInternal {
return renderTable(this);
}
}

export default TableInternal;
8 changes: 5 additions & 3 deletions src/internalTable/table-pre-processors.ts
@@ -1,12 +1,14 @@
/* eslint-disable no-param-reassign */
import { Column, Row } from '../models/internal-table';
import { Row } from '../models/common';
import { ComputedColumn } from '../models/external-table';
import { Column } from '../models/internal-table';
import { findMaxLenOfColumn } from '../utils/table-helpers';
import { ComputedColumn, TableInternal } from './internal-table';
import TableInternal from './internal-table';

const createComputedColumnsIfNecessary = (table: TableInternal) => {
if (table.computedColumns.length) {
table.computedColumns.forEach((computedColumn: ComputedColumn) => {
table.addColumn(computedColumn.name);
table.addColumn(computedColumn);
table.rows.forEach((row: Row) => {
row.text[computedColumn.name] = computedColumn.function(row.text);
});
Expand Down
9 changes: 9 additions & 0 deletions src/models/common.ts
@@ -1,3 +1,12 @@
import { alignments, colors } from '../utils/table-constants';

export type ALIGNMENT = typeof alignments[number];

export type COLOR = typeof colors[number];
export interface Dictionary {
[key: string]: any;
}
export interface Row {
color: COLOR;
text: Dictionary;
}
31 changes: 31 additions & 0 deletions src/models/external-table.ts
@@ -0,0 +1,31 @@
import { ALIGNMENT, COLOR, Dictionary } from './common';
import { TABLE_STYLE_DETAILS } from './internal-table';

export { ALIGNMENT, COLOR };

export interface ColumnOptionsRaw {
name: string; // unique id
title?: string; // the value that will be printed, if not present this will be 'name'
alignment?: ALIGNMENT;
color?: COLOR;
maxLen?: number;
}

export interface ComputedColumn extends ColumnOptionsRaw {
function: (arg0: any) => any;
}

export type RowSortFunction = (row1: any, row2: any) => number;

export type RowFilterFunction = (row: any) => Boolean;

export interface ComplexOptions {
style?: TABLE_STYLE_DETAILS;
title?: string;
columns?: ColumnOptionsRaw[];
sort?: RowSortFunction;
filter?: RowFilterFunction;
enabledColumns?: string[];
disabledColumns?: string[];
computedColumns?: ComputedColumn[];
}
19 changes: 13 additions & 6 deletions src/models/internal-table.ts
@@ -1,5 +1,4 @@
import { ALIGNMENT, COLOR } from '../utils/table-constants';
import { Dictionary } from './common';
import { ALIGNMENT, COLOR } from './common';

/*
All the fields of Internal Table has to be mandatory
Expand All @@ -15,7 +14,15 @@ export interface Column {
maxLen?: number;
}

export interface Row {
color: COLOR;
text: Dictionary;
}
type TABLE_LINE_DETAILS_KEYS = 'left' | 'right' | 'mid' | 'other';

export type TABLE_LINE_DETAILS = {
[key in TABLE_LINE_DETAILS_KEYS]: string;
};

export type TABLE_STYLE_DETAILS = {
headerTop: TABLE_LINE_DETAILS;
headerBottom: TABLE_LINE_DETAILS;
tableBottom: TABLE_LINE_DETAILS;
vertical: string;
};
2 changes: 1 addition & 1 deletion src/utils/colored-console-line.ts
@@ -1,4 +1,4 @@
import { COLOR } from './table-constants';
import { COLOR } from '../models/common';

const COLOR_MAP: {
[key in COLOR]?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/string-utils.ts
@@ -1,5 +1,5 @@
import { ALIGNMENT } from '../models/common';
import findWidthInConsole from './console-utils';
import { ALIGNMENT } from './table-constants';
import { cellText } from './table-helpers';

// ("How are you?",center, 20) => " How are you? "
Expand Down
20 changes: 3 additions & 17 deletions src/utils/table-constants.ts
@@ -1,15 +1,5 @@
type TABLE_LINE_DETAILS_KEYS = 'left' | 'right' | 'mid' | 'other';

export type TABLE_LINE_DETAILS = {
[key in TABLE_LINE_DETAILS_KEYS]: string;
};

export type TABLE_STYLE_DETAILS = {
headerTop: TABLE_LINE_DETAILS;
headerBottom: TABLE_LINE_DETAILS;
tableBottom: TABLE_LINE_DETAILS;
vertical: string;
};
import { ALIGNMENT, COLOR } from '../models/common';
import { TABLE_STYLE_DETAILS } from '../models/internal-table';

export const DEFAULT_TABLE_STYLE: TABLE_STYLE_DETAILS = {
/*
Expand Down Expand Up @@ -40,7 +30,7 @@ export const DEFAULT_TABLE_STYLE: TABLE_STYLE_DETAILS = {
vertical: 'β”‚',
};

const alignments = ['right', 'left', 'center'];
export const alignments = ['right', 'left', 'center'];

export const colors = [
'red',
Expand All @@ -55,10 +45,6 @@ export const colors = [
'reset',
];

export type ALIGNMENT = typeof alignments[number];

export type COLOR = typeof colors[number];

export const defaultRowFontColor: COLOR = 'white';
export const defaultHeaderFontColor: COLOR = 'white_bold';

Expand Down
22 changes: 18 additions & 4 deletions src/utils/table-helpers.ts
@@ -1,8 +1,10 @@
import { Dictionary } from '../models/common';
import { Column, Row } from '../models/internal-table';
import { objIfExists } from '../internalTable/input-converter';
import { COLOR, Dictionary, Row } from '../models/common';
import { ComputedColumn } from '../models/external-table';
import { Column } from '../models/internal-table';
import findWidthInConsole from './console-utils';
import { biggestWordInSentence, limitWidth } from './string-utils';
import { COLOR } from './table-constants';
import { defaultRowAlignment } from './table-constants';

// takes any input that is given by user and converts to string
export const cellText = (text: string | number): string =>
Expand Down Expand Up @@ -53,7 +55,19 @@ export const createTableHorizontalBorders = (
return ret;
};

export const createColum = (name: string): Column => ({ name, title: name });
export const createColumFromOnlyName = (name: string): Column => ({
name,
title: name,
});
export const createColumFromComputedColumn = (
column: ComputedColumn
): Column => ({
name: column.name,
title: column.title || column.name,
...objIfExists('color', column.color as COLOR),
...objIfExists('maxLen', column.maxLen),
alignment: column.alignment || defaultRowAlignment,
});

export const createRow = (color: COLOR, text: Dictionary): Row => ({
color,
Expand Down
15 changes: 15 additions & 0 deletions test/__snapshots__/alignment.test.ts.snap
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing column alignment all kind of alignments are working 1`] = `
"β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ red_left_align_index β”‚  right_align_text β”‚ green_value_center β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 2  β”‚  This row is blue β”‚  10.212  β”‚
β”‚ 3  β”‚  I would like some red wine please β”‚  10.212  β”‚
β”‚ 4  β”‚  I would like some cyan wine please β”‚  10.212  β”‚
β”‚ 5  β”‚ I would like some white_bold wine please β”‚  10.212  β”‚
β”‚ 6  β”‚  I would like some crimson sky please β”‚  10.212  β”‚
β”‚ 7  β”‚  I would like some green gemuse please β”‚  20  β”‚
β”‚ 8  β”‚  I would like some gelb bananen bitte β”‚  100  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜"
`;
12 changes: 12 additions & 0 deletions test/__snapshots__/cellColor.test.ts.snap
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Example: Print a simple Table with cell colors cell colors are working 1`] = `
"β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ index β”‚  text β”‚  value β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  1 β”‚  red β”‚ 10.212 β”‚
β”‚  2 β”‚  green β”‚  20 β”‚
β”‚  3 β”‚  yellow β”‚  100 β”‚
β”‚  4 β”‚ magenta β”‚  300 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜"
`;

0 comments on commit 88ae550

Please sign in to comment.