Skip to content

Commit

Permalink
feat: you can now specify a custom delimiter when distincting on a co…
Browse files Browse the repository at this point in the history
…lumn (#20)

* feat: you can now specify a custom delimiter when distincting on a column
* chore: update readme
  • Loading branch information
jelmerveen authored Nov 8, 2023
1 parent 0be741b commit ca61595
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,10 @@ select(selection: Array<string> | 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> | string)
distinct(properties: Array<string> | 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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@pennions/joq",
"private": false,
"version": "1.3.0",
"version": "1.4.0",
"type": "module",
"files": [
"dist"
Expand Down
10 changes: 10 additions & 0 deletions src/functions/__tests__/distinct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions src/functions/distinct.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getColumnValue } from "./services/value";

export function distinctJsonProperties(jsonArray: Array<any>, columnNames: Array<string>) {
export function distinctJsonProperties(jsonArray: Array<any>, columnNames: Array<string>, concatenationToken: string) {

/** Nothing to distinct */
if (!columnNames || !columnNames.length) {
Expand Down Expand Up @@ -87,7 +87,7 @@ export function distinctJsonProperties(jsonArray: Array<any>, 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);
}
}
});
Expand Down
10 changes: 8 additions & 2 deletions src/joq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class JOQ {
private selection: Array<string> = [];
private groupByProperties: Array<string> = [];
private distinctProperties: Array<string> = [];
private concatenationToken: string = ', ';

/**
* Jelmers Object Query Class
Expand Down Expand Up @@ -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> | string) {
distinct(properties: Array<string> | string, concatenationToken?: string) {

if (concatenationToken) {
this.concatenationToken = concatenationToken;
}

if (Array.isArray(properties)) {
this.distinctProperties = properties;
}
Expand All @@ -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;
Expand Down

0 comments on commit ca61595

Please sign in to comment.