diff options
Diffstat (limited to 'src/lib/info.ts')
| -rw-r--r-- | src/lib/info.ts | 88 |
1 files changed, 88 insertions, 0 deletions
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`); +} |
