aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBobby <[email protected]>2022-01-28 17:22:24 -0500
committerBobby <[email protected]>2022-01-28 17:22:24 -0500
commitd70d0b343d70f7d8b5a87abdf8200de274ea8de0 (patch)
tree9685f15c08f749b9aa14123030562a8169c041b2 /src
parent6645a8a8ddcbc46b20f1c662ee259da3315e8d51 (diff)
downloadizuku.js-d70d0b343d70f7d8b5a87abdf8200de274ea8de0.tar.xz
izuku.js-d70d0b343d70f7d8b5a87abdf8200de274ea8de0.zip
feat: removeDuplicates function
Diffstat (limited to 'src')
-rw-r--r--src/index.ts10
-rw-r--r--src/lib/data.ts26
2 files changed, 35 insertions, 1 deletions
diff --git a/src/index.ts b/src/index.ts
index d705527..d69546d 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -8,7 +8,14 @@ import {
} from './lib/locate';
import { show, head, tail, showAll } from './lib/display';
import { getSize, info } from './lib/info';
-import { flatten, fromJSON, fromCSV, searchValue, sort } from './lib/data';
+import {
+ flatten,
+ fromJSON,
+ fromCSV,
+ searchValue,
+ sort,
+ removeDuplicates
+} from './lib/data';
import { toJSON, toCSV } from './lib/export';
import { isArrayOfType, range, flattenJSON } from './helpers/arrayFunctions';
@@ -86,6 +93,7 @@ class Izuku {
public toJSON = toJSON;
public toCSV = toCSV;
public sort = sort;
+ public removeDuplicates = removeDuplicates;
}
class Frame extends Izuku {
diff --git a/src/lib/data.ts b/src/lib/data.ts
index 480c309..c6ee585 100644
--- a/src/lib/data.ts
+++ b/src/lib/data.ts
@@ -230,3 +230,29 @@ export function sort(
this.rowdata = sorted;
return this;
}
+
+/**
+ * removeDuplicates - removes the duplicate rows from a column
+ * @param column: the column index or column name
+ * @returns the rowdata without duplicates
+ */
+export function removeDuplicates(this: Frame, column: string | number): Frame {
+ const columnIndex =
+ typeof column === 'number' ? column : this.columns.indexOf(column);
+ if (columnIndex === -1) {
+ throw new Error('Invalid column index');
+ }
+ const rowdata = this.rowdata;
+
+ // remove the duplicates from rowdata based on column index
+ const unique = rowdata.filter((row: any[], index: number) => {
+ for (let i = 0; i < index; i++) {
+ if (row[columnIndex] === rowdata[i][columnIndex]) {
+ return false;
+ }
+ }
+ return true;
+ });
+ this.rowdata = unique;
+ return this;
+}