Skip to content

Commit

Permalink
Remove zero-length terms
Browse files Browse the repository at this point in the history
Fix term check on empty text
  • Loading branch information
Ivan-Roger authored and Ivan ROGER committed Mar 9, 2023
1 parent 7fcd56f commit a28918e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/search-query-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ exports.parse = function (string, options) {
var term = match[0];
var sepIndex = term.indexOf(':');
if (sepIndex !== -1) {
var split = term.split(':'),
key = term.slice(0, sepIndex),
var key = term.slice(0, sepIndex),
val = term.slice(sepIndex + 1);
// Strip surrounding quotes
val = val.replace(/^\"|\"$|^\'|\'$/g, '');
Expand Down Expand Up @@ -92,6 +91,11 @@ exports.parse = function (string, options) {
}
});

if (term.length === 0) {
// Ignore empty strings after cleanup
continue
}

if (isExcludedTerm) {
if (exclusion['text']) {
if (exclusion['text'] instanceof Array) {
Expand Down Expand Up @@ -119,7 +123,7 @@ exports.parse = function (string, options) {
var term;
while (term = terms.pop()) {
// When just a simple term
if (term.text) {
if (term.text !== undefined) {
// We add it as pure text
query.text.push(term.text);
// When offsets is true, push a new offset
Expand Down
29 changes: 29 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,35 @@ describe('Search query syntax parser', function () {
parsedAfterStringifySearchQuery.should.be.eql(parsedSearchQuery);
});

it('should return a tokenized string without empty text terms', function () {
var searchQuery = "fancy pyjama wear ''";
var options = { tokenize: true };
var parsedSearchQuery = searchquery.parse(searchQuery, options);

parsedSearchQuery.should.be.an.Object;
parsedSearchQuery.should.have.property('text', ['fancy', 'pyjama', 'wear']);

var parsedAfterStringifySearchQuery = searchquery.parse(searchquery.stringify(parsedSearchQuery, options), options);
parsedAfterStringifySearchQuery.offsets = undefined;
parsedSearchQuery.offsets = undefined;
parsedAfterStringifySearchQuery.should.be.eql(parsedSearchQuery);
});

it('should return a simple string without empty text terms', function () {
var searchQuery = "key:value fancy pyjama wear ''";
var options = { keywords: ['key'] };
var parsedSearchQuery = searchquery.parse(searchQuery, options);

parsedSearchQuery.should.be.an.Object;
parsedSearchQuery.should.have.property('text', 'fancy pyjama wear');
parsedSearchQuery.should.have.property('key', 'value');

var parsedAfterStringifySearchQuery = searchquery.parse(searchquery.stringify(parsedSearchQuery, options), options);
parsedAfterStringifySearchQuery.offsets = undefined;
parsedSearchQuery.offsets = undefined;
parsedAfterStringifySearchQuery.should.be.eql(parsedSearchQuery);
});

it('should parse a single keyword with no text', function () {
var searchQuery = 'from:[email protected]';
var options = {keywords: ['from']};
Expand Down

0 comments on commit a28918e

Please sign in to comment.