Medium
Implementing Case-Insensitive Product Search with Name, Description and Tags in JavaScript
Your team is implementing a client-side search feature for a product catalog. The search needs to match products by name, description, or tags, and should be case-insensitive and support partial matches. What's missing in this implementation?
1class ProductSearch {2 constructor(products) {3 this.products = products;4 this.searchIndex = this.buildSearchIndex();5 }67 buildSearchIndex() {8 return this.products.map(product => ({9 ...product,10 searchText: this.generateSearchText(product)11 }));12 }1314 generateSearchText(product) {15 // Missing implementation16 _______________17 }1819 search(query) {20 const normalizedQuery = query.toLowerCase().trim();21 return this.searchIndex22 .filter(product => product.searchText.includes(normalizedQuery))23 .map(({ searchText, ...product }) => product);24 }25}2627const products = [28 {29 id: 1,30 name: "Wireless Headphones",31 description: "Premium bluetooth headphones with noise cancellation",32 tags: ["electronics", "audio", "wireless"]33 }34 // ... more products35];