Skip to content

Commit

Permalink
Merge pull request #299 from vizzuhq/doc_csvheader
Browse files Browse the repository at this point in the history
documentation: csv header generation fixed
  • Loading branch information
veghdev authored Feb 26, 2024
2 parents 4b1c90b + caa2376 commit c6e2dc5
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 c6e2dc5

Please sign in to comment.