-
Notifications
You must be signed in to change notification settings - Fork 2
/
every.ts
40 lines (35 loc) · 1007 Bytes
/
every.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { Operator, Observer, Unsubscribe } from '../index';
import { createSource } from '../sources';
import subscribe from '../utils/subscribe';
/**
* Returns a source that emits whether or not every item of the source satisfies the condition specified
*
* @param predicate - function for determining if an item meets a specified condition
* @return callbag operator
*
* @public
*/
function every<I>(predicate: (value: I) => boolean): Operator<I, boolean> {
return source => {
return createSource((next, complete, error) => {
let unsubscribe: Unsubscribe = () => {};
const observer: Observer<I> = {
next: value => {
if (!predicate(value)) {
unsubscribe();
next(false);
complete();
}
},
error,
complete: () => {
next(true);
complete();
},
};
unsubscribe = subscribe(source)(observer);
return unsubscribe;
});
};
}
export default every;