Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
katherinea opened this issue Jun 7, 2022 · 3 comments

Comments

@katherinea
Copy link

katherinea commented Jun 7, 2022

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,
}
@wdavidw
Copy link
Member

wdavidw commented Jun 7, 2022

Hum, as a matter of fact, node -e 'console.info(Date.parse("Test 2"))' return 980982000000 (using Node.js v16.13.0). This comes as a surprise, but it explains the behavior. You can always write you own cast_date function.

@katherinea
Copy link
Author

katherinea commented Jun 8, 2022

Hiya! Right sorry you're correct on that parsing.
Strange behaviour as

Date.parse("Test 2")
=> 980985600000
Date.parse("Test 2 v 2")
=> NaN

I've tried to pass a function to cast_date but am getting an error message. With the following parsing options:

const parsed: T[] = parse(content, {
      cast: true,
      cast_date: (value: string, context: CastingContext) => {
        const date = Date.parse(value)
        // test function, not correctly handling non-date strings
        return new Date(date)
      },
      columns: true,
    })

I'm getting the error:

{
   "type": "CsvError",
   "message": "Invalid option cast_date: cast_date must be true or a function, got undefined",
   "stack":
       Error: Invalid option cast_date: cast_date must be true or a function, got undefined
           at Parser.__normalizeOptions (/Users/katherine/RAcode/powertools/bulk-events-upload/node_modules/csv-parse/dist/cjs/sync.cjs:206:13)
           at new Parser (/Users/katherine/RAcode/powertools/bulk-events-upload/node_modules/csv-parse/dist/cjs/sync.cjs:153:10)
           at parse (/Users/katherine/RAcode/powertools/bulk-events-upload/node_modules/csv-parse/dist/cjs/sync.cjs:1281:18)
           at _callee$ (/Users/katherine/RAcode/powertools/bulk-events-upload/src/services/csv-service/csv-service.ts:15:25)
           at tryCatch (/Users/katherine/RAcode/powertools/bulk-events-upload/node_modules/regenerator-runtime/runtime.js:63:40)
           at Generator.invoke [as _invoke] (/Users/katherine/RAcode/powertools/bulk-events-upload/node_modules/regenerator-runtime/runtime.js:294:22)
           at Generator.next (/Users/katherine/RAcode/powertools/bulk-events-upload/node_modules/regenerator-runtime/runtime.js:119:21)
           at asyncGeneratorStep (/Users/katherine/RAcode/powertools/bulk-events-upload/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
           at _next (/Users/katherine/RAcode/powertools/bulk-events-upload/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
   "code": "CSV_INVALID_OPTION_CAST_DATE"
 }

Also had a question about the types - cast_date takes a CastingDateFunction

CastingDateFunction = (value: string, context: CastingContext) => Date;

Shouldn't the return type be Date | string to allow for strings to be returned if they're not able to be parsed as a date?
Thank you :)

@wdavidw
Copy link
Member

wdavidw commented Jun 14, 2022

Strange, option cast_date was defined in sync.t.ts but not implemented nowhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants