Skip to content
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

parseJSON - minimal implementation for parsing JSON dates from an API #1463

Merged
merged 3 commits into from Oct 22, 2019

Conversation

marnusw
Copy link
Contributor

@marnusw marnusw commented Oct 7, 2019

The ISO date parsing previously done in toDate was moved into parseISO to keep the minimal build size low when string date manipulation is not required (#1016). The problem is that any application that retrieves a date from an API will parse some form of serialized date string before passing it to other functions. The following is typical example:

const output = format(addDays(parseISO(dateFromAPI), 1), 'yyyy-MM-dd')

As soon as parseISO is used in this way the benefit of removing toDate has been negated. The parseISO and parse functions are still around 7000 and 8000 bytes respectively. The proposed parseUTC function is only ~520 bytes in size and sufficient to parse the standard JSON serialized date string formats of most server-side languages.

Aside from complete ISO dates and the input types accepted by toDate the function returns an Invalid Date. The intention of this function will not be to parse user input, for this parse would still be needed, but it permits convenient parsing of date strings from an API with a known ISO format.

This is a proposed compromise/solution for the issue I raised here, and something I have used in a couple of projects recently to good effect.

I am still undecided about the best name for this function. My initial thought was parseJSONDate, but then I realized only UTC dates are supported and calling it parseUTC might be more generic and describe the function more specifically.

I would like to propose this function for inclusion in the core date-fns library, however, if it is deemed not a good fit I can also add it to date-fns-tz.

Copy link
Member

@kossnocorp kossnocorp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love it! However, I think the name is confusing. I like parseJSON more because it's date string formatted by date.toJSON, and I believe that the fact that only UTC dates are supported is an implementation detail.

@marnusw
Copy link
Contributor Author

marnusw commented Oct 19, 2019

Thanks for the feedback. I agree calling it parseJSON makes sense after all and it has been renamed.

@marnusw marnusw changed the title parseUTC - minimal implementation for parsing JSON dates from an API parseJSON - minimal implementation for parsing JSON dates from an API Oct 19, 2019
kossnocorp
kossnocorp previously approved these changes Oct 21, 2019
Copy link
Member

@kossnocorp kossnocorp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, thank you a lot! Besides the small issue with the function name, the PR is ready to go.

src/parseJSON/index.js Outdated Show resolved Hide resolved
@kossnocorp
Copy link
Member

Please add /* eslint-env mocha */ to the top of the test.js to make ESLint happy.

@marnusw
Copy link
Contributor Author

marnusw commented Oct 21, 2019

Thank you for reviewing and accepting this @kossnocorp! The remaining issues have been fixed.

@kossnocorp
Copy link
Member

Thank you! I'll ship it with the next release.

Copy link
Member

@kossnocorp kossnocorp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@kossnocorp kossnocorp merged commit 83e7328 into date-fns:master Oct 22, 2019
+parts[4],
+parts[5],
+parts[6],
+(parts[7] || 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay, we have this in a few places in our app

@marnusw marnusw deleted the parseUTCDate branch October 22, 2019 12:34
@kossnocorp
Copy link
Member

I've shipped the PR with 2.6.0: https://github.com/date-fns/date-fns/releases/tag/v2.6.0

dmytro-gokun added a commit to dmytro-gokun/date-fns that referenced this pull request Oct 23, 2019
@alexdilley
Copy link

Is there an argument against simply using the parsing capabilities built into JS's Date type? i.e.

new Date('2000-03-15T05:20:10.123Z')

Is it a matter of trust in the format supplied by the (commonly) API that would necessitate using a library-supplied function such as this?

@marnusw
Copy link
Contributor Author

marnusw commented Nov 8, 2019

@alexdilley yes it is simply about trust in the underlying implementation of different browsers. These days that might not be as relevant as it once was, but using this function leaves no room for doubt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants