-
Notifications
You must be signed in to change notification settings - Fork 0
/
command-palette.js
85 lines (76 loc) · 2.33 KB
/
command-palette.js
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
const fs = require('fs');
const readline = require('readline');
function read_csv(filename, csvSeparator) {
const data = [];
const fileStream = fs.createReadStream(filename);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
return new Promise(resolve => {
rl.on('line', line => {
data.push(line.split(csvSeparator));
});
rl.on('close', () => {
resolve(data);
});
});
}
function longestRow(data) {
let max = 0;
for (const row of data) {
if (row.length > max) {
max = row.length;
}
}
return max;
}
function generateVertical(length, mode) {
let finalHeader = '{';
for (let i = 0; i < length; i++) {
if (i === 0 && (mode === 1 || mode === 3 || mode === 4)) {
finalHeader += "|";
}
finalHeader += "c";
if ((i !== length - 1 && mode === 2) || (mode === 1 || mode === 3)) {
finalHeader += "|";
}
if (i === length - 1 && mode === 4) {
finalHeader += "|";
}
}
return finalHeader + '}';
}
function generateTable(data, mode) {
let finalTable = "\\begin{table}[H]\n\\begin{tabular} ";
finalTable += generateVertical(longestRow(data), mode) + '\n';
if (mode === 1 || mode === 4 || mode === 3) {
finalTable += "\\hline\n";
}
const max_lengths = Array.from({ length: data[0].length }, (_, i) => Math.max(...data.map(row => row[i].length)));
for (let index = 0; index < data.length; index++) {
const row = data[index];
for (let i = 0; i < row.length; i++) {
finalTable += row[i].padEnd(max_lengths[i]) + " & ";
}
finalTable = finalTable.slice(0, -3) + " \\\\ ";
if ((index !== data.length - 1 && mode === 2) || mode === 1 || mode === 4) {
finalTable += "\\hline";
}
if (index === data.length - 1 && mode === 3) {
finalTable += "\\hline";
}
finalTable += '\n';
}
finalTable += "\\end{tabular}\n\\end{table}";
return finalTable;
}
function writeToFile(table, filename, folderPath) {
fs.writeFileSync(`${folderPath}/[latex-table]-${filename.slice(0, -4)}.txt`, table);
}
module.exports = {
read_csv,
longestRow,
generateTable,
writeToFile
};