From 69e59916e25be3fe8511ca8134df2e0b444de166 Mon Sep 17 00:00:00 2001 From: jeiea Date: Wed, 17 Aug 2022 02:12:39 +0900 Subject: [PATCH] feat: support boolean and null in JSON to CSV --- src/core/operations/JSONToCSV.mjs | 7 +++++-- tests/operations/tests/JSONtoCSV.mjs | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/core/operations/JSONToCSV.mjs b/src/core/operations/JSONToCSV.mjs index 7eb3e3b4..875ff6e8 100644 --- a/src/core/operations/JSONToCSV.mjs +++ b/src/core/operations/JSONToCSV.mjs @@ -114,8 +114,11 @@ class JSONToCSV extends Operation { * @returns {string} */ escapeCellContents(data, force=false) { - if (typeof data === "number") data = data.toString(); - if (force && typeof data !== "string") data = JSON.stringify(data); + if (data !== "string") { + const isPrimitive = data == null || typeof data !== "object"; + if (isPrimitive) data = `${data}`; + else if (force) data = JSON.stringify(data); + } // Double quotes should be doubled up data = data.replace(/"/g, '""'); diff --git a/tests/operations/tests/JSONtoCSV.mjs b/tests/operations/tests/JSONtoCSV.mjs index a9a0867e..faf373d1 100644 --- a/tests/operations/tests/JSONtoCSV.mjs +++ b/tests/operations/tests/JSONtoCSV.mjs @@ -46,6 +46,17 @@ TestRegister.addTests([ }, ], }, + { + name: "JSON to CSV: boolean and null as values", + input: JSON.stringify({a: false, b: null, c: 3}), + expectedOutput: "a,b,c\r\nfalse,null,3\r\n", + recipeConfig: [ + { + op: "JSON to CSV", + args: [",", "\\r\\n"] + }, + ], + }, { name: "JSON to CSV: JSON as an array", input: JSON.stringify([{a: 1, b: "2", c: 3}]),