-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathruleCitationUpdateScript.js
124 lines (111 loc) · 3.84 KB
/
ruleCitationUpdateScript.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"use strict";
//"700" will change all 700 rule citations to 701, 701 to 702, etc up to the end of the section. "700.1" will change 700.1 to 700.2, etc. You can probably guess what "700.1a" does. Type the rule that got added/removed.
const startingRuleToUpdate = "805.10f";
const change = "removed";//Can be "added" or "removed".
//Returns the next letter of the alphabet, skipping "l" and "o".
const nextLetter = function(letter) {
if (letter.length === 1) {
return "abcdefghijkmnpqrstuvwxyz"["abcdefghijkmnpqrstuvwxyz".indexOf(letter) + 1];
} else {
console.log(`"${letter}" is not a single letter.`)
}
}
const previousLetter = function(letter) {
if (letter.length === 1) {
return "abcdefghijkmnpqrstuvwxyz"["abcdefghijkmnpqrstuvwxyz".indexOf(letter) - 1];
} else {
console.log(`"${letter}" is not a single letter.`)
}
}
const letterAIsBeforeLetterB = function(letterA, letterB) {
return "abcdefghijkmnpqrstuvwxyz".indexOf(letterA) < "abcdefghijkmnpqrstuvwxyz".indexOf(letterB);
}
//Updates all rules citations in the given string.
const updateString = function(stringToUpdate) {
if (startingRuleToUpdate.length === 3) {
stringToUpdate = stringToUpdate.replace(/\[(\d{3}(\.\d{1,3}([a-z])?)?)\]/g, function(match) {
return match.replace(/(?<=\[)\d{3}/, function(subMatch) {
if (subMatch[0] === startingRuleToUpdate[0] && Number(subMatch) >= Number(startingRuleToUpdate)) {
if (change === "added") {
return Number(subMatch) + 1;
} else {
return Number(subMatch) - 1;
}
} else {
return subMatch;
}
});
});
} else if (/\d/.test(startingRuleToUpdate.slice(-1))) {
stringToUpdate = stringToUpdate.replace(/\[(\d{3}(\.\d{1,3}([a-z])?)?)\]/g, function(match) {
return match.replace(/(?<=\[\d{3}\.)\d{1,3}/, function(subMatch) {
if (match.slice(1, 4) === startingRuleToUpdate.slice(0, 3) && Number(subMatch) >= Number(startingRuleToUpdate.slice(4))) {
if (change === "added") {
return Number(subMatch) + 1;
} else {
return Number(subMatch) - 1;
}
} else {
return subMatch;
}
});
});
} else if (/[a-z]/.test(startingRuleToUpdate.slice(-1))) {
stringToUpdate = stringToUpdate.replace(/\[(\d{3}(\.\d{1,3}([a-z])?)?)\]/g, function(match) {
return match.replace(/(?<=\[\d{3}\.\d{1,3})[a-z]/g, function(subMatch) {
if (match.slice(1, -2) === startingRuleToUpdate.slice(0, -1) && !letterAIsBeforeLetterB(subMatch, startingRuleToUpdate.slice(-1))) {
if (change === "added") {
return nextLetter(subMatch);
} else {
return previousLetter(subMatch);
}
} else {
return subMatch;
}
});
});
} else {
console.log("Invalid startingRuleToUpdate");
return;
}
return stringToUpdate;
}
const fs = require("fs"),
sqlite = require("sqlite3").verbose();
var db = new sqlite.Database("questionDatabase.db", function(err) {
if (err) {
console.log(err);
}
});
let maxId;
db.all(`SELECT MAX("id") FROM questions`, function(err, result) {
if (err) {
console.log(err);
} else {
maxId = (result[0]['MAX("id")'] || 0);
db.all("SELECT json FROM questions", [], function(err, result) {
if (err) {
console.log(err);
} else {
result.forEach(function(currentValue, index){
result[index] = JSON.parse(currentValue.json);
});
for (let i in result) {
const oldQuestion = result[i].question;
const oldAnswer = result[i].answer;
result[i].question = updateString(result[i].question);
result[i].answer = updateString(result[i].answer);
if (oldQuestion !== result[i].question || oldAnswer !== result[i].answer) {
db.run(`UPDATE questions SET json = '${JSON.stringify(result[i]).replace(/'/g,"''")}' WHERE id = ${result[i].id}`, function(err) {
if (err) {
console.log(err);
} else {
console.log(`Updated question ${result[i].id}`);
}
});
}
}
}
});
}
});