UNPKG

kea-react

Version:

Componentes comunes de react

53 lines (52 loc) 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var moment = require("moment"); /**Formatea una fecha con un formato compatible con el Excel*/ function exportDate(x) { return moment(x).format("YYYY-MM-DD HH:mm:ss"); } /**Devuelve una celda de CSV sin el separador o las comillas */ function toCsvCell(data) { if (data == null) return ""; if (typeof data == "string") { //quitamos las comillas y las comas var r = data.replace(/(?:\r?\n|\r)|"|,/g, ""); return r; } else if (typeof data == "number") { var r = "" + data; return r; } else if (data instanceof Date) { //Ya que excel no soporta el formato ISO 8601 exportamos con un formato diferente: var r = exportDate(data); return r; } else if (typeof data == "boolean") { var r = data ? "Si" : "No"; return r; } else if (data instanceof Array) { return data.join(", "); } else if (typeof data == "object") { return toCsvCell(Object.keys(data)); } var x = data; return "" + x; } /**Convierte una fila de datos a un CSV */ function toCsvRow(row) { return row .map(function (cell) { return "\"" + toCsvCell(cell) + "\""; }) .reduce(function (a, b) { return a == "" ? b : a + "," + b; }, ""); } /**Codigo para convertir los datos a CSV */ function toCSV(data) { var newLine = "\r\n"; return data .map(function (row) { return toCsvRow(row); }) .reduce(function (a, b) { return a == "" ? b : a + newLine + b; }, ""); } exports.toCSV = toCSV;