Skip to content

Commit 0a20561

Browse files
committed
Improve search generation typing
1 parent e5b6eca commit 0a20561

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

javascript/generateSearchData.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,26 @@ const ignoreTags = new Set([
7474
"p",
7575
"WEB_ONLY"
7676
]);
77-
export const trieTree = {};
77+
78+
type TrieEnd = {
79+
value: string;
80+
pureIndex: [string, string][];
81+
subIndex: { value: string; id: [string, string]; order: string }[];
82+
};
83+
84+
type TrieTree = {
85+
[key: string]: TrieTree | { value: TrieEnd };
86+
};
87+
export const trieTree: TrieTree = {};
7888
export const trieTreeText = {};
7989
export const writeSearchData = () => {
8090
const outputDir = path.join(__dirname, "../json");
81-
8291
const outputFile = path.join(outputDir, "searchData.json");
83-
const searchData = {};
84-
searchData["textbook"] = textBook;
85-
searchData["indexSearch"] = trieTree;
86-
searchData["userSearch"] = trieTreeText;
87-
92+
const searchData = {
93+
textbook: textBook,
94+
indexSearch: trieTree,
95+
userSearch: trieTreeText
96+
};
8897
fs.writeFile(outputFile, JSON.stringify(searchData), err => {
8998
if (err) {
9099
console.error(err);
@@ -174,8 +183,8 @@ function maintainTextTrie(arr) {
174183
}
175184

176185
function maintainIndexTrie() {
177-
function add(obj, trie) {
178-
let current = trie;
186+
function add(obj, trie: TrieTree) {
187+
let current: TrieTree | { value: TrieEnd } = trie;
179188

180189
if (obj["parse"] == null || obj["parse"]["value"] == null) {
181190
if (obj["parse"]["DECLARATION"]) {
@@ -200,14 +209,13 @@ function maintainIndexTrie() {
200209

201210
var index = obj["parse"]["value"];
202211

203-
for (let i = 0; i < index.length; i++) {
204-
let char = index[i];
205-
char = char.toLowerCase();
206-
207-
if (!current.hasOwnProperty(char)) {
208-
current[char] = {};
212+
for (let char of index) {
213+
// FIXME: Once we can type `char` as `string`, fix the type gymnastics
214+
const key = char.toLowerCase();
215+
if (!Object.prototype.hasOwnProperty.call(current, key)) {
216+
current[key as keyof typeof current] = {};
209217
}
210-
current = current[char];
218+
current = (current as any)[key]!;
211219
}
212220
if (!current.value) {
213221
current.value = { value: index, pureIndex: [], subIndex: [] };
@@ -248,24 +256,28 @@ function maintainIndexTrie() {
248256
} else {
249257
toAdd["order"] = obj["parse"]["SUBINDEX"]["value"];
250258
}
259+
// TODO: Use type guard instead
260+
const _current = current as { value: TrieEnd };
251261
if (
252-
!current.value.subIndex.find(
262+
!_current.value.subIndex.find(
253263
obj =>
254264
obj.value === toAdd.value &&
255265
obj.id[0] === toAdd.id[0] &&
256266
obj.id[1] === toAdd.id[1]
257267
)
258268
) {
259-
current.value.subIndex.push(toAdd);
260-
for (let i = 0; i < current.value.subIndex.length; i++) {
269+
_current.value.subIndex.push(toAdd);
270+
for (let i = 0; i < _current.value.subIndex.length; i++) {
261271
//console.log(current.value.subIndex[i]["order"].toString.localeCompare("gh"));
262-
current.value.subIndex.sort((a, b) =>
272+
_current.value.subIndex.sort((a, b) =>
263273
a["order"].localeCompare(b.order)
264274
);
265275
}
266276
}
267277
} else {
268-
current.value.pureIndex.push(obj["parentId"]);
278+
// TODO: Use type guard instead
279+
const _current = current as { value: TrieEnd };
280+
_current.value.pureIndex.push(obj["parentId"]);
269281
}
270282
}
271283
let len = index.length;

0 commit comments

Comments
 (0)