Skip to content

Commit

Permalink
[gatsby-medium-source] convert timestamps to ISO 8601 date string (#2099
Browse files Browse the repository at this point in the history
)

* serialize big integers for gatsby-medium-source timestamps

* typos

* typo

* convert timestamps to  ISO 8601 date string
  • Loading branch information
pedrouid authored and KyleAMathews committed Sep 26, 2017
1 parent bd90512 commit 2c96699
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/gatsby-source-medium/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ const fetch = username => {

const prefix = `])}while(1);</x>`

const convertTimestamps = (nextObj, prevObj, prevKey) => {
if (typeof nextObj === 'object') {
Object.keys(nextObj).map(key => convertTimestamps(nextObj[key], nextObj, key));
} else {
if (typeof nextObj === 'number' && nextObj >> 0 !== nextObj) {
const date = new Date(nextObj);
if (date.getTime() === nextObj) {
prevObj[prevKey] = date.toISOString().slice(0, 10);
}
}
}
}

const strip = payload => payload.replace(prefix, ``)

exports.sourceNodes = async ({ boundActionCreators }, { username }) => {
Expand All @@ -29,6 +42,8 @@ exports.sourceNodes = async ({ boundActionCreators }, { username }) => {

const resources = Array.prototype.concat(...importableResources)
resources.map(resource => {
convertTimestamps(resource)

const digest = crypto
.createHash(`md5`)
.update(JSON.stringify(resource))
Expand Down

0 comments on commit 2c96699

Please sign in to comment.