From 87f4a8b4baf31d1bbe6ce080228493983c0dd6c5 Mon Sep 17 00:00:00 2001 From: Yuki Tanaka Date: Thu, 30 Mar 2023 15:36:17 +0900 Subject: [PATCH] fix(csv/csv_stringify_stream): output headers based on `columns` option (#3293) --- csv/csv_stringify_stream.ts | 11 +++++++++++ csv/csv_stringify_stream_test.ts | 1 + 2 files changed, 12 insertions(+) diff --git a/csv/csv_stringify_stream.ts b/csv/csv_stringify_stream.ts index 7dd80bb452d5..cf5767783f06 100644 --- a/csv/csv_stringify_stream.ts +++ b/csv/csv_stringify_stream.ts @@ -52,6 +52,17 @@ export class CsvStringifyStream super( { + start(controller) { + if (columns && columns.length > 0) { + try { + controller.enqueue( + stringify([columns], { separator, headers: false }), + ); + } catch (error) { + controller.error(error); + } + } + }, transform(chunk, controller) { try { controller.enqueue( diff --git a/csv/csv_stringify_stream_test.ts b/csv/csv_stringify_stream_test.ts index 1fd00fd320e6..a5e18a43b227 100644 --- a/csv/csv_stringify_stream_test.ts +++ b/csv/csv_stringify_stream_test.ts @@ -89,6 +89,7 @@ Deno.test({ output.push(r); } assertEquals(output, [ + "id,name\r\n", "1,foo\r\n", "2,bar\r\n", "3,baz\r\n",