d3 / d3-dsv

A parser and formatter for delimiter-separated values, such as CSV and TSV.
https://d3js.org/d3-dsv
ISC License
436 stars 76 forks source link

Export individual "formatRow" and "formatValue" functions #57

Closed rdmurphy closed 4 years ago

rdmurphy commented 5 years ago

Hello!

I've found myself more and more using d3-dsv as a sort of a Swiss Army knife of CSV manipulation — if I want to ensure that an Array of data gets properly delimited, I'll reach for formatRows and trust it'll do the right thing. But when I'm working with Node.js streams and looping through data line-by-line (meaning I don't have access to the entire "body" of data at any given time), it'd be neat if I could tap directly into formatRow.

It's not end of the world to have to use formatRows instead, but it requires me to create a dummy Array to wrap the individual row of data with every pass, which feels a little dirty.

const createStreamWriter = (outputPath, columns) => {
  const writer = fs.createWriteStream(outputPath)
  // with dsv.csvFormatRow, I could just pass in my single Array of values
  writer.write(`${dsv.csvFormatRows([columns])}\n`)

  return data => {
    writer.write(`${dsv.csvFormatBody([data], columns)}\n`)
  }
}

In the case of formatBody, it kind of defeats the point to "singularize" that, but if formatValue was made available I could reproduce the effect by creating my own single row function built on it for zipping purposes.

Thank you! Looking forward to your thoughts.

mbostock commented 5 years ago

I’m cool with exporting formatRow and formatValue. :+1:

curran commented 4 years ago

Concrete tasks appear to include:

rdmurphy commented 4 years ago

@curran I see now I named the pull request the wrong thing months ago but I've already done those four things here: https://github.com/d3/d3-dsv/pull/58

curran commented 4 years ago

Oh excellent! Somehow I didn't see that PR. Nice work!

mbostock commented 4 years ago

Fixed in 437da9a9ab034e4a42d4b0f5e29d833a96069a4d.