Skip to content

Commit aad1833

Browse files
committed
feat(isEmpty): add higher-order lettable version of isEmpty
1 parent 9267b30 commit aad1833

File tree

3 files changed

+44
-35
lines changed

3 files changed

+44
-35
lines changed

src/operator/isEmpty.ts

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Operator } from '../Operator';
2-
import { Subscriber } from '../Subscriber';
1+
32
import { Observable } from '../Observable';
3+
import { isEmpty as higherOrder } from '../operators';
44

55
/**
66
* If the source Observable is empty it returns an Observable that emits true, otherwise it emits false.
@@ -12,37 +12,5 @@ import { Observable } from '../Observable';
1212
* @owner Observable
1313
*/
1414
export function isEmpty<T>(this: Observable<T>): Observable<boolean> {
15-
return this.lift(new IsEmptyOperator());
16-
}
17-
18-
class IsEmptyOperator implements Operator<any, boolean> {
19-
call (observer: Subscriber<boolean>, source: any): any {
20-
return source.subscribe(new IsEmptySubscriber(observer));
21-
}
22-
}
23-
24-
/**
25-
* We need this JSDoc comment for affecting ESDoc.
26-
* @ignore
27-
* @extends {Ignored}
28-
*/
29-
class IsEmptySubscriber extends Subscriber<any> {
30-
constructor(destination: Subscriber<boolean>) {
31-
super(destination);
32-
}
33-
34-
private notifyComplete(isEmpty: boolean): void {
35-
const destination = this.destination;
36-
37-
destination.next(isEmpty);
38-
destination.complete();
39-
}
40-
41-
protected _next(value: boolean) {
42-
this.notifyComplete(false);
43-
}
44-
45-
protected _complete() {
46-
this.notifyComplete(true);
47-
}
15+
return higherOrder()(this);
4816
}

src/operators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export { findIndex } from './findIndex';
3131
export { first } from './first';
3232
export { groupBy } from './groupBy';
3333
export { ignoreElements } from './ignoreElements';
34+
export { isEmpty } from './isEmpty';
3435
export { map } from './map';
3536
export { materialize } from './materialize';
3637
export { max } from './max';

src/operators/isEmpty.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Operator } from '../Operator';
2+
import { Subscriber } from '../Subscriber';
3+
import { Observable } from '../Observable';
4+
import { OperatorFunction } from '../interfaces';
5+
6+
export function isEmpty<T>(): OperatorFunction<T, boolean> {
7+
return (source: Observable<T>) => source.lift(new IsEmptyOperator());
8+
}
9+
10+
class IsEmptyOperator implements Operator<any, boolean> {
11+
call (observer: Subscriber<boolean>, source: any): any {
12+
return source.subscribe(new IsEmptySubscriber(observer));
13+
}
14+
}
15+
16+
/**
17+
* We need this JSDoc comment for affecting ESDoc.
18+
* @ignore
19+
* @extends {Ignored}
20+
*/
21+
class IsEmptySubscriber extends Subscriber<any> {
22+
constructor(destination: Subscriber<boolean>) {
23+
super(destination);
24+
}
25+
26+
private notifyComplete(isEmpty: boolean): void {
27+
const destination = this.destination;
28+
29+
destination.next(isEmpty);
30+
destination.complete();
31+
}
32+
33+
protected _next(value: boolean) {
34+
this.notifyComplete(false);
35+
}
36+
37+
protected _complete() {
38+
this.notifyComplete(true);
39+
}
40+
}

0 commit comments

Comments
 (0)