-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paul Robinson
committed
Dec 9, 2016
1 parent
2a2c9bb
commit 7a31659
Showing
3 changed files
with
106 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }] ] ]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters