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 flattenSeparator option #314

Merged
merged 2 commits into from
Jul 31, 2018
Merged

Added flattenSeparator option #314

merged 2 commits into from
Jul 31, 2018

Conversation

fahrenq
Copy link
Contributor

@fahrenq fahrenq commented Jul 31, 2018

Hi @zemirco,

First of all - thank you very much for this great library.

I've added flattenSeparator option. Basically what it does is allowing us to use flatten: true not only to produce header values like Key1.Key2 but Key1__Key2 also.

Here few things I had to change while implementing this feature:

  • In lib/JSON2CSVBase.js we had JSON2CSVBase#flatten(dataRow) method that was implicitly called while processing rows. The bad thing about it - we couldn't access JSON2CSVBase instance (this) from the #flatten() method. I had to return another function from it, that actually handles the logic. As an alternative, we can move the flatten logic outside the class at all. What do you think?

  • In README.md I added documentation and added some dots to the end of options definitions (48-69 lines). Sorry that it shows everything as changed, I had to indent all lines to fit newly created option.

Thank you very much,
Looking forward to your comments and approval if everything is ok.

@coveralls
Copy link

coveralls commented Jul 31, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling 73f050c on fahrenq:master into b2690fe on zemirco:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 73f050c on fahrenq:master into b2690fe on zemirco:master.

@knownasilya knownasilya merged commit 5c5de9f into zemirco:master Jul 31, 2018
@knownasilya
Copy link
Collaborator

Thanks for the contribution @fahrenq!

@knownasilya
Copy link
Collaborator

Released as v4.2.0

@fahrenq
Copy link
Contributor Author

fahrenq commented Jul 31, 2018 via email

Copy link
Collaborator

@juanjoDiaz juanjoDiaz left a comment

Choose a reason for hiding this comment

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

A bit too late but I really don't like this change.

It introduces an unnecessary closure for no reason.
It adds a new option for something that could be achieved simply using the existing fields option.
And most importantly, doesn't test the new feature at all

:/

@@ -63,7 +64,7 @@ class JSON2CSVBase {
: [row];

if (this.opts.flatten) {
return processedRow.map(this.flatten);
return processedRow.map(this.flatten());
Copy link
Collaborator

Choose a reason for hiding this comment

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

This was adding a very unnecessary closure.

You could have accessed the right this but simple replacing this.flatten by this.flatten.bind(this) or (row => this.flatten(row))

Copy link
Collaborator

Choose a reason for hiding this comment

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

True, but that function is called only once, so it's really marginal. Feel free to submit a fix if it's gnawing at you 😉

Copy link
Collaborator

@knownasilya knownasilya Jul 31, 2018

Choose a reason for hiding this comment

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

I would probably keep the function, and have it take an object of options that are specific for flatten.

@fahrenq
Copy link
Contributor Author

fahrenq commented Aug 1, 2018

@juanjoDiaz

It adds a new option for something that could be achieved simply using the existing fields option.

How? I don't see any way to achieve this with fields option unless I have to dynamically compose fields option, which is super tedious and super unintuitive for those who'll read code later.

And most importantly, doesn't test the new feature at all.

Why? I've added a unit test for this. https://github.com/zemirco/json2csv/pull/314/files#diff-6ddc949419d029ce89b8b8cc1b161136

It introduces an unnecessary closure for no reason.

Sorry, I think you're wrong.

When I'm reading the code of any class, I expect to have access to this in all instance functions no matter how they called elsewhere. While using Function#bind will work as a solution, I see it as a dirty hack in our case, since there's only one right this that we can bind to the function. So why would we do this and make life harder for those who'll want to reuse #flatten?

Thanks.

P.S. (row => this.flatten(row)) this looks like a good solution, tell me if you'd like me to implement it.

@juanjoDiaz
Copy link
Collaborator

Just having the array of fields, yeah. Might be a bit tedious if you have too many fields... I can agree with that. In any case, the change is in now. No point crying over spilled milk :) I just want to avoid what happened in version 3.X that we ended up with multiple duplicated and overlapping flags just because of people PRing their solutions for their particular problem.

My bad. Didn't see that test. Then it's only missing the equivalent test for the sync API and the CLI.

Unless using arrow functions, this is bound at call time. Wouldn't you wrap every single function of all the classes in anonymous functions just to always force the call?
What if I want to use flatten somewhere else or extend the code? I'd need to do this.flatten()(row) which doesn't make any sense...

As said, no biggie! The change is in and contribution are always more than welcome. Just trying to get the library in the best possible shape by having open discussions :)

@fahrenq
Copy link
Contributor Author

fahrenq commented Aug 1, 2018

Yeah, 100% agree with you. In fact, this function now not flattens a row but returns a function that flattens a row. The name is misleading. Definitely not perfect.

Will PR in future to improve this and do everything as semantically perfect as possible.

Thanks.

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.

4 participants