Skip to content

Commit

Permalink
feat(PickPipe): Add pick pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
danrevah committed Dec 10, 2016
1 parent fe3e30c commit b8ac3c1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
- [keys](#keys)
- [values](#values)
- [pairs](#pairs)
- [pick](#pick)
- [Math](#math)
- [min](#min)
- [max](#max)
Expand Down Expand Up @@ -279,7 +280,6 @@ API: `string | underscore`
<p>{{'FooBar' | underscore }}</p> <!-- Output: "foo_bar" -->
```


## Array

### diff
Expand Down Expand Up @@ -579,6 +579,17 @@ API: `object | pairs`
<p>{{ {foo: [1, 2], bar: [3, 4]} | pairs }}</p> <!-- Output: "[['foo', [1, 2]], ['bar', [3, 4]]]" -->
```
### pick
Returns object with picked keys from object
API: `object | pick: [key | string]]`
```html
<p>{{ {foo: 1, bar: 2} | pick: 'foo' }}</p> <!-- Output: "{foo: 1}" -->
<p>{{ {foo: 1, bar: 2} | pick: 'foo': 'bar' }}</p> <!-- Output: "{foo: 1, bar: 2}" -->
```
## Math
### min
Expand Down
8 changes: 5 additions & 3 deletions src/app/pipes/object/index.ts
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';
26 changes: 26 additions & 0 deletions src/app/pipes/object/pick.spec.ts
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});
});
});
16 changes: 16 additions & 0 deletions src/app/pipes/object/pick.ts
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]}), {});
}
}

0 comments on commit b8ac3c1

Please sign in to comment.