-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Better checks for garbage input for setters #3704
Conversation
…is is as simply not catching being a falsy (but assumedly valid) time zone modifier (_tzm?) of 0
Hey @willdembinski Thanking you for your PR. Could you have a look at my PR and let me know what was causing my code to fail the tests? It would help me understand the library better. |
Yea no problem. I'm at work and don't have a lot of time. But - if you
want to compare/contrast the behaviors of the PRs that would probably be
the best way to learn something. Any suggestions/improvements you come up
with in that process would be much appreciated as well. <3 :)
|
Is there any reason why simply using |
That was exactly what I did, hence I wanted to know from @willdembinski himself. |
The primary issue with Also, there's little gems like these:
The take away is: Never underestimate the power of type coercion. I think there was one other issue that I don't remember atm? I'll check when I get back home and get some more details. |
+1. |
I hope to shorten it, this looks like hand-written and the wisdom of the internet should beat it. Not to mention, that this particular function should not be given floating point numbers, and isNumeric accepts them. |
We have things that are not numbers, and then for the numbers there are NaN, Infinity, floats and ints. The So we use the part where strings are coerced (because we happily pass strings through), but we don't want to use the part where arrays are coerced? Also NaN/Infinity doing an invalid date seems fine to me. Also, to top it off, your code does So why are we even doing this change? Coercion is part of JavaScript, its everywhere. If you want to guard like crazy 1) you're gonna have a hard time, and 2) everyone is going to hate you. I remember when we restricted some args to ints people complained that they couldn't pass strings now. The only thing that is questionable is the array coercion, which I didn't even now existed, but then -- everyone who takes out an integer for their purposes using the |
@ichernev - I agree with all the of points you outlined. In an effort to address the issues presented in #3483, I found that this approach handled the inputs that were causing failure. Here's a snippet/series of cases if you find yourself investigating this further:
Additionally - here's some SO posts that I used when looking into the issue: If you feel that this unacceptable - regardless of circumstance - please let me know and I'll head back to the drawing board. Learned a decent amount setter behavior in the process and could use another go ;) |
@icambron Is "Infinity" really a number tho? 😝 http://math.stackexchange.com/questions/36289/is-infinity-a-number |
Well, about the issue - maybe isValid should return false. Why add another layer of silent failures? |
@ichernev - The argument against invalidating the moment object is really a matter of opinion -
I personally support the idea of invalidating all of the things. However, this is the behavior. |
@willdembinski that is a fair point. Even adding If we do that I just want to make sure that we do it consistently everywhere, but I can't find more methods accepting user input (other than constructors). In 3.0 we can change this behavior. |
But lets use the logic from the add/subtract, which afaik is |
Ill do some tinkering on it - I think that using One thing to be aware of tho, I didn't believe this when I was first told either, however:
I've been wrong before, so I'd love some input on this. But if it is true (that
Where But again, I'll do some tinkering and see what comes of it. 😃 |
Coercion is fine, setting something obviously wrong and making the date invalid isnt. Next major we might rethink that. Also if you do something other than isNaN change both places. |
@ichernev - Apologies for going MIA. Work has required focus. #3735 uses the implementation we discussed. But the test cases that pass are not as robust as the previous implementation. Would be willing to discuss further unless you think its unnecessary. Additionally - add/subtract uses the |
@willdembinski so I'll merge the other one. About discussion about this one -- we already do some isNaN, isFinite checks elsewhere in the code. Doing 'hack' checks is no good unless you have a benchmark and a good reason. This PR is about fixing correctness, not speed. About the correctness issue -- moment is horrible at that and we can't just "fix" it without breaking it also for existing users. We should have the same policy for all methods, and enforce it everywhere. This is for v3+, not now. For now we can just fix horrible things (like the issue you started fixing with this PR). I also want to make it better, just now is not the right time (v3 is). |
@ichernev - I totally understand and sincerely appreciate the discussion. :) Let me know if there's anything else I can do on this one. In the mean time, on to the next one! :D |
Ok, closing this, we'll do something for v3 in this regard for sure. |
@wi-ski you can take a stab at this one, basing your change on the |
Added a helper for checking whether an argument is numeric. I feel as though its rather garish in implementation and would love some critiques if anyone feels this could/should be improved.