Skip to content

Commit

Permalink
feat(Object): Add diffObj Pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
danrevah committed Mar 9, 2017
1 parent 578e8d4 commit 1aa1f45
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 5 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
- [omit](#omit)
- [invert](#invert)
- [invertBy](#invertby)
- [diffObj](#diffobj)
- [Math](#math)
- [min](#min)
- [max](#max)
Expand Down Expand Up @@ -771,6 +772,19 @@ this.cb = (value): string => {
<p>{{ {a: 1, b: 2, c: 1, d: 2} | invertBy }}</p> <!-- Output: "{1: ['a', 'c'], 2: ['b', 'd']}" -->
```

### diffObj

Returns a diff object of two objects

**Usage:** `object | diffObj: Object`

```html
<p>{{ {a: 1} | diffObj: {a: 1} }}</p> <!-- Output: "{}" -->
<p>{{ {a: 1} | diffObj: {a: 2} }}</p> <!-- Output: "{a: 1}" -->
<p>{{ {a: 1, b: 2} | diffObj: {a: 1, b: 1} }}</p> <!-- Output: "{b: 2}" -->
<p>{{ {a: 1, b: 2, c: {d: 3} } | diffObj: {a: 1, b: 1, c: {d: 1} } }}</p> <!-- Output: "{b: 2, c: {d: 3}}" -->
```

## Math

### min
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-pipes",
"version": "1.4.5",
"version": "1.4.6",
"author": "Dan Revah",
"description": "Useful angular2 pipes",
"license": "MIT",
Expand Down
12 changes: 11 additions & 1 deletion src/app/pipes/helpers/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import {extractDeepPropertyByMapKey} from './helpers';
import {extractDeepPropertyByMapKey, isDeepEqual} from './helpers';

describe('Utils Tests', () => {

Expand Down Expand Up @@ -32,4 +32,14 @@ describe('Utils Tests', () => {
expect(extractDeepPropertyByMapKey(obj, 'f.i.j.k.l')).toEqual(6);
expect(extractDeepPropertyByMapKey(obj, 'f.i.j.k.l.')).toEqual(undefined);
});

it('should deep equal properly', () => {
expect(isDeepEqual({a: 1}, {a: 1})).toBeTruthy();
expect(isDeepEqual({a: 1}, {b: 1})).toBeFalsy();
expect(isDeepEqual({a: 1}, {a: 1, b: 1})).toBeFalsy();
expect(isDeepEqual({a: 1, b: 2}, {a: 1, b: 2})).toBeTruthy();
expect(isDeepEqual({a: 1, b: 2}, {a: 1, b: 1})).toBeFalsy();
expect(isDeepEqual({a: 1, b: 2, c: {d: 3}}, {a: 1, b: 2, c: {d: 1}})).toBeFalsy();
expect(isDeepEqual({a: 1, b: 2, c: {d: 3}}, {a: 1, b: 2, c: {d: 3}})).toBeTruthy();
});
});
21 changes: 21 additions & 0 deletions src/app/pipes/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,24 @@ export function extractDeepPropertyByMapKey(obj: any, map: string): any {
: undefined;
}, obj[key || '']);
}

export function getKeysTwoObjects(obj: any, other: any): any {
return [...Object.keys(obj), ...Object.keys(other)]
.filter((key, index, array) => array.indexOf(key) === index);
}

export function isDeepEqual(obj: any, other: any): any {
if (!isObject(obj) || !isObject(other)) {
return obj === other;
}

return getKeysTwoObjects(obj, other).every((key: any): boolean => {
if (!isObject(obj[key]) && !isObject(other[key])) {
return obj[key] === other[key];
}
if (!isObject(obj[key]) || !isObject(other[key])) {
return false;
}
return isDeepEqual(obj[key], other[key]);
});
}
30 changes: 30 additions & 0 deletions src/app/pipes/object/diff-obj.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {DiffObjPipe} from './diff-obj';

describe('DiffObj Pipe', () => {
let pipe: DiffObjPipe;

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

it('should keep the element the same way if its not an object', () => {
expect(pipe.transform([1, 2, 3], {})).toEqual({});
expect(pipe.transform([], {})).toEqual({});
expect(pipe.transform('foo', {})).toEqual({});
expect(pipe.transform(null, {})).toEqual({});
expect(pipe.transform(undefined, {})).toEqual({});
});

it('should return an empty object when there is no difference', () => {
expect(pipe.transform({}, {})).toEqual({});
expect(pipe.transform({a: 1}, {a: 1})).toEqual({});
expect(pipe.transform({a: {b: 1}, c: 3}, {a: {b: 1}, c: 3})).toEqual({});
});

it('should return a diff object', () => {
expect(pipe.transform({a: 1}, {a: 2})).toEqual({a: 1});
expect(pipe.transform({a: 1, b: 1}, {a: 1, b: 2})).toEqual({b: 1});
expect(pipe.transform({a: 1, b: true}, {a: 1, b: 2})).toEqual({b: true});
expect(pipe.transform({a: 1, b: {c: 1}}, {a: 1, b: {c: 2}})).toEqual({b: {c: 1}});
});
});
16 changes: 16 additions & 0 deletions src/app/pipes/object/diff-obj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {PipeTransform, Pipe} from '@angular/core';
import {isObject, getKeysTwoObjects, isDeepEqual} from '../helpers/helpers';

@Pipe({name: 'diffObj'})
export class DiffObjPipe implements PipeTransform {

transform(obj: any, original: any = {}): any {
if (Array.isArray(obj) || Array.isArray(original) || !isObject(obj) || !isObject(original)) {
return {};
}

return getKeysTwoObjects(obj, original).reduce((diff: any, key: any) => {
return (!isDeepEqual(original[key], obj[key]) ? diff[key] = obj[key] : {}), diff;
}, {});
}
}
7 changes: 4 additions & 3 deletions src/app/pipes/object/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import {PickPipe} from './pick';
import {OmitPipe} from './omit';
import {InvertPipe} from './invert';
import {InvertByPipe} from './invert-by';
import {DiffObjPipe} from './diff-obj';
import {NgModule} from '@angular/core';

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

@NgModule({
declarations: OBJECT_PIPES,
imports: [],
exports: OBJECT_PIPES
})
export class NgObjectPipesModule {
}
export class NgObjectPipesModule {}

export {KeysPipe} from './keys';
export {ValuesPipe} from './values';
Expand All @@ -27,3 +27,4 @@ export {PickPipe} from './pick';
export {OmitPipe} from './omit';
export {InvertPipe} from './invert';
export {InvertByPipe} from './invert-by';
export {DiffObjPipe} from './diff-obj';

0 comments on commit 1aa1f45

Please sign in to comment.