adaltas/node-csv

document: how to detect types? eg string vs number

dcsan opened this issue · 6 comments

dcsan commented

Summary

Apparently this feature works but is very hard to find in the docs.
Since it's so basic a requirement, can it be highlighted in the docs somehow?

how to detect the right types? even just for basic stuff like string vs int?

Motivation

basic need

Alternative

writing your own parser or casting fns?

Draft

await fs.promises.writeFile(`${os.tmpdir()}/input.csv`, [
  'a,b,c',
  '1,2,3'
].join('\n'));

should return a: 1, etc. not a: "1"

Additional context

seems like such a basic feature of this lib, am I missing something?

I know dates and all kinds of edge cases but a simple option for at least basic types would be major

dcsan commented

note to future self! this combo seems to work:

  const records = parse(raw, {
    columns: true,
    trim: true,
    rtrim: true,
    cast: true,
    skip_empty_lines: true,
    auto_parse: true,
  });

{trim: true} is internally converted to {rtrim: true, ltrim: true}, you can remove it.

Please close the issue if you answered your question.

dcsan commented

changed it to a documentation request. I can submit a PR if it would be accepted.

From all the options above, only "auto_parse" is relevant to detect type and it is already documented. The option was renamed a few years ago as "cast". Improvements to the documentation are always welcome. Please be more specific on how you wish to improve the documentation if you wish a validation.

Closing for now. Don't hesitate to pursue the discussion if necessary.