-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
119 lines (97 loc) · 3.22 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
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
var tossupArr = [];
$.getJSON("https://mrbossosity.github.io/college-bowl-collections/clean-acf-regionals-2006-2019.json", function(data) {
tossupArr = [...data, ...tossupArr];
});
$.getJSON("https://mrbossosity.github.io/college-bowl-collections/clean-acf-fall-2008-2019.json", function(data) {
tossupArr = [...tossupArr, ...data]
});
$.getJSON("https://mrbossosity.github.io/college-bowl-collections/clean-eft-2016-2019-and-terrapin-2011-2020.json", function(data) {
tossupArr = [...tossupArr, ...data]
});
$.getJSON("https://mrbossosity.github.io/college-bowl-collections/clean-penn-bowl-2010-2018.json", function(data) {
tossupArr = [...tossupArr, ...data]
});
function searchTossups(regex) {
var qResults = [];
var aResults = [];
for (tossup of tossupArr) {
let question = tossup.formatted_text;
let answer = tossup.formatted_answer;
if (regex.test(question)) {
qResults.push(tossup)
}
if (regex.test(answer)) {
aResults.push(tossup)
}
}
sortTournamentABC(qResults);
sortTournamentABC(aResults);
return [qResults, aResults]
}
function sortTournamentABC(arr) {
arr.sort(function(a, b){
var x = a.tournament;
var y = b.tournament;
if (x < y) {return 1;}
if (x > y) {return -1;}
return 0;
});
}
function resultElement(result, regex) {
let tournament = result.tournament,
round = result.round,
question,
answer;
try {
question = decodeURIComponent(escape(result.formatted_text));
} catch {
question = result.formatted_text;
}
try {
answer = decodeURIComponent(escape(result.formatted_answer));
} catch {
answer = result.formatted_answer;
}
highlightedQuestion = highlightMatches(question, regex);
highlightedAnswer = highlightMatches(answer, regex);
let element = `<div class="search-result"><div class="search-result-tournament"><strong>${tournament}</strong> || ${round}</div><div class="search-result-question">${highlightedQuestion}</div><div class="search-result-answer">ANSWER: ${highlightedAnswer}</div></div>`;
return element
}
function highlightMatches(str, regex) {
return str.replace(regex, (match) => {
return `<span class="highlighted">${match}</span>`
})
}
function resetResults() {
$("#question-results, #answer-results").html("");
if ($(".results-container").is(":hidden")) {
$(".results-container").show()
};
$("#search").blur();
window.location.hash = ""
}
function run() {
let search = $("#search").val();
if (search.length < 3) {
alert("You probably don't want to query that...\nLike, do you expect me to display every question in the DB?");
return
}
resetResults();
let regex = new RegExp(`${search}`, 'ig');
let results = searchTossups(regex),
qResults = results[0],
aResults = results[1];
$("#question-results-length").text(`${qResults.length} tossups found`);
$("#answer-results-length").text(`${aResults.length} tossups found`)
qResults.forEach(result => {
let element = resultElement(result, regex);
$("#question-results").append(element)
});
aResults.forEach(result => {
let element = resultElement(result, regex);
$("#answer-results").append(element)
})
}
$("#query").on("submit", () => {
run()
})