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

fix(encoding/csv): remove stringify object array support #2660

Open
timreichen opened this issue Sep 15, 2022 · 0 comments
Open

fix(encoding/csv): remove stringify object array support #2660

timreichen opened this issue Sep 15, 2022 · 0 comments

Comments

@timreichen
Copy link
Contributor

timreichen commented Sep 15, 2022

Is your feature request related to a problem? Please describe.
There seems to be inconsistencies and unpredictable behaviours within stringify().
You can currently pass an array of objects, but it will throw an error if no columns option is passed, which is weird, since the options object is always optional. And the stringify()output changes with headers and columns options depending on the data type:

const arrayData = [["bar"], ["barz"]];
const objectData = [ {foo: "bar1"}, { foo: "bar2"} ]

stringify(arrayData) // works as expected
stringify(objectData) // throws error

stringify(arrayData, { headers: true }) // adds empty header?
stringify(objectData, { headers: true }) // throws error

stringify(arrayData, { columns: ["foo"] }) // throws error
stringify(objectData, { columns: ["foo"] }) // adds header without header: true ?!

stringify(arrayData, { columns: ["foo"], headers: true }) // throws error
stringify(objectData, { columns: ["foo"], header: true }) // the same behaviour as without setting a header:true ?!?
stringify(objectData, { columns: ["foo"], header: false }) // explicit headers: false removes headers

Describe the solution you'd like
I think we should drop support for object arrays and let the user prepare the data to avoid these inconsistencies.
headers option can be removed and columns renamed to headers. If headers is present in options, it adds the header names, else no headers are added.

This means just one more line for the user to map over the objects. But the code is also more clear in what is going on:

const objectData = [ {foo: "bar"}, { foo: "barz"} ]
const preparedData = objectData.map(object => [object.foo])
stringify(preparedData) // no headers added
stringify(preparedData, { headers: ["foo"] }) // adds header

Describe alternatives you've considered

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

No branches or pull requests

1 participant