-
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.
feat(Array Filters): Group by & values & keys
- Loading branch information
danrevah
committed
Dec 9, 2016
1 parent
5ba7c15
commit d8c145b
Showing
11 changed files
with
200 additions
and
88 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
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,39 @@ | ||
import {GroupByPipe} from './group-by'; | ||
|
||
describe('GroupByPipe', () => { | ||
let pipe: GroupByPipe; | ||
|
||
beforeEach(() => { | ||
pipe = new GroupByPipe(); | ||
}); | ||
|
||
|
||
it('should create an instance', () => { | ||
expect(pipe).toBeTruthy(); | ||
}); | ||
|
||
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', () => { | ||
const arrayWithDiscriminator = [{key: 'foo'}, {key: 'bar'}, {key: 'foo'}, {key: 'bar'}]; | ||
const result = pipe.transform(arrayWithDiscriminator, 'key'); | ||
expect(result).toEqual({ | ||
foo: [{key: 'foo'}, {key: 'foo'}], | ||
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']); | ||
expect(result).toEqual({ | ||
foo: [{key: 'foo'}, {key: 'foo'}], | ||
bar: [{key: 'bar'}, {key: 'bar'}] | ||
}); | ||
}); | ||
}); |
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,26 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
import GeneralHelper from '../helpers/helpers'; | ||
|
||
@Pipe({name: 'groupBy'}) | ||
export class GroupByPipe implements PipeTransform { | ||
|
||
transform(arr: any, ...args: any[]): any { | ||
if (!Array.isArray(arr)) { | ||
return arr; | ||
} | ||
|
||
return this.groupBy(arr, args[0]); | ||
} | ||
|
||
private groupBy(list: any[], discriminator: Function | string) { | ||
return list.reduce((acc, payload) => { | ||
const key = GeneralHelper.isFunction(discriminator) | ||
? (<Function>discriminator)(payload) | ||
: payload[<string>discriminator]; | ||
|
||
return acc[key] = Array.isArray(acc[key]) | ||
? acc[key].concat([payload]) | ||
: [payload], acc; | ||
}, {}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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 {KeysPipe} from './keys'; | ||
|
||
describe('Keys Pipe', () => { | ||
let pipe: KeysPipe; | ||
|
||
beforeEach(() => { | ||
pipe = new KeysPipe(); | ||
}); | ||
|
||
it('should keep the element the same way if its not an object', () => { | ||
expect(pipe.transform([1, 2, 3])).toEqual([1, 2, 3]); | ||
expect(pipe.transform([])).toEqual([]); | ||
expect(pipe.transform('foo')).toEqual('foo'); | ||
expect(pipe.transform(null)).toEqual(null); | ||
expect(pipe.transform(undefined)).toEqual(undefined); | ||
}); | ||
|
||
it('should return array of keys', () => { | ||
expect(pipe.transform({})).toEqual([]); | ||
expect(pipe.transform({foo: 'bar'})).toEqual(['foo']); | ||
expect(pipe.transform({foo: 1, bar: 42})).toEqual(['foo', 'bar']); | ||
}); | ||
}); |
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,14 @@ | ||
import {PipeTransform, Pipe} from '@angular/core'; | ||
import GeneralHelper from '../helpers/helpers'; | ||
|
||
@Pipe({name: 'keys'}) | ||
export class KeysPipe implements PipeTransform { | ||
|
||
transform(obj: any): any[] { | ||
if (Array.isArray(obj) || !GeneralHelper.isObject(obj)) { | ||
return obj; | ||
} | ||
|
||
return Object.keys(obj); | ||
} | ||
} |
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 {ValuesPipe} from './values'; | ||
|
||
describe('Values Pipe', () => { | ||
let pipe: ValuesPipe; | ||
|
||
beforeEach(() => { | ||
pipe = new ValuesPipe(); | ||
}); | ||
|
||
it('should keep the element the same way if its not an object', () => { | ||
expect(pipe.transform([1, 2, 3])).toEqual([1, 2, 3]); | ||
expect(pipe.transform([])).toEqual([]); | ||
expect(pipe.transform('foo')).toEqual('foo'); | ||
expect(pipe.transform(null)).toEqual(null); | ||
expect(pipe.transform(undefined)).toEqual(undefined); | ||
}); | ||
|
||
it('should return array of values', () => { | ||
expect(pipe.transform({})).toEqual([]); | ||
expect(pipe.transform({foo: 'bar'})).toEqual(['bar']); | ||
expect(pipe.transform({foo: 1, bar: 42})).toEqual([1, 42]); | ||
}); | ||
}); |
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,14 @@ | ||
import {PipeTransform, Pipe} from '@angular/core'; | ||
import GeneralHelper from '../helpers/helpers'; | ||
|
||
@Pipe({name: 'values'}) | ||
export class ValuesPipe implements PipeTransform { | ||
|
||
transform(obj: any): any[] { | ||
if (Array.isArray(obj) || !GeneralHelper.isObject(obj)) { | ||
return obj; | ||
} | ||
|
||
return Object.keys(obj).map(k => obj[k]); | ||
} | ||
} |
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