blob: a13ac02c4cbb00d9ad62828f1af8189153ced27c (
plain)
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
|
const fs = require('fs');
let average = (array) => array.reduce((a, b) => a + b) / array.length;
function tryrem(file) {
try {
data = JSON.parse(fs.readFileSync(file));
} catch (e) {
console.log("Failed to save, removing file");
fs.unlinkSync(file);
tryrem(file);
}
}
module.exports = (percs, words, file, silent) => {
if (typeof silent == "undefined") {
silent = false;
}
tryrem(file);
keys = Object.keys(percs);
keys2 = Object.keys(data);
for (var i=0; i<keys.length; i++) {
key = keys[i];
if (key !== "total") {
data[key].entries.push(percs[key]);
}
}
if (!silent) console.log("Saving words list...");
newwords = [];
words.forEach((word) => {
if (!data["words"].includes(word)) {
newwords.push(word);
}
})
data["words"] = [...data["words"], ...newwords];
if (!silent) console.log("Recalculating averages...");
for (var i=0; i<keys2.length; i++) {
key = keys2[i];
if (key !== "total" && key !== "words") {
data[key].average = average(data[key].entries);
}
}
fs.writeFileSync(file, JSON.stringify(data));
}
|