-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (99 loc) · 3.4 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const fs = require("fs");
const path = require("path");
// Detect diretory ./source and ./output
const source = "./source";
const output = "./output";
if (!fs.existsSync(source)) fs.mkdirSync(source);
if (!fs.existsSync(output)) fs.mkdirSync(output);
// Filer type judgement regex
const regexP = /Programme Code/gi;
const regexM = /Module code/gi;
const outputFileExtension = ".json";
// Read files list in source directory
const files = fs.readdirSync(source);
// Loop through files
files.forEach((file) => {
// Read file content
const data = fs.readFileSync(path.join(source, file), "utf-8");
let object = {};
const lines = data.split("\r\n");
// Judge file type
console.log("ℹ️ Processing file: " + file + " ...");
if (regexP.test(lines[0])) {
// PROGRAMME STATISTIC CONVERTER
regexP.lastIndex = 0; // Reset regex position
const titles = lines[0]
.split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/g)
.map((field) => field.trim().replace(/^"|"$/g, ""));
lastCode = "";
for (let i = 1; i < lines.length; i++) {
const line = lines[i]
.split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/g)
.map((field) => field.trim().replace(/^"|"$/g, ""));
if (line[0]) {
lastCode = line[0];
object[line[0]] = {};
for (let j = 1; j < line.length - 9; j++) {
object[line[0]][titles[j]] = line[j];
}
object[line[0]][titles[line.length - 9]] = {};
object[line[0]][titles[line.length - 9]][line[line.length - 9]] = {};
for (let j = line.length - 8; j < line.length; j++) {
object[line[0]][titles[line.length - 9]][line[line.length - 9]][
titles[j]
] = line[j];
}
} else {
object[lastCode][titles[line.length - 9]][line[line.length - 9]] = {};
for (let j = line.length - 8; j < line.length; j++) {
object[lastCode][titles[line.length - 9]][line[line.length - 9]][
titles[j]
] = line[j];
}
}
}
} else if (regexM.test(lines[0])) {
// MODULE STATISTIC CONVERTER
regexM.lastIndex = 0; // Reset regex position
const titles = lines[0]
.split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/g)
.map((field) => field.trim().replace(/^"|"$/g, ""));
for (let i = 1; i < lines.length; i++) {
const line = lines[i]
.split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/g)
.map((field) => field.trim().replace(/^"|"$/g, ""));
object[line[0]] = {};
for (let j = 1; j < 7; j++) {
object[line[0]][titles[j]] = line[j];
}
object[line[0]]["Grade Distribution"] = {};
for (let j = 7; j < line.length; j = j + 2) {
if (j === line.length - 1) {
object[line[0]][titles[line.length - 1]] = line[line.length - 1];
break;
}
object[line[0]]["Grade Distribution"][titles[j]] = {};
object[line[0]]["Grade Distribution"][titles[j]]["No."] = line[j];
object[line[0]]["Grade Distribution"][titles[j]]["Percentage"] =
line[j + 1];
}
}
} else {
// UNKNOWN FILE TYPE
console.error(
" ⛔️ Unknown file type: " +
file +
", please check the file type and try again."
);
return;
}
fs.writeFileSync(
path.join(
output,
path.basename(path.join(source, file)) + outputFileExtension
),
JSON.stringify(object),
"utf-8"
);
console.log(" ✅ File " + file + " has been processed successfully.");
});