Skip to content

Checkpoint: A Searchable Library

The Course
•
24 min study

Close the four earlier lessons and work from this brief. The workspace has an executable case table but an unfinished run function. Complete the rule before looking at the solution. Twenty-four minutes is a suggested first pass; split the work across sessions if you need more time.

The brief

Filter and paginate public records without mutating the source.

  • Search: keep a row when its title contains the query, ignoring case ("REACT" matches "react hooks"). Trim the query; a repeated query uses its first value; no query matches every row.
  • Pages: two matching rows per page. A page that isn't a whole number becomes 1, and a page past the end becomes the last page, as in the pagination lesson.
  • Output: only id and title for each row, plus the page actually shown.

Keep the function deterministic and leave its inputs unchanged. Explain how this rule connects to a Next.js page, action or route handler; the table is a contract harness, not a substitute for testing that transport.

Acceptance cases

  • Input {"rows": [{"id": 1, "title": "React", "private": "x"}, {"id": 2, "title": "Forms"}, {"id": 3, "title": "React tests"}], "query": [" react ", "forms"], "page": "9"} → {"page": 1, "items": [{"id": 1, "title": "React"}, {"id": 3, "title": "React tests"}]}.
  • Input {"rows": [{"id": 1, "title": "React"}, {"id": 2, "title": "react hooks"}, {"id": 3, "title": "Forms"}, {"id": 4, "title": "React tests"}], "query": "REACT", "page": "2"} → {"page": 2, "items": [{"id": 4, "title": "React tests"}]}.

Add one case of your own to examples: the least convenient input you can think of, such as an empty list, a missing field or a repeated value. Work out its expected result before you run it. The table's Input kept column must say yes on every row: the rule may not change what it was given.

Checkpoint workspace Next.js · 5 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <strong>Study Library · Practice lab</strong> </header> {children} </body> </html> ); } import Cases from "./Cases.jsx"; export default function Page() { return ( <main> <h1>A Searchable Library</h1> <p> Read the input and predict the result before running. Change lib/contract.js for the exercise. </p> <Cases /> </main> ); } import { run, examples } from "@/lib/contract.js"; const text = (value) => JSON.stringify(value) ?? "undefined"; export default async function Cases() { const results = await Promise.all( examples.map(async ([input, expected]) => { // Record the input before running, to show whether run changed it. const before = text(input); const actual = await run(input); return { input: before, expected, actual, kept: text(input) === before, pass: text(actual) === text(expected), }; }), ); return ( <section> <h2>Executable cases</h2> <p> Checks: {results.filter((row) => row.pass).length}/{results.length}{" "} passed </p> <table> <thead> <tr> <th>Input</th> <th>Expected</th> <th>Actual</th> <th>Input kept</th> </tr> </thead> <tbody> {results.map((row, i) => ( <tr key={i}> <td> <code>{row.input}</code> </td> <td> <code>{text(row.expected)}</code> </td> <td className={row.pass ? "pass" : "fail"}> <code>{text(row.actual)}</code> </td> <td className={row.kept ? "pass" : "fail"}> {row.kept ? "yes" : "no: run changed it"} </td> </tr> ))} </tbody> </table> </section> ); } body { font: 16px/1.5 system-ui; margin: 0; color: #182b3c; background: #f4f8fc; } header, main { max-width: 900px; margin: auto; padding: 20px; } header { border-bottom: 1px solid #cbd5e1; } a { color: #075985; } button, input, textarea, select { font: inherit; padding: 8px; margin: 4px; } button { cursor: pointer; } table { border-collapse: collapse; width: 100%; } th, td { padding: 10px; border: 1px solid #cbd5e1; text-align: left; vertical-align: top; } pre, code { white-space: pre-wrap; overflow-wrap: anywhere; } .pass { color: #166534; } .fail, [role="alert"] { color: #a01818; } section { margin: 16px 0; padding: 12px; background: white; border-radius: 8px; } nav { display: flex; gap: 16px; } export function run(input) { // Implement the checkpoint contract from the brief. return null; } export const examples = [ [ { rows: [ { id: 1, title: "React", private: "x" }, { id: 2, title: "Forms" }, { id: 3, title: "React tests" }, ], query: [" react ", "forms"], page: "9", }, { page: 1, items: [ { id: 1, title: "React" }, { id: 3, title: "React tests" }, ], }, ], [ { rows: [ { id: 1, title: "React" }, { id: 2, title: "react hooks" }, { id: 3, title: "Forms" }, { id: 4, title: "React tests" }, ], query: "REACT", page: "2", }, { page: 2, items: [{ id: 4, title: "React tests" }] }, ], ];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.
Hint 1: the idea

Separate validation, calculation and output shaping. Decide the failure precedence before writing the happy path.

Hint 2: a first step

Normalize the URL inputs first, calculate the matching list, then derive the bounded page and public output.

Show one solution
One checkpoint solution Next.js · 5 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <strong>Study Library · Practice lab</strong> </header> {children} </body> </html> ); } import Cases from "./Cases.jsx"; export default function Page() { return ( <main> <h1>A Searchable Library</h1> <p> Read the input and predict the result before running. Change lib/contract.js for the exercise. </p> <Cases /> </main> ); } import { run, examples } from "@/lib/contract.js"; const text = (value) => JSON.stringify(value) ?? "undefined"; export default async function Cases() { const results = await Promise.all( examples.map(async ([input, expected]) => { // Record the input before running, to show whether run changed it. const before = text(input); const actual = await run(input); return { input: before, expected, actual, kept: text(input) === before, pass: text(actual) === text(expected), }; }), ); return ( <section> <h2>Executable cases</h2> <p> Checks: {results.filter((row) => row.pass).length}/{results.length}{" "} passed </p> <table> <thead> <tr> <th>Input</th> <th>Expected</th> <th>Actual</th> <th>Input kept</th> </tr> </thead> <tbody> {results.map((row, i) => ( <tr key={i}> <td> <code>{row.input}</code> </td> <td> <code>{text(row.expected)}</code> </td> <td className={row.pass ? "pass" : "fail"}> <code>{text(row.actual)}</code> </td> <td className={row.kept ? "pass" : "fail"}> {row.kept ? "yes" : "no: run changed it"} </td> </tr> ))} </tbody> </table> </section> ); } body { font: 16px/1.5 system-ui; margin: 0; color: #182b3c; background: #f4f8fc; } header, main { max-width: 900px; margin: auto; padding: 20px; } header { border-bottom: 1px solid #cbd5e1; } a { color: #075985; } button, input, textarea, select { font: inherit; padding: 8px; margin: 4px; } button { cursor: pointer; } table { border-collapse: collapse; width: 100%; } th, td { padding: 10px; border: 1px solid #cbd5e1; text-align: left; vertical-align: top; } pre, code { white-space: pre-wrap; overflow-wrap: anywhere; } .pass { color: #166534; } .fail, [role="alert"] { color: #a01818; } section { margin: 16px 0; padding: 12px; background: white; border-radius: 8px; } nav { display: flex; gap: 16px; } export function run({ rows, query, page }) { const value = Array.isArray(query) ? query[0] : query; const q = typeof value === "string" ? value.trim().toLowerCase() : ""; const matching = rows.filter((row) => row.title.toLowerCase().includes(q)); const pages = Math.max(1, Math.ceil(matching.length / 2)); const n = Number(page); const current = Number.isInteger(n) ? Math.min(pages, Math.max(1, n)) : 1; return { page: current, items: matching .slice((current - 1) * 2, current * 2) .map((row) => ({ id: row.id, title: row.title })), }; } export const examples = [ [ { rows: [ { id: 1, title: "React", private: "x" }, { id: 2, title: "Forms" }, { id: 3, title: "React tests" }, ], query: [" react ", "forms"], page: "9", }, { page: 1, items: [ { id: 1, title: "React" }, { id: 3, title: "React tests" }, ], }, ], [ { rows: [ { id: 1, title: "React" }, { id: 2, title: "react hooks" }, { id: 3, title: "Forms" }, { id: 4, title: "React tests" }, ], query: "REACT", page: "2", }, { page: 2, items: [{ id: 4, title: "React tests" }] }, ], ];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.

A matching output alone is not the whole check: the Input kept column must say yes too. Then explain the rule to yourself without reading the implementation.

Before the next day

  1. Which layer owns this decision, and which layer translates it for the reader?
  2. What happens on the least convenient input, not only the happy path?
  3. What would you test in a real browser or server that this pure function cannot establish?

Was this article helpful?

Your response is saved on this device.