Skip to content

Instantly share code, notes, and snippets.

@andrei-cacio
Created November 24, 2018 10:02
Show Gist options
  • Save andrei-cacio/87465d127e83844b47997bebdf0f8d9e to your computer and use it in GitHub Desktop.
Save andrei-cacio/87465d127e83844b47997bebdf0f8d9e to your computer and use it in GitHub Desktop.
wasm2
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