Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/csv-parse/lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ const transform = function (original_options = {}) {
for (const key in options) {
this.options[key] = options[key];
}
// Refresh buffers and lookahead sizes derived from the encoding.
const state = init_state(options);
for (const key of [
"timchars",
"timcharFirstBytes",
"needMoreDataSize",
"recordDelimiterMaxLength",
]) {
this.state[key] = state[key];
}
// Options will re-evaluate the Buffer with the new encoding
({ comment, escape, quote } = this.options);
break;
Expand Down
34 changes: 34 additions & 0 deletions packages/csv-parse/test/option.encoding.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "should";
import { parse } from "../lib/index.js";
import { parse as parseSync } from "../lib/sync.js";

describe("Option `encoding`", function () {
describe("validation & normalization", function () {
Expand Down Expand Up @@ -64,6 +65,39 @@ describe("Option `encoding`", function () {
});

describe("with BOM", function () {
for (const option of ["trim", "ltrim"] as const) {
for (const whitespace of [" ", "\u00a0"]) {
it(`utf16le auto detected with ${option} and whitespace ${JSON.stringify(whitespace)} in sync API`, function () {
const input = Buffer.from(
`\ufeff${whitespace}a,${whitespace}b`,
"utf16le",
);
parseSync(input, { bom: true, [option]: true }).should.eql([
["a", "b"],
]);
});

it(`utf16le auto detected with ${option} and whitespace ${JSON.stringify(whitespace)} across byte-sized writes`, function (next) {
const input = Buffer.from(
`\ufeff${whitespace}a,${whitespace}b`,
"utf16le",
);
const parser = parse(
{ bom: true, [option]: true },
(err, records) => {
if (err) return next(err);
records.should.eql([["a", "b"]]);
next();
},
);
for (let i = 0; i < input.length; i++) {
parser.write(input.subarray(i, i + 1));
}
parser.end();
});
}
}

it("utf16le auto detected", function (next) {
const parser = parse(
{ bom: true, encoding: "utf16le" },
Expand Down