diff --git a/README.md b/README.md index e23007ed..14d250b9 100644 --- a/README.md +++ b/README.md @@ -578,16 +578,35 @@ Returns sample items randomly from array ### groupBy -Returns object of grouped by items by discriminator +Returns object of grouped by items by discriminator, and supports nested properties. -**Usage:** `array | groupBy: [string | Function]` +**Usage:** `array | groupBy: [string[] | string | Function]` ```typescript -this.arrayObject = [{elm: 'foo', value: 0}, {elm: 'bar', value: 1}, {elm: 'foo', value: 2}]; +this.arrayObject = [ + {id: 1, elm: 'foo', value: 0}, + {id: 2, elm: 'bar', value: 1}, + {id: 3, elm: 'foo', value: 2}, + {id: 4, elm: 'foo', value: 2} +]; + +this.arrayNestedObject = [ + {id: 1, prop: { deep: 'foo' }}, + {id: 2, prop: { deep: 'bar' }}, + {id: 3, prop: { deep: 'foo' }}, + {id: 4, prop: { deep: 'bar' }} +]; ``` ```html -
{{ arrayObject | groupBy: 'elm' }}
+{{ arrayObject | groupBy: 'elm' }}
+ + +{{ arrayObject | groupBy: ['elm', 'value'] }}
+ + +{{ arrayNestedObject | groupBy: 'prop.deep' }}
+ ``` ### filterBy diff --git a/src/app/pipes/array/group-by.spec.ts b/src/app/pipes/array/group-by.spec.ts index c768598f..812e26fb 100644 --- a/src/app/pipes/array/group-by.spec.ts +++ b/src/app/pipes/array/group-by.spec.ts @@ -28,6 +28,7 @@ describe('GroupByPipe', () => { bar: [{key: 'bar'}, {key: 'bar'}] }); }); + it('allow function to be used as discriminator', () => { const arrayWithDiscriminator = [{key: 'foo'}, {key: 'bar'}, {key: 'foo'}, {key: 'bar'}]; const result = pipe.transform(arrayWithDiscriminator, _ => _['key']); @@ -36,4 +37,46 @@ describe('GroupByPipe', () => { bar: [{key: 'bar'}, {key: 'bar'}] }); }); + + it('group on multiple discriminator', () => { + const arrayWithDiscriminator = [ + {id: 1, key: 'foo', type: 1}, + {id: 2, key: 'foo', type: 2}, + {id: 3, key: 'foo', type: 1}, + {id: 4, key: 'foo', type: 2} + ]; + const result = pipe.transform(arrayWithDiscriminator, ['key', 'type']); + expect(result).toEqual({ + foo_1: [{id: 1, key: 'foo', type: 1}, {id: 3, key: 'foo', type: 1}], + foo_2: [{id: 2, key: 'foo', type: 2}, {id: 4, key: 'foo', type: 2}] + }); + }); + + it('group on deep property discriminator', () => { + const arrayWithDiscriminator = [ + {id: 1, prop: { deep: 'foo' }}, + {id: 2, prop: { deep: 'bar' }}, + {id: 3, prop: { deep: 'foo' }}, + {id: 4, prop: { deep: 'bar' }} + ]; + const result = pipe.transform(arrayWithDiscriminator, 'prop.deep'); + expect(result).toEqual({ + foo: [{id: 1, prop: { deep: 'foo' }}, {id: 3, prop: { deep: 'foo' }}], + bar: [{id: 2, prop: { deep: 'bar' }}, {id: 4, prop: { deep: 'bar' }}] + }); + }); + + it('group on multiple deep property discriminator', () => { + const arrayWithDiscriminator = [ + {id: 1, prop: { deep: 'foo', type: 1 }}, + {id: 2, prop: { deep: 'foo', type: 2 }}, + {id: 3, prop: { deep: 'foo', type: 1 }}, + {id: 4, prop: { deep: 'foo', type: 2 }} + ]; + const result = pipe.transform(arrayWithDiscriminator, ['prop.deep', 'prop.type']); + expect(result).toEqual({ + foo_1: [{id: 1, prop: { deep: 'foo', type: 1 }}, {id: 3, prop: { deep: 'foo', type: 1 }}], + foo_2: [{id: 2, prop: { deep: 'foo', type: 2 }}, {id: 4, prop: { deep: 'foo', type: 2 }}] + }); + }); }); diff --git a/src/app/pipes/array/group-by.ts b/src/app/pipes/array/group-by.ts index 6bb37f2c..99af21a8 100644 --- a/src/app/pipes/array/group-by.ts +++ b/src/app/pipes/array/group-by.ts @@ -4,19 +4,24 @@ import GeneralHelper from '../helpers/helpers'; @Pipe({name: 'groupBy'}) export class GroupByPipe implements PipeTransform { - transform(arr: any, ...args: any[]): any { + transform(arr: any, discriminator: any = []): any { if (!Array.isArray(arr)) { return arr; } - return this.groupBy(arr, args[0]); + return this.groupBy(arr, discriminator); } - private groupBy(list: any[], discriminator: Function | string) { + private groupBy(list: any[], discriminator: any) { return list.reduce((acc, payload) => { - const key = GeneralHelper.isFunction(discriminator) - ? (