Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Create a merge pipe
  • Loading branch information
flohw committed Feb 1, 2021
commit 0d8d8d9d9f063f2fe4a9bdcfbc5231899270183b
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ You can find the documentations in the [`docs`](./docs) folder or on [`GitBook`]
- [`union`](./docs/array.md#union)
- [`range`](./docs/array.md#range)
- [`map`](./docs/array.md#map)
- [`merge`](./docs/array.md#merge)
- [`pluck`](./docs/array.md#pluck)
- [`where`](./docs/array.md#where)
- [`firstOrDefault`](./docs/array.md#firstordefault)
Expand Down
24 changes: 24 additions & 0 deletions docs/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [`union`](#union)
- [`range`](#range)
- [`map`](#map)
- [`merge`](#merge)
- [`pluck`](#pluck)
- [`where`](#where)
- [`firstOrDefault`](#firstordefault)
Expand Down Expand Up @@ -282,6 +283,29 @@ addOne (item) {
<!-- [2, 3, 4] -->
```

#### merge

Returns the merge of two collection, works with deep equal.

##### File

```typescript
import { NgMergePipeModule } from 'angular-pipes';
```

##### Usage

```html
{{ [1, 2, 3] | union: [1, 2] }}
<!-- [1, 2, 3, 1, 2] -->
{{ [1, 2] | union: [3, 4] }}
<!-- [1, 2, 3, 4] -->
{{ [{ a: 1 }, { a: 2 }] | union: [{ a: 1 }, { a: 3 }] }}
<!-- [{ a: 1 }, { a: 2 }, { a: 1 }, { a: 3 }] (no deep here)-->
{{ [{ a: 1 }, { a: 2 }] | deep | union: [{ a: 1 }, { a: 3 }] }}
<!-- [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 1 }, { a: 3 }] -->
```

#### pluck

Returns an array of the given property of the object in the array.
Expand Down
51 changes: 51 additions & 0 deletions src/array/merge.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { UnionPipe } from './union.pipe';
import { DeepPipe } from './deep.pipe';
import {MergePipe} from "./merge.pipe";

describe('MergePipe', () => {
let pipe: MergePipe;
let deepPipe: DeepPipe;

beforeEach(() => {
pipe = new MergePipe();
deepPipe = new DeepPipe();
});

it('Should return the merge', () => {
const a = [1, 1, 1, 2, 3, 3, 4, 5];
const b = [1, 2];
const result = pipe.transform(a, b);
expect(result).toEqual([1, 1, 1, 2, 3, 3, 4, 5, 1, 2]);
expect(a).toEqual([1, 1, 1, 2, 3, 3, 4, 5]); // Check integrity
expect(b).toEqual([1, 2]); // Check integrity
});

it('Should return the union #2', () => {
const result = pipe.transform([1, 2], [3, 4]);
expect(result).toEqual([1, 2, 3, 4]);
});

it('Should return an empty array', () => {
expect(pipe.transform('a')).toEqual([]);
expect(pipe.transform([], 'a')).toEqual([]);
expect(pipe.transform(deepPipe.transform({ a: 1 }), [])).toEqual([]);
});

it('Should return the union with no deep equal', () => {
const collection = [{ a: 1, b: { c: 2 } }, { a: 2, b: { c: 3 } }, { a: 2, b: { c: 3 } }, { a: 1, b: { c: 2 } }];

const collection2 = [{ a: 1, b: { c: 2 } }];

expect(pipe.transform(collection, collection2)).toEqual(collection.concat(collection2));
});

it('Should return union with deep equal', () => {
const collection = [{ a: 1, b: { c: 2 } }, { a: 2, b: { c: 3 } }, { a: 2, b: { c: 3 } }, { a: 1, b: { c: 2 } }];

const collection2 = [{ a: 1, b: { c: 2 } }, { a: 2, b: { c: 3 } }, { a: 3, b: { c: 2 } }];

const deep = deepPipe.transform(collection);

expect(pipe.transform(deep, collection2)).toEqual(collection.concat(collection2));
});
});
34 changes: 34 additions & 0 deletions src/array/merge.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Pipe, PipeTransform, NgModule } from '@angular/core';
import { isArray, isDeepObject, unwrapDeep, deepIndexOf } from '../utils/utils';

@Pipe({
name: 'merge',
})
export class MergePipe implements PipeTransform {
transform(a?: any, b?: any): any {
if ((!isArray(a) && !isDeepObject(a)) || !isArray(b)) {
return [];
}

if (isDeepObject(a)) {
const unwrapped = unwrapDeep(a);
if (!isArray(unwrapped)) {
return [];
}

return []
.concat(unwrapped)
.concat(b);
}

return []
.concat(a)
.concat(b);
}
}

@NgModule({
declarations: [MergePipe],
exports: [MergePipe],
})
export class NgMergePipeModule {}