@spare/csv
Version:
Csv table formatter
48 lines (42 loc) • 1.35 kB
JavaScript
import { AEU, LF } from '@spare/enum-chars';
import { tableMargin } from '@spare/table-margin';
import { tablePadder } from '@spare/table-padder';
import { size, mutate } from '@vect/matrix';
class Csv {
/***
*
* @param {Object} table
* @param {Object} option
*
*
* @param {Function} [option.read]
* @param {Function} [option.headRead]
*
* @param {number} [option.top]
* @param {number} [option.bottom]
* @param {number} [option.left]
* @param {number} [option.right]
*
* @param {boolean} [option.ansi=true]
* @param {boolean} [option.fullAngle]
* @param {number} [option.level=0]
*
* @returns {string}
*/
static table(table, option = {}) {
if (!table) return AEU;
let matrix = table.rows || table.matrix,
banner = table.head || table.banner;
const [height, width] = size(matrix),
labelWidth = banner === null || banner === void 0 ? void 0 : banner.length;
if (!height || !width || !labelWidth) return AEU;
table = tableMargin(table, option); // use: top, left, bottom ,right, read, headRead
let {
head,
rows
} = tablePadder(table, option); // use: ansi, full
mutate(rows, text => text.includes(',') ? `"${text}"` : text);
return [head.join(','), ...rows.map(row => row.join(','))].join(LF);
}
}
export { Csv };