-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add header
option support to the CSV module
#4295
Conversation
I find the DX introduced here not optimal. To be honest, without reading the docs, my intuition would be that I would prefer a clearer name, something like |
I see where you're coming from @codebien, and I think I agree, I reused the name paparse uses, but in hindsight, I also find it a bit confusing 👍🏻 Giving this a thought, I propose we rename it to |
Also heads-up @going-confetti 👋🏻 |
1c41c35
to
9b66e3b
Compare
Yes, this is better. I like it. @oleiade Now, another question as a devil's advocate. Are we sure that it shouldn't be the default option? Why do we think an array of arrays is better for the default? |
// users.csv
// Username,First name,Last name
// jdoe,john,doe
// immicky,mickey,mouse
const arrayOfArrays = csv.parse("./users.csv", { rowsAs: "array" })
const arrayOfObjects = csv.parse("./users.csv", { rowsAs: "object" })
// Future possibility:
const customMapping = csv.parse("./users.csv", {
// Might seem similar to just doing `array.map` but this would avoid having to
// iterate the list twice.
rowsAs: ([userName, firstName, lastName]) => {
return {
userName,
fullName: `${firstName} ${lastName}`
}
}
})
"If only this was an array of arrays", said no one ever. |
I think the approach you folks suggest makes a ton of sense, looking around about this, I actually noticed that Deno's 2.0 csv module took a similar approach: import { parse, stringify } from "jsr:@std/csv";
let text = `
url,views,likes
https://deno.land,10,7
https://deno.land/x,20,15
https://deno.dev,30,23
`;
let data = parse(text, {
skipFirstRow: true,
strip: true,
});
console.log(data[0].url); // https://deno.land
console.log(data[0].views); // 10
console.log(data[0].likes); // 7
text = `
https://deno.land,10,7
https://deno.land/x,20,15
https://deno.dev,30,23
`;
data = parse(text, {
columns: ["url", "views", "likes"],
});
console.log(data[0].url); // https://deno.land
console.log(data[0].views); // 10
console.log(data[0].likes); // 7
const obj = [
{ mascot: "dino", fans: { old: 100, new: 200 } },
{ mascot: "bread", fans: { old: 5, new: 2 } },
];
const csv = stringify(obj, {
columns: [
"mascot",
["fans", "new"],
],
});
console.log(csv);
// mascot,new
// dino,200
// bread,2 Note that by default rows are treated as maps (arrays are also objects in js after all), with the option to provide the column names yourself if you want. I find it aligns pretty well with your example @allansson too. I really like this direction, but I suggest we open an issue, and address such a redesign separately, because that would be a big chunk of work, and as to not block this PR which would bring immediate value already. What's your take, do you folks agree, or would you say we should completely change the design itself? (bringing @Llandy3d in the loop to give him visibility on this) |
Co-authored-by: Ivan <[email protected]>
b3cf5b5
to
b3208dc
Compare
@oleiade I'm fine with it. Please, remember to open an issue so we can link it into the epic issue to stabilize the csv module. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍 Just one suggestion.
Co-authored-by: İnanç Gümüş <[email protected]>
What?
Adds support for a
header
option to the csv module's option. When set totrue
, theheader
option leads the module'sparse
function as well as theParser.next
method to return records as objects with the header corresping row name as the property key, and value as the value.Would print:
As opposed to, for reference, treating records as array without the header option set:
Note: that the same behavior can be observed when using a
Parser
'snext
method with theheader
option set totrue
.Why?
This PR tackles #4284, and directly addresses a request from the team behind k6 studio, and one of the project's issues k6-studio/#363.
It also nicely complements the feature set of the csv parser in general, and aligns its options-set with papaparse's.
Checklist
make lint
) and all checks pass.make tests
) and all tests pass.Related PR(s)/Issue(s)
k6/#4284
k6-studio/#363