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
Extract parseISO
from toDate
and disallow passing strings into functions
#1016
Comments
I agree. A lot of people choose date-fns because it is lightweight and we should try to be the best library possible for this type of users — while maintaining the same flexibility and cover all possible date use cases. Our main intent was always to work with
|
And BTW I think that this change would mean that most of the functions wouldn't need |
Shouldn't this issue be closed as #1023 was merged ? |
I've been thinking about this change quite a bit because, to be honest, for me it has been a blocker upgrading many of my projects from It was awfully convenient to know one could read a I'm completely on-board with taking the bulk of the string parsing out of if (typeof dirtyDate === 'string' && dirtyDate.length === 24) {
let parts = dirtyDate.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})Z/)
if (!parts) {
throw new Error('...')
}
return new Date(+parts[1], parts[2] - 1, +parts[3], +parts[4], +parts[5], +parts[6], +parts[7])
} There may be edge cases I'm not aware of or other complications but I thought it worth asking your thoughts? |
@marnusw I feel your pain. I want to keep date-fns as reliable and lightweight as possible but it shouldn't be too painful. After a while, I see why this change might be painful, so I find your idea appealing. I need to think a bit about what you just said, however here's what I don't like about it:
I see the problem and I'd like to find a solution to it, but I'm not certain that's the one. |
Was fixed in v2.x.x! |
See #1015 for the background.
toDate
is the function that converts passed argument to a date object and used virtually in every function in the library. The thing is that it weights1.27 KB
(after heavy size optimization), so the minimal build size couldn't go below the number.For example,
addDays
size is1.31 KB
(it was983 B
in v1).The size bottleneck is in ISO 8601 string parsing code, so even if you don't use strings as arguments, you're going to pay the price.
The solution to the problem is to extract this code into
parseISO
and make date-fns functions accept only dates and numbers as arguments. It will make the library slightly more difficult to use for those who use strings to store dates but more lightweight for others.This is a breaking change and will cause a backlash, but I believe it's worth it. Many choose date-fns because it's light and without this change, we won't be able to achieve the best possible result.
The text was updated successfully, but these errors were encountered: