Skip to content

Commit cb953f2

Browse files
authored
Fix empty CSV generation. (#24)
1 parent e7be8ed commit cb953f2

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cats-configurator",
3-
"version": "0.3.3",
3+
"version": "0.3.4",
44
"private": true,
55
"scripts": {
66
"start": "vue-cli-service electron:serve",

src/modules/flightlog.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ export function exportFlightLogToCSVs(flightLog) {
44
const flightLogDir = "flight-log-export"
55
let flightLogSections = ["imu", "baro", "flightInfo", "orientationInfo", "filteredDataInfo", "gnssInfo", "flightStates", "eventInfo", "voltageInfo"];
66

7-
if (!fs.existsSync(flightLogDir)){
7+
if (!fs.existsSync(flightLogDir)) {
88
fs.mkdirSync(flightLogDir);
99
}
1010

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

32-
function objectArrayToCSV(arr, separator = ",") {
32+
function objectArrayToCSV(section, arr, separator = ",") {
3333
if (!Array.isArray(arr)) {
3434
console.log("objectArrayToCSV first argument must be an array.");
3535
return;
3636
}
3737

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

40-
let CSVHeader = CSVColumnNames.join(separator);
41-
let CSVBody = arr.map(obj =>
42-
CSVColumnNames.map(header => getObjectValue(obj, header)).join(separator)
43-
).join("\n");
42+
let CSVHeader = CSVColumnNames.join(separator);
43+
let CSVBody = arr.map(obj =>
44+
CSVColumnNames.map(header => getObjectValue(obj, header)).join(separator)
45+
).join("\n");
4446

45-
return CSVHeader + "\n" + CSVBody;
47+
return CSVHeader + "\n" + CSVBody;
48+
} else {
49+
console.log("Array length of " + section + " is 0!");
50+
return "No data recorded"
51+
}
4652
}
4753

4854
function getCSVColumnNames(obj) {

0 commit comments

Comments
 (0)