Skip to content
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

fix(csv/csv_stringify_stream): output headers based on columns option #3293

Merged
merged 1 commit into from
Mar 30, 2023

Conversation

uki00a
Copy link
Contributor

@uki00a uki00a commented Mar 29, 2023

This PR changes CsvStringifyStream to output headers when the columns option is given. This change is intended to make it easier to output header columns when streaming objects.

Example

// test.js
import { CsvStringifyStream } from "./csv/csv_stringify_stream.ts";
import { readableStreamFromIterable } from "./streams/readable_stream_from_iterable.ts";

const file = await Deno.open("data.csv", { create: true, write: true });
const readable = readableStreamFromIterable([
  { id: 1, name: "one" },
  { id: 2, name: "two" },
  { id: 3, name: "three" },
]);

await readable
  .pipeThrough(new CsvStringifyStream({ columns: ["id", "name"] }))
  .pipeThrough(new TextEncoderStream())
  .pipeTo(file.writable);

Before:

$ deno run --allow-write main.js
$ cat data.csv
1,one
2,two
3,three

After:

$ deno run --allow-write main.js
$ cat data.csv
id,name
1,one
2,two
3,three

@uki00a uki00a requested a review from kt3k as a code owner March 29, 2023 13:11
Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. LGTM.

@kt3k kt3k merged commit 87f4a8b into denoland:main Mar 30, 2023
@uki00a uki00a deleted the output-headers branch March 30, 2023 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants