A lightweight, powerful object search engine for JavaScript with advanced query syntax
npm install @ecromaneli/search-engine
For browser usage, you can download and use the pre-built version:
- Go to the Releases page
- Download the
search-engine-web.zip
file - Extract the ZIP and include the script in your HTML:
<!-- Include the script in your HTML file -->
<script src="path/to/search-engine.js"></script>
<!-- Or use the minified version -->
<!-- <script src="path/to/search-engine.min.js"></script> -->
<script>
// Sample data
const users = [
{ id: 1, name: 'John Doe', age: 28, tags: ['developer', 'javascript'] },
{ id: 2, name: 'Jane Smith', age: 34, tags: ['designer', 'ui/ux'] },
{ id: 3, name: 'Bob Johnson', age: 45, tags: ['manager', 'finance'] }
];
// Use SearchEngine.search() directly
const results = SearchEngine.search(users, 'name:john');
console.log(results);
</script>
- Powerful query language with boolean operators (AND/OR)
- Field-specific searches
- Support for wildcards and regex pattern matching
- Numeric range searches
- Logical negation of search terms
- Nested property searching
- Logical grouping with parentheses
- Zero dependencies
const { search } = require('@ecromaneli/search-engine')
// Sample data
const users = [
{ id: 1, name: 'John Doe', age: 28, tags: ['developer', 'javascript'] },
{ id: 2, name: 'Jane Smith', age: 34, tags: ['designer', 'ui/ux'] },
{ id: 3, name: 'Bob Johnson', age: 45, tags: ['manager', 'finance'] }
]
// Simple search
const result1 = search(users, 'john') // Find users with "john" in any field
// Field-specific search
const result2 = search(users, 'name:jane') // Find users with "jane" in the name field
// Age range search
const result3 = search(users, 'age~:25-35') // Find users with age between 25 and 35
// Complex search with boolean operators and grouping
const result4 = search(users, '("developer" or "designer") and not age~:40-50')
field
- Search for "field" with any value"value"
- Search for "value" in any fieldfield:value
- Search for "value" in the specific field
field*:pattern
- Regex pattern match on fieldfield~:min-max
- Numeric range search (min and max are optional)
term1 and term2
- Both terms must matchterm1 or term2
- Either term must match
not term
- Term must not match(term1 or term2) and term3
- Logical grouping with parentheses
- objList (Array): Array of objects to search through
- queryStr (String): Query string following the syntax described above
- exclude (Array, optional): Array of property names to exclude from searching
- Returns (Array): Array of matching objects
Created by Emerson Capuchi Romaneli (@ECRomaneli).
This project is licensed under the MIT License.