2022-10-03 06:28:47 +00:00
|
|
|
/**
|
|
|
|
* Transforms an object to a raw data CSV file.
|
|
|
|
*
|
|
|
|
* @param {Object} rows Data
|
|
|
|
* @return {String} Formatted CSV data
|
|
|
|
*/
|
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;
|
|
|
|
}
|
2021-10-01 10:41:31 +00:00
|
|
|
|
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);
|
|
|
|
}
|
2021-10-01 10:41:31 +00:00
|
|
|
|
2022-01-18 12:15:42 +00:00
|
|
|
module.exports = {
|
|
|
|
toCSV,
|
|
|
|
formatDate
|
2021-10-01 10:41:31 +00:00
|
|
|
};
|