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