deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

std/encoding/csv.ts - `stringify` could use improvement and better documentation

Open dsherret opened this issue 3 years ago • 9 comments

stringify in std/encoding/csv.ts takes a lot of reading to figure it out. After looking over the documentation and source code, I've discovered that I need to provide objects then specify what each column is along with how each column should access a property on the objects.

Some suggestions:

  1. Unfortunately the property access is not type safe and so it's possible to change a property name and break this code without being notified. The API could be improved if it were type safe between the property names and the objects being provided as this would prevent bugs.
  2. It would be nice if there were something analogous to JSON.stringify(data) here, where I can just have data in memory being string[][] and then do stringify(data) and get the result. I don't want to bother having to specify column names and property names on an object.
  3. It might be easier to understand if there were some examples, but I think examples wouldn't be required if it wasn't so complex.

By the way, a simple API I've found for csv parsing and stringifying is https://www.npmjs.com/package/csv-string -- I almost feel like the csv parsing in std should be really simple then people can map the data in and out to objects if they feel like. There's a lot going on right now in there.

dsherret avatar Apr 26 '21 21:04 dsherret

I'm taking this issue! I'll take a look and see what can I do.

luk3skyw4lker avatar Apr 03 '22 05:04 luk3skyw4lker

About number 2, I think that the best solution would be to move the columns argument into the options object, this way the user could specify which columns he wants in the output and if no option is provided, then all columns will de present into the output.

luk3skyw4lker avatar Apr 09 '22 02:04 luk3skyw4lker

About 1, would be the case to think about adding a generic parameter to stringify to specify the type of the input object or am I wrong? I didn't fully understand the type safety that this funciton would have, can you clarify it for me?

luk3skyw4lker avatar Apr 09 '22 02:04 luk3skyw4lker

And about 3, I think that the examples would do a good job, but I'll take a deep look at the code you provided as a simple solution and see if it fits into the case.

I took a quick look at it already and at least for me the deno implementation looks simpler, but would benefit from a better documentation.

luk3skyw4lker avatar Apr 09 '22 02:04 luk3skyw4lker

I took a deeper look at the csv-string implementation and it really looks simpler. I still stand with the idea of passing columns through the options object though which it would add some complexity to deno's implementation. What do you guys think? @bartlomieju @crowlKats @dsherret

luk3skyw4lker avatar Apr 09 '22 18:04 luk3skyw4lker

Columns in json objects don’t guarantee order. I would assume an array of arrays is the only way to maintain column order or possibly a “Map” interface that does retain order in keys.

ralyodio avatar Apr 22 '22 22:04 ralyodio

I didn't specifically thought about order, but your suggestion is good. But in this case a normal array wouldn't do the job? Why a array of arrays? Didn't manage to think this through

luk3skyw4lker avatar Apr 23 '22 02:04 luk3skyw4lker

Arrays will maintain order. You would just need to add a header array first. So first array is first row and that contains an array of columns.

You’ll need to add empty spaces for missing cells.

I think. Map might be better suited here

ralyodio avatar Apr 25 '22 11:04 ralyodio

Nice, I got the idea, thanks for your suggestions! I'll make a PR this week with the modifications

luk3skyw4lker avatar Apr 25 '22 11:04 luk3skyw4lker

Fixed in https://github.com/denoland/deno_std/pull/2168

kamilogorek avatar Dec 18 '22 23:12 kamilogorek