denoland / denoland/std

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

Open
#2,660 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
3.6k
Forks
681
PR merge metrics
No merged PRs in 30d

Description

**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:
```ts
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:

```ts
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**

Contributor guide

Open the contributing guide

Research direction

Start at the encoding/csv stringify() entry point and inspect the current handling of object arrays, headers, and columns. Compare its behavior with the examples in the issue, then update the relevant tests and implementation so object arrays are no longer supported and headers consistently control whether header names are emitted.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.