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
4 changes: 1 addition & 3 deletions packages/csv-parse/lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,7 @@ const transform = function (original_options = {}) {
),
);
} else {
if (lappend === false) {
pos += this.__isCharTrimable(buf, pos) - 1;
}
pos += this.__isCharTrimable(buf, pos) - 1;
continue;
}
}
Expand Down
62 changes: 62 additions & 0 deletions packages/csv-parse/test/option.rtrim.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,71 @@
import "should";
import dedent from "dedent";
import { parse } from "../lib/index.js";
import { parse as parseSync } from "../lib/sync.js";
import { assert_error } from "./api.assert_error.js";

describe("Option `rtrim`", function () {
for (const encoding of ["utf8", "utf16le"] as const) {
for (const trimming of [{ rtrim: true }, { trim: true }]) {
describe(`${encoding} with ${Object.keys(trimming)[0]}`, function () {
const whitespaces = [" ", "\t", "\u00a0", "\u2003", "\u3000"];

it("trims after quoted fields before delimiters and end of input", function () {
for (const whitespace of whitespaces) {
const data = Buffer.from(
`" a "${whitespace},""${whitespace}|"b"${whitespace},"c"${whitespace}`,
encoding,
);
parseSync(data, {
...trimming,
encoding,
record_delimiter: "|",
}).should.eql([
[" a ", ""],
["b", "c"],
]);
}
});

it("trims after quoted fields with one-byte stream writes", async function () {
for (const whitespace of whitespaces) {
const data = Buffer.from(
`" a "${whitespace},""${whitespace}|"b"${whitespace},"c"${whitespace}`,
encoding,
);
const records = await new Promise<string[][]>((resolve, reject) => {
const parser = parse(
{ ...trimming, encoding, record_delimiter: "|" },
(err, records) => (err ? reject(err) : resolve(records)),
);
for (const byte of data) {
parser.write(Buffer.from([byte]));
}
parser.end();
});
records.should.eql([
[" a ", ""],
["b", "c"],
]);
}
});

it("rejects non-whitespace after trailing whitespace", function () {
for (const whitespace of whitespaces) {
(() => {
parseSync(Buffer.from(`"a"${whitespace}x`, encoding), {
...trimming,
encoding,
});
}).should.throw({
code: "CSV_NON_TRIMABLE_CHAR_AFTER_CLOSING_QUOTE",
});
}
});
});
}
}

it("validation", function () {
parse("", { rtrim: true }, () => {});
parse("", { rtrim: false }, () => {});
Expand Down