requested changes

This commit is contained in:
Daniel Hougaard
2025-01-15 01:55:10 +01:00
parent ab3ee775bb
commit ff74e020fc
3 changed files with 18 additions and 26 deletions

View File

@@ -1,11 +1,8 @@
/**
* Converts a JSON array of objects to CSV format
* @param {Array} jsonData - Array of objects to convert
* @param {string} filename - Name of the file to download (without extension)
*/
export const convertJsonToCsv = (jsonData: Record<string, string | number | boolean>[]) => {
// Get headers from the first object
if (jsonData.length === 0) {
return new Blob([""], { type: "text/csv;charset=utf-8;" });
}
@@ -14,24 +11,19 @@ export const convertJsonToCsv = (jsonData: Record<string, string | number | bool
const csvRows = [
headers.join(","),
// Add all data rows
...jsonData.map((row) => {
return headers
.map((header) => {
// Handle special cases in the data
let cell = row[header];
// Convert null/undefined to empty string
if (cell === null || cell === undefined) {
return "";
}
// Handle normal values
if (typeof cell === "number" || typeof cell === "boolean") {
return cell;
}
// Convert objects/arrays to JSON string
if (typeof cell === "object") {
cell = JSON.stringify(cell);
}