smart-searcher
v1.0.3
Published
⚡ Tiny, fast, zero-dependency search toolkit (exact + fuzzy + indexed)
Downloads
353
Maintainers
Readme
smart-searcher
⚡ Tiny, fast, zero-dependency search toolkit for JavaScript/TypeScript. Supports exact lookup, fuzzy search, weighted multi-key search, indexing, filtering, and plugins.
Features
- 🔹 Exact search (
findExact) - 🔹 Indexed lookup (
createIndex) - 🔹 Fuzzy search (
createSearchwithfuzzy: true) - 🔹 Weighted multi-key fuzzy search
- 🔹 Filtering by criteria
- 🔹 Plugin system for query/response transforms
- ✅ Tiny bundle, tree-shakable, zero dependencies
- ✅ TypeScript ready
Installation
npm install smart-searcher
# or
yarn add smart-searcherUsage Examples
1️⃣ Exact search
import { findExact } from 'smart-searcher';
const data = [
{ id: 1, title: 'Apartment Gyumri' },
{ id: 2, title: 'House Yerevan' },
];
const index = findExact(data, 2, 'id');
console.log(index); // 1 (array index)2️⃣ Indexed lookup (O(1))
import { createIndex } from 'smart-searcher';
const data = [
{ id: 1, title: 'Apartment Gyumri' },
{ id: 2, title: 'House Yerevan' },
];
const indexMap = createIndex(data, 'id');
console.log(indexMap.get(2)); // 13️⃣ Filter by criteria
import { filter } from 'smart-searcher';
const data = [
{ id: 1, title: 'Apartment Gyumri', price: 30000 },
{ id: 2, title: 'House Yerevan', price: 80000 },
];
const results = filter(data, { price: { min: 20000, max: 50000 } });
console.log(results);
// [{ id: 1, title: 'Apartment Gyumri', price: 30000 }]4️⃣ Fuzzy search
import { createSearch } from 'smart-searcher';
const data = [
{ id: 1, title: 'Apartment Gyumri' },
{ id: 2, title: 'House Yerevan' },
];
const search = createSearch(data, { key: 'title', fuzzy: true });
console.log(search.search('gyumr'));
// [{ id: 1, title: 'Apartment Gyumri' }]5️⃣ Weighted multi-key search
const search = createSearch(data, {
keys: [
{ key: 'title', weight: 0.7 },
{ key: 'location', weight: 0.3 },
],
fuzzy: true
});
console.log(search.search('gyumr'));
// prioritizes matches in `title` over `location`6️⃣ Plugins
const searchWithPlugin = createSearch(data, {
key: 'title',
fuzzy: true,
plugins: [
() => ({
beforeSearch: (query) => query.trim().toLowerCase(),
afterSearch: (results) => results.slice(0, 5) // top 5 only
})
]
});
console.log(searchWithPlugin.search(' gyumr '));7️⃣ Combined API
const search = createSearch(data, { keys: ['title', 'location'], fuzzy: true });
console.log(search.find(1)); // exact find by id
console.log(search.search('gyumr')); // fuzzy
console.log(search.filter({ price: { max: 50000 } })); // filtered results📦 Why use smart-searcher?
- Lightweight ✅
- TypeScript support ✅
- Zero dependencies ✅
- Very fast (precomputed tokens, optional index) ✅
- Multi-key weighted fuzzy search ✅
- Plugins for custom transformations ✅
License
MIT License
