createStream.js.flow
2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import _ from 'lodash';
import makeStreamConfig from './makeStreamConfig';
import drawRow from './drawRow';
import {
drawBorderBottom,
drawBorderJoin,
drawBorderTop
} from './drawBorder';
import stringifyTableData from './stringifyTableData';
import truncateTableData from './truncateTableData';
import mapDataUsingRowHeightIndex from './mapDataUsingRowHeightIndex';
import alignTableData from './alignTableData';
import padTableData from './padTableData';
import calculateRowHeightIndex from './calculateRowHeightIndex';
/**
* @param {Array} data
* @param {Object} config
* @returns {Array}
*/
const prepareData = (data, config) => {
let rows;
rows = stringifyTableData(data);
rows = truncateTableData(data, config);
const rowHeightIndex = calculateRowHeightIndex(rows, config);
rows = mapDataUsingRowHeightIndex(rows, rowHeightIndex, config);
rows = alignTableData(rows, config);
rows = padTableData(rows, config);
return rows;
};
/**
* @param {string[]} row
* @param {number[]} columnWidthIndex
* @param {Object} config
* @returns {undefined}
*/
const create = (row, columnWidthIndex, config) => {
const rows = prepareData([row], config);
const body = rows.map((literalRow) => {
return drawRow(literalRow, config.border);
}).join('');
let output;
output = '';
output += drawBorderTop(columnWidthIndex, config.border);
output += body;
output += drawBorderBottom(columnWidthIndex, config.border);
output = _.trimEnd(output);
process.stdout.write(output);
};
/**
* @param {string[]} row
* @param {number[]} columnWidthIndex
* @param {Object} config
* @returns {undefined}
*/
const append = (row, columnWidthIndex, config) => {
const rows = prepareData([row], config);
const body = rows.map((literalRow) => {
return drawRow(literalRow, config.border);
}).join('');
let output = '';
const bottom = drawBorderBottom(columnWidthIndex, config.border);
if (bottom !== '\n') {
output = '\r\u001B[K';
}
output += drawBorderJoin(columnWidthIndex, config.border);
output += body;
output += bottom;
output = _.trimEnd(output);
process.stdout.write(output);
};
/**
* @param {Object} userConfig
* @returns {Object}
*/
export default (userConfig = {}) => {
const config = makeStreamConfig(userConfig);
// @todo Use 'Object.values' when Node.js v6 support is dropped.
const columnWidthIndex = _.values(_.mapValues(config.columns, (column) => {
return column.width + column.paddingLeft + column.paddingRight;
}));
let empty;
empty = true;
return {
/**
* @param {string[]} row
* @returns {undefined}
*/
write: (row) => {
if (row.length !== config.columnCount) {
throw new Error('Row cell count does not match the config.columnCount.');
}
if (empty) {
empty = false;
return create(row, columnWidthIndex, config);
} else {
return append(row, columnWidthIndex, config);
}
}
};
};