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