diff options
| author | Priyansh <[email protected]> | 2022-01-19 13:19:26 -0500 |
|---|---|---|
| committer | Priyansh <[email protected]> | 2022-01-19 13:19:26 -0500 |
| commit | 6af5afc1b0d3b08b731bead0a4e2cad27dfd472c (patch) | |
| tree | 01adfbdfc7b57ce9b2cc8c85985782a6524bdc21 /src/lib | |
| parent | 420085e4a3ab242523b462fd12a2d07c1693f2aa (diff) | |
| download | izuku.js-6af5afc1b0d3b08b731bead0a4e2cad27dfd472c.tar.xz izuku.js-6af5afc1b0d3b08b731bead0a4e2cad27dfd472c.zip | |
feat: info functions
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/display.ts | 7 | ||||
| -rw-r--r-- | src/lib/frame.ts | 54 | ||||
| -rw-r--r-- | src/lib/info.ts | 88 |
3 files changed, 139 insertions, 10 deletions
diff --git a/src/lib/display.ts b/src/lib/display.ts index 432ac82..c7a20c8 100644 --- a/src/lib/display.ts +++ b/src/lib/display.ts @@ -1,6 +1,13 @@ import Izuku from '../index'; import { table } from 'table'; +/** + * getTable returns the data compatible with table.table + * @param rowdata the rowdata to be sent to the frame + * @param columns the header columns to be sent to the frame + * @param indexRow index of the row to be printed + * @returns Array of arrays + */ function getTable( rowdata: unknown[][], columns: string[], diff --git a/src/lib/frame.ts b/src/lib/frame.ts index 1106ede..06ba4a9 100644 --- a/src/lib/frame.ts +++ b/src/lib/frame.ts @@ -1,4 +1,5 @@ import Izuku from '../index'; +import { getSize } from './info'; /** * 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 @@ -10,6 +11,7 @@ export function data( ): unknown[][] | any { if (rowdata) { this.rowdata = rowdata; + this.size = getSize(this.rowdata); } return this; @@ -24,19 +26,51 @@ export function header( this: Izuku, header: Array<string> ): Array<string> | any { - if (!this.rowdata.length) { - throw new Error('Set data before setting header'); + console.log(header); + if (!header.length) { + this.columns = generateHeader(this.rowdata); } 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'); + this.columns = setHeader(this.rowdata, header); + } + + return this; +} + +/** + * setHeader sets the names of the columns of the frame + * @param rowdata: the rowdata to be sent to the frame + * @param header: the header to be attached to the frame + * @returns a new header + */ + +export function setHeader(rowdata: any[][], header: any[]): Array<string> { + const maxSizedArrayLength = rowdata.reduce((acc, curr) => { + return acc.length > curr.length ? acc : curr; + }).length; + const newHeaderArray = Array(maxSizedArrayLength).fill(''); + for (let i = 0; i < maxSizedArrayLength; i++) { + if (header[i]) { + newHeaderArray[i] = header[i]; } else { - this.columns = header; + newHeaderArray[i] = `Column ${i + 1}`; } } + return newHeaderArray; +} - return this; +/** + * generateHeader generates the names of the columns of the frame + * @param rowdata: the rowdata to be sent to the frame + * @returns a new header + */ + +export function generateHeader(rd: Array<any[]>): Array<string> { + const maxSizedArrayLength = rd.reduce((acc, curr) => { + return acc.length > curr.length ? acc : curr; + }).length; + const header: Array<string> = []; + for (let i = 0; i < maxSizedArrayLength; i++) { + header.push(`Column ${i + 1}`); + } + return header; } diff --git a/src/lib/info.ts b/src/lib/info.ts new file mode 100644 index 0000000..f7d3378 --- /dev/null +++ b/src/lib/info.ts @@ -0,0 +1,88 @@ +import Izuku from '../index'; +import { table } from 'table'; +import { sizeof } from '../helpers/memorySize'; +/** + * size returns the total number of elements in the frame + * @returns the total number of elements in the frame + * @returns 0 if the frame is empty + */ +export function getSize(rowdata: any[]): number { + // Get the number of elements in 2D array, do not count nulls + const numberOfElements = rowdata.reduce( + ( + acc: any, + row: { + filter: (arg0: (item: any) => boolean) => { + (): any; + new (): any; + length: any; + }; + } + ) => { + return acc + row.filter((item: null) => item !== null).length; + }, + 0 + ); + return numberOfElements; +} + +/** + * info returns the type of data present in each column of the frame + * @returns the type of data present in each column of the frame + */ +export function info(this: Izuku): void { + const info: Array<any[]> = []; + info.push(['#', 'Column Name', 'Types', 'Empty Values']); + let counter = 0; + const countDataTypes = {} as any; + this.columns.forEach((column: string, index: number) => { + // get all the types of data in the column, do not repeat the same type + let nullValuesPresentInRow = false; + const columnDataTypes = this.rowdata.map((row: any[]) => { + if ( + row[index] === null || + row[index] === undefined || + row[index] === '' + ) { + nullValuesPresentInRow = true; + } + const currentType = String(typeof row[index]); + if (!Object.keys(countDataTypes).includes(currentType)) { + Object.assign(countDataTypes, { [currentType]: 1 }); + } else { + const getKeyValue = <T, K extends keyof T>(obj: T, key: K): T[K] => + obj[key]; + const currentValue = getKeyValue(countDataTypes, currentType); + Object.assign(countDataTypes, { [currentType]: currentValue + 1 }); + } + return typeof row[index]; + }); + const uniqueDataTypes = [...new Set(columnDataTypes)]; + info.push([counter, column, [...uniqueDataTypes], nullValuesPresentInRow]); + counter++; + }); + let dataTypesString = ''; + // Iterate through the countDataTypes object and create a string of the data types + Object.keys(countDataTypes).forEach((key: string) => { + dataTypesString += `${key}(${countDataTypes[key]})`; + // check if the key is not the last key in the object + if ( + Object.keys(countDataTypes).indexOf(key) !== + Object.keys(countDataTypes).length - 1 + ) { + dataTypesString += ', '; + } + }); + + console.log(`RangeIndex: ${this.size} elements, 0 to ${this.size - 1}`); + console.log( + `Shape: ${this.shape.split(' x ')[0].trim()} rows, ${this.shape + .split(' x ')[1] + .trim()} columns` + ); + console.log(table(info)); + // Remove the previous printed newline + process.stdout.write('\x1B[1A\x1B[2K'); + console.log(`Data Types: ${dataTypesString}`); + console.log(`Memory Usage: ${sizeof(this)} bytes`); +} |
