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
2 changes: 1 addition & 1 deletion packages/csv-parse/lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ const transform = function (original_options = {}) {
return;
}
}
this.info.bytes_records += this.info.bytes;
this.info.bytes_records = this.info.bytes;
push(record);
},
// Return a tuple with the error and the casted value
Expand Down
6 changes: 3 additions & 3 deletions packages/csv-parse/test/api.info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("API info", function () {
if (!info) return next(Error("Invalid assessment"));
info.should.eql({
bytes: 10,
bytes_records: 16,
bytes_records: 10,
columns: false,
comment_lines: 0,
empty_lines: 0,
Expand All @@ -25,7 +25,7 @@ describe("API info", function () {
if (!info) return next(Error("Invalid assessment"));
info.should.eql({
bytes: 11,
bytes_records: 17,
bytes_records: 11,
columns: false,
comment_lines: 0,
empty_lines: 0,
Expand Down Expand Up @@ -61,7 +61,7 @@ describe("API info", function () {
if (!info) return next(Error("Invalid assessment"));
info.should.eql({
bytes: 20,
bytes_records: 41,
bytes_records: 20,
columns: false,
comment_lines: 0,
empty_lines: 0,
Expand Down
53 changes: 53 additions & 0 deletions packages/csv-parse/test/info.bytes_records.offset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import assert from "node:assert/strict";
import { parse } from "../lib/index.js";

describe("info bytes_records as the last emitted record offset", function () {
it("keeps the previous record offset throughout field casting", function (next) {
const fieldOffsets = [];
parse(
"a,b\nc,d\ne,f",
{
cast(value, info) {
fieldOffsets.push(info.bytes_records);
return value;
},
},
(err, records, info) => {
if (err) return next(err);
assert.deepEqual(fieldOffsets, [0, 0, 4, 4, 8, 8]);
assert.equal(info.bytes, 11);
assert.equal(info.bytes_records, 11);
next();
},
);
});

for (const [name, input, options, expected] of [
[
"skipped middle record",
"a,b\nc,d\ne,f",
{ on_record: (record) => (record[0] === "c" ? null : record) },
11,
],
[
"skipped final record",
"a,b\nc,d\ne,f",
{ on_record: (record) => (record[0] === "e" ? null : record) },
8,
],
["column headers", "x,y\na,b\nc,d", { columns: true }, 11],
["from_line", "a,b\nc,d\ne,f", { from_line: 2 }, 11],
["UTF8 BOM", "\ufeffa,b\nc,d", { bom: true }, 10],
["no emitted records", "a,b\nc,d", { on_record: () => null }, 0],
["one emitted record", "a,b", {}, 3],
]) {
it(`reports an absolute offset with ${name}`, function (next) {
parse(input, options, (err, records, info) => {
if (err) return next(err);
assert.equal(info.bytes_records, expected);
assert.ok(info.bytes_records <= Buffer.byteLength(input));
next();
});
});
}
});
2 changes: 1 addition & 1 deletion packages/csv-parse/test/info.bytes_records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("info bytes_records", function () {
records.should.eql([
[0, 0],
[18, 18],
[46, 46],
[28, 28],
]);
});
});