diff --git a/README.md b/README.md index 16c5eb74..97fabaf2 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ - [keys](#keys) - [values](#values) - [pairs](#pairs) + - [pick](#pick) - [Math](#math) - [min](#min) - [max](#max) @@ -279,7 +280,6 @@ API: `string | underscore`

{{'FooBar' | underscore }}

``` - ## Array ### diff @@ -579,6 +579,17 @@ API: `object | pairs`

{{ {foo: [1, 2], bar: [3, 4]} | pairs }}

``` +### pick + +Returns object with picked keys from object + +API: `object | pick: [key | string]]` + +```html +

{{ {foo: 1, bar: 2} | pick: 'foo' }}

+

{{ {foo: 1, bar: 2} | pick: 'foo': 'bar' }}

+``` + ## Math ### min diff --git a/src/app/pipes/object/index.ts b/src/app/pipes/object/index.ts index 005189d6..dbc4d2c5 100644 --- a/src/app/pipes/object/index.ts +++ b/src/app/pipes/object/index.ts @@ -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'; diff --git a/src/app/pipes/object/pick.spec.ts b/src/app/pipes/object/pick.spec.ts new file mode 100644 index 00000000..675e366a --- /dev/null +++ b/src/app/pipes/object/pick.spec.ts @@ -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}); + }); +}); diff --git a/src/app/pipes/object/pick.ts b/src/app/pipes/object/pick.ts new file mode 100644 index 00000000..d8ddb516 --- /dev/null +++ b/src/app/pipes/object/pick.ts @@ -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): 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]}), {}); + } +}