This repository has been 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
/
indexof.js
56 lines (49 loc) · 1.85 KB
/
indexof.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
var IndexOfObservable = (function (__super__) {
inherits(IndexOfObservable, __super__);
function IndexOfObservable(source, e, n) {
this.source = source;
this._e = e;
this._n = n;
__super__.call(this);
}
IndexOfObservable.prototype.subscribeCore = function (o) {
if (this._n < 0) {
o.onNext(-1);
o.onCompleted();
return disposableEmpty;
}
return this.source.subscribe(new IndexOfObserver(o, this._e, this._n));
};
return IndexOfObservable;
}(ObservableBase));
var IndexOfObserver = (function (__super__) {
inherits(IndexOfObserver, __super__);
function IndexOfObserver(o, e, n) {
this._o = o;
this._e = e;
this._n = n;
this._i = 0;
__super__.call(this);
}
IndexOfObserver.prototype.next = function (x) {
if (this._i >= this._n && x === this._e) {
this._o.onNext(this._i);
this._o.onCompleted();
}
this._i++;
};
IndexOfObserver.prototype.error = function (e) { this._o.onError(e); };
IndexOfObserver.prototype.completed = function () { this._o.onNext(-1); this._o.onCompleted(); };
return IndexOfObserver;
}(AbstractObserver));
/**
* Returns the first index at which a given element can be found in the observable sequence, or -1 if it is not present.
* @param {Any} searchElement Element to locate in the array.
* @param {Number} [fromIndex] The index to start the search. If not specified, defaults to 0.
* @returns {Observable} And observable sequence containing the first index at which a given element can be found in the observable sequence, or -1 if it is not present.
*/
observableProto.indexOf = function(searchElement, fromIndex) {
var n = +fromIndex || 0;
Math.abs(n) === Infinity && (n = 0);
return new IndexOfObservable(this, searchElement, n);
};