d3/d3-dsv

dsv.format could take an optional columns array?

mariocasciaro opened this issue · 5 comments

Sometimes it is useful to explicitly specify a column order for the generated CSV file, other times we may want to exclude one or more columns, moreover, providing a column list in input will avoid the initial scan of the row list required to extract the column names.
I would mainly implement it for the first reason, since a csv file is often opened using a spreadsheet app like Excel, where the order of the columns should not be just random.

If you are interested, I can work on a PR, since I need the feature anyway.

Thanks for your time and for the lib!

This is why there’s a dsv.formatRows method. I’ve added some examples to the API reference. Hope this helps!

Thanks for the answer, it's all clear now!

Even though you can do this with dsv.formatRows, I think it would be easy to support this, so I’m reopening this issue. As you suggested, the dsv.format method could take an optional array of column names. Then, instead of this:

var string = csv.formatRows([[
    "year",
    "make",
    "model",
    "length"
  ]].concat(data.map(function(d, i) {
  return [
    d.year,
    d.make,
    d.model,
    d.length
  ];
})));

You could say this:

var string = csv.format(data, ["year", "make", "model", "length"]);

Which is obviously much nicer. 😁

Another possibility is that we could check if the passed-in rows array exposes a columns array, now that dsv.parse supports this feature. But I think this is probably too magical, since you might forget about the existence of the columns property after transforming the array. So, better to make it an explicit argument.

Fixed in 0.1.11.

Thanks for the feature! It's definitely a much neater approach