-
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
Calling moment(undefined) should not return a valid moment date #3762
Comments
That > new Date().getTime()
1487150538775
> new Date(undefined).getTime()
NaN Why is moment.js not consistent with this? |
Because that is not standard JS behavior; it's a weird legacy decision inconsistent with the purpose of |
This is indeed the behavior of ES6 default arguments:
Unfortunately the current behavior of moment leads to hard to spot bugs like: const object = {
behaviourUpdate: new Date(),
};
console.log(moment(object.behaviorUpdate)); How can this be mitigated? Is there a way of constructing moments that does not accept |
If you want to mitigate it, which I don't recommend, you can do this: var protectiveMoment = function(...args){
if (args.length > 0 && typeof(args[0]) == "undefined"){
throw "Don't pass undefined to moment"
}
else {
return moment.apply(args);
}
} And then use |
Yeah, that is basically patching moment to behave safer. I thought more about adding a helper like |
Description of the Issue and Steps to Reproduce:
Calling moment with a single argument that is undefined returns a valid moment object with the current time. I would expect it to not silently ignore the argument passed as
undefined
. For examplenew Date
does return anInvalid Date
. I would expect that moment behaves similar. For example calling moment withnull
returns anNaN
date:My particular scenario that took me quite some time to figure out looked basically like this:
Environment:
The text was updated successfully, but these errors were encountered: