diff options
| author | Priyansh <[email protected]> | 2022-01-19 06:05:34 -0500 |
|---|---|---|
| committer | Priyansh <[email protected]> | 2022-01-19 06:05:34 -0500 |
| commit | 420085e4a3ab242523b462fd12a2d07c1693f2aa (patch) | |
| tree | 7be7f56c2321f96c3f0c16d978d4853c3462c2c0 /src/lib | |
| parent | a5ff0a01d3c3924a556d84ee6653e4aa7a8e6863 (diff) | |
| download | izuku.js-420085e4a3ab242523b462fd12a2d07c1693f2aa.tar.xz izuku.js-420085e4a3ab242523b462fd12a2d07c1693f2aa.zip | |
refactor: split functions into multiple files
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/display.ts | 80 | ||||
| -rw-r--r-- | src/lib/frame.ts | 42 | ||||
| -rw-r--r-- | src/lib/locate.ts | 29 |
3 files changed, 150 insertions, 1 deletions
diff --git a/src/lib/display.ts b/src/lib/display.ts index 591429a..432ac82 100644 --- a/src/lib/display.ts +++ b/src/lib/display.ts @@ -1,3 +1,6 @@ +import Izuku from '../index'; +import { table } from 'table'; + function getTable( rowdata: unknown[][], columns: string[], @@ -24,4 +27,79 @@ function getTable( return [['Index', ...columns], ...frameRows]; } -export { getTable }; +/** + * show prints the frame in console.table format + * @returns the current frame + * @throws Error if the frame is empty + */ +export function show(this: Izuku): void { + if (!this.rowdata.length) { + throw new Error('Set data before printing'); + } + const numberOfRows = this.rowdata.length; + if (numberOfRows < 7) { + console.log(table(getTable(this.rowdata, this.columns))); + } else { + const firstThreeRows = this.rowdata.slice(0, 3); + const lastThreeRows = this.rowdata.slice(numberOfRows - 3); + const middleRow = []; + for (let i = 0; i < this.columns.length; i++) { + middleRow.push('...'); + } + const indexRow = [ + 0, + 1, + 2, + '...', + numberOfRows - 3, + numberOfRows - 2, + numberOfRows - 1 + ]; + const combinedRow = [...firstThreeRows, [...middleRow], ...lastThreeRows]; + console.log(table(getTable(combinedRow, this.columns, indexRow))); + } +} + +/** + * head prints maximum first n rows of the frame + * @param n: the number of rows to be returned + * @returns the first n rows of the frame as array of arrays + * @throws Error if the frame is empty + */ +export function head(this: Izuku, n = 5): void { + if (!this.rowdata.length) { + throw new Error('Set data before printing'); + } + // Check if n is greater than the number of rows + if (n > this.rowdata.length) { + n = this.rowdata.length; + } + + // Generate the index row + const indexRow = this.rowdata.map((row, index) => index); + const data = this.rowdata.slice(0, n); + console.log(table(getTable(data, this.columns, indexRow))); +} + +/** + * tail prints maximum last n rows of the frame + * @param n: the number of rows to be returned + * @returns the last n rows of the frame as array of arrays + * @throws Error if the frame is empty + */ +export function tail(this: Izuku, n = 5): void { + if (!this.rowdata.length) { + throw new Error('Set data before printing'); + } + // Check if n is greater than the number of rows + if (n > this.rowdata.length) { + n = this.rowdata.length; + } + + // Generate the index row + const indexRow = this.rowdata.map((row, index) => index); + const data = this.rowdata.slice(this.rowdata.length - n); + // Slice the index row to match the data + const slicedIndexRow = indexRow.slice(indexRow.length - n); + console.log(table(getTable(data, this.columns, slicedIndexRow))); +} diff --git a/src/lib/frame.ts b/src/lib/frame.ts new file mode 100644 index 0000000..1106ede --- /dev/null +++ b/src/lib/frame.ts @@ -0,0 +1,42 @@ +import Izuku from '../index'; +/** + * data prints the data of the frame in console.table format. It also sets the new data to the frame if data is passed as a parameter + * @param rowdata: the rowdata to be sent to the frame + * @returns the current frame + */ +export function data( + this: Izuku, + rowdata?: Array<unknown[]> +): unknown[][] | any { + if (rowdata) { + this.rowdata = rowdata; + } + + return this; +} + +/** + * header sets the names of the columns of the frame + * @param header: the header to be attached to the frame + * @returns the current frame + */ +export function header( + this: Izuku, + header: Array<string> +): Array<string> | any { + if (!this.rowdata.length) { + throw new Error('Set data before setting header'); + } else { + const passedHeaderLength = header.length; + const maxSizedArrayLength = this.rowdata.reduce((acc, curr) => { + return acc.length > curr.length ? acc : curr; + }).length; + if (passedHeaderLength !== maxSizedArrayLength) { + throw new Error('Header length does not match data length'); + } else { + this.columns = header; + } + } + + return this; +} diff --git a/src/lib/locate.ts b/src/lib/locate.ts new file mode 100644 index 0000000..b176e07 --- /dev/null +++ b/src/lib/locate.ts @@ -0,0 +1,29 @@ +interface Izuku { + rowdata: unknown[][]; + columns: string[]; +} +/** + * column returns a single column of the frame, option is either the column name or the column index + * @param column: the column to be returned + * @returns a single column of the frame as array of arrays + */ +export function getSingleColumnDetails(iz: Izuku, column: number | string) { + let extractedColumn: any[] = []; + let columnName = ''; + if (typeof column === 'string') { + extractedColumn = iz.rowdata.map((row) => { + return row[iz.columns.indexOf(column)]; + }); + columnName = column; + } + if (typeof column === 'number') { + extractedColumn = iz.rowdata.map((row) => { + return row[column]; + }); + columnName = iz.columns[column] + ? iz.columns[column] + : `column${column + 1}`; + } + extractedColumn = extractedColumn.map((item) => [item]); + return { rowd: extractedColumn, rowh: [columnName] }; +} |
