Skip to content

Commit

Permalink
Fix empty CSV generation. (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
stojadin2701 authored Oct 18, 2023
1 parent e7be8ed commit cb953f2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cats-configurator",
"version": "0.3.3",
"version": "0.3.4",
"private": true,
"scripts": {
"start": "vue-cli-service electron:serve",
Expand Down
24 changes: 15 additions & 9 deletions src/modules/flightlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ export function exportFlightLogToCSVs(flightLog) {
const flightLogDir = "flight-log-export"
let flightLogSections = ["imu", "baro", "flightInfo", "orientationInfo", "filteredDataInfo", "gnssInfo", "flightStates", "eventInfo", "voltageInfo"];

if (!fs.existsSync(flightLogDir)){
if (!fs.existsSync(flightLogDir)) {
fs.mkdirSync(flightLogDir);
}

for (let flightLogSection of flightLogSections) {
fs.writeFile(`${flightLogDir}/${flightLogSection}.csv`, objectArrayToCSV(flightLog[flightLogSection]), "utf8", function (err) {
fs.writeFile(`${flightLogDir}/${flightLogSection}.csv`, objectArrayToCSV(flightLogSection, flightLog[flightLogSection]), "utf8", function (err) {
if (err) {
console.log("An error occurred while writing CSV object to file.");
return console.log(err);
Expand All @@ -29,20 +29,26 @@ export function exportJSON(flightLog) {
});
}

function objectArrayToCSV(arr, separator = ",") {
function objectArrayToCSV(section, arr, separator = ",") {
if (!Array.isArray(arr)) {
console.log("objectArrayToCSV first argument must be an array.");
return;
}

let CSVColumnNames = getCSVColumnNames(arr[0]);
if (arr.length > 0) {
console.log("Section " + section + " :" + arr[0])
let CSVColumnNames = getCSVColumnNames(arr[0]);

let CSVHeader = CSVColumnNames.join(separator);
let CSVBody = arr.map(obj =>
CSVColumnNames.map(header => getObjectValue(obj, header)).join(separator)
).join("\n");
let CSVHeader = CSVColumnNames.join(separator);
let CSVBody = arr.map(obj =>
CSVColumnNames.map(header => getObjectValue(obj, header)).join(separator)
).join("\n");

return CSVHeader + "\n" + CSVBody;
return CSVHeader + "\n" + CSVBody;
} else {
console.log("Array length of " + section + " is 0!");
return "No data recorded"
}
}

function getCSVColumnNames(obj) {
Expand Down

0 comments on commit cb953f2

Please sign in to comment.