Skip to content

Commit

Permalink
bug(danrevah#22): Delimiter is now configurable, default has changed …
Browse files Browse the repository at this point in the history
…to '|'
  • Loading branch information
danrevah committed Feb 1, 2017
1 parent 4c4329f commit 2c4dc9f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -604,6 +604,9 @@ this.arrayNestedObject = [
<!-- Output: "{foo: [{id: 1, elm: 'foo', value: 0}, {id: 3, elm: 'foo', value: 2}, {id: 4, elm: 'foo', value: 2}], bar: [{id: 2, elm: 'bar', value: 1}]}" -->

<p>{{ arrayObject | groupBy: ['elm', 'value'] }}</p>
<!-- Output: "{'foo|0': [{elm: foo, value: 0}], 'bar|1': [{elm:bar,value: 1}], 'foo|2': [{elm:foo, value: 2}], 'bar|3': [{elm:bar, value: 3}]}" -->

<p>{{ arrayObject | groupBy: ['elm', 'value']: '_' }}</p>
<!-- Output: "{foo_0: [{elm: foo, value: 0}], bar_1: [{elm:bar,value: 1}], foo_2: [{elm:foo, value: 2}], bar_3: [{elm:bar, value: 3}]}" -->

<p>{{ arrayNestedObject | groupBy: 'prop.deep' }}</p>
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
22 changes: 18 additions & 4 deletions src/app/pipes/array/group-by.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }},
Expand All @@ -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 }}]
});
});
});
12 changes: 6 additions & 6 deletions src/app/pipes/array/group-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -24,13 +24,13 @@ export class GroupByPipe implements PipeTransform {
}, {});
}

private extractKeyByDiscriminator(discriminator, payload) {
private extractKeyByDiscriminator(discriminator, payload, delimiter) {
if (isFunction(discriminator)) {
return (<Function>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, <string>discriminator);
Expand Down

0 comments on commit 2c4dc9f

Please sign in to comment.