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

Commit 072e2fe

Browse files
committed
takeUntil and takeWhile pipes
1 parent c5cd616 commit 072e2fe

File tree

11 files changed

+212
-7
lines changed

11 files changed

+212
-7
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# 6.3.0
2+
3+
## New pipes
4+
5+
* [`takeWhile`](./docs/array.md#takeWhile)
6+
* [`takeUntil`](./docs/array.md#takeUntil)
7+
8+
# 6.2.0
9+
10+
## New pipes
11+
12+
* [`union`](./docs/array.md#union)
13+
114
# 6.1.0
215

316
## New pipes

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ You can find the documentations in the [`docs`](./docs) folder.
4545
* [`every`](./docs/array.md#every)
4646
* [`shuffle`](./docs/array.md#shuffle)
4747
* [`take`](./docs/array.md#take)
48+
* [`takeUntil`](./docs/array.md#takeuntil)
49+
* [`takeWhile`](./docs/array.md#takewhile)
4850
* [`drop`](./docs/array.md#drop)
4951
* [`deep`](./docs/array.md#deep)
5052
* [`chunk`](./docs/array.md#chunk)

docs/array.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* [`every`](#every)
2323
* [`shuffle`](#shuffle)
2424
* [`take`](#take)
25+
* [`takeUntil`](#takeuntil)
26+
* [`takeWhile`](#takewhile)
2527
* [`drop`](#drop)
2628
* [`deep`](#deep)
2729
* [`chunk`](#chunk)
@@ -556,6 +558,52 @@ import { TakePipe } from 'angular-pipes/src/array/take.pipe';
556558
{{ [1, 2, 3, 4] | take: 2 }} <!-- [1, 2] -->
557559
```
558560

561+
562+
####takeuntil
563+
564+
Take until the condition is met.
565+
566+
##### File
567+
568+
```typescript
569+
import { TakeUntilPipe } from 'angular-pipes/src/array/take-until.pipe';
570+
```
571+
572+
##### Usage
573+
574+
```typescript
575+
function predicate (item: any) {
576+
return item >= 4;
577+
}
578+
```
579+
580+
```html
581+
{{ [1, 2, 3, 4] | takeUntil: predicate }} <!-- [1, 2, 3] -->
582+
```
583+
584+
####takewhile
585+
586+
Take while the condition is met.
587+
588+
##### File
589+
590+
```typescript
591+
import { TakeWhilePipe } from 'angular-pipes/src/array/take-while.pipe';
592+
```
593+
594+
##### Usage
595+
596+
```typescript
597+
function predicate (item: any) {
598+
return item < 4;
599+
}
600+
```
601+
602+
```html
603+
{{ [1, 2, 3, 4] | takeWhile }} <!-- [1, 2, 3] -->
604+
```
605+
606+
559607
####drop
560608

561609
Drop the last `n` items of an array.

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.2.0",
3+
"version": "6.3.0",
44
"description": "Angular pipes library",
55
"main": "src/index.js",
66
"jsnext:main": "esm/index.js",

src/array/every.pipe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Pipe, PipeTransform } from '@angular/core';
2-
import { every } from '../utils/utils';
2+
import { every, CollectionPredicate } from '../utils/utils';
33

44
@Pipe({
55
name: 'every'
66
})
77
export class EveryPipe implements PipeTransform {
88

9-
transform (input: any, predicate: Function): any {
9+
transform (input: any, predicate: CollectionPredicate): any {
1010

1111
return every(input, predicate);
1212
}

src/array/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import { ChunkPipe } from './chunk.pipe';
2626
import { FlattenPipe } from './flatten.pipe';
2727
import { IntersectionPipe } from './intersection.pipe';
2828
import { UnionPipe } from './union.pipe';
29+
import { TakeWhilePipe } from './take-while.pipe';
30+
import { TakeUntilPipe } from './take-until.pipe';
2931

3032
export * from './empty.pipe';
3133
export * from './head.pipe';
@@ -53,7 +55,8 @@ export * from './chunk.pipe';
5355
export * from './flatten.pipe';
5456
export * from './intersection.pipe';
5557
export * from './union.pipe';
56-
58+
export * from './take-while.pipe';
59+
export * from './take-until.pipe';
5760

5861

5962
@NgModule({
@@ -83,7 +86,9 @@ export * from './union.pipe';
8386
FlattenPipe,
8487
FirstOrDefaultPipe,
8588
IntersectionPipe,
86-
UnionPipe
89+
UnionPipe,
90+
TakeWhilePipe,
91+
TakeUntilPipe
8792
],
8893
exports: [
8994
EmptyPipe,
@@ -111,7 +116,9 @@ export * from './union.pipe';
111116
FlattenPipe,
112117
FirstOrDefaultPipe,
113118
IntersectionPipe,
114-
UnionPipe
119+
UnionPipe,
120+
TakeWhilePipe,
121+
TakeUntilPipe
115122
]
116123
})
117124
export class NgArrayPipesModule {}

src/array/take-until.pipe.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { TakeUntilPipe } from './take-until.pipe';
2+
3+
4+
describe('TakeUntilPipe', () => {
5+
6+
let pipe: TakeUntilPipe;
7+
8+
beforeEach(() => {
9+
pipe = new TakeUntilPipe();
10+
});
11+
12+
it ('Should return until the condition is met', () => {
13+
const predicate = (item) => item === 1;
14+
15+
expect(pipe.transform([2, 3, 4, 5, 1, 7, 8], predicate)).toEqual([2, 3, 4, 5]);
16+
});
17+
18+
it ('Should return until the condition is met #2', () => {
19+
const predicate = (item) => item === 1;
20+
21+
expect(pipe.transform([2, 3, 4, 5, 7, 8], predicate)).toEqual([2, 3, 4, 5, 7, 8]);
22+
});
23+
24+
25+
it('Should return the objects until a is 5', () => {
26+
27+
const predicate = (item: any) => item.a === 4;
28+
29+
const array = [{a:2}, {a:3}, {a:5}, {a:4}, {a:1}];
30+
31+
expect(pipe.transform(array, predicate)).toEqual([{a:2}, {a:3}, {a:5}]);
32+
});
33+
34+
it('Should return the input', () => {
35+
expect(pipe.transform([1])).toEqual([1]);
36+
});
37+
38+
it('Should return the value unchanged', () => {
39+
expect(pipe.transform('a', null)).toEqual('a');
40+
});
41+
});

src/array/take-until.pipe.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
import { isArray, takeUntil, CollectionPredicate, isNil } from '../utils/utils';
3+
4+
@Pipe({
5+
name: 'takeUntil'
6+
})
7+
export class TakeUntilPipe implements PipeTransform {
8+
9+
transform (input: any, predicate?: CollectionPredicate): any {
10+
11+
if (!isArray(input) || isNil(predicate)) {
12+
return input;
13+
}
14+
15+
return takeUntil(input, predicate);
16+
}
17+
}

src/array/take-while.pipe.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
import { isArray, takeWhile, CollectionPredicate, isNil } from '../utils/utils';
3+
4+
@Pipe({
5+
name: 'takeWhile'
6+
})
7+
export class TakeWhilePipe implements PipeTransform {
8+
9+
transform (input: any, predicate?: CollectionPredicate): any {
10+
11+
if (!isArray(input) || isNil(predicate)) {
12+
return input;
13+
}
14+
15+
return takeWhile(input, predicate);
16+
}
17+
}

src/array/take-while.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { TakeWhilePipe } from './take-while.pipe';
2+
3+
4+
describe('TakeWhilePipe', () => {
5+
6+
let pipe: TakeWhilePipe;
7+
8+
beforeEach(() => {
9+
pipe = new TakeWhilePipe();
10+
});
11+
12+
it ('Should return while the condition is met', () => {
13+
14+
const predicate = (item) => item < 4;
15+
16+
expect(pipe.transform([1, 2, 3, 4, 1, 5], predicate)).toEqual([1, 2, 3]);
17+
});
18+
19+
it ('Should return while the condition is met #2', () => {
20+
21+
const predicate = (item) => item < 6;
22+
23+
expect(pipe.transform([1, 2, 3, 4, 5], predicate)).toEqual([1, 2, 3, 4, 5]);
24+
});
25+
26+
27+
it('Should return the objects while the condition is met', () => {
28+
29+
const predicate = (item: any) => item.a < 5;
30+
31+
const array = [{a:2}, {a:3}, {a:5}, {a:4}, {a:1}];
32+
33+
expect(pipe.transform(array, predicate)).toEqual([{a:2}, {a:3}]);
34+
});
35+
36+
37+
38+
it('Should return the input', () => {
39+
expect(pipe.transform([1])).toEqual([1]);
40+
});
41+
42+
it('Should return the value unchanged', () => {
43+
expect(pipe.transform('a', null)).toEqual('a');
44+
});
45+
});

0 commit comments

Comments
 (0)