simple-search-lib
Javascript library for an simple search engine to power searchbars
License
This project is licensed under AGPL-3.0. See the LICENSE
file for more informations.
Usage
import { compileQuery, executeQuery } from '@bithero/simple-search-lib';
var query = 'name:John';
var person = {
name: 'John',
age: 24,
}
// First we need to "compile" the query
var compiledQuery = compileQuery(query);
// Before we can start using the query, we first need an namespace resolver.
// an namespace resolve is responsible to feed the execution process with the data
// an namespace (or attribute) has; an namespace has following syntax: `field:value`.
// You also can specify an negative match so: `field:-value`.
//
// Note: the resolver below is the same as the libraries defaultResolver.
function resolver(name: string, data: any): string {
return data[name].toString();
}
// An global matcher is also needed; this function is responsible to
// match any given text across all namespaces. Its typically called on normal words.
//
// Note: the matcher below is the same as the libraries defaultMatcher
function matcher(value: string, data: any): boolean {
for (const key in obj) {
if (data[key].toString().includes(value)) {
return true;
}
}
return false;
}
// Next, we can execute the compiled query as many times as we want:
var result = executeQuery(compiledQuery, person, resolver, matcher);
// result should now be `true`