diff options
| author | Bobby <[email protected]> | 2022-01-26 14:24:20 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-01-26 14:24:20 -0500 |
| commit | 228960a756ec4417d5337eaacfffb9212eca2399 (patch) | |
| tree | 7e512f1bee99897818c5db14b29c48663b9cabd1 /src/lib | |
| parent | 8953cdd7fae112688ec31945f4257bcfb16e82b0 (diff) | |
| download | izuku.js-228960a756ec4417d5337eaacfffb9212eca2399.tar.xz izuku.js-228960a756ec4417d5337eaacfffb9212eca2399.zip | |
feat: add title to table
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/display.ts | 35 | ||||
| -rw-r--r-- | src/lib/frame.ts | 12 |
2 files changed, 34 insertions, 13 deletions
diff --git a/src/lib/display.ts b/src/lib/display.ts index d56b9d9..c189434 100644 --- a/src/lib/display.ts +++ b/src/lib/display.ts @@ -11,8 +11,9 @@ import { Table } from '../helpers/tableBuilder'; function getTable( rowdata: unknown[][], columns: string[], - indexRow?: any[] -): any[] { + indexRow?: any[], + title?: any +): any { const maxSizedArrayLength = rowdata.reduce((acc, curr) => { return acc.length > curr.length ? acc : curr; }).length; @@ -31,17 +32,20 @@ function getTable( } frameRows.push(rowArray); } - return [['Index', ...columns], ...frameRows]; + return { + title: title ? title : '', + data: [['Index', ...columns], ...frameRows] + }; } /** * displayTable prints the table to the console * @param rowdata the rowdata to be sent to the frame */ -export function displayTable(rowdata: any[][]): void { +export function displayTable(rowdata: any): void { // Convert row data into object with keys and values, keys are the column names stored in rowdata[0] const headerObject: any = []; - rowdata[0].forEach((column) => { + rowdata.data[0].forEach((column: any) => { headerObject.push({ name: column, alignment: 'left', @@ -51,13 +55,14 @@ export function displayTable(rowdata: any[][]): void { }); }); const table = new Table({ - columns: headerObject + columns: headerObject, + title: String(rowdata.title) }); - const rowdataObject = rowdata.map((row) => { + const rowdataObject = rowdata.data.map((row: any[]) => { const rowObject: any = {}; - row.forEach((value, index) => { - rowObject[rowdata[0][index]] = value; + row.forEach((value: any, index: string | number) => { + rowObject[rowdata.data[0][index]] = value; }); return rowObject; }); @@ -79,7 +84,9 @@ export function show(this: Frame): void { } else { const numberOfRows = this.rowdata.length; if (numberOfRows < 7) { - displayTable(getTable(this.rowdata, this.columns)); + displayTable( + getTable(this.rowdata, this.columns, undefined, this.tableTitle) + ); } else { const firstThreeRows = this.rowdata.slice(0, 3); const lastThreeRows = this.rowdata.slice(numberOfRows - 3); @@ -97,7 +104,9 @@ export function show(this: Frame): void { numberOfRows - 1 ]; const combinedRow = [...firstThreeRows, [...middleRow], ...lastThreeRows]; - displayTable(getTable(combinedRow, this.columns, indexRow)); + displayTable( + getTable(combinedRow, this.columns, indexRow, this.tableTitle) + ); } } } @@ -120,7 +129,7 @@ export function head(this: Frame, n = 5): void { // Generate the index row const indexRow = this.rowdata.map((row, index) => index); const data = this.rowdata.slice(0, n); - displayTable(getTable(data, this.columns, indexRow)); + displayTable(getTable(data, this.columns, indexRow, this.tableTitle)); } } @@ -144,6 +153,6 @@ export function tail(this: Frame, n = 5): void { const data = this.rowdata.slice(this.rowdata.length - n); // Slice the index row to match the data const slicedIndexRow = indexRow.slice(indexRow.length - n); - displayTable(getTable(data, this.columns, slicedIndexRow)); + displayTable(getTable(data, this.columns, slicedIndexRow, this.tableTitle)); } } diff --git a/src/lib/frame.ts b/src/lib/frame.ts index d2d9eb5..26b187e 100644 --- a/src/lib/frame.ts +++ b/src/lib/frame.ts @@ -80,3 +80,15 @@ export function generateHeader(rd: Array<any[]>): Array<string> { return []; } } + +/** + * title sets the title of the frame + * @param title: the title to be attached to the frame + * @returns the current frame + */ +export function title(this: Frame, title?: string): string | any { + if (title) { + this.tableTitle = title; + } + return this; +} |
