adaltas / node-stream-transform

Object transformations implementing the Node.js `stream.Transform` API
https://csv.js.org/transform/
49 stars 13 forks source link

Problem with object as input #5

Closed pensierinmusica closed 9 years ago

pensierinmusica commented 9 years ago

Hi,

I can't figure out how to make this module work with an object as input. According to the documentation it should "Accept object, array or JSON as input and output".

Let's take this simple code:

csv.transform(input, function (data) {
  console.log("Progress");
  console.log(data);
  return data;
}, function (err, output) {
  console.log('Done!');
  console.log(output);
});

When

var input = [[1,2,3], ['a','b','c']];

The console prints:

Progress
[ 1, 2, 3 ]
Progress
[ 'a', 'b', 'c' ]
Done!
[ [ 1, 2, 3 ], [ 'a', 'b', 'c' ] ]

Which seems perfectly fine. Unfortunately though when

var input = {
  foo: {
    number: 1,
    color: 'red',
    desc: 'foo'
  },
  bar: {
    number: 2,
    color: 'blue',
    desc: 'bar'
  },
};

The console prints:

Done!
[]

It looks like the function is not accepting an object as input. Any idea why?

Thanks!

wdavidw commented 9 years ago

It should be understood as an array of arrays, an array of objects....

pensierinmusica commented 9 years ago

@wdavidw I see, If you like I can do a PR to update the documentation and make it a bit more clear on this point :)

Regarding the JSON input, I imagine it needs to represent one of the other two cases (i.e. array of arrays, array of objects). Is this interpretation correct?

Thanks!

wdavidw commented 9 years ago

Please re-formulate the documentation. The JSON is an error, you can pass a string and eventually transform it from JSON to Object inside the provided callback (look at the "types" test.

pensierinmusica commented 9 years ago

Ok @wdavidw, thanks for the clarification. Should I generate a PR to make docs a bit more clear on this point? Cheers

wdavidw commented 9 years ago

yes please, i'll apply it