You've already forked search-query-parser
Added support for tokenizing terms, negating terms, and quoted terms
This commit is contained in:
@@ -16,11 +16,11 @@ exports.parse = function (string, options) {
|
||||
}
|
||||
|
||||
// When a simple string, return it
|
||||
if (-1 === string.indexOf(':')) {
|
||||
if (-1 === string.indexOf(':') && !options.tokenize) {
|
||||
return string;
|
||||
}
|
||||
// When no keywords or ranges set, treat as a simple string
|
||||
else if (!options.keywords && !options.ranges){
|
||||
else if (!options.keywords && !options.ranges && !options.tokenize){
|
||||
return string;
|
||||
}
|
||||
// Otherwise parse the advanced query syntax
|
||||
@@ -30,7 +30,7 @@ exports.parse = function (string, options) {
|
||||
var exclusion = {};
|
||||
var terms = [];
|
||||
// Get a list of search terms respecting single and double quotes
|
||||
var regex = /(\S+:'(?:[^'\\]|\\.)*')|(\S+:"(?:[^"\\]|\\.)*")|\S+|\S+:\S+/g;
|
||||
var regex = /(\S+:'(?:[^'\\]|\\.)*')|(\S+:"(?:[^"\\]|\\.)*")|(-?"(?:[^"\\]|\\.)*")|(-?'(?:[^'\\]|\\.)*')|\S+|\S+:\S+/g;
|
||||
var match;
|
||||
while ((match = regex.exec(string)) !== null) {
|
||||
var term = match[0];
|
||||
@@ -61,11 +61,47 @@ exports.parse = function (string, options) {
|
||||
offsetEnd: match.index + term.length
|
||||
});
|
||||
} else {
|
||||
terms.push({
|
||||
text: term,
|
||||
offsetStart: match.index,
|
||||
offsetEnd: match.index + term.length
|
||||
var isExcludedTerm = false;
|
||||
if (term[0] === '-') {
|
||||
isExcludedTerm = true;
|
||||
term = term.slice(1);
|
||||
}
|
||||
|
||||
// Strip surrounding quotes
|
||||
term = term.replace(/^\"|\"$|^\'|\'$/g, '');
|
||||
// Strip backslashes respecting escapes
|
||||
term = (term + '').replace(/\\(.?)/g, function (s, n1) {
|
||||
switch (n1) {
|
||||
case '\\':
|
||||
return '\\';
|
||||
case '0':
|
||||
return '\u0000';
|
||||
case '':
|
||||
return '';
|
||||
default:
|
||||
return n1;
|
||||
}
|
||||
});
|
||||
|
||||
if (isExcludedTerm) {
|
||||
if (exclusion['text']) {
|
||||
if (exclusion['text'] instanceof Array) {
|
||||
exclusion['text'].push(term);
|
||||
} else {
|
||||
exclusion['text'] = [exclusion['text']];
|
||||
exclusion['text'].push(term);
|
||||
}
|
||||
} else {
|
||||
// First time seeing an excluded text term
|
||||
exclusion['text'] = term;
|
||||
}
|
||||
} else {
|
||||
terms.push({
|
||||
text: term,
|
||||
offsetStart: match.index,
|
||||
offsetEnd: match.index + term.length
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
// Reverse to ensure proper order when pop()'ing.
|
||||
@@ -228,7 +264,9 @@ exports.parse = function (string, options) {
|
||||
|
||||
// Concatenate all text terms if any
|
||||
if (query.text.length) {
|
||||
query.text = query.text.join(' ').trim();
|
||||
if (!options.tokenize) {
|
||||
query.text = query.text.join(' ').trim();
|
||||
}
|
||||
}
|
||||
// Just remove the attribute text when it's empty
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user