diff --git a/README.md b/README.md index 4621834a..a2cf36b3 100644 --- a/README.md +++ b/README.md @@ -581,7 +581,7 @@ Returns sample items randomly from array Returns object of grouped by items by discriminator, and supports nested properties. -**Usage:** `array | groupBy: [string[] | string | Function]` +**Usage:** `array | groupBy: [string[] | string | Function]: [delimiter: string | optional, default = '|']` ```typescript this.arrayObject = [ @@ -604,6 +604,9 @@ this.arrayNestedObject = [

{{ arrayObject | groupBy: ['elm', 'value'] }}

+ + +

{{ arrayObject | groupBy: ['elm', 'value']: '_' }}

{{ arrayNestedObject | groupBy: 'prop.deep' }}

diff --git a/package.json b/package.json index f05a00c6..a2392334 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "ngx-pipes", - "version": "1.3.1", + "name": "angular2-pipes", + "version": "1.3.2", "author": "Dan Revah", "description": "Useful angular2 pipes", "license": "MIT", diff --git a/src/app/pipes/array/group-by.spec.ts b/src/app/pipes/array/group-by.spec.ts index 812e26fb..65cdd90b 100644 --- a/src/app/pipes/array/group-by.spec.ts +++ b/src/app/pipes/array/group-by.spec.ts @@ -38,20 +38,34 @@ describe('GroupByPipe', () => { }); }); - it('group on multiple discriminator', () => { + it('group on multiple discriminator with delimiter', () => { 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']); + 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 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' }}, @@ -75,8 +89,8 @@ describe('GroupByPipe', () => { ]; 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 }}] + '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 fbd5f139..3e16f89c 100644 --- a/src/app/pipes/array/group-by.ts +++ b/src/app/pipes/array/group-by.ts @@ -4,17 +4,17 @@ import {extractDeepPropertyByMapKey, isFunction} from '../helpers/helpers'; @Pipe({name: 'groupBy'}) export class GroupByPipe implements PipeTransform { - transform(input: any, discriminator: any = []): any { + transform(input: any, discriminator: any = [], delimiter = '|'): any { if (!Array.isArray(input)) { return input; } - return this.groupBy(input, discriminator); + return this.groupBy(input, discriminator, delimiter); } - private groupBy(list: any[], discriminator: any) { + private groupBy(list: any[], discriminator: any, delimiter: string) { return list.reduce((acc, payload) => { - const key = this.extractKeyByDiscriminator(discriminator, payload); + const key = this.extractKeyByDiscriminator(discriminator, payload, delimiter); acc[key] = Array.isArray(acc[key]) ? acc[key].concat([payload]) @@ -24,13 +24,13 @@ export class GroupByPipe implements PipeTransform { }, {}); } - private extractKeyByDiscriminator(discriminator, payload) { + private extractKeyByDiscriminator(discriminator, payload, delimiter) { if (isFunction(discriminator)) { return (discriminator)(payload); } if (Array.isArray(discriminator)) { - return discriminator.map(k => extractDeepPropertyByMapKey(payload, k)).join('_'); + return discriminator.map(k => extractDeepPropertyByMapKey(payload, k)).join(delimiter); } return extractDeepPropertyByMapKey(payload, discriminator);