diff options
| -rw-r--r-- | src/index.ts | 13 | ||||
| -rw-r--r-- | src/lib/data.ts | 108 | ||||
| -rw-r--r-- | src/lib/display.ts | 89 | ||||
| -rw-r--r-- | src/lib/frame.ts | 24 | ||||
| -rw-r--r-- | src/lib/info.ts | 2 | ||||
| -rw-r--r-- | tests/data.test.ts | 12 |
6 files changed, 193 insertions, 55 deletions
diff --git a/src/index.ts b/src/index.ts index 6e7bfc8..6dba20c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ import { } from './lib/locate'; import { show, head, tail } from './lib/display'; import { getSize, info } from './lib/info'; -import { flatten, fromJSON, fromCSV } from './lib/data'; +import { flatten, fromJSON, fromCSV, searchValue } from './lib/data'; import { isArrayOfType, range, flattenJSON } from './helpers/arrayFunctions'; class Izuku { @@ -63,6 +63,17 @@ class Izuku { }; public fromJSON = fromJSON; public fromCSV = fromCSV; + public find = ( + value: string | number, + options?: { + row?: number | Array<number>; + column?: number | string | Array<number> | Array<string>; + strict?: boolean; + } + ) => { + const rowdata = searchValue(this, value, options); + return new Izuku(rowdata, this.columns); + }; } class Frame extends Izuku { diff --git a/src/lib/data.ts b/src/lib/data.ts index 8174096..6a2fe34 100644 --- a/src/lib/data.ts +++ b/src/lib/data.ts @@ -85,3 +85,111 @@ export function fromCSV(this: Frame, csvpath: string): Frame { } return this; } + +/** + * searchValue = searchValue all the rows that contain the passed string or number + * @param value: the value to be searched + * @returns the rowdata that contains the value + */ +export function searchValue( + iz: Frame, + value: string | number, + options?: { + row?: number | Array<number>; + column?: number | string | Array<number> | Array<string>; + strict?: boolean; + } +): any[][] { + options = options || {}; + if (!options?.strict) { + options['strict'] = false; + } + // if no options are passed search everything + if (!options || (!options.row && !options.column)) { + const rowdata: any[][] = []; + for (let i = 0; i < iz.rowdata.length; i++) { + for (let j = 0; j < iz.rowdata[i].length; j++) { + if (options.strict) { + if (String(iz.rowdata[i][j]) === String(value)) { + rowdata.push(iz.rowdata[i]); + break; + } + } else { + if (String(iz.rowdata[i][j]).includes(String(value))) { + rowdata.push(iz.rowdata[i]); + break; + } + } + } + } + return rowdata; + } else if (options.row && !options.column) { + // if only row is passed, search the row + const rowdata: any[][] = []; + // get those particular rows + const rows = iz.row(options.row).rowdata; + for (let i = 0; i < rows.length; i++) { + for (let j = 0; j < rows[i].length; j++) { + if (options.strict) { + if (String(rows[i][j]) === String(value)) { + rowdata.push(rows[i]); + break; + } + } else { + if (String(rows[i][j]).includes(String(value))) { + rowdata.push(rows[i]); + break; + } + } + } + } + return rowdata; + } else if (options.column && !options.row) { + // if only column is passed, search the column + const columnIndexes: number[] = []; + // get those particular columns + const columns = iz.column(options.column).rowdata; + console.log(columns); + for (let i = 0; i < columns.length; i++) { + for (let j = 0; j < columns[i].length; j++) { + if (options.strict) { + if (String(columns[i][j]) === String(value)) { + columnIndexes.push(i); + break; + } + } else { + if (String(columns[i][j]).includes(String(value))) { + columnIndexes.push(i); + break; + } + } + } + } + console.log(columnIndexes); + return iz.row(columnIndexes).rowdata; + } else if (options.column && options.row) { + // if both row and column are passed, search the row and column + // get those particular rows + const rows = iz.row(options.row); + const columns = rows.column(options.column).rowdata; + const columnIndexes: number[] = []; + for (let i = 0; i < columns.length; i++) { + for (let j = 0; j < columns[i].length; j++) { + if (options.strict) { + if (String(columns[i][j]) === String(value)) { + columnIndexes.push(i); + break; + } + } else { + if (String(columns[i][j]).includes(String(value))) { + columnIndexes.push(i); + break; + } + } + } + } + return rows.row(columnIndexes).rowdata; + } else { + throw new Error('Invalid options'); + } +} diff --git a/src/lib/display.ts b/src/lib/display.ts index c33df70..4349aa5 100644 --- a/src/lib/display.ts +++ b/src/lib/display.ts @@ -41,29 +41,30 @@ function getTable( */ export function show(this: Frame): void { if (this.rowdata.length === 0) { - throw new Error('Set data before printing'); - } - const numberOfRows = this.rowdata.length; - if (numberOfRows < 7) { - console.log(table(getTable(this.rowdata, this.columns))); + console.log('No data found'); } else { - const firstThreeRows = this.rowdata.slice(0, 3); - const lastThreeRows = this.rowdata.slice(numberOfRows - 3); - const middleRow = []; - for (let i = 0; i < this.columns.length; i++) { - middleRow.push('...'); + const numberOfRows = this.rowdata.length; + if (numberOfRows < 7) { + console.log(table(getTable(this.rowdata, this.columns))); + } else { + const firstThreeRows = this.rowdata.slice(0, 3); + const lastThreeRows = this.rowdata.slice(numberOfRows - 3); + const middleRow = []; + for (let i = 0; i < this.columns.length; i++) { + middleRow.push('...'); + } + const indexRow = [ + 0, + 1, + 2, + '...', + numberOfRows - 3, + numberOfRows - 2, + numberOfRows - 1 + ]; + const combinedRow = [...firstThreeRows, [...middleRow], ...lastThreeRows]; + console.log(table(getTable(combinedRow, this.columns, indexRow))); } - const indexRow = [ - 0, - 1, - 2, - '...', - numberOfRows - 3, - numberOfRows - 2, - numberOfRows - 1 - ]; - const combinedRow = [...firstThreeRows, [...middleRow], ...lastThreeRows]; - console.log(table(getTable(combinedRow, this.columns, indexRow))); } } @@ -75,17 +76,18 @@ export function show(this: Frame): void { */ export function head(this: Frame, n = 5): void { if (this.rowdata.length === 0) { - throw new Error('Set data before printing'); - } - // Check if n is greater than the number of rows - if (n > this.rowdata.length) { - n = this.rowdata.length; - } + console.log('No data found'); + } else { + // Check if n is greater than the number of rows + if (n > this.rowdata.length) { + n = this.rowdata.length; + } - // Generate the index row - const indexRow = this.rowdata.map((row, index) => index); - const data = this.rowdata.slice(0, n); - console.log(table(getTable(data, this.columns, indexRow))); + // Generate the index row + const indexRow = this.rowdata.map((row, index) => index); + const data = this.rowdata.slice(0, n); + console.log(table(getTable(data, this.columns, indexRow))); + } } /** @@ -96,17 +98,18 @@ export function head(this: Frame, n = 5): void { */ export function tail(this: Frame, n = 5): void { if (this.rowdata.length === 0) { - throw new Error('Set data before printing'); - } - // Check if n is greater than the number of rows - if (n > this.rowdata.length) { - n = this.rowdata.length; - } + console.log('No data found'); + } else { + // Check if n is greater than the number of rows + if (n > this.rowdata.length) { + n = this.rowdata.length; + } - // Generate the index row - const indexRow = this.rowdata.map((row, index) => index); - const data = this.rowdata.slice(this.rowdata.length - n); - // Slice the index row to match the data - const slicedIndexRow = indexRow.slice(indexRow.length - n); - console.log(table(getTable(data, this.columns, slicedIndexRow))); + // Generate the index row + const indexRow = this.rowdata.map((row, index) => index); + const data = this.rowdata.slice(this.rowdata.length - n); + // Slice the index row to match the data + const slicedIndexRow = indexRow.slice(indexRow.length - n); + console.log(table(getTable(data, this.columns, slicedIndexRow))); + } } diff --git a/src/lib/frame.ts b/src/lib/frame.ts index d257fb4..d2d9eb5 100644 --- a/src/lib/frame.ts +++ b/src/lib/frame.ts @@ -42,18 +42,22 @@ export function header( */ export function setHeader(rowdata: any[][], header: any[]): Array<string> { - const maxSizedArrayLength = rowdata.reduce((acc, curr) => { - return acc.length > curr.length ? acc : curr; - }).length; - const newHeaderArray = Array(maxSizedArrayLength).fill(''); - for (let i = 0; i < maxSizedArrayLength; i++) { - if (header[i]) { - newHeaderArray[i] = header[i]; - } else { - newHeaderArray[i] = `Column ${i + 1}`; + if (rowdata?.length) { + const maxSizedArrayLength = rowdata.reduce((acc, curr) => { + return acc.length > curr.length ? acc : curr; + }).length; + const newHeaderArray = Array(maxSizedArrayLength).fill(''); + for (let i = 0; i < maxSizedArrayLength; i++) { + if (header[i]) { + newHeaderArray[i] = header[i]; + } else { + newHeaderArray[i] = `Column ${i + 1}`; + } } + return newHeaderArray; + } else { + return header; } - return newHeaderArray; } /** diff --git a/src/lib/info.ts b/src/lib/info.ts index 9338c75..3753782 100644 --- a/src/lib/info.ts +++ b/src/lib/info.ts @@ -18,7 +18,7 @@ export function getSize(rowdata: any[]): number { */ export function info(this: Frame): void { if (this.rowdata.length === 0) { - throw new Error('Frame is empty'); + console.log('Frame is empty'); } else { const info: Array<any[]> = []; info.push(['#', 'Column Name', 'Types', 'Empty Values']); diff --git a/tests/data.test.ts b/tests/data.test.ts index d922a66..b68bdcd 100644 --- a/tests/data.test.ts +++ b/tests/data.test.ts @@ -1,6 +1,7 @@ import { Frame } from '../src/index'; import { expect } from 'chai'; import JSONData from './support/users.json'; +import { data, header } from './support/people'; import path = require('path'); describe('data.ts', () => { @@ -74,4 +75,15 @@ describe('data.ts', () => { expect(new Frame().fromCSV(csvPath).columns).to.deep.equal(header); }); }); + describe('find', () => { + it('should search for a specific value', () => { + const frame = new Frame(data, header).find('Victor', { + column: 'Name', + row: 6, + strict: false + }); + expect(frame.rowdata).to.deep.equal([data[6]]); + expect(frame.columns).to.deep.equal(header); + }); + }); }); |
