mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
feat(radar): export radar data
This commit is contained in:
48
frontend/src/lib/fn/csv.ts
Normal file
48
frontend/src/lib/fn/csv.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* 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;" });
|
||||
}
|
||||
|
||||
const headers = Object.keys(jsonData[0]);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// Escape quotes and wrap in quotes
|
||||
cell = cell.toString().replace(/"/g, '""');
|
||||
return `"${cell}"`;
|
||||
})
|
||||
.join(",");
|
||||
})
|
||||
].join("\n");
|
||||
|
||||
return new Blob([csvRows], { type: "text/csv;charset=utf-8;" });
|
||||
};
|
||||
Reference in New Issue
Block a user