UNPKG

@pawan9303/react-search

Version:

A simple utility for searching through arrays of strings or numbers. Case-insensitive search functionality.

24 lines (17 loc) 693 B
/** * Search through a list of items based on a query string. * @param {Array} list - The array to search through * @param {string} query - The search query (case insensitive) * @returns {Array} - List of items that match the search query */ function search(list, query) { if (!Array.isArray(list)) { throw new Error('The first argument must be an array.'); } if (typeof query !== 'string') { throw new Error('The search query must be a string.'); } const lowerCaseQuery = query.toLowerCase(); return list.filter(item => item.toString().toLowerCase().includes(lowerCaseQuery)); } module.exports = { search };