fix(single): predicate function receives indices starting at 0 #2396
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When using the predicate function in the
single()
operator it passes indices starting at1
instead of0
.This prints to console:
Live demo: https://jsbin.com/raveji/2/edit?js,console
That's because it's first incremented (single.ts#L65) and then read again from property (single.ts#L75).
In RxJS 4 the indices start at
0
(which is probably more expected behavior). Live demo: https://jsbin.com/qihaqur/3/edit?js,consoleIf the user is relying on indices starting at
1
than this could be a BC. Otherwise now it's compatible with RxJS 4.There's one more inconsistency with RxJS 4.
When there's no item matching the predicate function the RxJS 4 version sends an error notification
EmptyError
. Live demo: https://jsbin.com/pukikes/3/edit?js,consoleThis prints to console:
But the RxJS 5 implementation just emits
undefined
asnext
and sendserror
only when thesingle()
operator received no value at all (single.ts#L87).Live demo: https://jsbin.com/yavepan/2/edit?js,console.
The docblock for
single()
says:This describes the RxJS 4 behavior but the RxJS 5 tests cover a use-case where
single()
emitsundefined
single-spec.ts#L142. Also the two exception classes mentioned don't exist.So I'm wondering what's the correct behavior?