Skip to content

Extending the App

The Course
•
25 min study

Choose a feature with a visible contract

Add a saved-title filter to the app. It should change only the displayed Saved books list, preserve saved records, match without case sensitivity, and show all saved records for a blank filter. Changing the filter must not start a catalogue search. This is a small feature, but it crosses a model/UI boundary.

First write a pure selector: given saved books and filter text, return the matching array. A selector derives data for display without changing state. Test matching, a nonmatch, blank input, and unchanged input. Then add an input with a label in the HTML and register one input listener in app.js. The filter text is UI state: it lives in the input itself, and render reads it each time, so changing it only has to call render(controller.getState()).

Do not clear saved just because a filter hides every row. No visible match means zero displayed records, not lost data. Likewise, keep the saved-count summary clear: does it describe all saved books or just visible books? Name the count accordingly.

Only after the first feature works should you consider storage. Inject a storage adapter, validate reads, and make failed writes visible, using Day 6's contract. Neither a framework nor a class is required for these features. Modules, pure functions, and a controller give the code enough boundaries to change it safely.

A useful review asks: does the new behavior live in the smallest responsible layer, can its rule be tested without the browser, and did the keyboard journey remain usable? Resist rewriting the whole app while adding one filter.

Predict and trace

Write the output before running. Follow the values through each call; if your prediction differs, identify the first step that surprised you.

Predict, then run JavaScript
function visibleSaved(saved, text) {
  const query = text.trim().toLowerCase();

  return saved.filter(book => book.title.toLowerCase().includes(query));
}

const saved = [
  { id: "js", title: "JavaScript Notes" },
  { id: "web", title: "The Web" }
];

console.log(visibleSaved(saved, " WEB ")[0].id);
console.log(visibleSaved(saved, "").length);
console.log(saved.length);
Check your prediction
Text
web
2
2

Every string includes the empty string, so a blank query preserves all saved records without a special mutation.

Plan a feature across its boundaries

Write one concrete user action and its expected next state before adding a control. Identify which layer owns validation, state transition, dependency calls and rendering. A new happy path is incomplete if it loses saved data, accepts stale work or keeps effects alive after disposal. Use two unequal records so accidental changes outside the target are visible.

Boundary or caseWhat to inspect
Public actionName its input and exactly which fields may change
ModelPreserve unrelated fields and specify missing-id behavior
Controller and UIObserve call count, loading ownership and recovery

Implement one small vertical slice and add a distinguishing test before generalizing. Re-run old failure and lifecycle tests. A new green assertion is insufficient if the existing guarantees changed.

Test the selector before wiring it

A first attempt at the selector often uses includes directly. Predict both counts for each filter text.

First try versus the selector JavaScript
function visibleSavedFirstTry(saved, text) {
  return saved.filter(book => book.title.includes(text));
}

function visibleSaved(saved, text) {
  const query = text.trim().toLowerCase();

  return saved.filter(book => book.title.toLowerCase().includes(query));
}

const saved = [
  { id: "web", title: "The Small Web" },
  { id: "tests", title: "Testing Useful Programs" }
];

for (const text of ["Web", " web", "WEB"]) {
  console.log(`"${text}": ${visibleSavedFirstTry(saved, text).length} vs ${visibleSaved(saved, text).length}`);
}
Check your prediction
Text
"Web": 1 vs 1
" web": 0 vs 1
"WEB": 0 vs 1

includes is case-sensitive and counts spaces, so the first try only works when the learner types the title's exact spelling. Normalising the query and the title the same way, trimmed and lowercased, makes all three agree. Settling this in the console first means that, in the lab, any remaining problem is in the wiring, not the rule.

Build it yourself

Exercise

Add a saved-title filter

Build the feature in the lab below; no download is needed. Add a labelled text input, Filter saved books, under the Saved books heading. As you type, the Saved books list shows only titles containing the text, ignoring case and surrounding spaces. Check these acceptance cases with two saved books (search web and save, then search testing and save):

CaseExpected
Blank filterBoth saved books
WEBOnly The Small Web
zzz (no match)An empty list, while the status line still says 2 saved
Filter web, then search JavaScriptThe filter text stays and the saved list stays filtered
Typing in the filterNo new search starts (the status does not change to Searching…)

Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.

Hint 1: the idea

Three pieces, each in its own place: the input in the HTML; a pure visibleSaved(books, text) selector like the one traced above; and render looping over visibleSaved(state.saved, filter.value) instead of state.saved. The filter's value is never copied into the controller's state, so a search cannot reset it.

Hint 2: a first step

Add <label for="filter">Filter saved books</label> and <input id="filter" /> after the Saved books heading, find the input next to const saved = ..., and paste visibleSaved above render. Change the saved loop to use it and run: typing does nothing yet, but saving a book after typing a filter shows the filtered list. What is missing is a listener that re-renders on each keystroke.

Show one solution

Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.

The input's input listener calls render(controller.getState()) and nothing else: it re-projects the current state, so no search starts and no saved record changes. Because render reads filter.value every time, the filter also applies after a search, a save or a cancel without extra code, which is the "filter kept across a search" case. An empty query matches every title, since every string includes "", so the blank case needs no special branch. The status line counts state.saved, all saved books, which is why "2 saved" stays true while the list shows none. In the downloaded project, visibleSaved would move to model.js with tests beside toggleSaved, and the listener would join the others in app.js with { signal: requests.signal } so it is removed on dispose, as it is here.

Explain before moving on

Why is filtering different from removing? Which tests can run without a DOM?

Was this article helpful?

Your response is saved on this device.