From ca61595f59023b88a4789aa817a005f88d4fb306 Mon Sep 17 00:00:00 2001 From: Jelmer Veen Date: Wed, 8 Nov 2023 18:49:25 +0100 Subject: [PATCH] feat: you can now specify a custom delimiter when distincting on a column (#20) * feat: you can now specify a custom delimiter when distincting on a column * chore: update readme --- README.md | 3 ++- package.json | 2 +- src/functions/__tests__/distinct.test.ts | 10 ++++++++++ src/functions/distinct.ts | 4 ++-- src/joq.ts | 10 ++++++++-- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0fe7f57..3cf0c3c 100644 --- a/README.md +++ b/README.md @@ -230,9 +230,10 @@ select(selection: Array | string) This function takes an array of values which will be treated as unique. Merging the properties from the objects that also have the exact same value of this property. +The optional concatenation token will default to ', ' but in some cases you want to use a different token, for example when you are working with csv. ```js -distinct(properties: Array | string) +distinct(properties: Array | string, concatenationToken?: string) ``` **N.B.** this will also sum any numeric fields. If you do not want this behaviour, make that field distinct as well. diff --git a/package.json b/package.json index 41a9328..b0c9a61 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@pennions/joq", "private": false, - "version": "1.3.0", + "version": "1.4.0", "type": "module", "files": [ "dist" diff --git a/src/functions/__tests__/distinct.test.ts b/src/functions/__tests__/distinct.test.ts index bc3ce05..0c27480 100644 --- a/src/functions/__tests__/distinct.test.ts +++ b/src/functions/__tests__/distinct.test.ts @@ -48,6 +48,16 @@ describe("Tests the distinct function", () => { expect(result[0]).toEqual(expected); }); + test("It can distinct on a property and merge the other columns with a custom concatenator", () => { + + const expected = "R01 | R04 | R05 | R06" + + const joq = new JOQ(testArray); + joq.distinct("age", " | "); + const result = joq.execute(); + expect(result[0].rollNo).toEqual(expected); + }); + test("It can distinct on multiple columns", () => { const expected = { diff --git a/src/functions/distinct.ts b/src/functions/distinct.ts index 9a32602..9ff2af6 100644 --- a/src/functions/distinct.ts +++ b/src/functions/distinct.ts @@ -1,6 +1,6 @@ import { getColumnValue } from "./services/value"; -export function distinctJsonProperties(jsonArray: Array, columnNames: Array) { +export function distinctJsonProperties(jsonArray: Array, columnNames: Array, concatenationToken: string) { /** Nothing to distinct */ if (!columnNames || !columnNames.length) { @@ -87,7 +87,7 @@ export function distinctJsonProperties(jsonArray: Array, columnNames: Array mergedObjects.forEach((jsonObject: any) => { for (const prop in jsonObject) { if (Array.isArray(jsonObject[prop])) { - jsonObject[prop] = jsonObject[prop].join(", "); + jsonObject[prop] = jsonObject[prop].join(concatenationToken); } } }); diff --git a/src/joq.ts b/src/joq.ts index 5d40f7b..aa65beb 100644 --- a/src/joq.ts +++ b/src/joq.ts @@ -13,6 +13,7 @@ class JOQ { private selection: Array = []; private groupByProperties: Array = []; private distinctProperties: Array = []; + private concatenationToken: string = ', '; /** * Jelmers Object Query Class @@ -114,7 +115,12 @@ class JOQ { /** * distinct on specified columns in objects and make them unique and merge the other properties */ - distinct(properties: Array | string) { + distinct(properties: Array | string, concatenationToken?: string) { + + if (concatenationToken) { + this.concatenationToken = concatenationToken; + } + if (Array.isArray(properties)) { this.distinctProperties = properties; } @@ -130,7 +136,7 @@ class JOQ { const copyOfModel = JSON.parse(JSON.stringify(this.model)); const filteredJsonArray = filterJsonArray(copyOfModel, this.filterDetails); const sortedJsonArray = sortJsonArray(filteredJsonArray, this.sortDetails); - const distinctJsonArray = distinctJsonProperties(sortedJsonArray, this.distinctProperties); + const distinctJsonArray = distinctJsonProperties(sortedJsonArray, this.distinctProperties, this.concatenationToken); const selectedJsonArray = selectJsonArray(distinctJsonArray, this.selection); const groupedJsonArray = groupJsonArray(selectedJsonArray, this.groupByProperties); return groupedJsonArray;