32 lines
762 B
JavaScript
32 lines
762 B
JavaScript
function toCSV(rows) {
|
|
const [columns] = rows;
|
|
let content = Object.keys(columns).join('\t');
|
|
for (let row of rows) {
|
|
const values = Object.values(row);
|
|
const finalValues = values.map(value => {
|
|
if (value instanceof Date) return formatDate(value);
|
|
if (value === null) return '';
|
|
return value;
|
|
});
|
|
content += '\n';
|
|
content += finalValues.join('\t');
|
|
}
|
|
return content;
|
|
}
|
|
|
|
function formatDate(date) {
|
|
return new Intl.DateTimeFormat('es', {
|
|
year: 'numeric',
|
|
month: 'numeric',
|
|
day: 'numeric',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
second: '2-digit'
|
|
}).format(date);
|
|
}
|
|
|
|
module.exports = {
|
|
toCSV,
|
|
formatDate
|
|
};
|