From de7c73e5d09e51bed3664cc103824fc49aaab874 Mon Sep 17 00:00:00 2001 From: 3eef8a28f26fb2bcc514e6f1938929a1f931762 <116031952+3eef8a28f26fb2bcc514e6f1938929a1f931762@users.noreply.github.com> Date: Fri, 10 Feb 2023 21:10:08 -0500 Subject: [PATCH] Continue working on search --- js/search_helper.js | 47 +++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/js/search_helper.js b/js/search_helper.js index e26ee3e3d..5f1f642cd 100644 --- a/js/search_helper.js +++ b/js/search_helper.js @@ -1,16 +1,35 @@ -const search_helper = function() { - const input = document.getElementById("search").value; +"use strict"; +const search_helper = function(event) { + event.preventDefault(); + // "a b cde 12" => "a b cde 12" + const input = document.getElementById("search").value.split(" ").join(" "); console.log(input); -/* - const courseCodeInput = elem.value.replace(" ", "").replace("-", ""); - const subCode = courseCodeInput.substring(0,4).toUpperCase(); - const courseNum = courseCodeInput.substring(courseCodeInput.length-4,courseCodeInput.length); - if(`${subCode}-${courseNum}` in catalog){ - // course exists - window.location.href = "./coursedisplay.html?course="+subCode+"-"+courseNum; - } else { - // course doesn't exist - window.location.href = "./search.html?search="+encodeURIComponent(elem.value); - showSearchResults(); - }*/ + + var course_code = false; + if(input.length == 8) { + // "abcd1345" => ["abcd","1345"] + course_code = input.match(/.{1,3}/g) + } else if(input.length == 9) { + // "abcd - 1345" => ["abcd","1345"] + course_code = input.split(/(?:,| )+/); + } + // only do this logic if the string might be a course code + if(course_code) { + // ["abcd","1345"] => "ABCD-1345" + course_code[0] = course_code[0].toUpperCase(); + const code_str = course_code.join("-"); + + // check if "ABCD-1345" is a real course code + const course_exists = fetch("../courses_list.json") + .then(list => list.json()) + .then(list => code_str in list); + + // if it is, redirect to it + if(course_exists) { + location.href = code_str; + return; + } + } + + location.href = "../search?search=" + encodeURIComponent(input); }