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 pathlastindexof.js
65 lines (58 loc) · 2.03 KB
/
lastindexof.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
59
60
61
62
63
64
65
var LastIndexOfObservable = (function (__super__) {
inherits(LastIndexOfObservable, __super__);
function LastIndexOfObservable(source, e, n) {
this.source = source;
this._e = e;
this._n = n;
__super__.call(this);
}
LastIndexOfObservable.prototype.subscribeCore = function (o) {
if (this._n < 0) {
o.onNext(-1);
o.onCompleted();
return disposableEmpty;
}
return this.source.subscribe(new LastIndexOfObserver(o, this._e, this._n));
};
return LastIndexOfObservable;
}(ObservableBase));
var LastIndexOfObserver = (function (__super__) {
inherits(LastIndexOfObserver, __super__);
function LastIndexOfObserver(o, e, n) {
this._o = o;
this._e = e;
this._n = n;
this._v = 0;
this._hv = false;
this._i = 0;
__super__.call(this);
}
LastIndexOfObserver.prototype.next = function (x) {
if (this._i >= this._n && x === this._e) {
this._hv = true;
this._v = this._i;
}
this._i++;
};
LastIndexOfObserver.prototype.error = function (e) { this._o.onError(e); };
LastIndexOfObserver.prototype.completed = function () {
if (this._hv) {
this._o.onNext(this._v);
} else {
this._o.onNext(-1);
}
this._o.onCompleted();
};
return LastIndexOfObserver;
}(AbstractObserver));
/**
* Returns the last 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 last index at which a given element can be found in the observable sequence, or -1 if it is not present.
*/
observableProto.lastIndexOf = function(searchElement, fromIndex) {
var n = +fromIndex || 0;
Math.abs(n) === Infinity && (n = 0);
return new LastIndexOfObservable(this, searchElement, n);
};