diff --git a/README.md b/README.md index e58c02a1..c4516f1a 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,9 @@ - [values](#values) - [pairs](#pairs) - [pick](#pick) + - [omit](#omit) - [invert](#invert) - - [invertby](#invertby) + - [invertBy](#invertby) - [Math](#math) - [min](#min) - [max](#max) @@ -592,6 +593,17 @@ API: `object | pick: [key | string]]`
{{ {foo: 1, bar: 2} | pick: 'foo': 'bar' }}
``` +### omit + +Returns object after omitting keys from object (opposite of pick) + +API: `object | omit: [key | string]]` + +```html +{{ {foo: 1, bar: 2} | omit: 'foo' }}
+{{ {foo: 1, bar: 2} | omit: 'foo': 'bar' }}
+``` + ### invert Returns object with inverted keys and values. in case of equal values, subsequent values overwrite property assignments of previous values. diff --git a/src/app/pipes/object/index.ts b/src/app/pipes/object/index.ts index 6c879862..4ba02305 100644 --- a/src/app/pipes/object/index.ts +++ b/src/app/pipes/object/index.ts @@ -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({ @@ -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'; diff --git a/src/app/pipes/object/omit.spec.ts b/src/app/pipes/object/omit.spec.ts new file mode 100644 index 00000000..e1f83fbc --- /dev/null +++ b/src/app/pipes/object/omit.spec.ts @@ -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}); + }); +}); diff --git a/src/app/pipes/object/omit.ts b/src/app/pipes/object/omit.ts new file mode 100644 index 00000000..4b9ecbec --- /dev/null +++ b/src/app/pipes/object/omit.ts @@ -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