Skip to content

Commit 3bd9655

Browse files
juanjoDiazknownasilya
authored andcommitted
fix: pretty print issues (#242)
1 parent 6cb407c commit 3bd9655

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

bin/json2csv.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,25 @@ function getInputFromStdin() {
115115
}
116116

117117
function logPretty(csv) {
118-
const lines = csv.split(os.EOL);
119-
const table = new Table({
120-
head: lines[0].split(','),
121-
colWidths: lines[0].split('","').map(elem => elem.length * 2)
122-
});
118+
let lines = csv.split(os.EOL);
119+
const header = program.header ? lines.shift().split(',') : undefined;
120+
121+
const table = new Table(header ? {
122+
head: header,
123+
colWidths: header.map(elem => elem.length * 2)
124+
} : undefined);
123125

124-
for (let i = 1; i < lines.length; i++) {
125-
table.push(lines[i].split(','));
126-
}
127-
return table.toString();
126+
lines.forEach(line => table.push(line.split(',')));
127+
128+
// eslint-disable-next-line no-console
129+
console.log(table.toString());
128130
}
129131

130132
function processOutput(csv) {
131133
if (!outputPath) {
132134
// eslint-disable-next-line no-console
133-
console.log(program.pretty ? logPretty(csv) : csv);
135+
program.pretty ? logPretty(csv) : console.log(csv);
136+
return;
134137
}
135138

136139
return new Promise((resolve, reject) => {
@@ -189,15 +192,13 @@ getFields(program.fieldList, program.fields)
189192
});
190193
}
191194

192-
let csv = '';
193195
return new Promise((resolve, reject) => {
196+
let csv = '';
194197
stream
195198
.on('data', chunk => (csv += chunk.toString()))
196199
.on('end', () => resolve(csv))
197200
.on('error', reject);
198-
})
199-
// eslint-disable-next-line no-console
200-
.then(() => console.log(logPretty(csv)));
201+
}).then(logPretty);
201202
})
202203
// eslint-disable-next-line no-console
203204
.catch(console.log);

0 commit comments

Comments
 (0)