Chapter 15 · Beyond the DOM
Searching lists
People make typos when they search: "kolkta", "rahul kumr", "mumbay". In this chapter you'll build search boxes that still find the right thing, for lists of words, lists of records, and lists already on the page, and suggest "Did you mean...?" when nothing matches.
How Nyx search works
A plain search only finds exact text, so one wrong letter finds nothing. Nyx's search is more forgiving, and follows a few simple rules:
"KOLKATA" and "bengaluru" find "Kolkata" and "Bengalūru"."kol" finds "Kolkata" and "Kolar"."kolkta" finds "Kolkata". Longer words may have more mistakes: about one for every four letters."de", aren't corrected, or they'd match almost everything."rahul kol" finds Rahul from Kolkata, not every Rahul.Searching a list of words
Give nyx.search() a list and the text to look for. It gives back a new list of the matches, best first:
nyx.search(search
cities,this list
"kolkta"for this
)Done
Type a city, with or without mistakes:
Empty the box and the whole list comes back: searching for nothing matches everything. Try "pne", "hydrabad" and "del".
Searching records
Real data is usually a list of records, like students with a name, a city and marks. Add a list of the fields to search as the third value:
nyx.search(students, "rahul kumr", ["name", "city"]);Try "rahul" (both Rahuls), "rahul delhi" (just one), and "dehli" (a typo in a city).
["name"]Only the name.["name", "city"]The name and the city together.["address.city"]A field inside another field, with a dot: { address: { city: "Pune" } }.Spell a field wrong, like ["nmae"], and Nyx tells you which fields the records really have.
Filtering a list on the page
Sometimes the list is already on the page, written in HTML. Use .search() on the elements: it gives back the ones whose text matches, in their page order. Hide everything, then show the matches:
- When does the library open?
- How do I join the science club?
- Where is the lost and found?
- When are the half-yearly exams?
- How do I get a new ID card?
nyx.search(list, text)
nyx.search(cities, "kol")Searches data in JavaScript. Gives back data, best match first.
elements.search(text)
nyx("#faq li").search("kol")Searches elements on the page by their text. Gives back elements, in page order.
Counts and "no results"
A search that finds nothing should say so, rather than showing an empty space. .length tells you how many matches there are, and nyx.plural() from Formatting words the count:
found.length === 0Nothing matched, so show a message and stop.returnleaves the function early.found.slice(0, 5)Show at most the five best matches..slice(0, 5)takes the first five items of a list.
Empty the box to see all eight books counted, but only five shown.
"Did you mean...?": nyx.closest
nyx.closest() picks the one choice closest to what was typed, or gives null if nothing is close. It's how Nyx itself suggests fixes for your typos. Here it offers a button that fixes the word:
nyx.search(list, text)Every match, best first. For search boxes.nyx.closest(text, list)The single closest choice, or null. For "Did you mean...?".Very big lists
Searching in the browser is fast for lists of a few thousand items, which covers most school and shop websites. For bigger data, like every book in a city library, let the server search, and ask it as the person types. You did exactly that in Servers and AJAX, with nyx.debounce() and { latest: "search" }.
Putting it together
A "Find a classmate" box: it searches names and cities, counts the matches, and suggests a name when nothing matches:
classmates.map((c) => c.name)A list of just the names, fornyx.closest()..map()makes a new list by changing each item.nyx.search(classmates, q, ["name", "city"])Search both fields, so "kolkata" finds everyone from Kolkata.nyx.closest(q, names)When the search finds no one, look for the closest whole name, and mention it if there is one.
Try "kolkata", "kabeer" and "ishita salt". Then "snea ry": too many typos for the search, but close enough for a suggestion. And "xyz", which is close to nothing at all.
Common mistakes
["name", "city"].["name"], not "name".nyx("#list li").search(text) for elements, and nyx.search() for data.found.length === 0 and show a message.Exercise
Fill in the blanks, then press Check answers.
1. Search the list fruits for "banan".
const found = nyx.(fruits, "banan");
2. Search the students' names and cities.
nyx.search(students, q, ["name", ""]);
3. Search a field inside address.
nyx.search(people, q, ["address."]); // { address: { city: "Pune" } }
4. Show only the list items on the page that match.
items.hide(); items.(box.value()).show();
5. Did the search find nothing?
if (found. === 0) { ... }
6. Keep only the first five matches.
found.(0, 5);
7. Find the one city closest to "mumbay".
const guess = nyx.("mumbay", cities);
Remember
nyx.search(list, text) gives back the matching data, best first. Empty text gives back everything.["name", "city"].nyx("#list li").search(text) finds matching elements on the page, in page order.found.length === 0, and use .slice(0, 5) to show only the best few.nyx.closest(text, list) gives the closest choice for "Did you mean...?", or null.