-
Notifications
You must be signed in to change notification settings - Fork 204
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
danrevah
committed
Dec 10, 2016
1 parent
fe3e30c
commit b8ac3c1
Showing
4 changed files
with
59 additions
and
4 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 |
---|---|---|
@@ -1,19 +1,21 @@ | ||
import { KeysPipe } from './keys'; | ||
import { ValuesPipe } from './values'; | ||
import { PairsPipe } from './pairs'; | ||
import { PickPipe } from './pick'; | ||
import { NgModule } from '@angular/core'; | ||
|
||
const OBJECT_PIPES = [ | ||
KeysPipe, ValuesPipe, PairsPipe | ||
KeysPipe, ValuesPipe, PairsPipe, PickPipe | ||
]; | ||
|
||
@NgModule({ | ||
declarations: [ ...OBJECT_PIPES ], | ||
declarations: OBJECT_PIPES, | ||
imports: [], | ||
exports: [ ...OBJECT_PIPES ] | ||
exports: OBJECT_PIPES | ||
}) | ||
export class NgObjectPipesModule {} | ||
|
||
export * from './keys'; | ||
export * from './values'; | ||
export * from './pairs'; | ||
export * from './pick'; |
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 {PickPipe} from './pick'; | ||
|
||
describe('PickPipe', () => { | ||
let pipe: PickPipe; | ||
|
||
beforeEach(() => { | ||
pipe = new PickPipe(); | ||
}); | ||
|
||
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 picked object by args', () => { | ||
expect(pipe.transform({})).toEqual({}); | ||
expect(pipe.transform({foo: 1})).toEqual({}); | ||
expect(pipe.transform({a: 1, b: 2, c: 3}, 'a')).toEqual({a: 1}); | ||
expect(pipe.transform({a: 1, b: 2, c: 3}, 'a', 'b')).toEqual({a: 1, b: 2}); | ||
expect(pipe.transform({a: 1, b: 2, c: 3}, 'b', 'c')).toEqual({b: 2, c: 3}); | ||
expect(pipe.transform({a: 1, b: 2, c: 3}, 'b')).toEqual({b: 2}); | ||
}); | ||
}); |
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,16 @@ | ||
import {PipeTransform, Pipe} from '@angular/core'; | ||
import GeneralHelper from '../helpers/helpers'; | ||
|
||
@Pipe({name: 'pick'}) | ||
export class PickPipe implements PipeTransform { | ||
|
||
transform(obj: any, ...args: Array<string>): Object { | ||
if (Array.isArray(obj) || !GeneralHelper.isObject(obj)) { | ||
return obj; | ||
} | ||
|
||
return Object.keys(obj) | ||
.filter(k => !!~args.indexOf(k)) | ||
.reduce((o, k) => Object.assign(o, {[k]: obj[k]}), {}); | ||
} | ||
} |