Bladeren bron

Add bang-random to pokemon search

Kirk Trombley 3 jaren geleden
bovenliggende
commit
b282c4ee7d
1 gewijzigde bestanden met toevoegingen van 9 en 4 verwijderingen
  1. 9 4
      nearest.js

+ 9 - 4
nearest.js

@@ -294,12 +294,17 @@ const onUpdate = (event) => {
 
   // Lookup by name
   if (lastPkmnSearch !== pokemonName || newParams) {
+    let found;
+    if (pokemonName.trim().toLowerCase() === "!random") {
+      found = Array.from({ length: 10 }, () => database[Math.floor(Math.random() * database.length)]);
+    } else {
+      found = pokemonLookup.search(pokemonName, { limit: 10 }).map(({ item }) => item);
+    }
+
     const searchList = document.getElementById("search-list");
     searchList.innerHTML = '';
-    pokemonLookup
-      .search(pokemonName, { limit: 10 })
-      // If scoring is impossible, totalScorer will just be identity
-      .map(({ item }) => ({ ...item, ...calculator(item) }))
+    // If scoring is impossible, calculator will just return {}
+    found.map((info) => ({ ...info, ...calculator(info) }))
       .forEach(item => {
         const li = document.createElement("li");
         li.appendChild(renderPokemon(item))