From e28e54d7ec641ed6e1e8c0329fd10ecae8e863df Mon Sep 17 00:00:00 2001 From: Chi Kim Date: Sun, 6 Dec 2020 15:36:25 -0800 Subject: [PATCH] fixes an issue in search where data results is parsed incorrectly --- .gitignore | 2 + src/search.js | 116 ++++++++++++++++++++++++++------------------------ 2 files changed, 62 insertions(+), 56 deletions(-) diff --git a/.gitignore b/.gitignore index f6ad6b3..bf148f8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ data public/assets logs public/main.js +node_modules +.git diff --git a/src/search.js b/src/search.js index 65ec7cd..8bce5ba 100644 --- a/src/search.js +++ b/src/search.js @@ -1,48 +1,54 @@ let resultContainer; let searchBox; let allNotes; -let resultElements = [] -let noteMap = {} +let resultElements = []; +let noteMap = {}; const search = () => { - const searchQuery = searchBox.value + const searchQuery = searchBox.value; if (searchQuery.length < 2) { - return resultContainer.innerHTML = '' + return (resultContainer.innerHTML = ""); } - fetch(`/search/${searchQuery}`).then( r=> r.text() ).then(data => { - // invalidate old requests - if( searchBox.value !== searchQuery) return - const dataArr = data.split('\n').filter( d => d ).map( d => { - const result = d.substr(5) // get rid of data/ prefix - const resultArr = result.split(':') - const title = resultArr.shift() - const match = resultArr.join(':') - return { - title, match - } - }) - resultContainer.innerHTML = '' - const newTitleMap = {...noteMap} - resultElements = dataArr.map( (obj, idx) => { - delete newTitleMap[obj.title.toLowerCase()] - return new Result(obj.title, obj.match, idx) - }) - Object.keys(newTitleMap).forEach( title => { - if (!title.includes(searchQuery.toLowerCase())) { - return - } - resultElements.push( - new Result(newTitleMap[title], '', resultElements.length) - ) - }) - }) -} + fetch(`/search/${searchQuery}`) + .then((r) => r.text()) + .then((data) => { + // invalidate old requests + if (searchBox.value !== searchQuery) return; + const dataArr = data + .split("\n") + .filter((d) => d) + .map((d) => { + const result = d.replace("./data/", ""); // get rid of ./data/ prefix + const resultArr = result.split(":"); + const title = resultArr.shift(); + const match = resultArr.join(":"); + return { + title, + match, + }; + }); + resultContainer.innerHTML = ""; + const newTitleMap = { ...noteMap }; + resultElements = dataArr.map((obj, idx) => { + delete newTitleMap[obj.title.toLowerCase()]; + return new Result(obj.title, obj.match, idx); + }); + Object.keys(newTitleMap).forEach((title) => { + if (!title.includes(searchQuery.toLowerCase())) { + return; + } + resultElements.push( + new Result(newTitleMap[title], "", resultElements.length) + ); + }); + }); +}; function Result(name, value, idx) { - const div = document.createElement('div') - div.className = 'resultContainer' + const div = document.createElement("div"); + div.className = "resultContainer"; if (!idx) { - div.className += ' selected' + div.className += " selected"; } div.innerHTML = `

@@ -51,31 +57,29 @@ function Result(name, value, idx) { ${value}

- ` - div.addEventListener('click', () => { - this.select() - }) + `; + div.addEventListener("click", () => { + this.select(); + }); - resultContainer.append(div) + resultContainer.append(div); this.select = () => { - window.location = `/notes/${name}` - } + window.location = `/notes/${name}`; + }; } const initSearch = (titleMap) => { - resultContainer = document.querySelector('#resultListContainer') - searchBox = document.querySelector('#searchBox') - allNotes = document.querySelectorAll('.card-columns .card-title') - searchBox.focus() - noteMap = titleMap - searchBox.addEventListener('keyup', (e) => { - console.log(e.key) - if (e.key === 'Enter') { - return resultElements[0].select() + resultContainer = document.querySelector("#resultListContainer"); + searchBox = document.querySelector("#searchBox"); + allNotes = document.querySelectorAll(".card-columns .card-title"); + searchBox.focus(); + noteMap = titleMap; + searchBox.addEventListener("keyup", (e) => { + if (e.key === "Enter") { + return resultElements[0].select(); } - search() - }) - -} + search(); + }); +}; -module.exports = initSearch +module.exports = initSearch;