aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorPriyansh <[email protected]>2022-01-20 18:55:01 -0500
committerPriyansh <[email protected]>2022-01-20 18:55:01 -0500
commit90be11f8e8ad16ef2a68d71d4523f813db18e6ef (patch)
tree19b9fced8668b83066f3f98a95af67877e721072 /src/lib
parentd0352ed005d0bceb345368274bff4c4c605ed396 (diff)
downloadizuku.js-90be11f8e8ad16ef2a68d71d4523f813db18e6ef.tar.xz
izuku.js-90be11f8e8ad16ef2a68d71d4523f813db18e6ef.zip
feat: added arrayFunctions and rename Izuku to Frame
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/data.ts16
-rw-r--r--src/lib/display.ts8
-rw-r--r--src/lib/frame.ts6
-rw-r--r--src/lib/info.ts22
4 files changed, 27 insertions, 25 deletions
diff --git a/src/lib/data.ts b/src/lib/data.ts
new file mode 100644
index 0000000..dcc948b
--- /dev/null
+++ b/src/lib/data.ts
@@ -0,0 +1,16 @@
+/**
+ * flattenArray - flattens a 2D into a single array
+ * @param array: the array to be flattened
+ * @returns the flattened array
+ */
+export function flatten(array: any[][]): any[] {
+ let flattenedArray: any[] = [];
+ for (let i = 0; i < array.length; i++) {
+ if (Array.isArray(array[i])) {
+ flattenedArray = flattenedArray.concat(flatten(array[i]));
+ } else {
+ flattenedArray.push(array[i]);
+ }
+ }
+ return flattenedArray;
+}
diff --git a/src/lib/display.ts b/src/lib/display.ts
index c7a20c8..6aeb029 100644
--- a/src/lib/display.ts
+++ b/src/lib/display.ts
@@ -1,4 +1,4 @@
-import Izuku from '../index';
+import { Frame } from '../index';
import { table } from 'table';
/**
@@ -39,7 +39,7 @@ function getTable(
* @returns the current frame
* @throws Error if the frame is empty
*/
-export function show(this: Izuku): void {
+export function show(this: Frame): void {
if (!this.rowdata.length) {
throw new Error('Set data before printing');
}
@@ -73,7 +73,7 @@ export function show(this: Izuku): void {
* @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 {
+export function head(this: Frame, n = 5): void {
if (!this.rowdata.length) {
throw new Error('Set data before printing');
}
@@ -94,7 +94,7 @@ export function head(this: Izuku, n = 5): void {
* @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 {
+export function tail(this: Frame, n = 5): void {
if (!this.rowdata.length) {
throw new Error('Set data before printing');
}
diff --git a/src/lib/frame.ts b/src/lib/frame.ts
index f52243e..f81141d 100644
--- a/src/lib/frame.ts
+++ b/src/lib/frame.ts
@@ -1,4 +1,4 @@
-import Izuku from '../index';
+import { Frame } 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
@@ -6,7 +6,7 @@ import { getSize } from './info';
* @returns the current frame
*/
export function data(
- this: Izuku,
+ this: Frame,
rowdata?: Array<unknown[]>
): unknown[][] | any {
if (rowdata) {
@@ -23,7 +23,7 @@ export function data(
* @returns the current frame
*/
export function header(
- this: Izuku,
+ this: Frame,
header: Array<string>
): Array<string> | any {
if (!header.length) {
diff --git a/src/lib/info.ts b/src/lib/info.ts
index f7d3378..814992b 100644
--- a/src/lib/info.ts
+++ b/src/lib/info.ts
@@ -1,6 +1,7 @@
-import Izuku from '../index';
+import { Frame } from '../index';
import { table } from 'table';
import { sizeof } from '../helpers/memorySize';
+import { flatten } from './data';
/**
* size returns the total number of elements in the frame
* @returns the total number of elements in the frame
@@ -8,29 +9,14 @@ import { sizeof } from '../helpers/memorySize';
*/
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;
+ return flatten(rowdata).length;
}
/**
* 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 {
+export function info(this: Frame): void {
const info: Array<any[]> = [];
info.push(['#', 'Column Name', 'Types', 'Empty Values']);
let counter = 0;