From 6645a8a8ddcbc46b20f1c662ee259da3315e8d51 Mon Sep 17 00:00:00 2001 From: Bobby Date: Fri, 28 Jan 2022 17:16:01 -0500 Subject: feat: showAll function and fix sort method --- src/index.ts | 3 ++- src/lib/data.ts | 13 ++++++++++--- src/lib/display.ts | 14 ++++++++++++++ tests/simple.test.ts | 2 +- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index ad6ef9e..d705527 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import { getMultipleRowDetails, rangeIndex } from './lib/locate'; -import { show, head, tail } from './lib/display'; +import { show, head, tail, showAll } from './lib/display'; import { getSize, info } from './lib/info'; import { flatten, fromJSON, fromCSV, searchValue, sort } from './lib/data'; import { toJSON, toCSV } from './lib/export'; @@ -36,6 +36,7 @@ class Izuku { public data = data; public header = header; public show = show; + public showAll = showAll; public head = head; public tail = tail; public info = info; diff --git a/src/lib/data.ts b/src/lib/data.ts index e3c1b7d..480c309 100644 --- a/src/lib/data.ts +++ b/src/lib/data.ts @@ -213,11 +213,18 @@ export function sort( if (columnIndex === -1) { throw new Error('Invalid column index'); } + // sort the rowdata based on the column index, if the value is null or undefined, sort it to the end const sorted = rowdata.sort((a: any, b: any) => { - if (order === 'ascending') { - return a[columnIndex] > b[columnIndex] ? 1 : -1; + if (a[columnIndex] === null || a[columnIndex] === undefined) { + return 1; + } else if (b[columnIndex] === null || b[columnIndex] === undefined) { + return -1; } else { - return a[columnIndex] > b[columnIndex] ? -1 : 1; + if (order === 'ascending') { + return a[columnIndex] > b[columnIndex] ? 1 : -1; + } else { + return a[columnIndex] < b[columnIndex] ? 1 : -1; + } } }); this.rowdata = sorted; diff --git a/src/lib/display.ts b/src/lib/display.ts index c189434..1af1f84 100644 --- a/src/lib/display.ts +++ b/src/lib/display.ts @@ -111,6 +111,20 @@ export function show(this: Frame): void { } } +/** + * showAll prints the frame without truncating + * @returns the current frame + */ +export function showAll(this: Frame): void { + if (this.rowdata.length === 0) { + console.log('No data found'); + } else { + displayTable( + getTable(this.rowdata, this.columns, undefined, this.tableTitle) + ); + } +} + /** * head prints maximum first n rows of the frame * @param n: the number of rows to be returned diff --git a/tests/simple.test.ts b/tests/simple.test.ts index 24e0126..b813a6e 100644 --- a/tests/simple.test.ts +++ b/tests/simple.test.ts @@ -5,6 +5,6 @@ const frame = new Frame(data, header); describe('Print a frame', () => { it('should print a frame', () => { - frame.title('Simple Frame').sort(3).show(); + frame.title('Simple Frame').sort(1).showAll(); }); }); -- cgit v1.2.3