-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
65 lines (60 loc) · 1.69 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
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
var wiki = readTextFile("wiktionary.txt"),
cities = readTextFile("cities.txt");
document.getElementById("field").onkeyup = ()=>{
document.getElementById("dropdown").innerHTML = "";
var terms = search(document.getElementById("field").value, document.getElementById("wik").checked ? wiki : cities);
for (i=0;i<terms.length;i++){
var li = document.createElement("li");
li.innerHTML = terms[i];
document.getElementById("dropdown").appendChild(li);
}
};
function readTextFile(file){
var rawFile = new XMLHttpRequest(),
assignto;
rawFile.open("GET", file, false);
rawFile.onreadystatechange = ()=>{
if(rawFile.readyState === 4){
if(rawFile.status === 200 || rawFile.status === 0){
var allText = rawFile.responseText,
stuff = parseTheFile(allText);
assignto = stuff;
}
}
};
rawFile.send(null);
return assignto;
}
function parseTheFile(content){
var lines = content.split('\n'),
words = {};
for (i=0;i<lines.length;i++){
words[lines[i].split("\t")[1]] = lines[i].split("\t")[0];
}
return words;
}
function search(term, list){
var items = Object.keys(list),
words = {},
final = [];
for (i=0;i<items.length;i++){
var w = items[i].split("").splice(0,term.length).join("");
if (w === term){
words[list[items[i]]] = items[i];
}
}
var sorted = Object.keys(words).sort(),
ordered = {};
for (i=0;i<sorted.length;i++){
ordered[sorted[i]] = words[sorted[i]];
}
for (i=0;i<7;i++){
if (Object.values(ordered)[i] === undefined) {
break;
}
final.push(Object.values(ordered)[i]);
}
console.log('final', final)
console.log('words', words)
return final.reverse();
}