Switched to an array of URLs instead of lengthy switch statement. Await format simplified down
This commit is contained in:
60
docs/main.js
60
docs/main.js
@@ -48,53 +48,33 @@ async function parseName() {
|
|||||||
* Displays an alert if nothing could be found.
|
* Displays an alert if nothing could be found.
|
||||||
*/
|
*/
|
||||||
async function PapaParse(department, num, name, sem) {
|
async function PapaParse(department, num, name, sem) {
|
||||||
let cData = '';
|
const semesterURLs = {
|
||||||
let url = '';
|
'f2022': 'https://derec4.github.io/ut-grade-data/2022%20Fall.json',
|
||||||
switch (sem) {
|
's2022': 'https://derec4.github.io/ut-grade-data/2022%20Summer.json',
|
||||||
case 'f2022':
|
'sp2022': 'https://derec4.github.io/ut-grade-data/2022%20Spring.json',
|
||||||
url = 'https://derec4.github.io/ut-grade-data/2022%20Fall.json';
|
'f2021': 'https://derec4.github.io/ut-grade-data/2021%20Fall.json',
|
||||||
break;
|
's2021': 'https://derec4.github.io/ut-grade-data/2021%20Summer.json',
|
||||||
case 's2022':
|
'sp2021': 'https://derec4.github.io/ut-grade-data/2021%20Spring.json',
|
||||||
url = 'https://derec4.github.io/ut-grade-data/2022%20Summer.json';
|
'f2020': 'https://derec4.github.io/ut-grade-data/2020%20Fall.json',
|
||||||
break;
|
's2020': 'https://derec4.github.io/ut-grade-data/2020%20Summer.json',
|
||||||
case 'sp2022':
|
'sp2020': 'https://derec4.github.io/ut-grade-data/2020%20Spring.json',
|
||||||
url = 'https://derec4.github.io/ut-grade-data/2022%20Spring.json';
|
};
|
||||||
break;
|
|
||||||
case 'f2021':
|
//Update to an array of URL options instead of a lengthy switch statement for readability
|
||||||
url = 'https://derec4.github.io/ut-grade-data/2021%20Fall.json';
|
const url = semesterURLs[sem] || 'https://derec4.github.io/ut-grade-data/2022%20Fall.json';
|
||||||
break;
|
const response = await fetch(url);
|
||||||
case 's2021':
|
const cData = await response.json();
|
||||||
url = 'https://derec4.github.io/ut-grade-data/2021%20Summer.json';
|
|
||||||
break;
|
|
||||||
case 'sp2021':
|
|
||||||
url = 'https://derec4.github.io/ut-grade-data/2021%20Spring.json';
|
|
||||||
break;
|
|
||||||
case 'f2020':
|
|
||||||
url = 'https://derec4.github.io/ut-grade-data/2020%20Fall.json';
|
|
||||||
break;
|
|
||||||
case 's2020':
|
|
||||||
url = 'https://derec4.github.io/ut-grade-data/2020%20Summer.json';
|
|
||||||
break;
|
|
||||||
case 'sp2020':
|
|
||||||
url = 'https://derec4.github.io/ut-grade-data/2020%20Spring.json';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
url = 'https://derec4.github.io/ut-grade-data/2022%20Fall.json';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
await fetch(url)
|
|
||||||
.then(res => res.json())
|
|
||||||
.then(data => {
|
|
||||||
cData = data;
|
|
||||||
});
|
|
||||||
let selectedClass = '';
|
let selectedClass = '';
|
||||||
if (sem.substring(0, 2) === 's2') {
|
if (sem.substring(0, 2) === 's2') {
|
||||||
|
/**
|
||||||
|
* Summer names are really weird but we can safely assume the prefix of the semester
|
||||||
|
* will not become "s3" within the next lifetime
|
||||||
|
*/
|
||||||
console.log("Summer Semester Detected");
|
console.log("Summer Semester Detected");
|
||||||
selectedClass = cData.filter(cData => cData["Course Prefix"] == department)
|
selectedClass = cData.filter(cData => cData["Course Prefix"] == department)
|
||||||
.filter(cData => cData["Course Number"].includes(num.toString().toUpperCase()))
|
.filter(cData => cData["Course Number"].includes(num.toString().toUpperCase()))
|
||||||
.filter(cData => cData["Course Title"].includes(name));
|
.filter(cData => cData["Course Title"].includes(name));
|
||||||
if (selectedClass.length == 0) {
|
if (selectedClass.length == 0) {
|
||||||
// summer names are weird
|
|
||||||
console.log("Invalid name; trying again with just the course number");
|
console.log("Invalid name; trying again with just the course number");
|
||||||
selectedClass = cData.filter(cData => cData["Course Prefix"] == department)
|
selectedClass = cData.filter(cData => cData["Course Prefix"] == department)
|
||||||
.filter(cData => cData["Course Number"].includes(num.toString().toUpperCase()))
|
.filter(cData => cData["Course Number"].includes(num.toString().toUpperCase()))
|
||||||
|
|||||||
Reference in New Issue
Block a user