~puer-robustus/whatarewehaving.today

[PATCH] add filtering by multiple search terms

Mateusz HaƂada <haladamateusz@gmail.com>
Details
Message ID
<CAFMXecvXAA_1sZtDEM5498nuR7szBqoyOwqmtTTKoJ9n_VoXSQ@mail.gmail.com>
DKIM signature
missing
Download raw message
---
 fathub/templates/filtering.js | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/fathub/templates/filtering.js b/fathub/templates/filtering.js
index c472e60..23a34e6 100644
--- a/fathub/templates/filtering.js
+++ b/fathub/templates/filtering.js
@@ -1,16 +1,24 @@
 document.addEventListener("DOMContentLoaded", function () {
     const recipes = document.querySelectorAll('tr[data-keywords]');
-    const recipesWithoutKeyword = (filter) => {
+    const hideRecipesWithoutKeywords = (userInput) => {
    recipes.forEach((recipe) => {
-       if (!filter || recipe.dataset.keywords.includes(filter.toLowerCase())) {
+       if (!userInput || filterRecipes(userInput, recipe.dataset.keywords)) {
       recipe.removeAttribute('style');
        } else {
       recipe.style ='display:none;';
-       };
+       }
    });
     };
     const input = document.querySelector('input[type=search]');
+
     input.addEventListener("keyup", (event) => {
-   setTimeout(() => recipesWithoutKeyword(event.target.value), 500);
+   setTimeout(() => hideRecipesWithoutKeywords(event.target.value), 500);
     });
 })
+
+function filterRecipes(userInput, recipeKeywords) {
+   const parsedRecipeKeywords = JSON.parse(recipeKeywords.replace(/'/g, '"'));
+
+   const parsedUserInput = [...userInput.trim().split(' ')];
+   return parsedUserInput.map(input =>
input.toLowerCase()).every(input => parsedRecipeKeywords.some(keyword
=> keyword.includes(input)));
+}
\ No newline at end of file
-- 
2.39.3 (Apple Git-145)
Reply to thread Export thread (mbox)