-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
30 lines (28 loc) · 957 Bytes
/
utils.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
const fs = require("fs");
const papaparse = require("papaparse");
const iconv = require("iconv-lite");
const readTabFile = (file_path) =>
new Promise((resolve, reject) => {
if (!fs.existsSync(file_path)) return resolve([]);
papaparse.parse(iconv.decode(fs.readFileSync(file_path), "gbk"), {
complete: function (results) {
let result = [];
let data = results.data;
let head = data.shift();
data.pop();
for (let d of data) {
let obj = {};
for (let i = 0; i < d.length; i++) {
if (d[i] === "" || d[i] === " ") d[i] = null;
obj[head[i]] = d[i];
}
result.push(obj);
}
resolve(result);
},
delimiter: "\t",
});
});
module.exports = {
readTabFile,
};