Skip to content

Commit

Permalink
feat: add option to specify initial rows, make add methods chainable,…
Browse files Browse the repository at this point in the history
… export renderSimpleTable (#396)
  • Loading branch information
ejose19 committed Oct 1, 2021
1 parent dcb28e8 commit 6e6d8f3
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -136,6 +136,7 @@ new Table({
{ name: 'column2', alignment: 'right', maxLen: 30 }, // lines bigger than this will be splitted in multiple lines
{ name: 'column3', title: 'Column3' }, // Title is what will be shown while printing, by default title = name
],
rows: [{ column1: 'row1' }, { column2: 'row2' }, { column3: 'row3' }],
sort: (row1, row2) => row2.column1 - row1.column1, // sorting order of rows (optional), this is normal js sort function for Array.sort
filter: (row) => row.column1 < 3, // filtering rows (optional)
enabledColumns: ['column1'], // array of columns that you want to see, all other will be ignored (optional)
Expand Down
7 changes: 5 additions & 2 deletions index.ts
@@ -1,6 +1,9 @@
import Table from './src/console-table-printer';
import { printSimpleTable as printTable } from './src/internalTable/internal-table-printer';
import {
printSimpleTable as printTable,
renderSimpleTable as renderTable,
} from './src/internalTable/internal-table-printer';

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

export { Table, printTable, COLOR, ALIGNMENT };
export { Table, printTable, renderTable, COLOR, ALIGNMENT };
4 changes: 4 additions & 0 deletions src/console-table-printer.ts
Expand Up @@ -15,21 +15,25 @@ export default class Table {

addColumn(column: string) {
this.table.addColumn(column);
return this;
}

addColumns(columns: string[]) {
this.table.addColumns(columns);
return this;
}

addRow(text: Dictionary, rowOptions?: RowOptionsRaw) {
this.table.addRow(text, convertRawRowOptionsToStandard(rowOptions));
return this;
}

addRows(toBeInsertedRows: any, rowOptions?: RowOptionsRaw) {
this.table.addRows(
toBeInsertedRows,
convertRawRowOptionsToStandard(rowOptions)
);
return this;
}

printTable() {
Expand Down
4 changes: 4 additions & 0 deletions src/internalTable/internal-table.ts
Expand Up @@ -65,6 +65,10 @@ class TableInternal {
this.columns =
options?.columns?.map(rawColumnToInternalColumn) || this.columns;
this.rowSeparator = options?.rowSeparator || this.rowSeparator;

if (options.rows !== undefined) {
this.addRows(options.rows);
}
}

constructor(options?: ComplexOptions | string[]) {
Expand Down
3 changes: 2 additions & 1 deletion src/models/external-table.ts
@@ -1,4 +1,4 @@
import { ALIGNMENT, COLOR } from './common';
import { ALIGNMENT, COLOR, Dictionary } from './common';
import { TableStyleDetails } from './internal-table';

export { ALIGNMENT, COLOR };
Expand All @@ -24,6 +24,7 @@ export interface ComplexOptions {
style?: TableStyleDetails;
title?: string;
columns?: ColumnOptionsRaw[];
rows?: Dictionary[];
sort?: RowSortFunction;
filter?: RowFilterFunction;
enabledColumns?: string[];
Expand Down
9 changes: 9 additions & 0 deletions test/__snapshots__/types.test.ts.snap
Expand Up @@ -7,3 +7,12 @@ exports[`Testing column alignment all kind of alignments are working 1`] = `
β”‚ 2  β”‚ This row is blue β”‚  10.212  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜"
`;

exports[`Testing column alignment should allow chaining add methods 1`] = `
"β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚ foo β”‚ bar β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€
β”‚  1 β”‚   β”‚
β”‚   β”‚  2 β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜"
`;
14 changes: 14 additions & 0 deletions test/general.test.ts
@@ -0,0 +1,14 @@
import { Table } from '../index';

describe('General tests', () => {
it('should yield the same state when adding rows during instantation than with method', () => {
const rows = [{ foo: '1', bar: '2' }];

const table1 = new Table();
table1.addRows(rows);

const table2 = new Table({ rows });

expect(table1.table).toStrictEqual(table2.table);
});
});
11 changes: 11 additions & 0 deletions test/types.test.ts
Expand Up @@ -38,4 +38,15 @@ describe('Testing column alignment', () => {
p.printTable();
expect(p.render()).toMatchSnapshot();
});

it('should allow chaining add methods', () => {
const p = new Table()
.addColumn('foo')
.addColumns(['bar'])
.addRow({ foo: '1' })
.addRows([{ bar: '2' }]);

p.printTable();
expect(p.render()).toMatchSnapshot();
});
});

0 comments on commit 6e6d8f3

Please sign in to comment.