-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): add elementAt operator
- adds ArgumentOutOfRangeError error type - adds elementAt operator
- Loading branch information
Showing
6 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var RxOld = require("rx"); | ||
var RxNew = require("../../../../index"); | ||
|
||
module.exports = function (suite) { | ||
|
||
var oldElementAtWithImmediateScheduler = RxOld.Observable.range(0, 25, RxOld.Scheduler.immediate).elementAt(5); | ||
var newElementAtWithImmediateScheduler = RxNew.Observable.range(0, 25).elementAt(5); | ||
|
||
return suite | ||
.add('old elementAt with immediate scheduler', function () { | ||
oldElementAtWithImmediateScheduler.subscribe(_next, _error, _complete); | ||
}) | ||
.add('new elementAt with immediate scheduler', function () { | ||
newElementAtWithImmediateScheduler.subscribe(_next, _error, _complete); | ||
}); | ||
|
||
function _next(x) { } | ||
function _error(e){ } | ||
function _complete(){ } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* globals describe, it, expect, hot, cold, expectObservable */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
|
||
describe('Observable.prototype.elementAt', function() { | ||
it("should return first element by zero-based index", function() { | ||
var source = hot('--a--b--c--|'); | ||
var expected = '--(a|)'; | ||
|
||
expectObservable(source.elementAt(0)).toBe(expected); | ||
}); | ||
|
||
it("should return non-first element by zero-based index", function() { | ||
var source = hot('--a--b--c--d--e--f--|'); | ||
var expected = '-----------(d|)'; | ||
|
||
expectObservable(source.elementAt(3)).toBe(expected); | ||
}); | ||
|
||
it("should return last element by zero-based index", function() { | ||
var source = hot('--a--b--c--|'); | ||
var expected = '--------(c|)'; | ||
|
||
expectObservable(source.elementAt(2)).toBe(expected); | ||
}); | ||
|
||
it("should throw if index is smaller than zero", function() { | ||
expect(function() { Observable.range(0,10).elementAt(-1); }) | ||
.toThrow(new Rx.ArgumentOutOfRangeError); | ||
}); | ||
|
||
it("should raise error if index is out of range but does not have default value", function() { | ||
var source = hot('--a--|'); | ||
var expected = '-----#'; | ||
|
||
expectObservable(source.elementAt(3)) | ||
.toBe(expected, null, new Rx.ArgumentOutOfRangeError); | ||
}); | ||
|
||
it("should return default value if index is out of range", function() { | ||
var source = hot('--a--|'); | ||
var expected = '-----(x|)'; | ||
var defaultValue = '42'; | ||
|
||
expectObservable(source.elementAt(3, defaultValue)).toBe(expected, { x: defaultValue }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Operator from '../Operator'; | ||
import Observer from '../Observer'; | ||
import Subscriber from '../Subscriber'; | ||
import ArgumentOutOfRangeError from '../util/ArgumentOutOfRangeError'; | ||
|
||
export default function elementAt(index: number, defaultValue?: any) { | ||
return this.lift(new ElementAtOperator(index, defaultValue)); | ||
} | ||
|
||
class ElementAtOperator<T, R> implements Operator<T,R> { | ||
|
||
constructor(private index: number, private defaultValue?: any) { | ||
if (index < 0) { | ||
throw new ArgumentOutOfRangeError; | ||
} | ||
} | ||
|
||
call(subscriber: Subscriber<T>): Subscriber<T> { | ||
return new ElementAtSubscriber(subscriber, this.index, this.defaultValue); | ||
} | ||
} | ||
|
||
class ElementAtSubscriber<T, R> extends Subscriber<T> { | ||
|
||
constructor(destination: Subscriber<T>, private index: number, private defaultValue?: any) { | ||
super(destination); | ||
} | ||
|
||
_next(x) { | ||
if (this.index-- === 0) { | ||
this.destination.next(x); | ||
this.destination.complete(); | ||
} | ||
} | ||
|
||
_complete() { | ||
const destination = this.destination; | ||
if (this.index >= 0) { | ||
if(typeof this.defaultValue !== 'undefined') { | ||
destination.next(this.defaultValue); | ||
} else { | ||
destination.error(new ArgumentOutOfRangeError); | ||
} | ||
} | ||
destination.complete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default class ArgumentOutOfRangeError implements Error { | ||
name = 'ArgumentOutOfRangeError'; | ||
message = 'argument out of range'; | ||
} |