Skip to content
Merged
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
6 changes: 2 additions & 4 deletions packages/csv-parse/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,11 @@ const parse = function () {
const parser = new Parser(options);
if (callback) {
const records =
options === undefined || options.objname === undefined
? []
: Object.create(null);
parser.options.objname === undefined ? [] : Object.create(null);
parser.on("readable", function () {
let record;
while ((record = this.read()) !== null) {
if (options === undefined || options.objname === undefined) {
if (parser.options.objname === undefined) {
records.push(record);
} else {
Object.assign(records, {
Expand Down
3 changes: 2 additions & 1 deletion packages/csv-parse/lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ const parse = function (data, opts = {}) {
if (typeof data === "string") {
data = Buffer.from(data);
}
const records = opts && opts.objname ? Object.create(null) : [];
const parser = transform(opts);
const records =
parser.options.objname === undefined ? [] : Object.create(null);
const push = (record) => {
if (parser.options.objname === undefined) records.push(record);
else {
Expand Down
28 changes: 28 additions & 0 deletions packages/csv-parse/test/option.objname.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import "should";
import assert from "node:assert/strict";
import { parse } from "../lib/index.js";
import { parse as parseSync } from "../lib/sync.js";

describe("Option `objname`", function () {
describe("validation", function () {
Expand All @@ -11,4 +13,30 @@ describe("Option `objname`", function () {
);
});
});

describe("normalized result mode", function () {
it("returns a dictionary with the first column as key", function () {
const records = parseSync("alice,A\nbob,B", { objname: 0 });
assert.deepEqual(
records,
Object.assign(Object.create(null), {
alice: ["alice", "A"],
bob: ["bob", "B"],
}),
);
});

for (const objname of [null, false]) {
it(`returns complete array records with objname ${objname}`, function (next) {
parse("alice,A,extra\nbob,B,extra", { objname }, (err, records) => {
if (err) return next(err);
assert.deepEqual(records, [
["alice", "A", "extra"],
["bob", "B", "extra"],
]);
next();
});
});
}
});
});
Loading