Skip to content

Commit

Permalink
feat(): Add omit pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
danrevah committed Dec 10, 2016
1 parent 4a88c9a commit 9d9da33
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@
- [values](#values)
- [pairs](#pairs)
- [pick](#pick)
- [omit](#omit)
- [invert](#invert)
- [invertby](#invertby)
- [invertBy](#invertby)
- [Math](#math)
- [min](#min)
- [max](#max)
Expand Down Expand Up @@ -592,6 +593,17 @@ API: `object | pick: [key | string]]`
<p>{{ {foo: 1, bar: 2} | pick: 'foo': 'bar' }}</p> <!-- Output: "{foo: 1, bar: 2}" -->
```
### omit
Returns object after omitting keys from object (opposite of pick)
API: `object | omit: [key | string]]`
```html
<p>{{ {foo: 1, bar: 2} | omit: 'foo' }}</p> <!-- Output: "{bar: 2}" -->
<p>{{ {foo: 1, bar: 2} | omit: 'foo': 'bar' }}</p> <!-- Output: "{}" -->
```
### invert
Returns object with inverted keys and values. in case of equal values, subsequent values overwrite property assignments of previous values.
Expand Down
5 changes: 4 additions & 1 deletion src/app/pipes/object/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { KeysPipe } from './keys';
import { ValuesPipe } from './values';
import { PairsPipe } from './pairs';
import { PickPipe } from './pick';
import { OmitPipe } from './omit';
import { InvertPipe } from './invert';
import { InvertByPipe } from './invert-by';
import { NgModule } from '@angular/core';

const OBJECT_PIPES = [
KeysPipe, ValuesPipe, PairsPipe, PickPipe, InvertPipe, InvertByPipe
KeysPipe, ValuesPipe, PairsPipe, PickPipe, InvertPipe, InvertByPipe,
OmitPipe
];

@NgModule({
Expand All @@ -21,5 +23,6 @@ export * from './keys';
export * from './values';
export * from './pairs';
export * from './pick';
export * from './omit';
export * from './invert';
export * from './invert-by';
26 changes: 26 additions & 0 deletions src/app/pipes/object/omit.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {OmitPipe} from './omit';

describe('OmitPipe', () => {
let pipe: OmitPipe;

beforeEach(() => {
pipe = new OmitPipe();
});

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({foo: 1});
expect(pipe.transform({a: 1, b: 2, c: 3}, 'a')).toEqual({b: 2, c: 3});
expect(pipe.transform({a: 1, b: 2, c: 3}, 'a', 'b')).toEqual({c: 3});
expect(pipe.transform({a: 1, b: 2, c: 3}, 'b', 'c')).toEqual({a: 1});
expect(pipe.transform({a: 1, b: 2, c: 3}, 'b')).toEqual({a: 1, c: 3});
});
});
16 changes: 16 additions & 0 deletions src/app/pipes/object/omit.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: 'omit'})
export class OmitPipe 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]}), {});
}
}
4 changes: 1 addition & 3 deletions src/app/pipes/object/pick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export class PickPipe implements PipeTransform {
return obj;
}

return Object.keys(obj)
.filter(k => !!~args.indexOf(k))
.reduce((o, k) => Object.assign(o, {[k]: obj[k]}), {});
return args.reduce((o, k) => Object.assign(o, {[k]: obj[k]}), {});
}
}

0 comments on commit 9d9da33

Please sign in to comment.