salix/print/methods/csv/csv.js

32 lines
762 B
JavaScript
Raw Normal View History

2022-01-18 12:15:42 +00:00
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;
}
2022-01-18 12:15:42 +00:00
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);
}
2022-01-18 12:15:42 +00:00
module.exports = {
toCSV,
formatDate
};