createStream.js
3.95 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _makeStreamConfig = require('./makeStreamConfig');
var _makeStreamConfig2 = _interopRequireDefault(_makeStreamConfig);
var _drawRow = require('./drawRow');
var _drawRow2 = _interopRequireDefault(_drawRow);
var _drawBorder = require('./drawBorder');
var _stringifyTableData = require('./stringifyTableData');
var _stringifyTableData2 = _interopRequireDefault(_stringifyTableData);
var _truncateTableData = require('./truncateTableData');
var _truncateTableData2 = _interopRequireDefault(_truncateTableData);
var _mapDataUsingRowHeightIndex = require('./mapDataUsingRowHeightIndex');
var _mapDataUsingRowHeightIndex2 = _interopRequireDefault(_mapDataUsingRowHeightIndex);
var _alignTableData = require('./alignTableData');
var _alignTableData2 = _interopRequireDefault(_alignTableData);
var _padTableData = require('./padTableData');
var _padTableData2 = _interopRequireDefault(_padTableData);
var _calculateRowHeightIndex = require('./calculateRowHeightIndex');
var _calculateRowHeightIndex2 = _interopRequireDefault(_calculateRowHeightIndex);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @param {Array} data
* @param {Object} config
* @returns {Array}
*/
const prepareData = (data, config) => {
let rows;
rows = (0, _stringifyTableData2.default)(data);
rows = (0, _truncateTableData2.default)(data, config);
const rowHeightIndex = (0, _calculateRowHeightIndex2.default)(rows, config);
rows = (0, _mapDataUsingRowHeightIndex2.default)(rows, rowHeightIndex, config);
rows = (0, _alignTableData2.default)(rows, config);
rows = (0, _padTableData2.default)(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 = _lodash2.default.map(rows, literalRow => {
return (0, _drawRow2.default)(literalRow, config.border);
}).join('');
let output;
output = '';
output += (0, _drawBorder.drawBorderTop)(columnWidthIndex, config.border);
output += body;
output += (0, _drawBorder.drawBorderBottom)(columnWidthIndex, config.border);
output = _lodash2.default.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 = _lodash2.default.map(rows, literalRow => {
return (0, _drawRow2.default)(literalRow, config.border);
}).join('');
let output;
output = '\r\x1b[K';
output += (0, _drawBorder.drawBorderJoin)(columnWidthIndex, config.border);
output += body;
output += (0, _drawBorder.drawBorderBottom)(columnWidthIndex, config.border);
output = _lodash2.default.trimEnd(output);
process.stdout.write(output);
};
/**
* @param {Object} userConfig
* @returns {Object}
*/
exports.default = function () {
let userConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
const config = (0, _makeStreamConfig2.default)(userConfig);
const columnWidthIndex = _lodash2.default.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);
}
}
};
};
module.exports = exports['default'];