A search operators parser to filter search requests
- Unicode support
- Case-insensitive
- Based on LR parser
- No dependencies
yarn add search-operators
or
npm install search-operators --save
const searchOperators = require('./search-operators');
var value = searchOperators.parse('+github.com useState useEffect');
console.log(value);
//the parser will separate the text in terms and filters.
//{
// "filters": [{ "type": "exact", "value": "github.com" }],
// "terms": ["useState","useEffect"]
//}
Operator | Description | Parser input | Parser output |
---|---|---|---|
" " | exact word or phrase | "bash history file" | { filters : [ { type: 'exact', value: 'bash history file' } ] } |
+ | exact word | +github.com | { filters:[ { type: 'exact', value: 'github.com' } ] } |
- | exclude word | -php | { filters:[ { type: 'exclude', value: 'php' } ] } |
: | match | in:general | { filters:[ { type: 'match', key:'in', value: 'general' } ] } |
not : | inverse match | not in:random | { filters:[ { type: 'not-match', key:'in', value: 'random' } ] } |
- Parse
parser.parse(
search, //string to parse
{ keys:[] } //keys used by match operator
);
- Tokens
//populated after parse, contains token location
//useful to highlight syntax!
parser.parse("-😊");
console.log(parser.tokens);
//{
// type: 'EXCLUDE_OP',
// value: '😊',
// startOffset: 0,
// endOffset: 3,
// startLine: 1,
// endLine: 1,
// startColumn: 0,
// endColumn: 3
//}
This project is intended to be extended or modified. It's uses syntax to generate the parser; in case you need to modify the grammar, syntax is very well documented. Any contribution is very appreciated.
- Fork search-operators repo.
- Make your changes
- Validate grammar with
npm run validate
- Test parser with
npm run test
Use npm run build
to transpile and generate production build.
MIT do whatever you want to do!