Skip to content

Commit 13f3503

Browse files
committedAug 9, 2017
feat(every): add higher-order lettable version of every
1 parent b8e956b commit 13f3503

File tree

3 files changed

+80
-56
lines changed

3 files changed

+80
-56
lines changed
 

‎src/operator/every.ts

+4-56
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Operator } from '../Operator';
2-
import { Observer } from '../Observer';
1+
32
import { Observable } from '../Observable';
4-
import { Subscriber } from '../Subscriber';
3+
import { every as higherOrder } from '../operators';
54

65
/**
76
* Returns an Observable that emits whether or not every item of the source satisfies the condition specified.
@@ -19,56 +18,5 @@ import { Subscriber } from '../Subscriber';
1918
*/
2019
export function every<T>(this: Observable<T>, predicate: (value: T, index: number, source: Observable<T>) => boolean,
2120
thisArg?: any): Observable<boolean> {
22-
return this.lift(new EveryOperator(predicate, thisArg, this));
23-
}
24-
25-
class EveryOperator<T> implements Operator<T, boolean> {
26-
constructor(private predicate: (value: T, index: number, source: Observable<T>) => boolean,
27-
private thisArg?: any,
28-
private source?: Observable<T>) {
29-
}
30-
31-
call(observer: Subscriber<boolean>, source: any): any {
32-
return source.subscribe(new EverySubscriber(observer, this.predicate, this.thisArg, this.source));
33-
}
34-
}
35-
36-
/**
37-
* We need this JSDoc comment for affecting ESDoc.
38-
* @ignore
39-
* @extends {Ignored}
40-
*/
41-
class EverySubscriber<T> extends Subscriber<T> {
42-
private index: number = 0;
43-
44-
constructor(destination: Observer<boolean>,
45-
private predicate: (value: T, index: number, source: Observable<T>) => boolean,
46-
private thisArg: any,
47-
private source?: Observable<T>) {
48-
super(destination);
49-
this.thisArg = thisArg || this;
50-
}
51-
52-
private notifyComplete(everyValueMatch: boolean): void {
53-
this.destination.next(everyValueMatch);
54-
this.destination.complete();
55-
}
56-
57-
protected _next(value: T): void {
58-
let result = false;
59-
try {
60-
result = this.predicate.call(this.thisArg, value, this.index++, this.source);
61-
} catch (err) {
62-
this.destination.error(err);
63-
return;
64-
}
65-
66-
if (!result) {
67-
this.notifyComplete(false);
68-
}
69-
}
70-
71-
protected _complete(): void {
72-
this.notifyComplete(true);
73-
}
74-
}
21+
return higherOrder(predicate, thisArg)(this);
22+
}

‎src/operators/every.ts

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { Operator } from '../Operator';
2+
import { Observer } from '../Observer';
3+
import { Observable } from '../Observable';
4+
import { Subscriber } from '../Subscriber';
5+
import { OperatorFunction } from '../interfaces';
6+
7+
/**
8+
* Returns an Observable that emits whether or not every item of the source satisfies the condition specified.
9+
*
10+
* @example <caption>A simple example emitting true if all elements are less than 5, false otherwise</caption>
11+
* Observable.of(1, 2, 3, 4, 5, 6)
12+
* .every(x => x < 5)
13+
* .subscribe(x => console.log(x)); // -> false
14+
*
15+
* @param {function} predicate A function for determining if an item meets a specified condition.
16+
* @param {any} [thisArg] Optional object to use for `this` in the callback.
17+
* @return {Observable} An Observable of booleans that determines if all items of the source Observable meet the condition specified.
18+
* @method every
19+
* @owner Observable
20+
*/
21+
export function every<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean,
22+
thisArg?: any): OperatorFunction<T, boolean> {
23+
return (source: Observable<T>) => source.lift(new EveryOperator(predicate, thisArg, source));
24+
}
25+
26+
class EveryOperator<T> implements Operator<T, boolean> {
27+
constructor(private predicate: (value: T, index: number, source: Observable<T>) => boolean,
28+
private thisArg?: any,
29+
private source?: Observable<T>) {
30+
}
31+
32+
call(observer: Subscriber<boolean>, source: any): any {
33+
return source.subscribe(new EverySubscriber(observer, this.predicate, this.thisArg, this.source));
34+
}
35+
}
36+
37+
/**
38+
* We need this JSDoc comment for affecting ESDoc.
39+
* @ignore
40+
* @extends {Ignored}
41+
*/
42+
class EverySubscriber<T> extends Subscriber<T> {
43+
private index: number = 0;
44+
45+
constructor(destination: Observer<boolean>,
46+
private predicate: (value: T, index: number, source: Observable<T>) => boolean,
47+
private thisArg: any,
48+
private source?: Observable<T>) {
49+
super(destination);
50+
this.thisArg = thisArg || this;
51+
}
52+
53+
private notifyComplete(everyValueMatch: boolean): void {
54+
this.destination.next(everyValueMatch);
55+
this.destination.complete();
56+
}
57+
58+
protected _next(value: T): void {
59+
let result = false;
60+
try {
61+
result = this.predicate.call(this.thisArg, value, this.index++, this.source);
62+
} catch (err) {
63+
this.destination.error(err);
64+
return;
65+
}
66+
67+
if (!result) {
68+
this.notifyComplete(false);
69+
}
70+
}
71+
72+
protected _complete(): void {
73+
this.notifyComplete(true);
74+
}
75+
}

‎src/operators/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export { dematerialize } from './dematerialize';
2020
export { distinctUntilChanged } from './distinctUntilChanged';
2121
export { distinctUntilKeyChanged } from './distinctUntilKeyChanged';
2222
export { elementAt } from './elementAt';
23+
export { every } from './every';
2324
export { filter } from './filter';
2425
export { ignoreElements } from './ignoreElements';
2526
export { map } from './map';

0 commit comments

Comments
 (0)
Please sign in to comment.