budibase/packages/server/src/api/controllers/view/exporters.js

24 lines
497 B
JavaScript

exports.csv = function (headers, rows) {
let csv = headers.map(key => `"${key}"`).join(",")
for (let row of rows) {
csv = `${csv}\n${headers
.map(header => {
let val = row[header]
val = typeof val === "object" ? JSON.stringify(val) : val
return `"${val}"`.trim()
})
.join(",")}`
}
return csv
}
exports.json = function (headers, rows) {
return JSON.stringify(rows, undefined, 2)
}
exports.ExportFormats = {
CSV: "csv",
JSON: "json",
}