Skip to content

Commit

Permalink
Add Group By Pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Robinson committed Dec 9, 2016
1 parent 2a2c9bb commit 7a31659
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 20 deletions.
61 changes: 61 additions & 0 deletions src/app/pipes/array/groupBy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { GroupByPipe } from './groupBy';

describe('Pipe: GroupBy', () => {
it('create an instance', () => {
let pipe = new GroupByPipe();
expect(pipe).toBeTruthy();
});

});
describe('GroupByPipe', () => {
let pipe: GroupByPipe;

beforeEach(() => {
pipe = new GroupByPipe();
});

it('should not change anything if not array', () => {
expect(pipe.transform('foo')).toEqual('foo');
expect(pipe.transform(null)).toEqual(null);
expect(pipe.transform(undefined)).toEqual(undefined);
expect(pipe.transform(42)).toEqual(42);
expect(pipe.transform({ foo: 1, bar: 2 })).toEqual({ foo: 1, bar: 2 });
});

it('group on discriminator', () => {
let arrayWithDiscriminator = [
{
key: 'value0'
},
{
key: 'value1'
},
{
key: 'value0'
},
{
key: 'value1'
}
];
let result = pipe.transform(arrayWithDiscriminator, 'key');
expect(result).toEqual([ [ 'value0', [ { key: 'value0' }, { key: 'value0' }] ], [ 'value1', [ { key: 'value1' }, { key: 'value1' }] ] ]);
});
it('allow function to be used as discriminator', () => {
let arrayWithDiscriminator = [
{
key: 'value0'
},
{
key: 'value1'
},
{
key: 'value0'
},
{
key: 'value1'
}
];
let result = pipe.transform(arrayWithDiscriminator, (instance: any) => { return instance[ 'key' ] });
expect(result).toEqual([ [ 'value0', [ { key: 'value0' }, { key: 'value0' }] ], [ 'value1', [ { key: 'value1' }, { key: 'value1' }] ] ]);
});
});
23 changes: 23 additions & 0 deletions src/app/pipes/array/groupBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'groupBy'
})
export class GroupByPipe implements PipeTransform {
groupBy = (list: any[], discriminator: Function | string) => {
let map = list.reduce((acc, payload, i) => {
let key = discriminator instanceof Function ? discriminator(payload) : payload[ discriminator ];
return Object.assign({}, acc, {
[ key ]: acc[ key ] instanceof Array ? acc[ key ].concat([ payload ]) : [ payload ]
});
}, {});
return Object.keys(map).map((key: string) => [ key, map[ key ] ]);
}
transform(arr: any, ...args: any[]): any {
if (!Array.isArray(arr)) {
return arr;
}
return this.groupBy(arr, args[ 0 ]);
}

}
42 changes: 22 additions & 20 deletions src/app/pipes/array/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import {DiffPipe} from './diff';
import {InitialPipe} from './initial';
import {FlattenPipe} from './flatten';
import {IntersectionPipe} from './intersection';
import {ReversePipe} from './reverse';
import {TailPipe} from './tail';
import {TrurthifyPipe} from './truthify';
import {UnionPipe} from './union';
import {UniquePipe} from './unique';
import {WithoutPipe} from './without';
import {PluckPipe} from './pluck';
import {ShufflePipe} from './shuffle';
import {EveryPipe} from './every';
import {SomePipe} from './some';
import {SamplePipe} from './sample';
import {NgModule} from '@angular/core';
import { DiffPipe } from './diff';
import { InitialPipe } from './initial';
import { FlattenPipe } from './flatten';
import { IntersectionPipe } from './intersection';
import { ReversePipe } from './reverse';
import { TailPipe } from './tail';
import { TrurthifyPipe } from './truthify';
import { UnionPipe } from './union';
import { UniquePipe } from './unique';
import { WithoutPipe } from './without';
import { PluckPipe } from './pluck';
import { ShufflePipe } from './shuffle';
import { EveryPipe } from './every';
import { SomePipe } from './some';
import { SamplePipe } from './sample';
import { GroupByPipe } from './groupBy';
import { NgModule } from '@angular/core';

const ARRAY_PIPES = [
DiffPipe, FlattenPipe, InitialPipe, IntersectionPipe, ReversePipe, TailPipe,
TrurthifyPipe, UnionPipe, UniquePipe, WithoutPipe, PluckPipe, ShufflePipe,
EveryPipe, SomePipe, SamplePipe
EveryPipe, SomePipe, SamplePipe, GroupByPipe
];

@NgModule({
declarations: [...ARRAY_PIPES],
declarations: [ ...ARRAY_PIPES ],
imports: [],
exports: [...ARRAY_PIPES]
exports: [ ...ARRAY_PIPES ]
})
export class NgArrayPipesModule {}
export class NgArrayPipesModule { }

export * from './diff';
export * from './initial';
Expand All @@ -43,3 +44,4 @@ export * from './shuffle';
export * from './every';
export * from './some';
export * from './sample';
export * from './groupBy';

0 comments on commit 7a31659

Please sign in to comment.