Restaurant search utilities

You build app/lib/search.ts utilities that debounce input values, normalize search terms, create restaurant query keys, and determine when a term is long enough to search.

Ticket

Build app/lib/search.ts with a framework-independent debouncer and restaurant search utilities. The module must support generic values, injectable timers, normalized search terms, hierarchical query keys and a minimum search length check.

Export these declarations:

typescript
export type Timers = {
  setTimeout(fn: () => void, ms: number): unknown;
  clearTimeout(id: unknown): void;
};

export function createDebouncer<T>(
  fn: (value: T) => void,
  delayMs: number,
  timers?: Timers
): {
  call(value: T): void;
  cancel(): void;
  flush(): void;
};

export function normalizeTerm(term: string): string;

export function searchKey(
  term: string
): readonly ["restaurants", "search", string];

export function canSearch(term: string): boolean;

When timers is omitted, the debouncer uses the global setTimeout and clearTimeout.

Restaurant search should not send a request for every keystroke. A search screen can use these utilities to wait until the customer pauses typing, normalize the input and give TanStack Query a consistent cache key.

Acceptance criteria

  • Calling the debouncer does not invoke the callback before the configured delay has passed.
  • Multiple calls within the waiting period produce exactly one callback with the latest value.
  • Every new call restarts the debounce waiting period.
  • Separate bursts of calls produce separate callback invocations.
  • Calling cancel() drops the pending call and leaves no pending timer.
  • Calling flush() immediately invokes the pending callback exactly once.
  • Advancing the clock after flush() does not invoke the callback again.
  • Calling flush() when nothing is pending does nothing.
  • normalizeTerm(" Butter CHICKEN ") returns "butter chicken".
  • Equivalent normalized terms produce equal search keys, while different normalized terms produce different search keys.
  • searchKey(term) returns a key beginning with ["restaurants", "search"] and places the normalized term in its third element.
  • canSearch() returns false for empty and single-character normalized terms and true for normalized terms containing at least 2 characters.

Constraints

  • Create only app/lib/search.ts.
  • Use pure TypeScript logic. Do not import React or React Native modules.
  • Do not modify the provided tests.

Design direction

Keep the debouncer and search-term utilities together in app/lib/search.ts. Use a closure to hold the timer identifier, pending value and pending-state boolean. Keep timer operations behind the Timers interface so the implementation does not depend on React or a particular testing framework.

Consider these trade-offs:

  • Trailing-edge debouncing waits until new input stops arriving. This reduces unnecessary requests, but the callback does not execute immediately when the customer starts typing.
  • A separate pending-state boolean is necessary for generic values. Treating undefined as an empty state would incorrectly discard a valid input.
  • A hierarchical query key factory keeps restaurant search queries grouped under ["restaurants"]. Normalizing the search term before building the key avoids separate cache entries for equivalent inputs.

Any layout that meets the contract passes.

How to check your work

Run the exercise tests and then the TypeScript check:

bash
cd backend && make test W=4O3
cd app && npx tsc --noEmit

Read the first failing assertion before changing code. It identifies whether the problem concerns timer scheduling, pending values, callback execution, term normalization or query-key behaviour.

Finished Exercise 4.22?

Client resilience extensions

4 items

  • 4.19Resilient client patternsnot completed
  • 4.20API request retry utilitynot completed
  • 4.21Cart synchronization helpernot completed
  • 4.22Restaurant search utilitiesnot completed

Week 5 · Live order tracking and rider management

Week 4 done · Up next · 6 modules

Open week 5

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Create app/lib/search.ts with the required exports.
  2. Implement trailing-edge debouncing with injectable timers.
  3. Support cancellation and immediate flushing without duplicate callbacks.
  4. Normalize search terms and build hierarchical restaurant query keys.
  5. Implement the minimum two-character search check.
  6. Pass make test W=4O3 and npx tsc --noEmit.

Commands

terminal
cd backend && make test W=4O3
terminal
cd app && npx tsc --noEmit

Tests

  • W4 O3 Debounced search (12 tests) in app-tests/lib/search.test.ts