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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. | ||
|
||
import { parseJSON } from 'date-fns/fp' | ||
export default parseJSON |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// This file is generated automatically by `scripts/build/fp.js`. Please, don't change it. | ||
|
||
import fn from '../../parseJSON/index.js' | ||
import convertToFP from '../_lib/convertToFP/index.js' | ||
|
||
var parseJSON = convertToFP(fn, 1) | ||
|
||
export default parseJSON |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// @flow | ||
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. | ||
|
||
export type Interval = { | ||
start: Date | number, | ||
end: Date | number | ||
} | ||
|
||
export type Locale = { | ||
formatDistance: Function, | ||
formatRelative: Function, | ||
localize: { | ||
ordinalNumber: Function, | ||
era: Function, | ||
quarter: Function, | ||
month: Function, | ||
day: Function, | ||
dayPeriod: Function | ||
}, | ||
formatLong: Object, | ||
date: Function, | ||
time: Function, | ||
dateTime: Function, | ||
match: { | ||
ordinalNumber: Function, | ||
era: Function, | ||
quarter: Function, | ||
month: Function, | ||
day: Function, | ||
dayPeriod: Function | ||
}, | ||
options?: { | ||
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, | ||
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | ||
} | ||
} | ||
|
||
type CurriedFn1<A, R> = <A>(a: A) => R | ||
|
||
declare module.exports: CurriedFn1<string | number | Date, Date> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// @flow | ||
/* eslint-env mocha */ | ||
/* global suite, benchmark */ | ||
|
||
import parseJSON from '.' | ||
import moment from 'moment' | ||
|
||
suite('toDate', function() { | ||
benchmark('date-fns', function() { | ||
return parseJSON('2014-10-25T13:46:20+00:00') | ||
}) | ||
|
||
benchmark('Moment.js', function() { | ||
return moment('2014-10-25T13:46:20+00:00') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. | ||
|
||
import { parseJSON } from 'date-fns' | ||
export default parseJSON |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import toDate from '../toDate/index.js' | ||
|
||
/** | ||
* @name parseJSON | ||
* @category Common Helpers | ||
* @summary Parse a JSON date string | ||
* | ||
* @description | ||
* Converts a complete ISO date string in UTC time, the typical format for transmitting | ||
* a date in JSON, to a JavaScript `Date` instance. | ||
* | ||
* This is a minimal implementation for converting dates retrieved from a JSON API to | ||
* a `Date` instance which can be used with other functions in the `date-fns` library. | ||
* The following formats are supported: | ||
* | ||
* - `2000-03-15T05:20:10.123Z`: The output of `.toISOString()` and `JSON.stringify(new Date())` | ||
* - `2000-03-15T05:20:10Z`: Without milliseconds | ||
* - `2000-03-15T05:20:10+00:00`: With a zero offset, the default JSON encoded format in some other languages | ||
* - `2000-03-15T05:20:10+0000`: With a zero offset without a colon | ||
* | ||
* For convenience and ease of use these other input types are also supported | ||
* via [toDate]{@link https://date-fns.org/docs/toDate}: | ||
* | ||
* - A `Date` instance will be cloned | ||
* - A `number` will be treated as a timestamp | ||
* | ||
* Any other input type or invalid date strings will return an `Invalid Date`. | ||
* | ||
* @param {String|Number|Date} argument A fully formed ISO1806 date string to convert | ||
* @returns {Date} the parsed date in the local time zone | ||
* @throws {TypeError} 1 argument required | ||
*/ | ||
export default function parseJSON(argument) { | ||
if (arguments.length < 1) { | ||
throw new TypeError( | ||
'1 argument required, but only ' + arguments.length + ' present' | ||
) | ||
} | ||
|
||
if (typeof argument === 'string') { | ||
var parts = argument.match( | ||
/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{3}))?(?:Z|\+00:?00)/ | ||
) | ||
if (parts) { | ||
return new Date( | ||
Date.UTC( | ||
+parts[1], | ||
parts[2] - 1, | ||
+parts[3], | ||
+parts[4], | ||
+parts[5], | ||
+parts[6], | ||
+(parts[7] || 0) | ||
) | ||
) | ||
} | ||
} | ||
return toDate(argument) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// @flow | ||
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. | ||
|
||
export type Interval = { | ||
start: Date | number, | ||
end: Date | number | ||
} | ||
|
||
export type Locale = { | ||
formatDistance: Function, | ||
formatRelative: Function, | ||
localize: { | ||
ordinalNumber: Function, | ||
era: Function, | ||
quarter: Function, | ||
month: Function, | ||
day: Function, | ||
dayPeriod: Function | ||
}, | ||
formatLong: Object, | ||
date: Function, | ||
time: Function, | ||
dateTime: Function, | ||
match: { | ||
ordinalNumber: Function, | ||
era: Function, | ||
quarter: Function, | ||
month: Function, | ||
day: Function, | ||
dayPeriod: Function | ||
}, | ||
options?: { | ||
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, | ||
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | ||
} | ||
} | ||
|
||
declare module.exports: (argument: string | number | Date) => Date |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// @flow | ||
/* eslint-env mocha */ | ||
|
||
import assert from 'power-assert' | ||
import parseJSON from '.' | ||
|
||
describe('parseJSON', function() { | ||
it('parses a fully formed ISO date with Z', () => { | ||
const date = '2000-03-15T05:20:10.123Z' | ||
const parsedDate = parseJSON(date) | ||
assert.equal(parsedDate.toISOString(), date) | ||
}) | ||
|
||
it('parses a fully formed ISO date with Z without ms', () => { | ||
const date = '2000-03-15T05:20:10Z' | ||
const expectedDate = '2000-03-15T05:20:10.000Z' | ||
const parsedDate = parseJSON(date) | ||
assert.equal(parsedDate.toISOString(), expectedDate) | ||
}) | ||
|
||
it('parses a fully formed ISO date with zero offset', () => { | ||
const zeroOffset = '2000-03-15T05:20:10+00:00' | ||
const expectedDate = '2000-03-15T05:20:10.000Z' | ||
const parsedDate = parseJSON(zeroOffset) | ||
assert.equal(parsedDate.toISOString(), expectedDate) | ||
}) | ||
|
||
it('parses a fully formed ISO date with zero offset without colon', () => { | ||
const zeroOffset = '2000-03-15T05:20:10+0000' | ||
const expectedDate = '2000-03-15T05:20:10.000Z' | ||
const parsedDate = parseJSON(zeroOffset) | ||
assert.equal(parsedDate.toISOString(), expectedDate) | ||
}) | ||
|
||
it('clones a date object', () => { | ||
const date = new Date(2000, 2, 15, 5, 20, 10, 20) | ||
const parsedDate = parseJSON(date) | ||
assert.deepEqual(parsedDate, date) | ||
assert.notEqual(parsedDate, date) | ||
}) | ||
|
||
it('assumes a number is a timestamp', () => { | ||
const date = new Date(2000, 2, 15, 5, 20, 10, 20) | ||
const timestamp = date.getTime() | ||
const parsedDate = parseJSON(timestamp) | ||
assert.deepEqual(parsedDate, date) | ||
}) | ||
|
||
it('returns an invalid date for anything else', () => { | ||
assert.equal(parseJSON('').toString(), 'Invalid Date') | ||
assert.equal(parseJSON('invalid').toString(), 'Invalid Date') | ||
assert.equal(parseJSON('2020-10-10').toString(), 'Invalid Date') | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Yay, we have this in a few places in our app