console-toolkit
Version:
Toolkit to produce a fancy console output (boxes, tables, charts, colors).
25 lines (19 loc) • 852 B
JavaScript
import defaultTheme from '../themes/default.js';
import {normalizeData, sumValues} from '../utils.js';
export const drawChart =
drawRow =>
(values, width, options = {}) => {
const {maxValue, gap = 0, theme = defaultTheme} = options;
if (isNaN(width) || width <= 0) throw new Error(`"width" should be a positive integer instead of "${width}"`);
const data = normalizeData(values, theme),
max =
typeof maxValue == 'number' ? (maxValue < 0 ? -1 : maxValue) : Math.max(0, ...data.map(row => sumValues(row)));
if (gap < 1) return data.map(row => drawRow(row, width, max, options)).flat(1);
const result = [];
data.forEach((row, i) => {
if (i) result.push(...new Array(gap).fill(''));
result.push(drawRow(row, width, max, options));
});
return result.flat(1);
};
export default drawChart;