From a5ff0a01d3c3924a556d84ee6653e4aa7a8e6863 Mon Sep 17 00:00:00 2001 From: Priyansh Date: Wed, 19 Jan 2022 04:46:26 -0500 Subject: feat: add table dep, added printing test and moved display to function --- src/lib/display.ts | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) (limited to 'src/lib') diff --git a/src/lib/display.ts b/src/lib/display.ts index 2247938..591429a 100644 --- a/src/lib/display.ts +++ b/src/lib/display.ts @@ -1,20 +1,27 @@ -import { FrameInterface } from '../interface/frameInterface'; - -export class Display implements FrameInterface { - rowdata: unknown[][] = []; - columns!: string[]; - table(frame: FrameInterface): void { - const maxSizedArrayLength = frame.rowdata.reduce((acc, curr) => { - return acc.length > curr.length ? acc : curr; - }).length; - console.table( - frame.rowdata.map((row) => { - const rowobj: any = {}; - for (let i = 0; i < maxSizedArrayLength; i++) { - rowobj[frame.columns[i]] = row[i] ? row[i] : null; - } - return rowobj; - }) - ); +function getTable( + rowdata: unknown[][], + columns: string[], + indexRow?: any[] +): any[] { + const maxSizedArrayLength = rowdata.reduce((acc, curr) => { + return acc.length > curr.length ? acc : curr; + }).length; + const frameRows: unknown[][] = []; + for (let i = 0; i < rowdata.length; i++) { + const row = rowdata[i]; + const rowLength = row.length; + const rowArray = []; + rowArray.push(indexRow ? indexRow[i] : i); + for (let j = 0; j < maxSizedArrayLength; j++) { + if (j < rowLength) { + rowArray.push(row[j]); + } else { + rowArray.push(''); + } + } + frameRows.push(rowArray); } + return [['Index', ...columns], ...frameRows]; } + +export { getTable }; -- cgit v1.2.3