This repository was archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathcount.js
58 lines (51 loc) · 2.03 KB
/
count.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var CountObservable = (function (__super__) {
inherits(CountObservable, __super__);
function CountObservable(source, fn) {
this.source = source;
this._fn = fn;
__super__.call(this);
}
CountObservable.prototype.subscribeCore = function (o) {
return this.source.subscribe(new CountObserver(o, this._fn, this.source));
};
return CountObservable;
}(ObservableBase));
var CountObserver = (function (__super__) {
inherits(CountObserver, __super__);
function CountObserver(o, fn, s) {
this._o = o;
this._fn = fn;
this._s = s;
this._i = 0;
this._c = 0;
__super__.call(this);
}
CountObserver.prototype.next = function (x) {
if (this._fn) {
var result = tryCatch(this._fn)(x, this._i++, this._s);
if (result === errorObj) { return this._o.onError(result.e); }
Boolean(result) && (this._c++);
} else {
this._c++;
}
};
CountObserver.prototype.error = function (e) { this._o.onError(e); };
CountObserver.prototype.completed = function () {
this._o.onNext(this._c);
this._o.onCompleted();
};
return CountObserver;
}(AbstractObserver));
/**
* Returns an observable sequence containing a value that represents how many elements in the specified observable sequence satisfy a condition if provided, else the count of items.
* @example
* res = source.count();
* res = source.count(function (x) { return x > 3; });
* @param {Function} [predicate]A function to test each element for a condition.
* @param {Any} [thisArg] Object to use as this when executing callback.
* @returns {Observable} An observable sequence containing a single element with a number that represents how many elements in the input sequence satisfy the condition in the predicate function if provided, else the count of items in the sequence.
*/
observableProto.count = function (predicate, thisArg) {
var fn = bindCallback(predicate, thisArg, 3);
return new CountObservable(this, fn);
};