-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
36 lines (31 loc) · 1.21 KB
/
script.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
const fs = require("fs");
const readline = require("readline");
const object = [];
async function processLineByLine() {
const fileStream = fs.createReadStream("data/data.txt");
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('\r\n') in input.txt as a single line break.
for await (const line of rl) {
const splitted_1 = line.split(" - ");
const categoryID = splitted_1[0];
const categories = splitted_1[1].split(" > ");
const depth = categories.length;
const objectToPush = { id: categoryID };
if (depth > 0) objectToPush.level1 = categories[0];
if (depth > 1) objectToPush.level2 = categories[1];
if (depth > 2) objectToPush.level3 = categories[2];
if (depth > 3) objectToPush.level4 = categories[3];
if (depth > 4) objectToPush.level5 = categories[4];
if (depth > 6) objectToPush.level6 = categories[5];
if (depth > 7) objectToPush.level7 = categories[6];
if (depth > 8) objectToPush.level8 = categories[7];
object.push(objectToPush);
}
const json = JSON.stringify(object);
fs.writeFileSync("data/data.json", json);
}
processLineByLine();