mafintosh / csv-parser

Streaming csv parser inspired by binary-csv that aims to be faster than everyone else
MIT License
1.41k stars 134 forks source link

how to use mapValues? #141

Closed dcsan closed 4 years ago

dcsan commented 4 years ago

Documentation Is:

Please Explain in Detail...

can you show how to use mapValues ? https://www.npmjs.com/package/csv-parser#mapvalues

the example is abstract and not working code.

A function that can be used to modify the value of each column value.


csv({
  mapValues: ({ header, index, value }) => value.toLowerCase()
})

so is header a value? or a variable that contains the name of a header field? index is the ... index into the fields? this is an option instead of using a named header? and value is the value prop?

Is there anyway to filter here? eg only pass named columns a "makeFloat" function?

Your Proposal for Changes

I ended up using it with a separately (not inline) function as below. using with typescript and it's awkward to type within those inline function definitions

      const makeFloat = (header: string, index: number, value: string) => {
        if (/Open|High|Low/.test(header)) {
          return parseFloat(value)
        } else {
          return value // string
        }
      }

      let options = {
        // @ts-ignore
        mapValues: ({ header, index, value}) => makeFloat(header, index, value)
      }

alternately is there a way to force casting / recognize values on certain fields?

dcsan commented 4 years ago

related https://github.com/mafintosh/csv-parser/issues/49

shellscape commented 4 years ago

I've added verbosity to the README. If that's still confusing to people then I'm not sure what else can be done. If you'd like to add recipes to the repo, please fork and create a pull request, and add markdown files to a recipes/ directory.