Skip to content

Commit

Permalink
feat(Pickers): groupBy supports dot notation (#2397)
Browse files Browse the repository at this point in the history
* Group By Accepts Dot Notation Keys

* Fixing Typescript Error Expected 1 arguments, but got 2

* feat: updated package-lock.json file

* feat: replaced reinforcements with lodash

* fix: restored package-lock.json file

* feat: added test case for the group by function

* feat: added test case for the group by function

* fix: fixed test case expected result

* feat: undone the type alias for the defer call
  • Loading branch information
hassanzohdy committed Mar 16, 2022
1 parent 7b5a195 commit 2ba7c0b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/utils/getDataGroupBy.ts
@@ -1,19 +1,26 @@
import _ from 'lodash';
import { flattenTree } from '../utils/treeUtils';

const hasSymbol = typeof Symbol === 'function';
export const KEY_GROUP = hasSymbol ? Symbol('_$grouped') : '_$grouped';
export const KEY_GROUP_TITLE = 'groupTitle';

export default function getDataGroupBy(data: any[] = [], key: string, sort): any[] {
export default function getDataGroupBy(data: any[] = [], key: string, sort?): any[] {
const tempData: { [key: string]: any[] } = {};
const isSort = typeof sort === 'function';

data.forEach(item => {
if (!tempData[item[key]]) {
tempData[item[key]] = [];
// this will allow getting data using dot notation
// i.e groupBy="country.name" as country will be a nested object
// to the item and the name will be nested key to the country object
// can be used with values in arrays, i.e groupBy="addresses.0.country.name"
const groupByValue: any = _.get(item, key, '');

if (!tempData[groupByValue]) {
tempData[groupByValue] = [];
}

tempData[item[key]].push(item);
tempData[groupByValue].push(item);
});

let nextData = Object.entries(tempData).map(([groupTitle, children]: [string, any[]]) => ({
Expand Down
45 changes: 45 additions & 0 deletions src/utils/test/getDataGroupBySpec.js
Expand Up @@ -20,6 +20,51 @@ describe('[utils] getDataGroupBy', () => {
assert.equal(groups.length, 3);
});

it('Should be grouped by country.name Using dot.notation syntax', () => {
const items = [
{
label: 'Cairo',
value: '1',
country: {
id: '1',
name: 'Egypt'
}
},
{
label: 'Alexandria',
value: '2',
country: {
id: '1',
name: 'Egypt'
}
},
{
label: 'New York',
value: '3',
country: {
id: '2',
name: 'USA'
}
},
{
label: 'Washington',
value: '4',
country: {
id: '2',
name: 'USA'
}
}
];

const groups = getDataGroupBy(items, 'country.name');

assert.equal(groups[0].groupTitle, 'Egypt');
assert.equal(groups[0][KEY_GROUP], true);
assert.equal(groups[3].groupTitle, 'USA');
assert.equal(groups[3][KEY_GROUP], true);
assert.equal(groups.length, 6);
});

it('Should be grouped by title and sorted', () => {
const items = [
{
Expand Down

1 comment on commit 2ba7c0b

@vercel
Copy link

@vercel vercel bot commented on 2ba7c0b Mar 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.