A defensive, in-memory product filtering pipeline for a Flipkart-style shopping list. Built with 100% pure vanilla JavaScript — zero runtime dependencies, zero frameworks, zero UI.
A shopping app receives a large product list where:
- Some rows are malformed or have invalid values.
- The user wants only Mobile products.
- The budget is limited to ₹50,000.
- The minimum acceptable rating is 4.
- Out-of-stock products must not be shown.
- The original product list must remain unchanged.
The pipeline validates the input, applies every rule, creates a small display shape, and returns products in ascending price order.
| Stage | What it does | Result |
|---|---|---|
getFlipkartDeals(products) |
Validates the outer input | 400 failure or 200 success |
| Row validation | Skips malformed rows safely | Only valid rows continue |
| Eligibility filter | Applies category, price, rating, and stock rules | Matching products only |
| Display mapping | Converts [name, category, price, rating, inStock] |
[name, price, rating] |
| Price ordering | Sorts from low price to high price | New, sorted result array |
- Exact Category Match — Only
category === "Mobile"is accepted. - Inclusive Budget Boundary —
price <= 50000; exactly50000is valid. - Inclusive Rating Boundary —
rating >= 4; exactly4is valid. - Stock Check — Only
inStock === trueis accepted. - Safe Row Validation — Price must be a finite number
>= 0; rating must be a finite number from0to5. - Immutability —
toSorted()returns a new sorted array; the original products list is not mutated. - Consistent Return Type — Every result uses a predictable response envelope:
{ status: 200, ok: true, data: [...], productsCount: 3 } { status: 400, ok: false, error: "..." }
- Guard Clauses First — Invalid input is handled before the pipeline does any work.
# 1. Enter the project
cd /FLIPKART-MULTI-FILTER
# 2. Run the proof suite (no install needed — zero dependencies!)
node /tests/flipkart-filter.test.jsconst { getFlipkartDeals } = require("./CODE/src/flipkart-filter");
const products = [
["Pixel 8", "Mobile", 44999, 4.4, true],
["Moto G", "Mobile", 14999, 4.1, true],
["Nokia G", "Mobile", 8999, 3.8, true],
["iPhone 15", "Mobile", 69999, 4.7, true],
["Realme C", "Mobile", 50000, 4.0, true],
];
getFlipkartDeals(products);
// {
// status: 200,
// ok: true,
// data: [
// ["Moto G", 14999, 4.1],
// ["Pixel 8", 44999, 4.4],
// ["Realme C", 50000, 4.0]
// ],
// productsCount: 3
// }| Check | Covers |
|---|---|
| 1 | Normal product fixture and expected sorted output |
| 2 | Invalid outer input returns the 400 failure envelope |
| 3 | Empty input returns a successful empty result |
| 4 | Malformed rows are skipped without a crash |
| 5 | No product matching all rules returns empty data |
| 6 | Inclusive boundary values: 50000 price and 4 rating |
| 7 | Negative price and out-of-range rating are rejected |
| 8 | Original products array remains unchanged |
Run the suite directly:
node CODE/tests/flipkart-filter.test.jsExpected final line:
J2 test suite passed.
- Guard clauses — reject invalid outer input immediately
Array.isArray()— validate rows and the outer collectionNumber.isFinite()— validate price and rating safely.reduce()— clean and preserve only valid rows.filter()— apply the product eligibility rules.map()— create the final display shape.toSorted()— order results without mutating the source- Result objects — HTTP-style
{ status, ok, data, error }responses - Node's built-in
assert/strict— dependency-free acceptance testing
The important review decisions for this module are:
sort()mutates an array. The implementation usestoSorted()so the source list remains unchanged.- Price and rating ranges are validated before eligibility filtering, so invalid numeric data cannot silently pass.
- The rules use inclusive comparisons:
price <= 50000andrating >= 4. - Production source and acceptance tests are separated:
CODE/src/flipkart-filter.jsCODE/tests/flipkart-filter.test.js
- The ticket remains code-free in
PLAN/; implementation belongs only inCODE/src/.
- Module 01 — Flipkart Multi-Filter Product Pipeline
- Module 02 — J3 Mini Pub/Sub
- Module 03 — J4 Debounce Engine
- Module 04 — J5 STREAM-OS Real Pipeline
- Module 05 — J6 Memoize/LRU (optional)
Debjeet Dhar — Kolkata, India 🇮🇳
Learning in public. Building reliable JavaScript logic modules, one proof-driven project at a time.