Skip to content

Commit

Permalink
documentation: csv header generation fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
veghdev committed Feb 26, 2024
1 parent 4b1c90b commit caa2376
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions tools/docs/examples/mjs2csv.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ class Js2csv {
this.data = data
}

addApostrophesIfContainsComma(value) {
if (typeof value === 'string' && value.includes(',')) {
return `"${value}"`
}
return value
}

getHeaderLine() {
const header = []
for (const series in this.data.series) {
header.push(this.data.series[series].name)
const value = this.addApostrophesIfContainsComma(this.data.series[series].name)
header.push(value)
}
return header.join(',') + '\n'
}
Expand All @@ -15,10 +23,7 @@ class Js2csv {
const line = []
const record = {}
for (const j in this.data.series) {
let value = this.data.series[j].values[i]
if (typeof value === 'string' && value.includes(',')) {
value = `"${value}"`
}
const value = this.addApostrophesIfContainsComma(this.data.series[j].values[i])
record[this.data.series[j].name] = value
line.push(value)
}
Expand All @@ -34,10 +39,7 @@ class Js2csv {
const line = []
const record = {}
for (const j in this.data.series) {
let value = this.data.records[i][j]
if (typeof value === 'string' && value.includes(',')) {
value = `"${value}"`
}
const value = this.addApostrophesIfContainsComma(this.data.records[i][j])
record[this.data.series[j].name] = value
line.push(value)
}
Expand Down

0 comments on commit caa2376

Please sign in to comment.