Created
November 24, 2018 10:02
-
-
Save andrei-cacio/87465d127e83844b47997bebdf0f8d9e to your computer and use it in GitHub Desktop.
wasm2
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
import * as fuzzySearch from "fuzzy-search"; | |
import books from './books-db'; | |
import lorem from './lorem'; | |
const loremOpts = { | |
isHardcore: false | |
}; | |
renderLorem(); | |
renderBooks(books); | |
async function renderLorem() { | |
let text = await lorem.getLorem(loremOpts); | |
document.getElementById('text').innerHTML = text; | |
document.getElementById('searchField').disabled = false; | |
} | |
async function handleSearch(e) { | |
const content = await lorem.getLorem(loremOpts); | |
const query = e.target.value; | |
const fuzzyed = fuzzySearch.fuzzy(query, content); | |
const filteredBooks = fuzzySearch.filter_books({ col: books }, query); | |
renderBooks(filteredBooks.col); | |
document.getElementById('text').innerHTML = fuzzyed; | |
} | |
function renderBooks(books) { | |
const booksContainer = document.getElementById('books'); | |
booksContainer.innerHTML = ''; | |
let booksHTML = ''; | |
books.forEach(book => { | |
booksHTML += `<div class="book">${book.author} - ${book.title}</div>`; | |
}); | |
booksContainer.innerHTML = booksHTML; | |
} | |
document.getElementById('searchField').addEventListener('input', handleSearch); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment