Skip to content
This repository was archived by the owner on Jun 28, 2021. It is now read-only.
This repository was archived by the owner on Jun 28, 2021. It is now read-only.

First column not accessible via property name in case of BOM mismatch #284

@jacobq

Description

@jacobq

Describe the bug

When parsing a CSV file that includes BOM using the columns: true option, serializing the record shows the expected values, but the first column is not accessible by its key name. Setting option bom: true fixes the problem (and I'd love to tell that to my 2-hours-ago self), but why can't the default behavior deal with this case correctly?

To Reproduce

test.csv (input)
X
1
3
test.js (program)
const { createReadStream } = require('fs');

const parse = require('csv-parse');
const parser = parse({
  columns: true,
});
const transform = require('stream-transform');
const transformer = transform(function(record, callback) {
  callback(null, `X=${record.X}, record=${JSON.stringify(record)}\n`);
}, {
  parallel: 5
});

createReadStream('test.csv')
  .pipe(parser)
  .pipe(transformer)
  .pipe(process.stdout)
  ;
Execution / Demo (Git Bash / MinGW64)
$ file test.csv
test.csv: UTF-8 Unicode (with BOM) text, with CRLF line terminators

$ node test.js
X=undefined, record={"X":"1"}
X=undefined, record={"X":"3"}

$ dos2unix.exe test.csv
dos2unix: converting file test.csv to Unix format...

$ file test.csv
test.csv: ASCII text

$ node test.js
X=1, record={"X":"1"}
X=3, record={"X":"3"}

Additional context

  • CSV file was created with Microsoft Excel 2016 MSO (16.0.12527.20260) 64-bit using the "CSV UTF-8 (Comma delimited) (*.csv)" format
  • OS: Windows 10 64-bit (Version 10.0.18363.720)
  • node: v12.16.0
  • npm: 6.13.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions