aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPriyansh <[email protected]>2022-01-19 00:57:06 -0500
committerPriyansh <[email protected]>2022-01-19 00:57:06 -0500
commit80eabe37c21ae4965dbafbb9abb5c7a153b209c5 (patch)
tree913376ae548fecbe72b2099fa006f81d50dbb5ab /src
parentbc6fa1856be7ec26208bb2c4c63b693a163506c3 (diff)
downloadizuku.js-80eabe37c21ae4965dbafbb9abb5c7a153b209c5.tar.xz
izuku.js-80eabe37c21ae4965dbafbb9abb5c7a153b209c5.zip
feat: display data in table function
Diffstat (limited to 'src')
-rw-r--r--src/index.ts41
-rw-r--r--src/interface/frameInterface.ts4
-rw-r--r--src/lib/display.ts20
-rw-r--r--src/lib/frame.ts72
4 files changed, 98 insertions, 39 deletions
diff --git a/src/index.ts b/src/index.ts
index 37fa533..f085d82 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,40 +1,3 @@
-export default class Izuku {
- constructor(rowdata?: Array<unknown[]>, header?: Array<string>) {
- this.rowdata = rowdata || [];
- this.header = header || [];
- }
+import { Frame } from './lib/frame';
- protected rowdata: Array<unknown[]> = [];
- protected header: Array<string> = [];
- /**
- * data sets the rowdata of the frame, it creates a new frame if the frame is undefined
- * @param rowdata: the rowdata to be sent to the frame
- * @returns a new frame with the rowdata or updates the current frame with new rowdata or returns the current rowdata if rowdata is present
- */
- data(rowdata?: Array<unknown[]>): Izuku | unknown[][] {
- if (rowdata) {
- this.rowdata = rowdata;
- return new Izuku(rowdata, this.header);
- } else if (this.rowdata) {
- return this.rowdata;
- } else {
- throw new Error('Frame has no rowdata.');
- }
- }
-
- /**
- * columns sets the header of the frame, it creates a new frame if the frame is undefined
- * @param header: the header to be sent to the frame
- * @returns a new frame with the header or updates the current frame with new header or returns the current header if header is present
- */
- columns(header?: Array<string>): Izuku | Array<string> {
- if (header) {
- this.header = header;
- return new Izuku(this.rowdata, header);
- } else if (this.header) {
- return this.header;
- } else {
- throw new Error('Frame has no header.');
- }
- }
-}
+export { Frame };
diff --git a/src/interface/frameInterface.ts b/src/interface/frameInterface.ts
new file mode 100644
index 0000000..bc61c90
--- /dev/null
+++ b/src/interface/frameInterface.ts
@@ -0,0 +1,4 @@
+export interface FrameInterface {
+ rowdata: Array<unknown[]>;
+ columns: Array<string>;
+}
diff --git a/src/lib/display.ts b/src/lib/display.ts
new file mode 100644
index 0000000..2247938
--- /dev/null
+++ b/src/lib/display.ts
@@ -0,0 +1,20 @@
+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;
+ })
+ );
+ }
+}
diff --git a/src/lib/frame.ts b/src/lib/frame.ts
new file mode 100644
index 0000000..dc93a2c
--- /dev/null
+++ b/src/lib/frame.ts
@@ -0,0 +1,72 @@
+import { FrameInterface } from '../interface/frameInterface';
+import { Display } from './display';
+
+export class Frame implements FrameInterface {
+ rowdata: unknown[][] = [];
+ columns!: string[];
+
+ constructor(rowdata?: Array<unknown[]>, header?: Array<string>) {
+ this.rowdata = rowdata || [];
+ this.columns = header || [];
+ }
+ /**
+ * 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
+ */
+ data(rowdata?: Array<unknown[]>): this | unknown[][] {
+ if (rowdata) {
+ this.rowdata = rowdata;
+ } else {
+ new Display().table(this);
+ return this;
+ }
+
+ 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
+ */
+ header(header: Array<string>): this | Array<string> {
+ 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;
+ }
+ }
+ }
+
+ /**
+ * 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
+ * @example
+ * const frame = new Izuku([
+ * ['a', 'b', 'c'],
+ * ['d', 'e', 'f'],
+ * ['g', 'h', 'i']
+ * ]);
+ * frame.setheader(['a', 'b', 'c']);
+ * console.log(frame.column('a'));
+ * // ['a', 'd', 'g']
+ * console.log(frame.column(1));
+ * // ['b', 'e', 'h']
+ */
+ public column(column: string | number): Array<unknown> {
+ if (typeof column === 'string') {
+ return this.rowdata.map((row) => row[this.columns.indexOf(column)]);
+ }
+ return this.rowdata.map((row) => row[column]);
+ }
+}