Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to specify initial rows, make add methods chainable,… #396

Merged
merged 1 commit into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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();
});
});