From a42bc40b1679cec5cb5adec3adde19d3ea2fb917 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Wed, 26 Jun 2024 18:39:48 +0900 Subject: [PATCH] test(csv): improve stringify testing --- csv/stringify.ts | 4 +++- csv/stringify_test.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/csv/stringify.ts b/csv/stringify.ts index 10a9703e9e56..705533f3d23b 100644 --- a/csv/stringify.ts +++ b/csv/stringify.ts @@ -218,7 +218,9 @@ function getValuesFromItem( let value: unknown = item; for (const prop of column.prop) { - if (typeof value !== "object" || value === null) continue; + if (typeof value !== "object" || value === null) { + continue; + } if (Array.isArray(value)) { if (typeof prop === "number") value = value[prop]; else { diff --git a/csv/stringify_test.ts b/csv/stringify_test.ts index 9671829df506..77e4bc14b777 100644 --- a/csv/stringify_test.ts +++ b/csv/stringify_test.ts @@ -275,6 +275,17 @@ Deno.test({ }, }, ); + await t.step( + { + name: "Column: array string accessor via prop field", + fn() { + const columns = [{ prop: ["msg", "value"] }]; + const data = [{ msg: { value: "foo" } }, { msg: { value: "bar" } }]; + const output = `value${CRLF}foo${CRLF}bar${CRLF}`; + assertEquals(stringify(data, { columns }), output); + }, + }, + ); await t.step( { name: "Explicit header", @@ -367,6 +378,18 @@ Deno.test({ }, }, ); + await t.step( + { + name: "The case when the entire item is null", + fn() { + const columns = [0]; + const data = [null, null]; + const output = `0${CRLF}${CRLF}${CRLF}`; + // deno-lint-ignore no-explicit-any + assertEquals(stringify(data as any, { columns }), output); + }, + }, + ); await t.step( { name: "Targeted value: hex number", @@ -510,6 +533,16 @@ Deno.test({ assertEquals(stringify(data), output); }, }); + await t.step({ + name: "The case when each item is single data, no columns", + fn() { + const data = [1, 2, 3, 4, 5, 6]; + const output = `1${CRLF}2${CRLF}3${CRLF}4${CRLF}5${CRLF}6${CRLF}`; + + // deno-lint-ignore no-explicit-any + assertEquals(stringify(data as any), output); + }, + }); await t.step( { name: "byte-order mark with bom=true",