Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit f2bed33

Browse files
committed
Add union pipe
1 parent eefda21 commit f2bed33

File tree

7 files changed

+136
-4
lines changed

7 files changed

+136
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ You can find the documentations in the [`docs`](./docs) folder.
3232
* [`uniq`](./docs/array.md#uniq)
3333
* [`without`](./docs/array.md#without)
3434
* [`intersection`](./docs/array.md#intersection)
35+
* [`union`](./docs/array.md#union)
3536
* [`range`](./docs/array.md#range)
3637
* [`map`](./docs/array.md#map)
3738
* [`pluck`](./docs/array.md#pluck)

docs/array.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [`uniq`](#uniq)
1010
* [`without`](#without)
1111
* [`intersection`](#intersection)
12+
* [`union`](#union)
1213
* [`range`](#range)
1314
* [`map`](#map)
1415
* [`pluck`](#pluck)
@@ -188,6 +189,25 @@ import { IntersectionPipe } from 'angular-pipes/src/array/intersection.pipe';
188189
{{ [{ a: 1 }, { a: 2 }] | deep | intersection: [{ a: 1 }, { a: 3 }] }} <!-- [{a: 1}] -->
189190
```
190191

192+
####union
193+
194+
Returns the union of two collection, works with deep equal.
195+
196+
##### File
197+
198+
```typescript
199+
import { UnionPipe } from 'angular-pipes/src/array/union.pipe';
200+
```
201+
202+
##### Usage
203+
204+
```html
205+
{{ [1, 2, 3] | union: [1, 2] }} <!-- [1, 2, 3] -->
206+
{{ [1, 2] | union: [3, 4] }} <!-- [1, 2, 3, 4] -->
207+
{{ [{ a: 1 }, { a: 2 }] | union: [{ a: 1 }, { a: 3 }] }} <!-- [{ a: 1 }, { a: 2 }, { a: 1 }, { a: 3 }] (no deep here)-->
208+
{{ [{ a: 1 }, { a: 2 }] | deep | union: [{ a: 1 }, { a: 3 }] }} <!-- [{ a: 1 }, { a: 2 }, { a: 3 }] -->
209+
```
210+
191211
####range
192212

193213
Returns a range of number with a given size (`default: 0`) and start (`default: 1`).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-pipes",
3-
"version": "6.1.2",
3+
"version": "6.2.2",
44
"description": "Angular pipes library",
55
"main": "src/index.js",
66
"jsnext:main": "esm/index.js",

src/array/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { DeepPipe } from './deep.pipe';
2525
import { ChunkPipe } from './chunk.pipe';
2626
import { FlattenPipe } from './flatten.pipe';
2727
import { IntersectionPipe } from './intersection.pipe';
28+
import { UnionPipe } from './union.pipe';
2829

2930
export * from './empty.pipe';
3031
export * from './head.pipe';
@@ -51,6 +52,7 @@ export * from './deep.pipe';
5152
export * from './chunk.pipe';
5253
export * from './flatten.pipe';
5354
export * from './intersection.pipe';
55+
export * from './union.pipe';
5456

5557

5658

@@ -80,7 +82,8 @@ export * from './intersection.pipe';
8082
ChunkPipe,
8183
FlattenPipe,
8284
FirstOrDefaultPipe,
83-
IntersectionPipe
85+
IntersectionPipe,
86+
UnionPipe
8487
],
8588
exports: [
8689
EmptyPipe,
@@ -107,7 +110,8 @@ export * from './intersection.pipe';
107110
ChunkPipe,
108111
FlattenPipe,
109112
FirstOrDefaultPipe,
110-
IntersectionPipe
113+
IntersectionPipe,
114+
UnionPipe
111115
]
112116
})
113117
export class NgArrayPipesModule {}

src/array/intersection.pipe.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('IntersectionPipe', () => {
5252
expect(pipe.transform(collection, collection2)).toEqual([]);
5353
});
5454

55-
it ('Should return unique values with deep equal', () => {
55+
it ('Should return intersection with deep equal', () => {
5656

5757
const collection = [
5858
{ a: 1, b: { c: 2 } },

src/array/union.pipe.spec.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { UnionPipe } from './union.pipe';
2+
import { DeepPipe } from './deep.pipe';
3+
4+
5+
describe('UnionPipe', () => {
6+
7+
let pipe: UnionPipe;
8+
let deepPipe: DeepPipe;
9+
10+
beforeEach(() => {
11+
pipe = new UnionPipe();
12+
deepPipe = new DeepPipe();
13+
});
14+
15+
it('Should return the union', () => {
16+
17+
const a = [1, 1, 1, 2, 3, 3, 4, 5];
18+
const b = [1, 2];
19+
const result = pipe.transform(a, b)
20+
expect(result).toEqual([1, 2, 3, 4, 5]);
21+
expect(a).toEqual([1, 1, 1, 2, 3, 3, 4, 5]); // Check integrity
22+
expect(b).toEqual([1, 2]); // Check integrity
23+
});
24+
25+
it('Should return the union #2', () => {
26+
27+
const result = pipe.transform([1, 2], [3, 4])
28+
expect(result).toEqual([1, 2, 3, 4]);
29+
});
30+
31+
it('Should return an empty array', () => {
32+
33+
expect(pipe.transform('a')).toEqual([]);
34+
expect(pipe.transform([], 'a')).toEqual([]);
35+
expect(pipe.transform(deepPipe.transform({ a: 1 }), [])).toEqual([]);
36+
});
37+
38+
39+
it ('Should return the union with no deep equal', () => {
40+
41+
const collection = [
42+
{ a: 1, b: { c: 2 } },
43+
{ a: 2, b: { c: 3 } },
44+
{ a: 2, b: { c: 3 } },
45+
{ a: 1, b: { c: 2 } }
46+
];
47+
48+
const collection2 = [
49+
{ a: 1, b: { c: 2 }}
50+
];
51+
52+
expect(pipe.transform(collection, collection2)).toEqual(collection.concat(collection2));
53+
});
54+
55+
it ('Should return union with deep equal', () => {
56+
57+
const collection = [
58+
{ a: 1, b: { c: 2 } },
59+
{ a: 2, b: { c: 3 } },
60+
{ a: 2, b: { c: 3 } },
61+
{ a: 1, b: { c: 2 } }
62+
];
63+
64+
const collection2 = [
65+
{ a: 1, b: { c: 2 }},
66+
{ a: 2, b: { c: 3 }},
67+
{ a: 3, b: { c: 2 }}
68+
];
69+
70+
const deep = deepPipe.transform(collection);
71+
72+
expect(pipe.transform(deep, collection2)).toEqual([
73+
{ a: 1, b: { c: 2 } },
74+
{ a: 2, b: { c: 3 } },
75+
{ a: 3, b: { c: 2 } }
76+
]);
77+
});
78+
});

src/array/union.pipe.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
import { isArray, isDeepObject, unwrapDeep, deepIndexOf } from '../utils/utils';
3+
4+
@Pipe({
5+
name: 'union'
6+
})
7+
export class UnionPipe implements PipeTransform {
8+
9+
transform (a?: any, b?: any): any {
10+
11+
if ((!isArray(a) && !isDeepObject(a)) || !isArray(b)) {
12+
return [];
13+
}
14+
15+
if (isDeepObject(a)) {
16+
const unwrapped = unwrapDeep(a);
17+
if (!isArray(unwrapped)) {
18+
return [];
19+
}
20+
21+
return []
22+
.concat(unwrapped)
23+
.concat(b)
24+
.filter((value: any, index: number, input: any[]) => deepIndexOf(input, value) === index);
25+
}
26+
27+
return [].concat(a).concat(b).filter((value: any, index: number, input: any[]) => input.indexOf(value) === index);
28+
}
29+
}

0 commit comments

Comments
 (0)