Closed juanjoDiaz closed 4 years ago
I'm also considering if it make sense the current API:
fromInput
allows you to set the input which can be a stream, an array or a single object.throughTransform
allows you to add transforms to the input stream.toOutput
allows you to set the output stream.promise
returns a promise that resolves when the stream ends or errors. Takes a boolean parameter to indicate if the resulting CSV should be kept in-memory and be resolved by the promise.or if they should be replaced by something simpler and more aligned with the sync API like
parse(input)
which takes an input (stream, array or object) and returns the CSV.The methods throughTransform
and toOutput
are just wrappers around pipe
and don't really provide anything.
The method promise
is very similar to the finished
method that has been added to the stream/promise
API in node.
The only difference is that it can gather the whole JSON in a string and resolve that.
I can see 2 options here:
I could move the promise
method to either the JSON2CSVTransform
so you can do
const csv = await asyncParse.parse(data).promise();
I could add a utils
module with a StreamToString
class that gathers the string and exposes a promise
method that resolves when the stream ends, so the user can do
const streamToString = new StreamToString();
asyncParse.parse(data).pipe(streamToString);
const csv = await asyncParse.parse(data).pipe(streamToString).promise()
I like the second one better.
This PR already includes the ArrayAsStream
method wich is basically ArrayToStream
.
So having StreamToString
seems pretty consistent.
Thoughts?
I like option 2 as well
This should be good to merge.
All feedback has been fixed I think.
Closes #468