Last active
June 11, 2020 11:15
-
-
Save CodHeK/651964da46e211079201955be42714c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const $fakeDiv = document.getElementById('fake-div'); // used to measure space taken by the query content | |
const main = (e) => { | |
const query = e.target.textContent.toLowerCase(); | |
if(query !== '') { | |
const find_start = new Date().getTime(); | |
/* | |
Ex: | |
query = 'what is the most pop' | |
wordToComplete = 'pop' // find suggestions for this | |
rest = 'what is the most ' | |
*/ | |
let parts = query.split(' '); | |
const wordToComplete = parts.pop(); | |
rest = parts.join(' ') + ' '; | |
if(wordToComplete !== '') { | |
// get best match using popularity | |
suggestion = getBestMatch(trie.complete(wordToComplete)); | |
if(suggestion) { | |
$fakeDiv.innerText = query; | |
$span.style.left = r.left + $fakeDiv.clientWidth + 'px'; | |
const ghost = suggestion.slice(wordToComplete.length); | |
trie.clear(); | |
$span.textContent = ghost; // fill in the ghost span | |
const find_end = new Date().getTime(); | |
const execTime = find_end - find_start; | |
$time.textContent = `fetched in ${execTime}ms`; | |
} | |
} | |
else { | |
$span.textContent = ''; // clear ghost span | |
} | |
} | |
else { | |
$time.textContent = ''; | |
$span.textContent = ''; // clear ghost span | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment