aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPriyansh <[email protected]>2022-01-19 01:56:04 -0500
committerPriyansh <[email protected]>2022-01-19 01:56:04 -0500
commitfbd2fa4fd9182429e54bfe96483961a98f10ac04 (patch)
treeacad8dbbe4b9c51609babc64918ee42a7c2c284b /src
parent80eabe37c21ae4965dbafbb9abb5c7a153b209c5 (diff)
downloadizuku.js-fbd2fa4fd9182429e54bfe96483961a98f10ac04.tar.xz
izuku.js-fbd2fa4fd9182429e54bfe96483961a98f10ac04.zip
refactor: make package usable in npm
Diffstat (limited to 'src')
-rw-r--r--src/index.ts75
-rw-r--r--src/lib/frame.ts72
2 files changed, 73 insertions, 74 deletions
diff --git a/src/index.ts b/src/index.ts
index f085d82..2ad9ef2 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,3 +1,74 @@
-import { Frame } from './lib/frame';
+import { FrameInterface } from './interface/frameInterface';
+import { Display } from './lib/display';
-export { Frame };
+class Izuku 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]);
+ }
+}
+
+export = Izuku;
diff --git a/src/lib/frame.ts b/src/lib/frame.ts
deleted file mode 100644
index dc93a2c..0000000
--- a/src/lib/frame.ts
+++ /dev/null
@@ -1,72 +0,0 @@
-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]);
- }
-}