Skip to content

CSV parse - setting cast_date to true is converting unparsable strings to dates #342

@katherinea

Description

@katherinea

Describe the bug
Using csv-parse: 5.0.4

When cast_date is set to true, if you parse a string that ends with a number, the string is cast to a date.
According to the docs https://csv.js.org/parse/options/cast_date/, "The implementation relies on Date.parse. The CSV value is left untouched if the function returns NaN."

Date.parse("Test 2") => NaN, however the parsed field is returned as 2001-02-01T00:00:00.000Z

To Reproduce

with file test.csv

title
test 2

and implementation

  async parseCsv<T>(filePath: string): Promise<T[]> {
    const content: Buffer = await fs.readFile(filePath)
    const parsed: T[] = parse(content, {
      cast: true,
      cast_date: true,
      columns: true,
    })

    return parsed
  }

returned data is:

{
  title: 2001-02-01T00:00:00.000Z,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions