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

Added transformRaw option to modify source data before JSON parsing #73

Merged
merged 6 commits into from
Aug 11, 2017

Conversation

drewwestrick
Copy link
Contributor

No description provided.

Copy link
Member

@jescalan jescalan left a comment

Choose a reason for hiding this comment

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

This is amazing, thank you! Just a couple small changes before we merge 😁

@@ -107,7 +107,7 @@ function renderFile (root, obj) {
}

function renderUrl (obj) {
return rest(obj.url).then((res) => { return JSON.parse(res.entity) })
return rest(obj.url).then((res) => { return JSON.parse(transformRaw(obj, res.entity)) })
Copy link
Member

Choose a reason for hiding this comment

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

Ok so in this case the transformRaw option would only work for url sources. I think this is ok, but should be noted in the docs!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could add this to the renderFile, renderData, and renderGraphql. Do you think this would be beneficial? I'm assuming that with a File or Data you could feed in a proper JSON format.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, yeah I feel like it couldn't hurt to do so. I can see a situation where someone might want to run a transform before the json parse in any of those situations. To abstract it out a little more, I might just add it in one place though. Here's what I thought when I looked at the code:

  • Remove the json.parse from the direct renderX methods
  • Add back the json.parse a single time in the run function, but only run the parse if the data is a string
  • Before that json.parse run the transformRaw function once 😁 - this way it wouldnt even need to be abstracted as a function, could just run the ternary inline.

Does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just saw this. Yeah that makes sense. I'll give it a shot. My latest commit takes care of that ternary and I've got tests that work for all the methods. Right now I'm just calling the transformRaw function in each method, but I think your suggestion is much more efficient and elegant :)

lib/index.js Outdated
function transformRaw (obj, data) {
if (!obj.transformRaw) { return data }
return obj.transformRaw(data)
}
Copy link
Member

Choose a reason for hiding this comment

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

This can be obj.transformRaw ? obj.transformRaw(data) : data

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call here. I will implement this change.

t.is(out.trim(), '<p>true</p>')
}
})
})
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for adding a test here, makes a big difference!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep no problem. It's always super easy to add tests when the framework is already there and done nicely.

lib/index.js Outdated
@@ -98,16 +98,16 @@ function run (compiler, compilation, done) {
// Below are the methods we use to resolve data from each type. Very
// straightforward, really.
function renderData (obj) {
return obj.data
return transformRaw(obj, obj.data)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need this here right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yeah I will go ahead and remove that. I was thinking if you wanted to do a transform on the data for some reason you want it there, but the regular transform function would be able to do that so it is redundant.

@jescalan jescalan merged commit 58af683 into static-dev:master Aug 11, 2017
@jescalan
Copy link
Member

Sweet, this is a great addition. Thanks Drew!

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

Successfully merging this pull request may close these issues.

None yet

2 participants