-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix take and takeLast #5326
Fix take and takeLast #5326
Conversation
@@ -14,7 +12,28 @@ describe('take operator', () => { | |||
testScheduler = new TestScheduler(observableMatcher); | |||
}); | |||
|
|||
asDiagram('take(2)')('should take two values of an observable with many values', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDK what the implications of removing this are.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing, really. I can be put back, but as of right now, we don't run the diagram generation anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll tackle these in another PR, cc @niklas-wortmann
return source.lift(new TakeOperator(count)); | ||
} | ||
}; | ||
if (isNaN(count)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use Number.isNaN
here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, not really, Number.isNaN
would only return true if it were actually NaN
. We want to account for strings that are valid numbers as well. There are people that use RxJS with plain JavaScript (like psychopaths!) and this should be valid for them:
const input = document.querySelector('input[type="text"]');
of(1, 2, 3, 4).pipe(take(input.value)).subscribe(x => console.log(x));
DOES THAT CODE SUCK? Yes. Yes it does. But JS has coercion as a feature. 🤷♂
*/ | ||
export function takeLast<T>(count: number): MonoTypeOperatorFunction<T> { | ||
if (isNaN(count)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use Number.isNaN
here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, that would only return true if it's actually NaN
. People could pass in 'blah' there and it would be false
if we used Number.isNaN
.
is it expected behavior not to complete if the operator throws because it is used inappropriately?
|
BREAKING CHANGE: take will not throw runtime error for arguments that are negative or NaN, this includes non-TS calls like `take()`.
BREAKING CHANGE: Calling takeLast without arguments or with an argument that is NaN will throw a TypeError
6828bb5
to
9af710d
Compare
Add runtime assertions for JavaScript usage, where invalid argument may be passed causing strange behavior.
BREAKING CHANGE:
take
will throw aTypeError
if called astake()
or with an isNaN argument passed.BREAKING CHANGE:
takeLast
will throw aTypeError
if called astakeLast()
or with an isNaN argument passed.