diff options
| author | Priyansh <[email protected]> | 2022-01-20 18:55:01 -0500 |
|---|---|---|
| committer | Priyansh <[email protected]> | 2022-01-20 18:55:01 -0500 |
| commit | 90be11f8e8ad16ef2a68d71d4523f813db18e6ef (patch) | |
| tree | 19b9fced8668b83066f3f98a95af67877e721072 | |
| parent | d0352ed005d0bceb345368274bff4c4c605ed396 (diff) | |
| download | izuku.js-90be11f8e8ad16ef2a68d71d4523f813db18e6ef.tar.xz izuku.js-90be11f8e8ad16ef2a68d71d4523f813db18e6ef.zip | |
feat: added arrayFunctions and rename Izuku to Frame
| -rw-r--r-- | src/helpers/arrayFunctions.ts | 56 | ||||
| -rw-r--r-- | src/index.ts | 33 | ||||
| -rw-r--r-- | src/lib/data.ts | 16 | ||||
| -rw-r--r-- | src/lib/display.ts | 8 | ||||
| -rw-r--r-- | src/lib/frame.ts | 6 | ||||
| -rw-r--r-- | src/lib/info.ts | 22 | ||||
| -rw-r--r-- | tests/columns.test.ts | 24 | ||||
| -rw-r--r-- | tests/frames.test.ts | 6 | ||||
| -rw-r--r-- | tests/info.test.ts | 4 | ||||
| -rw-r--r-- | tests/printing.test.ts | 4 |
10 files changed, 143 insertions, 36 deletions
diff --git a/src/helpers/arrayFunctions.ts b/src/helpers/arrayFunctions.ts new file mode 100644 index 0000000..7e78e29 --- /dev/null +++ b/src/helpers/arrayFunctions.ts @@ -0,0 +1,56 @@ +/** + * isArrayOfType check if the array contains only the specified type + * @param array: the array to be checked + * @param type: the type to be checked + * @returns true if the array contains only the specified type + */ + +export function isArrayOfType(array: any[], type: string): boolean { + return array.filter((i) => typeof i === type).length === array.length; +} + +/** + * range - returns an array of numbers from start to end + * @param start: the start of the range + * @param end: the end of the range + * @param step: the step of the range + * @returns an array of numbers from start to end + */ + +export function range( + start: number, + end: number, + step = 1, + remove?: Array<number> +): Array<number> { + // Check if start, end and step are integers + if ( + !Number.isInteger(start) || + !Number.isInteger(end) || + !Number.isInteger(step) + ) { + throw new Error('range parameters must be integers'); + } + const rangeArray: Array<number> = []; + // Start must be less than end + if (start > end) { + throw new Error('starting value must be less than end value'); + } + + // Step must be greater than 0 + if (step <= 0) { + throw new Error('step must be greater than 0'); + } + + // If remove is specified, remove the values from the range + if (remove) { + for (let i = start; i <= end; i += step) { + if (!remove.includes(i)) { + rangeArray.push(i); + } + } + } + + // return the range + return rangeArray; +} diff --git a/src/index.ts b/src/index.ts index d4be9aa..e97d401 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,8 @@ import { data, generateHeader, setHeader, header } from './lib/frame'; import { getSingleColumnDetails } from './lib/locate'; import { show, head, tail } from './lib/display'; import { getSize, info } from './lib/info'; +import { flatten } from './lib/data'; +import { isArrayOfType, range } from './helpers/arrayFunctions'; class Izuku { rowdata: unknown[][] = []; @@ -24,10 +26,33 @@ class Izuku { public head = head; public tail = tail; public info = info; - public column = (column: number | string) => { - const izSampler = getSingleColumnDetails(this, column); - return new Izuku(izSampler.rowd, izSampler.rowh); + public column = (column: number | string | Array<number> | Array<string>) => { + // if a number or string is passed, return a single column + if (typeof column === 'number' || typeof column === 'string') { + const izSampler = getSingleColumnDetails(this, column); + return new Izuku(izSampler.rowd, izSampler.rowh); + } else if (Array.isArray(column)) { + // check if each element is an integer + if (isArrayOfType(column, 'number')) { + // TODO + } else if (isArrayOfType(column, 'string')) { + // TODO + } else { + throw new Error('Columns must be an array of integers or column names'); + } + } else { + throw new Error('Unexpected type of column'); + } }; + public flatten = () => { + return flatten(this.rowdata); + }; +} + +class Frame extends Izuku { + constructor(rowdata?: Array<unknown[]>, columns?: Array<string>) { + super(rowdata, columns); + } } -export = Izuku; +export { Frame, range }; 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; diff --git a/tests/columns.test.ts b/tests/columns.test.ts new file mode 100644 index 0000000..32fb6f1 --- /dev/null +++ b/tests/columns.test.ts @@ -0,0 +1,24 @@ +import { Frame } from '../src/index'; +import { data, header } from './support/people'; +import { expect } from 'chai'; + +const frame = new Frame(data, header); + +describe('columns.ts', () => { + describe('Get a single column', () => { + it('should return a column of the frame using column name', () => { + const dataToExpect = [ + 'Arthur', + 'Betty', + 'Victor', + 'Dodger', + 'Rayan', + 'Skitley', + 'Victoria', + 'Tiger', + 'Killjoy' + ]; + expect(frame.column('Name').flatten()).to.deep.equal(dataToExpect); + }); + }); +}); diff --git a/tests/frames.test.ts b/tests/frames.test.ts index ca4ad35..05ded5c 100644 --- a/tests/frames.test.ts +++ b/tests/frames.test.ts @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import Izuku from '../src/index'; +import { Frame } from '../src/index'; const data = [ ['a', 'b', 'c'], @@ -20,7 +20,7 @@ const changedHeader = ['j', 'k', 'l']; describe('frames.test.ts', () => { describe('Create a new frame with column names', () => { it('should create a new frame', () => { - const frame = new Izuku(data); + const frame = new Frame(data); expect(frame.rowdata).to.deep.equal(data); frame.header(header); expect(frame.columns).to.deep.equal(header); @@ -28,7 +28,7 @@ describe('frames.test.ts', () => { }); describe('Update a frame and column names', () => { it('should update a frame', () => { - const frame = new Izuku(data); + const frame = new Frame(data); frame.header(header); expect(frame.rowdata).to.deep.equal(data); expect(frame.columns).to.deep.equal(header); diff --git a/tests/info.test.ts b/tests/info.test.ts index c761e7f..3f8c3dc 100644 --- a/tests/info.test.ts +++ b/tests/info.test.ts @@ -1,8 +1,8 @@ -import Izuku from '../src/index'; +import { Frame } from '../src/index'; import { expect } from 'chai'; import { data, header } from './support/people'; -const frame = new Izuku(data, header); +const frame = new Frame(data, header); describe('info.ts', () => { describe('Print size of frame', () => { diff --git a/tests/printing.test.ts b/tests/printing.test.ts index 49b53b2..6f025c9 100644 --- a/tests/printing.test.ts +++ b/tests/printing.test.ts @@ -1,7 +1,7 @@ -import Izuku from '../src/index'; +import { Frame } from '../src/index'; import { data, header } from './support/people'; -const frame = new Izuku(data, header); +const frame = new Frame(data, header); describe('printing.test.ts', () => { describe('Print a frame', () => { |
